@@ -9,7 +9,33 @@ import (
9
9
"github.com/matrix-org/complement/ct"
10
10
)
11
11
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
+ }
13
39
14
40
// TestMain is the main entry point for Complement.
15
41
//
@@ -19,23 +45,26 @@ var testPackage *TestPackage
19
45
// The 'namespace' should be unique for this test package, among all test packages which may run in parallel, to avoid
20
46
// docker containers stepping on each other. For MSCs, use the MSC name. For versioned releases, use the version number
21
47
// 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
+ }
25
58
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 ()) {
30
59
var err error
31
60
testPackage , err = NewTestPackage (namespace )
32
61
if err != nil {
33
62
fmt .Printf ("Error: %s" , err )
34
63
os .Exit (1 )
35
64
}
36
65
exitCode := m .Run ()
37
- if cleanup != nil {
38
- cleanup ()
66
+ if opts . cleanup != nil {
67
+ opts . cleanup ()
39
68
}
40
69
testPackage .Cleanup ()
41
70
os .Exit (exitCode )
@@ -61,5 +90,8 @@ func Deploy(t ct.TestLike, numServers int) Deployment {
61
90
if testPackage == nil {
62
91
ct .Fatalf (t , "Deploy: testPackage not set, did you forget to call complement.TestMain?" )
63
92
}
93
+ if customDeployer != nil {
94
+ return customDeployer (numServers )
95
+ }
64
96
return testPackage .Deploy (t , numServers )
65
97
}
0 commit comments