-
Notifications
You must be signed in to change notification settings - Fork 134
/
Copy pathcommands_test.go
129 lines (115 loc) · 4.3 KB
/
commands_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
package commands
import (
"context"
"fmt"
"os"
"testing"
"time"
"github.com/tritondatacenter/containerpilot/events"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func TestCommandRunWithTimeoutZero(t *testing.T) {
cmd, _ := NewCommand("sleep 2", time.Duration(0), nil)
got := runtestCommandRun(cmd)
timedout := events.Event{Code: events.ExitFailed, Source: "sleep"}
if got[timedout] != 1 {
t.Fatalf("stopped command prior to test timeout, got events %v", got)
}
}
func TestCommandRunWithTimeoutKilled(t *testing.T) {
cmd, _ := NewCommand("sleep 2", time.Duration(100*time.Millisecond), nil)
cmd.Name = t.Name()
got := runtestCommandRun(cmd)
testTimeout := events.Event{Code: events.TimerExpired, Source: "DebugSubscriberTimeout"}
expired := events.Event{Code: events.ExitFailed, Source: t.Name()}
errMsg := events.Event{Code: events.Error, Source: fmt.Sprintf("%s: signal: killed", cmd.Name)}
if got[testTimeout] > 0 || got[expired] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", expired, errMsg, got)
}
}
func TestCommandRunChildrenKilled(t *testing.T) {
cmd, _ := NewCommand("./testdata/test.sh sleepStuff",
time.Duration(100*time.Millisecond), nil)
cmd.Name = t.Name()
got := runtestCommandRun(cmd)
testTimeout := events.Event{Code: events.TimerExpired, Source: "DebugSubscriberTimeout"}
expired := events.Event{Code: events.ExitFailed, Source: t.Name()}
errMsg := events.Event{Code: events.Error, Source: fmt.Sprintf("%s: signal: killed", cmd.Name)}
if got[testTimeout] > 0 || got[expired] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", expired, errMsg, got)
}
}
func TestCommandRunExecFailed(t *testing.T) {
cmd, _ := NewCommand("./testdata/test.sh failStuff --debug", time.Duration(0), nil)
got := runtestCommandRun(cmd)
failed := events.Event{Code: events.ExitFailed, Source: "./testdata/test.sh"}
errMsg := events.Event{Code: events.Error, Source: "./testdata/test.sh: exit status 255"}
if got[failed] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", failed, errMsg, got)
}
}
func TestCommandRunExecInvalid(t *testing.T) {
cmd, _ := NewCommand("./testdata/invalidCommand", time.Duration(0), nil)
got := runtestCommandRun(cmd)
failed := events.Event{Code: events.ExitFailed, Source: "./testdata/invalidCommand"}
errMsg := events.Event{Code: events.Error,
Source: "fork/exec ./testdata/invalidCommand: no such file or directory"}
if got[failed] != 1 || got[errMsg] != 1 {
t.Fatalf("expected:\n%v\n%v\ngot events:\n%v", failed, errMsg, got)
}
}
func TestEmptyCommand(t *testing.T) {
if cmd, err := NewCommand("", time.Duration(0), nil); cmd != nil || err == nil {
t.Errorf("Expected exit (nil, err) but got %v, %s", cmd, err)
}
}
func TestCommandRunReuseCmd(t *testing.T) {
cmd, _ := NewCommand("true", time.Duration(0), nil)
runtestCommandRun(cmd)
runtestCommandRun(cmd)
}
func TestCommandPassthru(t *testing.T) {
cmd, _ := NewCommand("true", time.Duration(0), nil)
runtestCommandRun(cmd)
assert.Equal(t, cmd.Cmd.Stdout, os.Stdout)
cmd, _ = NewCommand("true", time.Duration(0), log.Fields{"job": "trueDat"})
runtestCommandRun(cmd)
assert.NotEqual(t, cmd.Cmd.Stdout, os.Stdout)
}
func TestEnvName(t *testing.T) {
tests := []struct {
name, input, output string
}{
{"mixed case", "testCase", "TESTCASE"},
{"hyphen", "test-case", "TEST_CASE"},
{"exec no ext", "/bin/to/testCase", "TESTCASE"},
{"exec hyphen", "/bin/to/test-case", "TEST_CASE"},
{"exec ext", "/bin/to/testCase.sh", "TESTCASE"},
{"exec cwd", "./bin/to/testCase.sh", "TESTCASE"},
{"exec hyphen", "/bin/to/test-Case.sh", "TEST_CASE"},
{"exec multi hyphen", "/bin/to/test-Case--now.sh", "TEST_CASE_NOW"},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
cmd, _ := NewCommand(test.input, time.Duration(0), nil)
assert.Equal(t, test.input, cmd.Name)
assert.Equal(t, test.output, cmd.EnvName())
})
}
}
// test helpers
func runtestCommandRun(cmd *Command) map[events.Event]int {
bus := events.NewEventBus()
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
cmd.Run(ctx, bus)
time.Sleep(300 * time.Millisecond)
defer cancel()
bus.Wait()
results := bus.DebugEvents()
got := map[events.Event]int{}
for _, result := range results {
got[result]++
}
return got
}