-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinitializer_tests.go
More file actions
96 lines (82 loc) · 2.73 KB
/
initializer_tests.go
File metadata and controls
96 lines (82 loc) · 2.73 KB
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
package magic
import (
"os"
"testing"
"time"
"github.com/Liphium/magic/v3/mrunner"
"github.com/Liphium/magic/v3/util"
)
var startSignalChan = make(chan struct{})
var magicTestRunner *mrunner.Runner = nil
// Call this function in any TestMain function if you want some tests to rely on your actual app or database.
//
// When calling this method, Magic will automatically start your app and any required containers, just like when you
// normally run your app.
//
// Please make sure to call the magic.AppStarted() function for the test runner to work properly. You can read its
// comments if you should still have questions about what it does. You can adjust the timeout for exiting in the Magic
// config.
//
// The handler will be called once everything is ready.
func PrepareTesting(t *testing.M, config Config) {
// Start all the containers using Magic
factory, runner := prepare(config, "default") // TODO: Make the profile definable and make parallel tests possible
if factory == nil || runner == nil {
util.Log.Fatal("Couldn't prepare containers with Magic")
return
}
// Load environment
util.Log.Println("Loading environment...")
for key, value := range runner.Plan().Environment {
if err := os.Setenv(key, value); err != nil {
util.Log.Fatalf("couldn't set environment variable %s: %s", key, err)
}
}
util.Log.Println("Setup finished.")
// Stop all containers and unlock once testing is done
defer func() {
recover()
factory.Unlock()
runner.StopContainers()
}()
// Start the app
go func() {
config.StartFunction()
}()
// Wait for the app's start signal
util.Log.Println("Waiting for start signal...")
util.Log.Println("If you don't call magic.AppStarted() when your app starts, this will fail.")
timeoutChan := make(chan struct{})
go func() {
if config.TestAppTimeout == nil {
config.TestAppTimeout = Ptr(time.Second * 10)
}
time.Sleep(*config.TestAppTimeout)
timeoutChan <- struct{}{}
}()
select {
case <-timeoutChan:
util.Log.Fatalln("Couldn't get start signal in time.")
case <-startSignalChan:
util.Log.Println("Signal received. Everything successfully prepared!")
}
magicTestRunner = runner
t.Run()
}
// This function has to be called when your app successfully started.
//
// It's used to start the test once your app is up and running in testing. The test runner does have a timeout of
// 10 seconds though, so if you're app takes longer than that to startup, you can modify the timeout in your Magic
// config.
func AppStarted() {
select {
case startSignalChan <- struct{}{}:
default:
}
}
// Get the current runner active while testing.
//
// For this to work, please make sure you call PrepareTesting in your TestMain function.
func GetTestRunner() *mrunner.Runner {
return magicTestRunner
}