comparison tests/conftest.py @ 1142:952f3c24a99d

tests: Improve servings tests' error reporting.
author Ludovic Chabant <ludovic@chabant.com>
date Tue, 05 Jun 2018 21:58:53 -0700
parents be550e1f6423
children cf6b2bf042fb
comparison
equal deleted inserted replaced
1141:5d9b2581b8a1 1142:952f3c24a99d
332 332
333 def repr_failure(self, excinfo): 333 def repr_failure(self, excinfo):
334 if isinstance(excinfo.value, UnexpectedBakeOutputError): 334 if isinstance(excinfo.value, UnexpectedBakeOutputError):
335 return ('\n'.join( 335 return ('\n'.join(
336 ['Unexpected bake output. Left is expected output, ' 336 ['Unexpected bake output. Left is expected output, '
337 'right is actual output'] + 337 'right is actual output.'] +
338 excinfo.value.args[0])) 338 excinfo.value.args[0]))
339 elif isinstance(excinfo.value, BakeError): 339 elif isinstance(excinfo.value, BakeError):
340 res = ('\n'.join( 340 res = ('\n'.join(
341 ['Errors occured during bake:'] + 341 ['Errors occured during bake:'] +
342 excinfo.value.args[0])) 342 excinfo.value.args[0]))
388 url, 388 url,
389 "Expected HTTP status '%s', got '%s'" % 389 "Expected HTTP status '%s', got '%s'" %
390 (expected_status, resp.status_code), 390 (expected_status, resp.status_code),
391 resp) 391 resp)
392 392
393 cctx = CompareContext()
394
393 if expected_headers: 395 if expected_headers:
394 for k, v in expected_headers.items(): 396 for k, v in expected_headers.items():
395 assert v == resp.headers.get(k) 397 cmpres = _compare_str(v, resp.headers.get(k), cctx)
398 if cmpres:
399 raise UnexpectedChefServingOutput(cmpres)
396 400
397 actual = resp.data.decode('utf8').rstrip() 401 actual = resp.data.decode('utf8').rstrip()
398 if expected_output: 402 if expected_output:
399 assert expected_output.rstrip() == actual 403 cmpres = _compare_str(expected_output.rstrip(), actual, cctx)
404 if cmpres:
405 raise UnexpectedChefServingOutput(cmpres)
400 406
401 if expected_contains: 407 if expected_contains:
402 assert expected_contains.rstrip() in actual 408 assert expected_contains.rstrip() in actual
403 409
404 def reportinfo(self): 410 def reportinfo(self):
422 elif isinstance(excinfo.value, UnexpectedChefServingError): 428 elif isinstance(excinfo.value, UnexpectedChefServingError):
423 res = str(excinfo.value) 429 res = str(excinfo.value)
424 res += '\nWhile requesting URL: %s' % excinfo.value.url 430 res += '\nWhile requesting URL: %s' % excinfo.value.url
425 res += '\nBody:\n%s' % excinfo.value.resp.data.decode('utf8') 431 res += '\nBody:\n%s' % excinfo.value.resp.data.decode('utf8')
426 return res 432 return res
433 elif isinstance(excinfo.value, UnexpectedChefServingOutput):
434 res = '\n'.join(
435 ["Unexpected serving output. Left is expected output, "
436 "right is actual output."] +
437 excinfo.value.args[0])
438 return res
427 return super(ServeTestItem, self).repr_failure(excinfo) 439 return super(ServeTestItem, self).repr_failure(excinfo)
428 440
429 441
430 class UnexpectedChefServingError(Exception): 442 class UnexpectedChefServingError(Exception):
431 def __init__(self, url, msg, resp): 443 def __init__(self, url, msg, resp):
432 super().__init__(msg) 444 super().__init__(msg)
433 self.url = url 445 self.url = url
434 self.resp = resp 446 self.resp = resp
447
448
449 class UnexpectedChefServingOutput(Exception):
450 pass
435 451
436 452
437 class ServeTestFile(YamlTestFileBase): 453 class ServeTestFile(YamlTestFileBase):
438 __item_class__ = ServeTestItem 454 __item_class__ = ServeTestItem
439 455