-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_test.go
211 lines (177 loc) · 4.66 KB
/
scheduler_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
203
204
205
206
207
208
209
210
211
package main
import (
"bytes"
"strings"
"testing"
)
func TestNextRunnableNodesAllReady(t *testing.T) {
node1 := &Node{ID: "node1"}
node2 := &Node{ID: "node2"}
node3 := &Node{ID: "node3"}
node4 := &Node{ID: "node4"}
node3.Dependencies = map[*Node]struct{}{
node1: {},
node2: {},
}
node4.Dependencies = map[*Node]struct{}{
node1: {},
}
waitingNodes := map[*Node]struct{}{
node3: {},
node4: {},
}
doneNodes := map[*Node]struct{}{
node1: {},
node2: {},
}
runnableNodes := nextRunnableNodes(waitingNodes, doneNodes)
expected := map[*Node]struct{}{
node3: {},
node4: {},
}
if len(runnableNodes) != len(expected) {
t.Fatalf("expected to get %d runnable nodes, but got %d", len(expected), len(runnableNodes))
}
var runnableNode *Node
for len(runnableNodes) > 0 {
runnableNode, runnableNodes = runnableNodes[0], runnableNodes[1:]
if _, ok := expected[runnableNode]; !ok {
t.Fatalf("unexpected %s node to be in runnable nodes", runnableNode.ID)
}
delete(expected, runnableNode)
}
}
func TestNextRunnableNodesOneNotReady(t *testing.T) {
node1 := &Node{ID: "node1"}
node2 := &Node{ID: "node2"}
node3 := &Node{ID: "node3"}
node4 := &Node{ID: "node4"}
node3.Dependencies = map[*Node]struct{}{
node1: {},
node2: {},
}
node4.Dependencies = map[*Node]struct{}{
node1: {},
}
waitingNodes := map[*Node]struct{}{
node3: {},
node4: {},
}
doneNodes := map[*Node]struct{}{
node1: {},
}
runnableNodes := nextRunnableNodes(waitingNodes, doneNodes)
expected := map[*Node]struct{}{
node4: {},
}
if len(runnableNodes) != len(expected) {
t.Fatalf("expected to get %d runnable nodes, but got %d", len(expected), len(runnableNodes))
}
var runnableNode *Node
for len(runnableNodes) > 0 {
runnableNode, runnableNodes = runnableNodes[0], runnableNodes[1:]
if _, ok := expected[runnableNode]; !ok {
t.Fatalf("unexpected %s node to be in runnable nodes", runnableNode.ID)
}
delete(expected, runnableNode)
}
}
func TestRunWithAndedCommands(t *testing.T) {
steps := []Step{{Name: "step1", Run: "echo test1 && echo test2"}}
job := Job{Steps: steps}
cfg := Config{
Jobs: map[string]Job{"job1": job},
}
var stdoutBuf, stderrBuf bytes.Buffer
err := Run(cfg, &stdoutBuf, &stderrBuf, 0)
if err != nil {
t.Fatal(err)
}
stdout := stdoutBuf.String()
if !strings.Contains(stdout, "test1") {
t.Fatalf("expected the output to contain test1, but got \"%s\"", stdout)
}
if !strings.Contains(stdout, "test2") {
t.Fatalf("expected the output to contain test2, but got \"%s\"", stdout)
}
stderr := stderrBuf.String()
if stderr != "" {
t.Fatalf("expected the error output to be empty, but got \"%s\"", stderr)
}
}
func TestRunWithCircularDependency(t *testing.T) {
cfg := Config{
Jobs: map[string]Job{
"job1": {Needs: []string{"job2"}},
"job2": {Needs: []string{"job1"}},
},
}
var stdoutBuf bytes.Buffer
err := Run(cfg, &stdoutBuf, nil, 0)
if err == nil {
t.Fatal("expected to get an error")
}
}
func TestRunWithMultipleSteps(t *testing.T) {
steps := []Step{
{Run: "echo step1"},
{Run: "echo step2"},
}
job := Job{Steps: steps}
cfg := Config{
Jobs: map[string]Job{"job1": job},
}
var stdoutBuf, stderrBuf bytes.Buffer
err := Run(cfg, &stdoutBuf, &stderrBuf, 0)
if err != nil {
t.Fatal(err)
}
stdout := strings.TrimSpace(stdoutBuf.String())
lines := strings.Split(stdout, "\n")
if len(lines) != 2 {
t.Fatalf("expected to get %d lines, but got %d lines", 2, len(lines))
}
if lines[0] != "step1" {
t.Fatalf("expected first line to be \"%s\", but got \"%s\"", "step1", lines[0])
}
if lines[1] != "step2" {
t.Fatalf("expected first line to be \"%s\", but got \"%s\"", "step2", lines[1])
}
}
func TestRunWithDependency(t *testing.T) {
job1 := Job{Steps: []Step{{Run: "echo job1"}}}
job2 := Job{Steps: []Step{{Run: "echo job2"}}, Needs: []string{"job1"}}
cfg := Config{
Jobs: map[string]Job{"job1": job1, "job2": job2},
}
var stdoutBuf bytes.Buffer
err := Run(cfg, &stdoutBuf, nil, 0)
if err != nil {
t.Fatal(err)
}
stdout := strings.TrimSpace(stdoutBuf.String())
lines := strings.Split(stdout, "\n")
if len(lines) != 2 {
t.Fatalf("expected to get %d lines, but got %d lines", 2, len(lines))
}
if lines[0] != "job1" {
t.Fatalf("expected first line to be \"%s\", but got \"%s\"", "job1", lines[0])
}
if lines[1] != "job2" {
t.Fatalf("expected first line to be \"%s\", but got \"%s\"", "job2", lines[1])
}
}
func TestRunWithStepHasError(t *testing.T) {
steps := []Step{
{Run: "exit 1"},
}
job := Job{Steps: steps}
cfg := Config{
Jobs: map[string]Job{"job1": job},
}
var stdoutBuf bytes.Buffer
err := Run(cfg, &stdoutBuf, nil, 0)
if err == nil {
t.Fatal("expected to get an error")
}
}