27
27
import time
28
28
import shlex
29
29
30
+ from pylxd .exceptions import NotFound
31
+
30
32
from metabox .core .actions import Start , Expect , Send , SelectTestPlan
31
33
from metabox .core .aggregator import aggregator
32
34
@@ -70,7 +72,7 @@ def __init__(
70
72
self .agent_machine = None
71
73
self .agent_revision = agent_revision
72
74
self .start_session = True
73
- self ._checks = []
75
+ self .failures = []
74
76
self ._ret_code = None
75
77
self ._stdout = ""
76
78
self ._stderr = ""
@@ -82,10 +84,6 @@ def get_output_streams(self):
82
84
return self ._pts .stdout_data_full
83
85
return self ._outstr_full
84
86
85
- def has_passed (self ):
86
- """Check whether all the assertions passed."""
87
- return all (self ._checks )
88
-
89
87
def run (self ):
90
88
# Simple scenarios don't need to specify a START step
91
89
# If there's no START step, add one unless the scenario
@@ -109,9 +107,11 @@ def run(self):
109
107
break
110
108
step .kwargs ["interactive" ] = interactive
111
109
try :
112
- step (self )
113
- except (TimeoutError , ConnectionError ):
114
- self ._checks .append (False )
110
+ # step that fail explicitly return false or raise an exception
111
+ if not step (self ):
112
+ self .failures .append (step )
113
+ except (TimeoutError , ConnectionError , NotFound ):
114
+ self .failures .append (step )
115
115
break
116
116
if self ._pts :
117
117
self ._stdout = self ._pts .stdout_data_full
@@ -126,17 +126,15 @@ def _assign_outcome(self, ret_code, stdout, stderr, outstr_full):
126
126
self ._stderr = stderr
127
127
self ._outstr_full = outstr_full
128
128
129
- # TODO: add storing of what actually failed in the assert methods
130
129
def assert_printed (self , pattern ):
131
130
"""
132
131
Check if during Checkbox execution a line produced that matches the
133
132
pattern.
134
133
:param patter: regular expresion to check against the lines.
135
134
"""
136
135
regex = re .compile (pattern )
137
- self ._checks .append (
138
- bool (regex .search (self ._stdout ))
139
- or bool (regex .search (self ._stderr ))
136
+ return bool (regex .search (self ._stdout )) or bool (
137
+ regex .search (self ._stderr )
140
138
)
141
139
142
140
def assert_not_printed (self , pattern ):
@@ -150,20 +148,20 @@ def assert_not_printed(self, pattern):
150
148
found = regex .search (self ._pts .stdout_data_full )
151
149
else :
152
150
found = regex .search (self ._stdout ) or regex .search (self ._stderr )
153
- self . _checks . append ( not found )
151
+ return not found
154
152
155
153
def assert_ret_code (self , code ):
156
154
"""Check if Checkbox returned given code."""
157
- self . _checks . append ( code == self ._ret_code )
155
+ return code == self ._ret_code
158
156
159
157
def assertIn (self , member , container ):
160
- self . _checks . append ( member in container )
158
+ return member in container
161
159
162
160
def assertEqual (self , first , second ):
163
- self . _checks . append ( first == second )
161
+ return first == second
164
162
165
163
def assertNotEqual (self , first , second ):
166
- self . _checks . append ( first != second )
164
+ return first != second
167
165
168
166
def start (self , cmd = "" , interactive = False , timeout = 0 ):
169
167
if self .mode == "remote" :
@@ -185,6 +183,7 @@ def start(self, cmd="", interactive=False, timeout=0):
185
183
self ._pts = outcome
186
184
else :
187
185
self ._assign_outcome (* outcome )
186
+ return True # return code is checked with a different operator
188
187
189
188
def start_all (self , interactive = False , timeout = 0 ):
190
189
self .start_agent ()
@@ -214,28 +213,33 @@ def start_agent(self, force=False):
214
213
def expect (self , data , timeout = 60 ):
215
214
assert self ._pts is not None
216
215
outcome = self ._pts .expect (data , timeout )
217
- self . _checks . append ( outcome )
216
+ return outcome
218
217
219
218
def expect_not (self , data , timeout = 60 ):
220
219
assert self ._pts is not None
221
220
outcome = self ._pts .expect_not (data , timeout )
222
- self . _checks . append ( outcome )
221
+ return outcome
223
222
224
223
def send (self , data ):
225
224
assert self ._pts is not None
226
225
self ._pts .send (data .encode ("utf-8" ), binary = True )
226
+ # send raises an exception on failure
227
+ return True
227
228
228
229
def sleep (self , secs ):
229
230
time .sleep (secs )
231
+ return True
230
232
231
233
def signal (self , signal ):
232
234
assert self ._pts is not None
233
235
self ._pts .send_signal (signal )
236
+ # same as send, this uses send under the hood
237
+ return True
234
238
235
239
def select_test_plan (self , testplan_id , timeout = 60 ):
236
240
assert self ._pts is not None
237
241
outcome = self ._pts .select_test_plan (testplan_id , timeout )
238
- self . _checks . append ( outcome )
242
+ return outcome
239
243
240
244
def run_cmd (self , cmd , env = {}, interactive = False , timeout = 0 , target = "all" ):
241
245
if self .mode == "remote" :
@@ -268,6 +272,7 @@ def reboot(self, timeout=0, target="all"):
268
272
self .agent_machine .reboot (timeout )
269
273
else :
270
274
self .local_machine .reboot (timeout )
275
+ return True
271
276
272
277
def put (self , filepath , data , mode = None , uid = 1000 , gid = 1000 , target = "all" ):
273
278
if self .mode == "remote" :
@@ -280,6 +285,8 @@ def put(self, filepath, data, mode=None, uid=1000, gid=1000, target="all"):
280
285
self .agent_machine .put (filepath , data , mode , uid , gid )
281
286
else :
282
287
self .local_machine .put (filepath , data , mode , uid , gid )
288
+ # put raises an exception on failure
289
+ return True
283
290
284
291
def switch_on_networking (self , target = "all" ):
285
292
if self .mode == "remote" :
@@ -292,6 +299,7 @@ def switch_on_networking(self, target="all"):
292
299
self .agent_machine .switch_on_networking ()
293
300
else :
294
301
self .local_machine .switch_on_networking ()
302
+ return True
295
303
296
304
def switch_off_networking (self , target = "all" ):
297
305
if self .mode == "remote" :
@@ -304,6 +312,7 @@ def switch_off_networking(self, target="all"):
304
312
self .agent_machine .switch_off_networking ()
305
313
else :
306
314
self .local_machine .switch_off_networking ()
315
+ return True
307
316
308
317
def stop_agent (self ):
309
318
return self .agent_machine .stop_agent ()
@@ -322,15 +331,15 @@ def mktree(self, path, privileged=False, timeout=0, target="all"):
322
331
if privileged :
323
332
cmd = ["sudo" ] + cmd
324
333
cmd_str = shlex .join (cmd )
325
- self .run_cmd (cmd_str , target = target , timeout = timeout )
334
+ return self .run_cmd (cmd_str , target = target , timeout = timeout )
326
335
327
336
def run_manage (self , args , timeout = 0 , target = "all" ):
328
337
"""
329
338
Runs the manage.py script with some arguments
330
339
"""
331
340
path = "/home/ubuntu/checkbox/metabox/metabox/metabox-provider"
332
341
cmd = f"bash -c 'cd { path } ; python3 manage.py { args } '"
333
- self .run_cmd (cmd , target = target , timeout = timeout )
342
+ return self .run_cmd (cmd , target = target , timeout = timeout )
334
343
335
344
def assert_in_file (self , pattern , path ):
336
345
"""
@@ -344,4 +353,4 @@ def assert_in_file(self, pattern, path):
344
353
345
354
result = self .run_cmd (f"cat { path } " )
346
355
regex = re .compile (pattern )
347
- self . _checks . append ( bool (regex .search (result .stdout ) ))
356
+ return bool (regex .search (result .stdout ))
0 commit comments