-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler_test.go
293 lines (239 loc) · 8.76 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
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package main
import (
"encoding/json"
"io/ioutil"
"testing"
. "github.com/smartystreets/goconvey/convey"
graph "gopkg.in/r3labs/graph.v2"
)
// markAs : sets the state of a collection of components
func markAs(cs []graph.Component, state string) {
for i := 0; i < len(cs); i++ {
cs[i].SetState(state)
}
}
func loadjsongraph(filename string) (map[string]interface{}, error) {
var ms map[string]interface{}
data, err := ioutil.ReadFile(filename)
if err != nil {
return ms, err
}
err = json.Unmarshal(data, &ms)
return ms, err
}
func cp(eg graph.Component) graph.Component {
g := make(graph.GenericComponent)
for k, v := range *eg.(*graph.GenericComponent) {
g[k] = v
}
return &g
}
func fakeComponent(id string) map[string]interface{} {
c := make(graph.GenericComponent)
c["_component_id"] = id
return c
}
func buildFakeQueryResult(c graph.Component) graph.Component {
gc := c.(*graph.GenericComponent)
(*gc)["components"] = make([]interface{}, 2)
(*gc)["components"].([]interface{})[0] = fakeComponent("component-1")
(*gc)["components"].([]interface{})[1] = fakeComponent("component-2")
return gc
}
func TestScheduler(t *testing.T) {
Convey("Given a new scheduler", t, func() {
bms, err := loadjsongraph("./fixtures/test-graph.json")
if err != nil {
panic(err)
}
ims, err := loadjsongraph("./fixtures/import-graph.json")
if err != nil {
panic(err)
}
var s Scheduler
Convey("When loading a new build graph", func() {
s.graph = graph.New()
gerr := s.graph.Load(bms)
So(gerr, ShouldBeNil)
Convey("And it receives the 'start' component", func() {
start := NewFakeComponent("start")
components, err := s.Receive(start)
Convey("It should return the first actionable components", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 5)
So(components[0].GetID(), ShouldEqual, "instance::db-1")
So(components[1].GetID(), ShouldEqual, "network::web-new")
So(components[2].GetID(), ShouldEqual, "instance::web-1")
So(components[3].GetID(), ShouldEqual, "instance::web-2")
So(components[4].GetID(), ShouldEqual, "instance::web-3")
})
})
Convey("And it receives a component with an 'create' action", func() {
Convey("That has completed successfully", func() {
c := cp(s.graph.ComponentAll("network::web-new"))
c.SetState(STATUSCOMPLETED)
components, err := s.Receive(c)
Convey("It should add it to the components field", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 3)
So(s.graph.Component(c.GetID()), ShouldNotBeNil)
})
Convey("And update it in changes", func() {
So(s.graph.ComponentAll(c.GetID()).GetState(), ShouldEqual, STATUSCOMPLETED)
})
})
Convey("That has errored", func() {
c := cp(s.graph.ComponentAll("network::web-new"))
c.SetState(STATUSERRORED)
components, err := s.Receive(c)
Convey("It should not add it to the components field", func() {
So(err, ShouldNotBeNil)
So(len(components), ShouldEqual, 0)
So(s.graph.Component(c.GetID()), ShouldBeNil)
})
Convey("And update it in changes", func() {
So(s.graph.ComponentAll(c.GetID()).GetState(), ShouldEqual, STATUSERRORED)
})
})
})
Convey("And it receives a component with an 'update' action", func() {
Convey("That has completed successfully", func() {
c := cp(s.graph.ComponentAll("instance::db-1"))
c.SetState(STATUSCOMPLETED)
components, err := s.Receive(c)
Convey("It should update it in the components field", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 1)
So(s.graph.Component(c.GetID()).GetState(), ShouldEqual, STATUSCOMPLETED)
})
Convey("And update it in changes", func() {
So(s.graph.ComponentAll(c.GetID()).GetState(), ShouldEqual, STATUSCOMPLETED)
})
})
Convey("That has errored", func() {
c := cp(s.graph.ComponentAll("instance::db-1"))
c.SetState(STATUSERRORED)
components, err := s.Receive(c)
Convey("It should not update it in the components field", func() {
So(err, ShouldNotBeNil)
So(len(components), ShouldEqual, 0)
So(s.graph.Component(c.GetID()).GetState(), ShouldEqual, "")
})
Convey("And update it in changes", func() {
So(s.graph.ComponentAll(c.GetID()).GetState(), ShouldEqual, STATUSERRORED)
})
})
})
Convey("And it receives a component with an 'delete' action", func() {
Convey("That has completed successfully", func() {
c := cp(s.graph.ComponentAll("instance::web-1"))
c.SetState(STATUSCOMPLETED)
components, err := s.Receive(c)
Convey("It should remove it from the components field", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 0)
So(s.graph.Component(c.GetID()), ShouldBeNil)
})
Convey("And update it in changes", func() {
So(s.graph.ComponentAll(c.GetID()).GetState(), ShouldEqual, STATUSCOMPLETED)
})
})
Convey("That has errored", func() {
c := cp(s.graph.ComponentAll("instance::web-1"))
c.SetState(STATUSERRORED)
components, err := s.Receive(c)
Convey("It should not remove it from the components field", func() {
So(err, ShouldNotBeNil)
So(len(components), ShouldEqual, 0)
So(s.graph.Component(c.GetID()), ShouldNotBeNil)
})
Convey("And update it in changes", func() {
So(s.graph.ComponentAll(c.GetID()).GetState(), ShouldEqual, STATUSERRORED)
})
})
})
Convey("And it receives a completed component", func() {
Convey("Which has dependants to be scheduled", func() {
c := s.graph.ComponentAll("network::web-new")
c.SetState(STATUSCOMPLETED)
components, err := s.Receive(c)
Convey("It should return the next actionable components", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 3)
So(components[0].GetID(), ShouldEqual, "instance::web-new-1")
So(components[1].GetID(), ShouldEqual, "instance::web-new-2")
So(components[2].GetID(), ShouldEqual, "instance::web-new-3")
Convey("And their status should be set to running", func() {
So(components[0].GetState(), ShouldEqual, STATUSRUNNING)
So(components[1].GetState(), ShouldEqual, STATUSRUNNING)
So(components[2].GetState(), ShouldEqual, STATUSRUNNING)
})
})
})
Convey("Which has waiting or running dependencies", func() {
c := s.graph.ComponentAll("instance::web-1")
c.SetState(STATUSCOMPLETED)
components, err := s.Receive(c)
Convey("It should return no components", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 0)
})
})
Convey("Which is the last component", func() {
markAs(s.graph.Changes, STATUSCOMPLETED)
c := cp(s.graph.ComponentAll("network::web"))
s.graph.ComponentAll("network::web").SetState(STATUSRUNNING)
components, err := s.Receive(c)
Convey("It should return no components", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 0)
So(s.Done(), ShouldBeTrue)
})
})
})
Convey("And it receives an errored component", func() {
Convey("When other components are running", func() {
s.graph.ComponentAll("instance::web-1").SetState(STATUSRUNNING)
s.graph.ComponentAll("instance::web-2").SetState(STATUSRUNNING)
c := s.graph.ComponentAll("instance::web-3")
c.SetState(STATUSERRORED)
components, err := s.Receive(c)
Convey("It should wait for the other components to complete", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 0)
})
})
Convey("When other no other components are running", func() {
c := s.graph.ComponentAll("instance::web-3")
c.SetState(STATUSERRORED)
components, err := s.Receive(c)
Convey("It should return an error", func() {
So(err, ShouldNotBeNil)
So(len(components), ShouldEqual, 0)
})
})
})
})
Convey("When loading a new import graph", func() {
s.graph = graph.New()
gerr := s.graph.Load(ims)
So(gerr, ShouldBeNil)
Convey("And it receives an completed query result", func() {
q := cp(s.graph.ComponentAll("vpc::query"))
q.SetState(STATUSCOMPLETED)
c := buildFakeQueryResult(q)
components, err := s.Receive(c)
Convey("It should move the result's components to the components field", func() {
So(err, ShouldBeNil)
So(len(components), ShouldEqual, 0)
So(len(s.graph.Components), ShouldEqual, 3)
So(s.graph.Component("component-1"), ShouldNotBeNil)
So(s.graph.Component("component-2"), ShouldNotBeNil)
})
})
})
})
}