def _unit_inventory(self, request): """ Build the unit inventory. :param request: A synchronization request. :type request: SyncRequest :return: The built inventory. :rtype: UnitInventory """ # fetch child units try: conduit = NodesConduit() child_units = conduit.get_units(request.repo_id) except NodeError: raise except Exception: log.exception(request.repo_id) raise GetChildUnitsError(request.repo_id) # fetch parent units try: request.progress.begin_manifest_download() url = request.config.get(constants.MANIFEST_URL_KEYWORD) manifest = Manifest() manifest.fetch(url, request.working_dir, request.downloader) manifest.fetch_units(url, request.downloader) parent_units = manifest.get_units() except NodeError: raise except Exception: log.exception(request.repo_id) raise GetParentUnitsError(request.repo_id) return UnitInventory(parent_units, child_units)
def test_delete_units_exception(self, *unused): # Setup request = self.request() # Test unit = dict(unit_id='abc', type_id='T', unit_key={}, metadata={}) inventory = UnitInventory(BASE_URL, [], [unit]) strategy = strategies.ImporterStrategy() strategy._delete_units(request, inventory) self.assertEqual(len(request.summary.errors), 1) self.assertEqual(request.summary.errors[0].error_id, error.DeleteUnitError.ERROR_ID)
def test_cancel_at_delete_units(self): # Setup request = self.request(1) unit = dict(unit_id='abc', type_id='T', unit_key={}, metadata={}) inventory = UnitInventory(TestManifest([]), [unit]) request.conduit.remove_unit = Mock() # Test strategy = ImporterStrategy() strategy._delete_units(request, inventory) self.assertEqual(request.cancelled_call_count, 1) request.conduit.remove_unit.assert_not_called()
def test_cancel_at_add_units(self): # Setup request = self.request(1) request.downloader.download = Mock() unit = dict(unit_id='abc', type_id='T', unit_key={}, metadata={}) units = [unit] inventory = UnitInventory(TestManifest(units), []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancelled_call_count, 1) self.assertFalse(request.downloader.download.called)
def test_cancel_at_add_units(self, mock_add_unit): # Setup request = self.request(1) request.downloader.download = Mock() unit = dict(unit_id='abc', type_id='T', unit_key={}, metadata={}) units = [unit] manifest = TestManifest(units) inventory = UnitInventory(BASE_URL, manifest.get_units(), []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancel_event.call_count, 1) self.assertEqual(mock_add_unit.call_count, 0)
def test_cancel_just_before_downloading(self): # Setup request = self.request(2) request.downloader.download = Mock() download = dict(url='http://redhat.com/file') unit = dict(unit_id='123', type_id='T', unit_key={}, metadata={}, _download=download, _storage_path='/tmp/file') units = [(unit, None)] inventory = UnitInventory(units, []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancelled_call_count, 2) self.assertFalse(request.downloader.download.called)
def test_cancel_just_before_downloading(self): # Setup unit_id = str(uuid4()) request = self.request(2) request.downloader.download = Mock() download = dict(url='http://redhat.com/%s' % unit_id) unit = dict(unit_id=unit_id, type_id='T', unit_key={}, metadata={}, storage_path='/tmp/node/testing/%s' % unit_id, relative_path='testing/%s' % unit_id) units = [unit] inventory = UnitInventory(TestManifest(units), []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancelled_call_count, 2) self.assertFalse(request.downloader.download.called)
def test_cancel_just_before_downloading(self, mock_download): # Setup unit_id = str(uuid4()) request = self.request(2) request.downloader.download = Mock() unit = dict(unit_id=unit_id, type_id='T', unit_key={}, metadata={}, storage_path=os.path.join(self.tmp_dir, unit_id), relative_path=os.path.join(self.tmp_dir, 'testing', unit_id)) units = [unit] manifest = TestManifest(units) inventory = UnitInventory(BASE_URL, manifest.get_units(), []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancel_event.call_count, 2) self.assertFalse(mock_download.called)
def test_cancel_begin_downloading(self): # Setup request = self.request(3) request.downloader = HTTPCurlDownloader(DownloaderConfig()) request.downloader.download = Mock( side_effect=request.downloader.download) request.downloader.cancel = Mock() download = dict(url='http://redhat.com/file') unit = dict(unit_id='123', type_id='T', unit_key={}, metadata={}, _download=download, _storage_path='/tmp/file') units = [(unit, None)] inventory = UnitInventory(units, []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancelled_call_count, 3) self.assertTrue(request.downloader.download.called) self.assertTrue(request.downloader.cancel.called)
def test_cancel_during_download_succeeded(self): # Setup unit_id = str(uuid4()) request = self.request(4) request.downloader = HTTPCurlDownloader(DownloaderConfig()) request.downloader.download = Mock( side_effect=request.downloader.download) request.downloader.cancel = Mock() download = dict(url='http://redhat.com/%s' % unit_id) unit = dict(unit_id=unit_id, type_id='T', unit_key={}, metadata={}, storage_path='/tmp/node/testing/%s' % unit_id, relative_path='testing/%s' % unit_id) units = [unit] inventory = UnitInventory(TestManifest(units), []) # Test strategy = ImporterStrategy() strategy._add_units(request, inventory) self.assertEqual(request.cancelled_call_count, 4) self.assertTrue(request.downloader.download.called) self.assertTrue(request.downloader.cancel.called)
not manifest.is_valid() or not manifest.has_valid_units(): fetched_manifest.write() fetched_manifest.fetch_units() manifest = fetched_manifest if not manifest.is_valid(): raise InvalidManifestError() except NodeError: raise except Exception: _log.exception(request.repo_id) raise GetParentUnitsError(request.repo_id) # build the inventory parent_units = manifest.get_units() base_URL = manifest.publishing_details[constants.BASE_URL] inventory = UnitInventory(base_URL, parent_units, child_units) return inventory def _reset_storage_path(self, unit): """ Reset the storage_path using the storage_dir defined in server.conf and the relative_path injected when the unit was published. :param unit: A published unit. :type unit: dict :return: The re-oriented storage path. :rtype: str """ storage_path = unit.get(constants.STORAGE_PATH) if not storage_path: return storage_dir = pulp_conf.get('server', 'storage_dir')