def test_verify_image_success(self, requests_mock, open_mock, md5_mock): image_info = _build_fake_image_info() response = requests_mock.return_value response.status_code = 200 hexdigest_mock = md5_mock.return_value.hexdigest hexdigest_mock.return_value = image_info['checksum'] image_location = '/foo/bar' image_download = standby.ImageDownload(image_info) image_download.verify_image(image_location)
def test_verify_image_failure(self, requests_mock, open_mock, md5_mock): image_info = _build_fake_image_info() response = requests_mock.return_value response.status_code = 200 image_download = standby.ImageDownload(image_info) image_location = '/foo/bar' hexdigest_mock = md5_mock.return_value.hexdigest hexdigest_mock.return_value = 'invalid-checksum' self.assertRaises(errors.ImageChecksumError, image_download.verify_image, image_location)
def test_verify_image_success_with_md5_fallback(self, requests_mock, open_mock, md5_mock): image_info = _build_fake_image_info() image_info['os_hash_algo'] = 'algo-beyond-milky-way' image_info['os_hash_value'] = 'mysterious-alien-codes' response = requests_mock.return_value response.status_code = 200 hexdigest_mock = md5_mock.return_value.hexdigest hexdigest_mock.return_value = image_info['checksum'] image_location = '/foo/bar' image_download = standby.ImageDownload(image_info) image_download.verify_image(image_location)
def test_verify_image_success_with_new_hash_fields(self, requests_mock, open_mock, hashlib_mock): image_info = _build_fake_image_info() image_info['os_hash_algo'] = 'sha512' image_info['os_hash_value'] = 'fake-sha512-value' response = requests_mock.return_value response.status_code = 200 hexdigest_mock = hashlib_mock.return_value.hexdigest hexdigest_mock.return_value = image_info['os_hash_value'] image_location = '/foo/bar' image_download = standby.ImageDownload(image_info) image_download.verify_image(image_location) hashlib_mock.assert_called_with('sha512')
def test_download_image(self, requests_mock, md5_mock): content = ['SpongeBob', 'SquarePants'] response = requests_mock.return_value response.status_code = 200 response.iter_content.return_value = content image_info = _build_fake_image_info() md5_mock.return_value.hexdigest.return_value = image_info['checksum'] image_download = standby.ImageDownload(image_info) self.assertEqual(content, list(image_download)) requests_mock.assert_called_once_with(image_info['urls'][0], stream=True, proxies={}) self.assertEqual(image_info['checksum'], image_download.md5sum())
def test_verify_image_failure_with_new_hash_fields(self, requests_mock, open_mock, hashlib_mock): image_info = _build_fake_image_info() image_info['os_hash_algo'] = 'sha512' image_info['os_hash_value'] = 'fake-sha512-value' response = requests_mock.return_value response.status_code = 200 image_download = standby.ImageDownload(image_info) image_location = '/foo/bar' hexdigest_mock = hashlib_mock.return_value.hexdigest hexdigest_mock.return_value = 'invalid-checksum' self.assertRaises(errors.ImageChecksumError, image_download.verify_image, image_location) hashlib_mock.assert_called_with('sha512')