@@ -9,7 +9,33 @@ import (
99 "github.com/matrix-org/complement/ct"
1010)
1111
12- var testPackage * TestPackage
12+ var (
13+ testPackage * TestPackage
14+ customDeployer func (numServers int ) Deployment
15+ )
16+
17+ type complementOpts struct {
18+ cleanup func ()
19+ customDeployment func (numServers int ) Deployment
20+ }
21+ type opt func (* complementOpts )
22+
23+ // WithCleanup adds a cleanup function which is called prior to terminating the test suite.
24+ // It is called BEFORE Complement containers are destroyed.
25+ // This function should be used for per-suite cleanup operations e.g tearing down containers, killing
26+ // child processes, etc.
27+ func WithCleanup (fn func ()) opt {
28+ return func (co * complementOpts ) {
29+ co .cleanup = fn
30+ }
31+ }
32+
33+ // WithDeployment adds a custom mechanism to deploy homeservers.
34+ func WithDeployment (fn func (numServers int ) Deployment ) opt {
35+ return func (co * complementOpts ) {
36+ co .customDeployment = fn
37+ }
38+ }
1339
1440// TestMain is the main entry point for Complement.
1541//
@@ -19,23 +45,26 @@ var testPackage *TestPackage
1945// The 'namespace' should be unique for this test package, among all test packages which may run in parallel, to avoid
2046// docker containers stepping on each other. For MSCs, use the MSC name. For versioned releases, use the version number
2147// along with any sub-directory name.
22- func TestMain (m * testing.M , namespace string ) {
23- TestMainWithCleanup (m , namespace , nil )
24- }
48+ //
49+ // Functional options can be used to control how Complement processes deployments.
50+ func TestMain (m * testing.M , namespace string , customOpts ... opt ) {
51+ opts := & complementOpts {}
52+ for _ , o := range customOpts {
53+ o (opts )
54+ }
55+ if opts .customDeployment != nil {
56+ customDeployer = opts .customDeployment
57+ }
2558
26- // TestMainWithCleanup is TestMain but with a cleanup function prior to terminating the test suite.
27- // This function should be used for per-suite cleanup operations e.g tearing down containers, killing
28- // child processes, etc.
29- func TestMainWithCleanup (m * testing.M , namespace string , cleanup func ()) {
3059 var err error
3160 testPackage , err = NewTestPackage (namespace )
3261 if err != nil {
3362 fmt .Printf ("Error: %s" , err )
3463 os .Exit (1 )
3564 }
3665 exitCode := m .Run ()
37- if cleanup != nil {
38- cleanup ()
66+ if opts . cleanup != nil {
67+ opts . cleanup ()
3968 }
4069 testPackage .Cleanup ()
4170 os .Exit (exitCode )
@@ -61,5 +90,8 @@ func Deploy(t ct.TestLike, numServers int) Deployment {
6190 if testPackage == nil {
6291 ct .Fatalf (t , "Deploy: testPackage not set, did you forget to call complement.TestMain?" )
6392 }
93+ if customDeployer != nil {
94+ return customDeployer (numServers )
95+ }
6496 return testPackage .Deploy (t , numServers )
6597}
0 commit comments