def test_get_software_version_data_unhandled_error_calling_get_direct(self): class GetDirect(object): def __init__(self): self.hits = 0 def __call__(self, *args, **kwargs): self.hits += 1 if self.hits < 2: raise Exception("Fail!") return get_direct(*args, **kwargs) data = json.dumps({"version": "1", "software": "foo"}) with nested( APITestServer("/software/foo/versions/1", code=OK, response=data), patch.object(software, "get_direct", GetDirect()), patch.object(software, "logger") ) as (_, _, logger): yield software.get_software_version_data("foo", "1") # Test the logger output itself since it's the only distinct way of # finding out what part of the code base ran. self.assertIn( "Failed to get data about software", logger.mock_calls[0][1][0]) self.assertIn( "Will retry in %s seconds", logger.mock_calls[0][1][0])
def test_get_software_version_data_unknown_http_code(self): with nested( APITestServer( "/software/foo/versions/1", code=499, response=json.dumps({})), self.assertRaises(Exception) ): yield software.get_software_version_data("foo", "1")
def test_get_software_version_data_internal_server_error_retries(self): data = json.dumps({"version": "1", "software": "foo"}) with APITestServer( "/software/foo/versions/1", code=INTERNAL_SERVER_ERROR, response=data) as server: reactor.callLater(.5, setattr, server.resource, "code", OK) result = yield software.get_software_version_data("foo", "1") self.assertEqual(json.loads(data), result)
def test_get_software_version_data_not_found_error(self): with nested( APITestServer( "/software/foo/versions/1", code=NOT_FOUND, response=json.dumps({})), self.assertRaises(software.VersionNotFound) ): yield software.get_software_version_data("foo", "1")
def test_get_software_version_data_ok(self): data = json.dumps({"version": "1", "software": "foo"}) with APITestServer("/software/foo/versions/1", code=OK, response=data): result = yield software.get_software_version_data("foo", "1") self.assertEqual(json.loads(data), result)