/
testmanager.go
598 lines (526 loc) · 17.3 KB
/
testmanager.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package libhive
import (
"crypto/rand"
"encoding/json"
"errors"
"fmt"
"net/http"
"os"
"path/filepath"
"sync"
"time"
"gopkg.in/inconshreveable/log15.v2"
)
var (
ErrNoSuchNode = errors.New("no such node")
ErrNoSuchTestSuite = errors.New("no such test suite")
ErrNoSuchTestCase = errors.New("no such test case")
ErrMissingClientType = errors.New("missing client type")
ErrNoAvailableClients = errors.New("no available clients")
ErrTestSuiteRunning = errors.New("test suite still has running tests")
ErrMissingOutputDestination = errors.New("test suite requires an output")
ErrNoSummaryResult = errors.New("test case must be ended with a summary result")
ErrDBUpdateFailed = errors.New("could not update results set")
ErrTestSuiteLimited = errors.New("testsuite test count is limited")
)
// SimEnv contains the simulation parameters.
type SimEnv struct {
LogDir string
// Parameters of simulation.
SimLogLevel int
SimParallelism int
SimRandomSeed int
SimTestPattern string
// This is the time limit for the simulation run.
// There is no default limit.
SimDurationLimit time.Duration
// These are the clients which are made available to the simulator.
// If unset (i.e. nil), all built clients are used.
ClientList []ClientDesignator
// This configures the amount of time the simulation waits
// for the client to open port 8545 after launching the container.
ClientStartTimeout time.Duration
}
// SimResult summarizes the results of a simulation run.
type SimResult struct {
Suites int
SuitesFailed int
Tests int
TestsFailed int
}
// TestManager collects test results during a simulation run.
type TestManager struct {
config SimEnv
backend ContainerBackend
clientDefs []*ClientDefinition
simContainerID string
simLogFile string
// all networks started by a specific test suite, where key
// is network name and value is network ID
networks map[TestSuiteID]map[string]string
networkMutex sync.RWMutex
testCaseMutex sync.RWMutex
testSuiteMutex sync.RWMutex
runningTestSuites map[TestSuiteID]*TestSuite
runningTestCases map[TestID]*TestCase
testSuiteCounter uint32
testCaseCounter uint32
results map[TestSuiteID]*TestSuite
}
func NewTestManager(config SimEnv, b ContainerBackend, clients []*ClientDefinition) *TestManager {
return &TestManager{
clientDefs: clients,
config: config,
backend: b,
runningTestSuites: make(map[TestSuiteID]*TestSuite),
runningTestCases: make(map[TestID]*TestCase),
results: make(map[TestSuiteID]*TestSuite),
networks: make(map[TestSuiteID]map[string]string),
}
}
// SetSimContainerInfo makes the manager aware of the simulation container.
// This must be called after creating the simulation container, but before starting it.
func (manager *TestManager) SetSimContainerInfo(id, logFile string) {
manager.simContainerID = id
manager.simLogFile = logFile
}
// Results returns the results for all suites that have already ended.
func (manager *TestManager) Results() map[TestSuiteID]*TestSuite {
manager.testSuiteMutex.RLock()
defer manager.testSuiteMutex.RUnlock()
// Copy results.
r := make(map[TestSuiteID]*TestSuite)
for id, suite := range manager.results {
r[id] = suite
}
return r
}
// API returns the simulation API handler.
func (manager *TestManager) API() http.Handler {
return newSimulationAPI(manager.backend, manager.config, manager)
}
// IsTestSuiteRunning checks if the test suite is still running and returns it if so
func (manager *TestManager) IsTestSuiteRunning(testSuite TestSuiteID) (*TestSuite, bool) {
manager.testSuiteMutex.RLock()
defer manager.testSuiteMutex.RUnlock()
suite, ok := manager.runningTestSuites[testSuite]
return suite, ok
}
// IsTestRunning checks if the test is still running and returns it if so.
func (manager *TestManager) IsTestRunning(test TestID) (*TestCase, bool) {
manager.testCaseMutex.RLock()
defer manager.testCaseMutex.RUnlock()
testCase, ok := manager.runningTestCases[test]
return testCase, ok
}
// Terminate forces the termination of any running tests with
// an error message. This can be called as a cleanup method.
// If there are no running tests, there is no effect.
func (manager *TestManager) Terminate() error {
terminationSummary := &TestResult{
Pass: false,
Timeout: true,
Details: "Test was terminated by host",
}
manager.testSuiteMutex.Lock()
defer manager.testSuiteMutex.Unlock()
for suiteID, suite := range manager.runningTestSuites {
for testID := range suite.TestCases {
if _, running := manager.IsTestRunning(testID); running {
// end any running tests and ensure that the host is notified to clean up
// any resources (e.g. docker containers).
err := manager.EndTest(suiteID, testID, terminationSummary)
if err != nil {
return err
}
}
}
// ensure the db is updated with results
manager.doEndSuite(suiteID)
}
return nil
}
// GetNodeInfo gets some info on a client belonging to some test
func (manager *TestManager) GetNodeInfo(testSuite TestSuiteID, test TestID, nodeID string) (*ClientInfo, error) {
manager.testCaseMutex.RLock()
defer manager.testCaseMutex.RUnlock()
testCase, ok := manager.runningTestCases[test]
if !ok {
return nil, ErrNoSuchTestCase
}
nodeInfo, ok := testCase.ClientInfo[nodeID]
if !ok {
return nil, ErrNoSuchNode
}
return nodeInfo, nil
}
// CreateNetwork creates a docker network with the given network name.
func (manager *TestManager) CreateNetwork(testSuite TestSuiteID, name string) error {
_, ok := manager.IsTestSuiteRunning(testSuite)
if !ok {
return ErrNoSuchTestSuite
}
// add network to network map
manager.networkMutex.Lock()
defer manager.networkMutex.Unlock()
id, err := manager.backend.CreateNetwork(getUniqueName(testSuite, name))
if err != nil {
return err
}
if _, exists := manager.networks[testSuite]; !exists {
// initialize network map for individual test suite
manager.networks[testSuite] = make(map[string]string)
}
manager.networks[testSuite][name] = id
return nil
}
// getUniqueName returns a unique network name to prevent network collisions
func getUniqueName(testSuite TestSuiteID, name string) string {
return fmt.Sprintf("hive_%d_%d_%s", os.Getpid(), testSuite, name)
}
// RemoveNetwork removes a docker network by the given network name.
func (manager *TestManager) RemoveNetwork(testSuite TestSuiteID, network string) error {
manager.networkMutex.Lock()
defer manager.networkMutex.Unlock()
id, exists := manager.networks[testSuite][network]
if !exists {
return ErrNetworkNotFound
}
if err := manager.backend.RemoveNetwork(id); err != nil {
return err
}
delete(manager.networks[testSuite], network)
return nil
}
// PruneNetworks removes all networks created by the given test suite.
func (manager *TestManager) PruneNetworks(testSuite TestSuiteID) []error {
var errs []error
for name := range manager.networks[testSuite] {
log15.Info("removing docker network", "name", name)
if err := manager.RemoveNetwork(testSuite, name); err != nil {
errs = append(errs, err)
}
}
// delete the test suite from the network map as all its networks have been torn down
manager.networkMutex.Lock()
delete(manager.networks, testSuite)
manager.networkMutex.Unlock()
return errs
}
// ContainerIP gets the IP address of the given container on the given network.
func (manager *TestManager) ContainerIP(testSuite TestSuiteID, networkName, containerID string) (string, error) {
manager.networkMutex.RLock()
defer manager.networkMutex.RUnlock()
_, ok := manager.IsTestSuiteRunning(testSuite)
if !ok {
return "", ErrNoSuchTestSuite
}
if containerID == "simulation" {
containerID = manager.simContainerID
}
var networkID string
// networkID "bridge" is special.
if networkName == "bridge" {
var err error
networkID, err = manager.backend.NetworkNameToID(networkName)
if err != nil {
return "", err
}
} else {
var exists bool
networkID, exists = manager.networks[testSuite][networkName]
if !exists {
return "", ErrNetworkNotFound
}
}
ipAddr, err := manager.backend.ContainerIP(containerID, networkID)
if err != nil {
return "", err
}
return ipAddr.String(), nil
}
// ConnectContainer connects the given container to the given network.
func (manager *TestManager) ConnectContainer(testSuite TestSuiteID, networkName, containerID string) error {
manager.networkMutex.RLock()
defer manager.networkMutex.RUnlock()
_, ok := manager.IsTestSuiteRunning(testSuite)
if !ok {
return ErrNoSuchTestSuite
}
if containerID == "simulation" {
containerID = manager.simContainerID
}
networkID, exists := manager.networks[testSuite][networkName]
if !exists {
return ErrNetworkNotFound
}
return manager.backend.ConnectContainer(containerID, networkID)
}
// NetworkExists reports whether a network exists in the current test context.
func (manager *TestManager) NetworkExists(testSuite TestSuiteID, networkName string) bool {
manager.networkMutex.RLock()
defer manager.networkMutex.RUnlock()
_, exists := manager.networks[testSuite][networkName]
return exists
}
// DisconnectContainer disconnects the given container from the given network.
func (manager *TestManager) DisconnectContainer(testSuite TestSuiteID, networkName, containerID string) error {
manager.networkMutex.RLock()
defer manager.networkMutex.RUnlock()
_, ok := manager.IsTestSuiteRunning(testSuite)
if !ok {
return ErrNoSuchTestSuite
}
if containerID == "simulation" {
containerID = manager.simContainerID
}
networkID, exists := manager.networks[testSuite][networkName]
if !exists {
return ErrNetworkNotFound
}
return manager.backend.DisconnectContainer(containerID, networkID)
}
// EndTestSuite ends the test suite by writing the test suite results to the supplied
// stream and removing the test suite from the running list
func (manager *TestManager) EndTestSuite(testSuite TestSuiteID) error {
manager.testSuiteMutex.Lock()
defer manager.testSuiteMutex.Unlock()
return manager.doEndSuite(testSuite)
}
func (manager *TestManager) doEndSuite(testSuite TestSuiteID) error {
suite, ok := manager.runningTestSuites[testSuite]
if !ok {
return ErrNoSuchTestSuite
}
// Check the suite has no running test cases.
for k := range suite.TestCases {
_, ok := manager.runningTestCases[k]
if ok {
return ErrTestSuiteRunning
}
}
if suite.testDetailsFile != nil {
suite.testDetailsFile.Close()
}
// Write the result.
if manager.config.LogDir != "" {
err := writeSuiteFile(suite, manager.config.LogDir)
if err != nil {
return err
}
}
// remove the test suite's left-over docker networks.
if errs := manager.PruneNetworks(testSuite); len(errs) > 0 {
for _, err := range errs {
log15.Error("could not remove network", "err", err)
}
}
// Move the suite to results.
delete(manager.runningTestSuites, testSuite)
manager.results[testSuite] = suite
return nil
}
// StartTestSuite starts a test suite and returns the context id
func (manager *TestManager) StartTestSuite(name string, description string) (TestSuiteID, error) {
manager.testSuiteMutex.Lock()
defer manager.testSuiteMutex.Unlock()
newSuiteID := TestSuiteID(manager.testSuiteCounter)
var (
testLogPath string
testLogFile *os.File
)
if manager.config.LogDir != "" {
testLogPath = fmt.Sprintf("details/%d-%s-%d.log", time.Now().Unix(), manager.simContainerID, newSuiteID)
fp := filepath.Join(manager.config.LogDir, filepath.FromSlash(testLogPath))
if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil {
return 0, err
}
file, err := os.OpenFile(fp, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return 0, err
}
testLogFile = file
}
manager.runningTestSuites[newSuiteID] = &TestSuite{
ID: newSuiteID,
Name: name,
Description: description,
ClientVersions: make(map[string]string),
TestCases: make(map[TestID]*TestCase),
SimulatorLog: manager.simLogFile,
TestDetailsLog: testLogPath,
testDetailsFile: testLogFile,
}
manager.testSuiteCounter++
return newSuiteID, nil
}
// StartTest starts a new test case, returning the testcase id as a context identifier
func (manager *TestManager) StartTest(testSuiteID TestSuiteID, name string, description string) (TestID, error) {
manager.testCaseMutex.Lock()
defer manager.testCaseMutex.Unlock()
// check if the testsuite exists
testSuite, ok := manager.runningTestSuites[testSuiteID]
if !ok {
return 0, ErrNoSuchTestSuite
}
// increment the testcasecounter
manager.testCaseCounter++
var newCaseID = TestID(manager.testCaseCounter)
// create a new test case and add it to the test suite
newTestCase := &TestCase{
Name: name,
Description: description,
Start: time.Now(),
}
// add the test case to the test suite
testSuite.TestCases[newCaseID] = newTestCase
// and to the general map of id:testcases
manager.runningTestCases[newCaseID] = newTestCase
return newCaseID, nil
}
// EndTest finishes the test case
func (manager *TestManager) EndTest(suiteID TestSuiteID, testID TestID, result *TestResult) error {
manager.testCaseMutex.Lock()
defer manager.testCaseMutex.Unlock()
// Check if the test case is running
testSuite, ok := manager.runningTestSuites[suiteID]
if !ok {
return ErrNoSuchTestCase
}
testCase, ok := manager.runningTestCases[testID]
if !ok {
return ErrNoSuchTestCase
}
// Make sure there is at least a result summary
if result == nil {
return ErrNoSummaryResult
}
// Add the results to the test case
testCase.End = time.Now()
if result.Details != "" && testSuite.testDetailsFile != nil {
offsets := manager.writeTestDetails(testSuite, testCase, result.Details)
result.Details = ""
result.LogOffsets = offsets
}
testCase.SummaryResult = *result
// Stop running clients.
for _, v := range testCase.ClientInfo {
if v.wait != nil {
manager.backend.DeleteContainer(v.ID)
v.wait()
v.wait = nil
}
}
// Delete from running, if it's still there.
delete(manager.runningTestCases, testID)
return nil
}
func (manager *TestManager) writeTestDetails(suite *TestSuite, testCase *TestCase, text string) *TestLogOffsets {
var (
begin = suite.testLogOffset
header = "-- " + testCase.Name + "\n"
footer = "\n\n"
offsets TestLogOffsets
)
n, err := fmt.Fprint(suite.testDetailsFile, header, text, footer)
suite.testLogOffset += int64(n)
if err != nil {
log15.Error("could not write details file", "err", err)
// Write was incomplete, so play it safe with the offsets.
offsets.Begin = begin
offsets.End = begin + int64(n)
} else {
// Otherwise, exclude the header and footer in offsets.
// They are just written to make the file more readable.
offsets.Begin = begin + int64(len(header))
offsets.End = offsets.Begin + int64(len(text))
}
return &offsets
}
// RegisterNode is used by test suite hosts to register the creation of a node in the context of a test
func (manager *TestManager) RegisterNode(testID TestID, nodeID string, nodeInfo *ClientInfo) error {
manager.testCaseMutex.Lock()
defer manager.testCaseMutex.Unlock()
// Check if the test case is running
testCase, ok := manager.runningTestCases[testID]
if !ok {
return ErrNoSuchTestCase
}
if testCase.ClientInfo == nil {
testCase.ClientInfo = make(map[string]*ClientInfo)
}
testCase.ClientInfo[nodeID] = nodeInfo
return nil
}
// StopNode stops a client container.
func (manager *TestManager) StopNode(testID TestID, nodeID string) error {
manager.testCaseMutex.Lock()
defer manager.testCaseMutex.Unlock()
testCase, ok := manager.runningTestCases[testID]
if !ok {
return ErrNoSuchNode
}
nodeInfo, ok := testCase.ClientInfo[nodeID]
if !ok {
return ErrNoSuchNode
}
// Stop the container.
if nodeInfo.wait != nil {
if err := manager.backend.DeleteContainer(nodeInfo.ID); err != nil {
return fmt.Errorf("unable to stop client: %v", err)
}
nodeInfo.wait()
nodeInfo.wait = nil
}
return nil
}
// PauseNode pauses a client container.
func (manager *TestManager) PauseNode(testID TestID, nodeID string) error {
manager.testCaseMutex.Lock()
defer manager.testCaseMutex.Unlock()
testCase, ok := manager.runningTestCases[testID]
if !ok {
return ErrNoSuchNode
}
nodeInfo, ok := testCase.ClientInfo[nodeID]
if !ok {
return ErrNoSuchNode
}
// Pause the container.
if err := manager.backend.PauseContainer(nodeInfo.ID); err != nil {
return fmt.Errorf("unable to pause client: %v", err)
}
return nil
}
// UnpauseNode unpauses a client container.
func (manager *TestManager) UnpauseNode(testID TestID, nodeID string) error {
manager.testCaseMutex.Lock()
defer manager.testCaseMutex.Unlock()
testCase, ok := manager.runningTestCases[testID]
if !ok {
return ErrNoSuchNode
}
nodeInfo, ok := testCase.ClientInfo[nodeID]
if !ok {
return ErrNoSuchNode
}
// Unpause the container.
if err := manager.backend.UnpauseContainer(nodeInfo.ID); err != nil {
return fmt.Errorf("unable to unpause client: %v", err)
}
return nil
}
// writeSuiteFile writes the simulation result to the log directory.
func writeSuiteFile(s *TestSuite, logdir string) error {
suiteData, err := json.Marshal(s)
if err != nil {
return err
}
// Randomize the name, but make it so that it's ordered by date - makes cleanups easier
b := make([]byte, 16)
rand.Read(b)
suiteFileName := fmt.Sprintf("%v-%x.json", time.Now().Unix(), b)
suiteFile := filepath.Join(logdir, suiteFileName)
// Write it.
return os.WriteFile(suiteFile, suiteData, 0644)
}