-
Notifications
You must be signed in to change notification settings - Fork 31
/
execution.group_test.go
89 lines (80 loc) · 2.5 KB
/
execution.group_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
package main
import (
"bytes"
"regexp"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type ExecutionGroupTestSuite struct {
suite.Suite
executionGroup *ExecutionGroup
logs bytes.Buffer
logger *Logger
}
func TestExecutionGroup(t *testing.T) {
suite.Run(t, new(ExecutionGroupTestSuite))
}
func (s *ExecutionGroupTestSuite) SetupTest() {
logger := InitLogger(&LoggerConfig{
Name: "ExecutionGroupTestSuite",
Format: "production",
Level: "trace",
})
logger.SetOutput(&s.logs)
s.executionGroup = &ExecutionGroup{
logger: logger,
}
}
func (s *ExecutionGroupTestSuite) TestIsRunning() {
t := s.T()
s.executionGroup.commands = []*Command{
mockCommand("echo", []string{"1"}, &s.logs),
}
s.executionGroup.commands[0].started = false
assert.False(t, s.executionGroup.IsRunning())
s.executionGroup.commands[0].started = true
assert.True(t, s.executionGroup.IsRunning())
s.executionGroup.commands[0].stopped = true
assert.False(t, s.executionGroup.IsRunning())
}
func (s *ExecutionGroupTestSuite) TestRun() {
s.executionGroup.commands = []*Command{
mockCommand("echo", []string{"1"}, &s.logs),
mockCommand("echo", []string{"2"}, &s.logs),
mockCommand("echo", []string{"3"}, &s.logs),
}
s.executionGroup.logger.SetOutput(&s.logs)
t := s.T()
s.executionGroup.Run()
assert.Contains(t, s.logs.String(), "command[echo[1]] is starting")
assert.Contains(t, s.logs.String(), "command[echo[2]] is starting")
assert.Contains(t, s.logs.String(), "command[echo[3]] is starting")
assert.Regexp(t, regexp.MustCompile(`execution group\[\d\] is starting`), s.logs.String())
assert.Regexp(t, regexp.MustCompile(`execution group\[\d\] exited`), s.logs.String())
}
func (s *ExecutionGroupTestSuite) TestTerminate() {
t := s.T()
s.executionGroup.commands = []*Command{
mockCommand("echo", []string{"1"}, &s.logs),
}
s.executionGroup.commands[0].handleInitialisation()
s.executionGroup.commands[0].started = true
s.executionGroup.commands[0].stopped = false
go func() {
select {
case signal := <-s.executionGroup.commands[0].signal:
assert.Equal(t, signal, syscall.SIGINT)
}
}()
s.executionGroup.Terminate()
}
func (s *ExecutionGroupTestSuite) Test_handleCommandStatus() {
t := s.T()
testCommand := mockCommand("echo", []string{"1"}, &s.logs)
s.executionGroup.waitGroup.Add(1)
s.executionGroup.handleCommandStatus(testCommand, nil)
s.executionGroup.waitGroup.Wait()
assert.Contains(t, s.logs.String(), "command[echo[1]] exited without error")
}