-
Notifications
You must be signed in to change notification settings - Fork 31
/
runner_test.go
90 lines (82 loc) · 2.4 KB
/
runner_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
package main
import (
"bytes"
"sync"
"syscall"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)
type RunnerTestSuite struct {
suite.Suite
runner *Runner
logs bytes.Buffer
executionGroupLogger *Logger
}
func TestRunnerTestSuite(t *testing.T) {
suite.Run(t, new(RunnerTestSuite))
}
func (s *RunnerTestSuite) SetupTest() {
logger := InitLogger(&LoggerConfig{
Name: "TestRunnerTestSuite",
})
logger.SetOutput(&s.logs)
executionGroups := []*ExecutionGroup{
&ExecutionGroup{
commands: []*Command{
mockCommand("echo", []string{"runner 1.0"}, &s.logs),
mockCommand("echo", []string{"runner 1.1"}, &s.logs),
},
logger: logger,
},
&ExecutionGroup{
commands: []*Command{
mockCommand("echo", []string{"runner 2"}, &s.logs),
},
logger: logger,
},
}
s.runner = InitRunner(&RunnerConfig{
Pipeline: executionGroups,
LogLevel: "trace",
})
s.runner.logger.SetOutput(&s.logs)
}
func (s *RunnerTestSuite) Test_startPipeline() {
s.runner.startPipeline()
assert.Contains(s.T(), s.logs.String(), "starting pipeline")
assert.Contains(s.T(), s.logs.String(), "completed pipeline")
}
func (s *RunnerTestSuite) Test_terminateIfRunning_withoutRunningCommand() {
s.runner.terminateIfRunning()
assert.Contains(s.T(), s.logs.String(), "is not running")
}
func (s *RunnerTestSuite) Test_terminateIfRunning_withRunningCommand() {
commandOfInterest := s.runner.config.Pipeline[0].commands[0]
commandOfInterest.started = true
commandOfInterest.stopped = false
commandOfInterest = s.runner.config.Pipeline[0].commands[1]
commandOfInterest.started = false
commandOfInterest.stopped = false
commandOfInterest = s.runner.config.Pipeline[1].commands[0]
commandOfInterest.started = false
commandOfInterest.stopped = false
commandOfInterest = s.runner.config.Pipeline[0].commands[0]
var wg sync.WaitGroup
wg.Add(1)
go func() {
select {
case signal := <-commandOfInterest.signal:
assert.Equal(s.T(), syscall.SIGINT, signal)
wg.Done()
return
}
}()
s.runner.terminateIfRunning()
wg.Wait()
assert.Contains(s.T(), s.logs.String(), "terminating pipeline")
assert.Contains(s.T(), s.logs.String(), "sending SIGINT to command")
assert.Contains(s.T(), s.logs.String(), "SIGINT received by command")
assert.Contains(s.T(), s.logs.String(), "SIGINT sent to command")
assert.Contains(s.T(), s.logs.String(), "terminated pipeline")
}