def test_download_succeeded_checksum_bad(self, super_download_succeeded, download_failed, checksum): """ Test the download_succeeded() method when the checksum of the downloaded package is incorrect. """ report = mock.MagicMock() report.data._checksum = 'expected checksum' report.data._checksum_type = 'md5' step = sync.DownloadPackagesStep('sync_step_download_packages', conduit=mock.MagicMock()) checksum.return_value = 'bad checksum' step.download_succeeded(report) # Since the checksum was bad, the superclass download_succeeded should not have been called self.assertEqual(super_download_succeeded.call_count, 0) # The report should have been altered to indicate the failure and then passed to # download_failed() self.assertEqual(report.state, 'failed') self.assertEqual( report.error_report, {'expected_checksum': 'expected checksum', 'actual_checksum': 'bad checksum'}) download_failed.assert_called_once_with(report) # Make sure the checksum was calculated with the correct data checksum.assert_called_once_with(report.destination, 'md5') # The unit should not have been saved since the download failed self.assertEqual(report.data.save.call_count, 0)
def test_download_succeeded_not_unique(self, super_download_succeeded, mock_objects, checksum, mock_associate): """ Test download_succeeded when the checksum is correct, but the unit already exists. """ report = mock.MagicMock() report.destination = '/tmp/foo.tar.gz' report.data.name = 'foo' report.data.version = '1.0.0' report.data._checksum = 'good checksum' report.data._checksum_type = 'md5' step = sync.DownloadPackagesStep('sync_step_download_packages', conduit=mock.MagicMock()) step.parent = mock.MagicMock() checksum.return_value = 'good checksum' report.data.save.side_effect = mongoengine.NotUniqueError step.download_succeeded(report) report.data.set_storage_path.assert_called_once_with( os.path.basename(report.destination)) mock_objects.get.return_value.import_content.assert_called_once_with( report.destination) report.data.save.assert_called_once_with() mock_associate.assert_called_once_with( step.parent.get_repo.return_value.repo_obj, mock_objects.get.return_value) mock_objects.get.assert_called_once_with(filename=report.data.filename)
def test_download_succeeded_not_unique(self, super_download_succeeded, mock_objects, from_archive, checksum, mock_associate): """ Test the download_succeeded() method when the checksum of the downloaded package is correct. """ report = mock.MagicMock() report.destination = '/tmp/foo.tar.gz' report.data = models.Package(name='foo', version='1.0.0', _checksum='good checksum', _checksum_type='md5') step = sync.DownloadPackagesStep('sync_step_download_packages', conduit=mock.MagicMock()) step.parent = mock.MagicMock() checksum.return_value = 'good checksum' package = from_archive.return_value package.name = 'foo' package.version = '1.0.0' package.save.side_effect = mongoengine.NotUniqueError step.download_succeeded(report) package.set_storage_path.assert_called_once_with( os.path.basename(report.destination)) mock_objects.get.return_value.import_content.assert_called_once_with( report.destination) package.save.assert_called_once_with() mock_associate.assert_called_once_with( step.parent.get_repo.return_value.repo_obj, mock_objects.get.return_value) mock_objects.get.assert_called_once_with(name='foo', version='1.0.0')
def test_download_succeeded_checksum_good(self, super_download_succeeded, download_failed, checksum, mock_associate): """ Test the download_succeeded() method when the checksum of the downloaded package is correct. """ report = mock.MagicMock() report.destination = '/tmp/foo.tar.gz' report.data._checksum = 'good checksum' report.data._checksum_type = 'sha512' step = sync.DownloadPackagesStep('sync_step_download_packages', conduit=mock.MagicMock()) step.parent = mock.MagicMock() checksum.return_value = 'good checksum' step.download_succeeded(report) # Download failed should not have been called self.assertEqual(download_failed.call_count, 0) # Make sure the checksum was calculated with the correct data checksum.assert_called_once_with(report.destination, 'sha512') report.data.set_storage_path.assert_called_once_with( os.path.basename(report.destination)) report.data.save.assert_called_once_with() report.data.import_content.assert_called_once_with(report.destination) mock_associate.assert_called_once_with( step.parent.get_repo.return_value.repo_obj, report.data)
def test_download_succeeded_checksum_good(self, super_download_succeeded, download_failed, from_archive, checksum, mock_associate): """ Test the download_succeeded() method when the checksum of the downloaded package is correct. """ report = mock.MagicMock() report.data = models.Package(name='foo', version='1.0.0', _checksum='good checksum', _checksum_type='md5') step = sync.DownloadPackagesStep('sync_step_download_packages', conduit=mock.MagicMock()) step.parent = mock.MagicMock() checksum.return_value = 'good checksum' step.download_succeeded(report) # Download failed should not have been called self.assertEqual(download_failed.call_count, 0) # Make sure the checksum was calculated with the correct data checksum.assert_called_once_with(report.destination, 'md5') # The from_archive method should have been given the destination from_archive.assert_called_once_with(report.destination) from_archive.return_value.save_and_import_content\ .assert_called_once_with(report.destination) mock_associate.assert_called_once_with(step.parent.get_repo.return_value.repo_obj, from_archive.return_value)