comparison tests/conftest.py @ 1096:2aa6174453c8

tests: Better error reporting for serving tests.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 17 Feb 2018 11:51:06 -0800
parents 63f118c773ee
children be550e1f6423
comparison
equal deleted inserted replaced
1095:c8518f5cedbb 1096:2aa6174453c8
244 244
245 def reportinfo(self): 245 def reportinfo(self):
246 return self.fspath, 0, "bake: %s" % self.name 246 return self.fspath, 0, "bake: %s" % self.name
247 247
248 def repr_failure(self, excinfo): 248 def repr_failure(self, excinfo):
249 if isinstance(excinfo.value, ExpectedChefOutputError): 249 if isinstance(excinfo.value, UnexpectedChefOutputError):
250 return ('\n'.join( 250 return ('\n'.join(
251 ['Unexpected command output. Left is expected output, ' 251 ['Unexpected command output. Left is expected output, '
252 'right is actual output'] + 252 'right is actual output'] +
253 excinfo.value.args[0])) 253 excinfo.value.args[0]))
254 return super(ChefTestItem, self).repr_failure(excinfo) 254 return super(ChefTestItem, self).repr_failure(excinfo)
255 255
256 256
257 class ExpectedChefOutputError(Exception): 257 class UnexpectedChefOutputError(Exception):
258 pass 258 pass
259 259
260 260
261 class ChefTestFile(YamlTestFileBase): 261 class ChefTestFile(YamlTestFileBase):
262 __item_class__ = ChefTestItem 262 __item_class__ = ChefTestItem
347 theme_site=self.is_theme_site) 347 theme_site=self.is_theme_site)
348 server = PieCrustServer(appfactory) 348 server = PieCrustServer(appfactory)
349 349
350 client = Client(server, BaseResponse) 350 client = Client(server, BaseResponse)
351 resp = client.get(url) 351 resp = client.get(url)
352 assert expected_status == resp.status_code 352 if expected_status != resp.status_code:
353 raise UnexpectedChefServingError(
354 url,
355 "Expected HTTP status '%s', got '%s'" %
356 (expected_status, resp.status_code),
357 resp)
353 358
354 if expected_headers: 359 if expected_headers:
355 for k, v in expected_headers.items(): 360 for k, v in expected_headers.items():
356 assert v == resp.headers.get(k) 361 assert v == resp.headers.get(k)
357 362
378 res = '\n'.join( 383 res = '\n'.join(
379 ["HTTP error %s returned:" % excinfo.value.code, 384 ["HTTP error %s returned:" % excinfo.value.code,
380 excinfo.value.description]) 385 excinfo.value.description])
381 res += repr_nested_failure(excinfo) 386 res += repr_nested_failure(excinfo)
382 return res 387 return res
388 elif isinstance(excinfo.value, UnexpectedChefServingError):
389 res = str(excinfo.value)
390 res += '\nWhile requesting URL: %s' % excinfo.value.url
391 res += '\nBody:\n%s' % excinfo.value.resp.data.decode('utf8')
392 return res
383 return super(ServeTestItem, self).repr_failure(excinfo) 393 return super(ServeTestItem, self).repr_failure(excinfo)
394
395
396 class UnexpectedChefServingError(Exception):
397 def __init__(self, url, msg, resp):
398 super().__init__(msg)
399 self.url = url
400 self.resp = resp
384 401
385 402
386 class ServeTestFile(YamlTestFileBase): 403 class ServeTestFile(YamlTestFileBase):
387 __item_class__ = ServeTestItem 404 __item_class__ = ServeTestItem
388 405
452 return None 469 return None
453 470
454 471
455 def _compare_lists(left, right, ctx): 472 def _compare_lists(left, right, ctx):
456 for i in range(min(len(left), len(right))): 473 for i in range(min(len(left), len(right))):
457 l = left[i] 474 lc = left[i]
458 r = right[i] 475 rc = right[i]
459 cmpres = _compare(l, r, ctx) 476 cmpres = _compare(lc, rc, ctx)
460 if cmpres: 477 if cmpres:
461 return cmpres 478 return cmpres
462 if len(left) > len(right): 479 if len(left) > len(right):
463 return (["Left '%s' contains more items. First extra item: " % 480 return (["Left '%s' contains more items. First extra item: " %
464 ctx.path, left[len(right)]]) 481 ctx.path, left[len(right)]])