-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathtest_main.go
97 lines (87 loc) · 2.95 KB
/
test_main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package complement
import (
"fmt"
"os"
"testing"
"github.com/matrix-org/complement/b"
"github.com/matrix-org/complement/ct"
)
var (
testPackage *TestPackage
customDeployer func(numServers int) Deployment
)
type complementOpts struct {
cleanup func()
customDeployment func(numServers int) Deployment
}
type opt func(*complementOpts)
// WithCleanup adds a cleanup function which is called prior to terminating the test suite.
// It is called BEFORE Complement containers are destroyed.
// This function should be used for per-suite cleanup operations e.g tearing down containers, killing
// child processes, etc.
func WithCleanup(fn func()) opt {
return func(co *complementOpts) {
co.cleanup = fn
}
}
// WithDeployment adds a custom mechanism to deploy homeservers.
func WithDeployment(fn func(numServers int) Deployment) opt {
return func(co *complementOpts) {
co.customDeployment = fn
}
}
// TestMain is the main entry point for Complement.
//
// It will clean up any old containers/images/networks from the previous run, then run the tests, then clean up
// again. No blueprints are made at this point as they are lazily made on demand.
//
// The 'namespace' should be unique for this test package, among all test packages which may run in parallel, to avoid
// docker containers stepping on each other. For MSCs, use the MSC name. For versioned releases, use the version number
// along with any sub-directory name.
//
// Functional options can be used to control how Complement processes deployments.
func TestMain(m *testing.M, namespace string, customOpts ...opt) {
opts := &complementOpts{}
for _, o := range customOpts {
o(opts)
}
if opts.customDeployment != nil {
customDeployer = opts.customDeployment
}
var err error
testPackage, err = NewTestPackage(namespace)
if err != nil {
fmt.Printf("Error: %s", err)
os.Exit(1)
}
exitCode := m.Run()
if opts.cleanup != nil {
opts.cleanup()
}
testPackage.Cleanup()
os.Exit(exitCode)
}
// Deploy will deploy the given blueprint or terminate the test.
// It will construct the blueprint if it doesn't already exist in the docker image cache.
// This function is the main setup function for all tests as it provides a deployment with
// which tests can interact with.
func OldDeploy(t ct.TestLike, blueprint b.Blueprint) Deployment {
t.Helper()
if testPackage == nil {
ct.Fatalf(t, "Deploy: testPackage not set, did you forget to call complement.TestMain?")
}
return testPackage.OldDeploy(t, blueprint)
}
// Deploy will deploy the given number of servers or terminate the test.
// This function is the main setup function for all tests as it provides a deployment with
// which tests can interact with.
func Deploy(t ct.TestLike, numServers int) Deployment {
t.Helper()
if testPackage == nil {
ct.Fatalf(t, "Deploy: testPackage not set, did you forget to call complement.TestMain?")
}
if customDeployer != nil {
return customDeployer(numServers)
}
return testPackage.Deploy(t, numServers)
}