def test__do_import_modules_handles_cancel(self, _add_new_module): """ Make sure _do_import_modules() handles the cancel signal correctly. We'll do this by setting up a side effect with the first module to call cancel so the second never happens. """ swpf = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config) def _side_effect(*args, **kwargs): swpf.cancel() _add_new_module.side_effect = _side_effect metadata = model.RepositoryMetadata() module_1 = model.Module('module_1', '1.0.0', 'simon') module_2 = model.Module('module_2', '2.0.3', 'garfunkel') metadata.modules = [module_1, module_2] swpf._do_import_modules(metadata) # If _add_new_module was called exactly once, then our cancel was successful because the # first call to _add_new_module set the cancel flag, and the loop exited the next time. # Because dictionaries are involved in the order in which the modules get downloaded, we # don't have a documented guarantee about which module will be the one. Therefore, we'll # just assert that only one was downloaded and that it was one of the two. self.assertEqual(_add_new_module.call_count, 1) downloaded_module = _add_new_module.mock_calls[0][1][1] self.assertTrue(downloaded_module in [module_1, module_2])
def test_cancel_downloader_none(self): """ Ensure correct operation of the cancel() method when the downloader is None. """ swpf = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config) # This should not blow up due to the downloader being None swpf.cancel() self.assertEqual(swpf._canceled, True)
def setUp(self): self.working_dir = tempfile.mkdtemp(prefix='puppet-sync-tests') self.repo = Repository('test-repo', working_dir=self.working_dir) self.conduit = MockConduit() self.config = PluginCallConfiguration({}, { constants.CONFIG_FEED: FEED, }) self.method = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config)
def test_cancel_downloader_set(self): """ Ensure correct operation of the cancel() method when the downloader is set. """ swpf = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config) swpf.downloader = mock.MagicMock() swpf.cancel() self.assertEqual(swpf._canceled, True) swpf.downloader.cancel.assert_called_once_with()
def test___init__(self): """ Ensure the __init__() method works properly. """ swpf = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config) self.assertEqual(swpf.repo, self.repo) self.assertEqual(swpf.sync_conduit, self.conduit) self.assertEqual(swpf.config, self.config) self.assertTrue( isinstance(swpf.progress_report, sync_progress.SyncProgressReport)) self.assertEqual(swpf.progress_report.conduit, self.conduit) self.assertEqual(swpf.downloader, None) self.assertEqual(swpf._canceled, False)
def setUp(self): self.working_dir = tempfile.mkdtemp(prefix='puppet-sync-tests') self.repo = Repository('test-repo', working_dir=self.working_dir) self.conduit = MockConduit() self.config = PluginCallConfiguration({}, { constants.CONFIG_FEED: FEED, }) self.method = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config) self.sample_units = [ Module(author='a1', name='n1', version='1.0'), Module(author='a2', name='n2', version='2.0'), Module(author='a3', name='n3', version='3.0') ]
def test_parse_metadata_retrieve_exception_canceled(self, mock_create): # Setup swpf = SynchronizeWithPuppetForge(self.repo, self.conduit, self.config) def _side_effect(*args, **kwargs): swpf.cancel() raise Exception("some download error") mock_create.side_effect = _side_effect # Test report = swpf().build_final_report() # Verify self.assertTrue(report.canceled_flag) pr = swpf.progress_report self.assertEqual(pr.metadata_state, constants.STATE_CANCELED) self.assertEqual(pr.modules_state, constants.STATE_NOT_STARTED)
def sync_repo(self, repo, sync_conduit, config): self.sync_cancelled = False # Supports two methods of synchronization. # 1. Synchronize with a directory containing a pulp manifest and puppet modules. # 2. Synchronize with Puppet Forge. # When the feed URL references a PULP_MANIFEST, the directory synchronization # method is used. Otherwise, the puppet forge synchronization method is used. # synchronize with a directory self.sync_method = SynchronizeWithDirectory(repo, sync_conduit, config) report = self.sync_method() # When fetching the PULP_MANIFEST is not successful, it's assumed that the # feed points to a puppet forge instance and the synchronization is retried # using puppet forge method. if report.metadata_state == constants.STATE_FAILED: self.sync_method = SynchronizeWithPuppetForge(repo, sync_conduit, config) report = self.sync_method() self.sync_method = None return report.build_final_report()