-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain_test.go
202 lines (151 loc) · 4.99 KB
/
main_test.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
/*
import (
"crypto/tls"
stdlog "log"
"os"
"testing"
"time"
"github.com/go-pluto/pluto/crypto"
"github.com/go-pluto/pluto/distributor"
"github.com/go-pluto/pluto/storage"
"github.com/go-pluto/pluto/utils"
"github.com/go-pluto/pluto/worker"
)
// Functions
// TestMain executes initialization and teardown
// code needed for all tests in package main.
func TestMain(m *testing.M) {
var err error
// Create needed test environment.
testEnv, err = utils.CreateTestEnv("./test-config.toml")
if err != nil {
stdlog.Fatal(err)
}
// Run all nodes in background.
RunAllNodes(testEnv, "worker-1")
// Run all tests of this package.
success := m.Run()
// Give background synchronization enough
// time to finish communication.
time.Sleep(10 * time.Second)
// Tear down test setup.
TearDownNormalSetup(testEnv)
// Return with test return value.
os.Exit(success)
}
// RunAllNodes runs all needed nodes for a proper multi-node
// test setup in background. It also handles shutdown of these
// nodes when appropriate signals are received.
func RunAllNodes(testEnv *utils.TestEnv, workerName string) {
go func() {
intlTLSConfig, err := crypto.NewInternalTLSConfig(testEnv.Config.Storage.CertLoc, testEnv.Config.Storage.KeyLoc, testEnv.Config.RootCertLoc)
if err != nil {
stdlog.Fatal(err)
}
// Create needed sockets. First, mail socket.
mailSocket, err := tls.Listen("tcp", testEnv.Config.Storage.ListenMailAddr, intlTLSConfig)
if err != nil {
stdlog.Fatal(err)
}
var storageS storage.Service
storageS = storage.NewService(&intlConn{intlTLSConfig, testEnv.Config.IntlConnRetry}, mailSocket, testEnv.Config.Storage, testEnv.Config.Workers)
go func() {
// Wait for shutdown signal on channel.
<-testEnv.DownStorage
stdlog.Printf("[utils.RunAllNodes] Closing storage socket")
// Shut down storage node.
mailSocket.Close()
// Signal back successful shutdown.
testEnv.DoneStorage <- struct{}{}
}()
// Run the storage node.
if err := storageS.Run(); err != nil {
stdlog.Fatal(err)
}
}()
// Wait shortly for storage node to have started.
time.Sleep(100 * time.Millisecond)
go func() {
workerConfig, ok := testEnv.Config.Workers[workerName]
if !ok {
stdlog.Fatal("can't find correct worker config")
}
intlTLSConfig, err := crypto.NewInternalTLSConfig(workerConfig.CertLoc, workerConfig.KeyLoc, testEnv.Config.RootCertLoc)
if err != nil {
stdlog.Fatal(err)
}
// Create needed sockets. First, mail socket.
mailSocket, err := tls.Listen("tcp", workerConfig.ListenMailAddr, intlTLSConfig)
if err != nil {
stdlog.Fatal(err)
}
var workerS worker.Service
workerS = worker.NewService(&intlConn{intlTLSConfig, testEnv.Config.IntlConnRetry}, mailSocket, workerConfig, workerName)
go func() {
// Wait for shutdown signal on channel.
<-testEnv.DownWorker
stdlog.Printf("[utils.RunAllNodes] Closing %s socket", workerName)
// Shut down worker node.
mailSocket.Close()
// Signal back successful shutdown.
testEnv.DoneWorker <- struct{}{}
}()
// Run the worker node.
if err := workerS.Run(); err != nil {
stdlog.Fatal(err)
}
}()
// Wait shortly for worker node to have started.
time.Sleep(100 * time.Millisecond)
go func() {
authenticator, err := initAuthenticator(testEnv.Config)
if err != nil {
stdlog.Fatal(err)
}
intlTLSConfig, err := crypto.NewInternalTLSConfig(testEnv.Config.Distributor.InternalTLS.CertLoc, testEnv.Config.Distributor.InternalTLS.KeyLoc, testEnv.Config.RootCertLoc)
if err != nil {
stdlog.Fatal(err)
}
distr := distributor.NewService(authenticator, &intlConn{intlTLSConfig, testEnv.Config.IntlConnRetry}, testEnv.Config.Workers)
go func() {
// Wait for shutdown signal on channel.
<-testEnv.DownDistr
stdlog.Printf("[utils.RunAllNodes] Closing distributor socket")
// Signal back successful shutdown.
testEnv.DoneDistr <- struct{}{}
}()
publicTLSConfig, err := crypto.NewPublicTLSConfig(testEnv.Config.Distributor.PublicCertLoc, testEnv.Config.Distributor.PublicKeyLoc)
if err != nil {
stdlog.Fatal(err)
}
mailSocket, err := tls.Listen("tcp", testEnv.Config.Distributor.ListenMailAddr, publicTLSConfig)
if err != nil {
stdlog.Fatal(err)
}
defer mailSocket.Close()
// Run the distributor node.
if err := distr.Run(mailSocket, testEnv.Config.IMAP.Greeting); err != nil {
stdlog.Fatal(err)
}
}()
// Wait shortly for worker node to have started.
time.Sleep(500 * time.Millisecond)
}
// TearDownNormalSetup takes care of shutting down the normally
// running nodes by sending shutdown signals and expecting
// success answers.
func TearDownNormalSetup(testEnv *utils.TestEnv) {
// Signal to all nodes running background that they
// are supposed to shut down now.
testEnv.DownDistr <- struct{}{}
testEnv.DownWorker <- struct{}{}
testEnv.DownStorage <- struct{}{}
// Wait for them to signal success back.
<-testEnv.DoneDistr
<-testEnv.DoneWorker
<-testEnv.DoneStorage
// Wait shortly.
time.Sleep(500 * time.Millisecond)
}
*/