def test_download_succeeded_fails_checksum(self, download_failed): """ This test verifies that download_succeeded does the right thing if the checksum fails. Note that we are also implicitly testing that the default behavior is to validate downloads by not setting it in this test. There are two other tests that verify that setting the boolean explicitly is honored. """ self.config.override_config[importer_constants.KEY_VALIDATE] = True iso_sync_run = ISOSyncRun(self.sync_conduit, self.config) destination = os.path.join(self.temp_dir, 'test.txt') with open(destination, 'w') as test_file: test_file.write('Boring test data.') unit = MagicMock() unit.storage_path = destination iso = models.ISO('test.txt', 114, 'wrong checksum', unit) iso.url = 'http://fake.com' report = DownloadReport(iso.url, destination, iso) # Let's fake having downloaded the whole file iso.bytes_downloaded = iso.size report.bytes_downloaded = iso.size iso_sync_run.progress_report._state = SyncProgressReport.STATE_ISOS_IN_PROGRESS iso_sync_run.download_succeeded(report) # Because we fail validation, the save_unit step will not be called self.assertEqual(self.sync_conduit.save_unit.call_count, 0) # The download should be marked failed self.assertEqual(download_failed.call_count, 1) download_failed.assert_called_once_with(report)
def test_download_succeeded_honors_validate_units_set_true(self, download_failed): """ We have a setting that makes download validation optional. This test ensures that download_succeeded() honors that setting. """ # In this config, we will set validate_units to False, which should make our # "wrong_checksum" OK config = importer_mocks.get_basic_config( **{importer_constants.KEY_FEED: 'http://fake.com/iso_feed/', importer_constants.KEY_VALIDATE: True}) iso_sync_run = ISOSyncRun(self.sync_conduit, config) destination = os.path.join(self.temp_dir, 'test.txt') with open(destination, 'w') as test_file: test_file.write('Boring test data.') unit = MagicMock() unit.storage_path = destination iso = models.ISO('test.txt', 114, 'wrong checksum', unit) iso.url = 'http://fake.com' report = DownloadReport(iso.url, destination, iso) # Let's fake having downloaded the whole file iso.bytes_downloaded = iso.size report.bytes_downloaded = iso.size iso_sync_run.progress_report._state = SyncProgressReport.STATE_ISOS_IN_PROGRESS iso_sync_run.download_succeeded(report) # Because we fail validation, the save_unit step will not be called self.assertEqual(self.sync_conduit.save_unit.call_count, 0) # The download should be marked failed self.assertEqual(download_failed.call_count, 1) download_failed.assert_called_once_with(report)
def test_download_succeeded_honors_validate_units_set_false(self, download_failed): """ We have a setting that makes download validation optional. This test ensures that download_succeeded() honors that setting. """ # In this config, we will set validate_units to False, which should make our "wrong_checksum" OK config = importer_mocks.get_basic_config(**{importer_constants.KEY_FEED: 'http://fake.com/iso_feed/', importer_constants.KEY_VALIDATE: False}) iso_sync_run = ISOSyncRun(self.sync_conduit, config) destination = os.path.join(self.temp_dir, 'test.iso') with open(destination, 'w') as test_iso: test_iso.write('What happens when you combine a mosquito with a mountain climber? Nothing. You ' 'can\'t cross a vector with a scalar.') unit = MagicMock() unit.storage_path = destination iso = models.ISO('test.txt', 114, 'wrong checksum', unit) iso.url = 'http://fake.com' report = DownloadReport(iso.url, destination, iso) # Let's fake having downloaded the whole file iso.bytes_downloaded = iso.size report.bytes_downloaded = iso.size iso_sync_run.progress_report._state = SyncProgressReport.STATE_ISOS_IN_PROGRESS iso_sync_run.download_succeeded(report) # The sync conduit should have been called to save the unit self.sync_conduit.save_unit.assert_any_call(unit) # The download should not fail self.assertEqual(download_failed.call_count, 0)
def test_download_succeeded(self, download_failed): destination = os.path.join(self.temp_dir, 'test.txt') with open(destination, 'w') as test_file: test_file.write( 'Descartes walks into a bar and sits down, the bartender walks up to him and says ' '"You, my ' 'man, look like you need a stiff drink." Descartes considers this, and shakes his ' 'head "No, ' 'I don\'t think-" and ceases to exist.') unit = MagicMock() unit.storage_path = destination iso = models.ISO('test.txt', 217, 'a1552efee6f04012bc7e1f3e02c00c6177b08217cead958c47ec83cb8f97f835', unit) iso.url = 'http://fake.com' report = DownloadReport(iso.url, destination, iso) # Simulate having downloaded the whole file iso.bytes_downloaded = iso.size report.bytes_downloaded = iso.size self.iso_sync_run.progress_report._state = SyncProgressReport.STATE_ISOS_IN_PROGRESS self.iso_sync_run.download_succeeded(report) # The sync conduit should have been called to save the unit self.sync_conduit.save_unit.assert_any_call(unit) # The download should not fail self.assertEqual(download_failed.call_count, 0)
def test_download_succeeded_honors_validate_units_set_false( self, download_failed): """ We have a setting that makes download validation optional. This test ensures that download_succeeded() honors that setting. """ # In this config, we will set validate_units to False, which should make our # "wrong_checksum" OK config = importer_mocks.get_basic_config( **{ importer_constants.KEY_FEED: 'http://fake.com/iso_feed/', importer_constants.KEY_VALIDATE: False }) iso_sync_run = ISOSyncRun(self.sync_conduit, config) destination = os.path.join(self.temp_dir, 'test.iso') with open(destination, 'w') as test_iso: test_iso.write( 'What happens when you combine a mosquito with a mountain climber? Nothing. You ' 'can\'t cross a vector with a scalar.') unit = MagicMock() unit.storage_path = destination iso = models.ISO('test.txt', 114, 'wrong checksum', unit) iso.url = 'http://fake.com' report = DownloadReport(iso.url, destination, iso) # Let's fake having downloaded the whole file iso.bytes_downloaded = iso.size report.bytes_downloaded = iso.size iso_sync_run.progress_report._state = SyncProgressReport.STATE_ISOS_IN_PROGRESS iso_sync_run.download_succeeded(report) # The sync conduit should have been called to save the unit self.sync_conduit.save_unit.assert_any_call(unit) # The download should not fail self.assertEqual(download_failed.call_count, 0)