-
Notifications
You must be signed in to change notification settings - Fork 2
/
traffic_light_test.rb
366 lines (288 loc) · 8.98 KB
/
traffic_light_test.rb
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
require_relative '../test_base'
require_relative '../data/python_pytest'
require_code 'traffic_light'
class TrafficLightTest < TestBase
def self.id58_prefix
'22E'
end
def id58_setup
set_context(
logger:StdoutLoggerSpy.new,
sheller:BashShellerStub.new
)
end
# - - - - - - - - - - - - - - - - -
# red, amber, green
test 'CB0', %w( red traffic-light ) do
rag = "lambda{|so,se,st| :red }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
traffic_light_colour(stdout:Test::Data::PythonPytest::STDOUT_RED)
assert_red
assert_no_fault_info
end
# - - - - - - - - - - - - - - - - -
test 'CB1', %w( amber traffic-light ) do
rag = "lambda{|so,se,st| :amber }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
traffic_light_colour(stdout:Test::Data::PythonPytest::STDOUT_AMBER)
assert_amber
assert_no_fault_info
end
# - - - - - - - - - - - - - - - - -
test 'CB2', %w( green traffic-light ) do
rag = "lambda{|so,se,st| :green }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
traffic_light_colour(stdout:Test::Data::PythonPytest::STDOUT_GREEN)
assert_green
assert_no_fault_info
end
# - - - - - - - - - - - - - - - - -
test 'CB3', %w( read rag-lambda message is logged once ) do
rag = "lambda{|so,se,st| :green }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
assert_log_read_rag_lambda_count 0
traffic_light_colour(stdout:Test::Data::PythonPytest::STDOUT_GREEN)
assert_log_read_rag_lambda_count 1
traffic_light_colour(stdout:Test::Data::PythonPytest::STDOUT_RED)
assert_log_read_rag_lambda_count 1
end
# - - - - - - - - - - - - - - - - -
test 'xJ5', %w( lambdas are cached ) do
rag = "lambda{|so,se,st| :green }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
image_name = python_pytest_image_name
f1 = traffic_light.send('[]', image_name)
f2 = traffic_light.send('[]', image_name)
assert_equal f1.object_id, f2.object_id, :caching
end
# - - - - - - - - - - - - - - - - -
test 'xJ7', %w( lambda status argument is an integer in a string ) do
rag = "lambda{|so,se,st| :red }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
traffic_light_colour(status:'0')
assert_red
assert_no_fault_info
end
# - - - - - - - - - - - - - - - - -
test 'xJ8', %w(
rag-lambda can return a string or a symbol (Postel's Law) ) do
rag = "lambda{|so,se,st| 'red' }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
traffic_light_colour
assert_red
assert_no_fault_info
rag = "lambda{|so,se,st| :red }"
bash_stub_capture(docker_run_command) { [rag, '', 0] }
traffic_light_colour
assert_red
assert_no_fault_info
end
# - - - - - - - - - - - - - - - - -
# faulty
test 'CB4', %w(
image_name with missing rag-lambda file,
always gives colour==faulty,
adds info to log
) do
stderr = "cat: can't open '/usr/local/bin/red_amber_green.rb': No such file or directory"
bash_stub_capture(docker_run_command) { ['', stderr, 1] }
traffic_light_colour
assert_faulty
context = 'image_name must have /usr/local/bin/red_amber_green.rb file'
assert_missing_lambda_logged(context)
end
# - - - - - - - - - - - - - - - - -
test 'CB5', %w(
rag-lambda which raises when eval'd,
gives colour==faulty,
adds message to log
) do
lambda_source = 'not-a-lambda'
bash_stub_capture(docker_run_command) { [lambda_source, '', 0] }
traffic_light_colour
assert_faulty
assert_bad_lambda_logged(
"exception when eval'ing lambda source",
lambda_source,
'SyntaxError',
"(eval):1: syntax error, unexpected '-'\nnot-a-lambda\n ^\n"
)
end
# - - - - - - - - - - - - - - - - -
test 'CB6', %w(
rag-lambda which raises when called,
gives colour==faulty,
adds message to log
) do
lambda_source = "lambda{ |so,se,st| fail RuntimeError, '42' }"
bash_stub_capture(docker_run_command) { [lambda_source, '', 0] }
traffic_light_colour
assert_faulty
assert_bad_lambda_logged(
'exception when calling lambda source',
lambda_source,
'RuntimeError',
'42'
)
end
# - - - - - - - - - - - - - - - - -
test 'CB7', %w(
rag-lambda with too few parameters,
gives colour==faulty,
adds message to log
) do
lambda_source = 'lambda{ |_a,_b| :red }'
bash_stub_capture(docker_run_command) { [lambda_source, '', 0] }
traffic_light_colour
assert_faulty
assert_bad_lambda_logged(
'exception when calling lambda source',
lambda_source,
'ArgumentError',
'wrong number of arguments (given 3, expected 2)'
)
end
# - - - - - - - - - - - - - - - - -
test 'CB8', %w(
rag-lambda with too many parameters,
gives colour==faulty,
adds message to log
) do
lambda_source = 'lambda{ |_a,_b,_c,_d| :red }'
bash_stub_capture(docker_run_command) { [lambda_source, '', 0] }
traffic_light_colour
assert_faulty
assert_bad_lambda_logged(
'exception when calling lambda source',
lambda_source,
'ArgumentError',
'wrong number of arguments (given 3, expected 4)'
)
end
# - - - - - - - - - - - - - - - - -
test 'CB9', %w(
rag-lambda which returns non red/amber/green,
gives colour==faulty,
adds message to log
) do
lambda_source = [
'lambda {|so,se,st|',
' :orange',
'}'
].join("\n")
bash_stub_capture(docker_run_command) { [lambda_source, '', 0] }
traffic_light_colour
assert_faulty
assert_illegal_colour_logged(
"illegal colour; must be one of ['red','amber','green']",
'orange',
lambda_source
)
end
private
def traffic_light_colour(options = {})
@image_name = python_pytest_image_name
@stdout = options.delete(:stdout) || Test::Data::PythonPytest::STDOUT_RED
@stderr = options.delete(:stderr) || 'unused'
@status = options.delete(:status) || 0
@outcome,@fault_info = *traffic_light.colour(@image_name, @stdout, @stderr, @status)
end
def python_pytest_image_name
'cyberdojofoundation/python_pytest'
end
def traffic_light
@traffic_light ||= TrafficLight.new(context)
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_red
assert_equal 'red', @outcome, log
end
def assert_amber
assert_equal 'amber', @outcome, log
end
def assert_green
assert_equal 'green', @outcome, log
end
def assert_faulty
assert_equal 'faulty', @outcome, log
end
def assert_no_fault_info
assert_equal({}, @fault_info, log)
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_log_read_rag_lambda_count(expected)
lines = log.split("\n")
actual = read_red_amber_green_lambda_message_count(lines)
assert_equal expected, actual, lines
end
def read_red_amber_green_lambda_message_count(lines)
message = "Read red-amber-green lambda for #{python_pytest_image_name}"
lines.count { |line| line.include?(message) }
end
# - - - - - - - - - - - - - - - - - - - - - -
def docker_run_command
[ 'docker run --rm --entrypoint=cat',
python_pytest_image_name,
RAG_LAMBDA_FILENAME
].join(' ')
end
RAG_LAMBDA_FILENAME = '/usr/local/bin/red_amber_green.rb'
def bash_stub_capture(command, &block)
stdout,stderr,status = *block.call
context.sheller.capture(command, &block)
@command = command
@command_stdout = stdout
@command_stderr = stderr
@command_status = status
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_missing_lambda_logged(context)
assert_call_info_logged(
context:context,
command:@command,
stdout:@command_stdout.lines,
stderr:@command_stderr.lines,
status:@command_status
)
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_bad_lambda_logged(context, lambda_source, klass, message)
assert_call_info_logged(
context:context,
lambda_source:lambda_source.lines,
class:klass,
message:message.lines
)
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_illegal_colour_logged(context, illegal_colour, lambda_source)
assert_call_info_logged(
context:context,
illegal_colour:illegal_colour,
lambda_source:lambda_source.lines
)
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_call_info_logged(properties)
assert_logged_fault_info({
call:'TrafficLight.colour(image_name,stdout,stderr,status)',
args:{
image_name:@image_name,
stdout:@stdout.lines,
stderr:@stderr.lines,
status:@status
},
exception:properties
})
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_logged_fault_info(hash)
json = JSON.pretty_generate(hash)
assert_logged(json, :logged)
assert_equal hash, @fault_info, :fault_info
end
# - - - - - - - - - - - - - - - - - - - - - -
def assert_logged(expected, context)
assert logged?(expected), "\nLOG:#{log}:\nCONTEXT:#{context}:\nEXPECTED:#{expected}:\n"
end
end