class RepoVersionRetentionTestCase(unittest.TestCase): """Test retain_repo_versions for repositories This test targets the following issues: * `Pulp #8368 <https:://pulp.plan.io/issues/8368>`_ """ @classmethod def setUp(self): """Add content to Pulp.""" self.cfg = config.get_config() self.client = api.Client(self.cfg, api.json_handler) self.core_client = CoreApiClient(configuration=self.cfg.get_bindings_config()) self.file_client = gen_file_client() self.content_api = ContentFilesApi(self.file_client) self.repo_api = RepositoriesFileApi(self.file_client) self.version_api = RepositoriesFileVersionsApi(self.file_client) self.distro_api = DistributionsFileApi(self.file_client) self.publication_api = PublicationsFileApi(self.file_client) delete_orphans() populate_pulp(self.cfg, url=FILE_LARGE_FIXTURE_MANIFEST_URL) self.content = sample(self.content_api.list().results, 3) self.publications = [] def _create_repo_versions(self, repo_attributes={}): self.repo = self.repo_api.create(gen_repo(**repo_attributes)) self.addCleanup(self.repo_api.delete, self.repo.pulp_href) if "autopublish" in repo_attributes and repo_attributes["autopublish"]: self.distro = create_distribution(repository_href=self.repo.pulp_href) self.addCleanup(self.distro_api.delete, self.distro.pulp_href) for content in self.content: result = self.repo_api.modify( self.repo.pulp_href, {"add_content_units": [content.pulp_href]} ) monitor_task(result.task) self.repo = self.repo_api.read(self.repo.pulp_href) self.publications += self.publication_api.list( repository_version=self.repo.latest_version_href ).results def test_retain_repo_versions(self): """Test repo version retention.""" self._create_repo_versions({"retain_repo_versions": 1}) versions = self.version_api.list(file_file_repository_href=self.repo.pulp_href).results self.assertEqual(len(versions), 1) latest_version = self.version_api.read( file_file_repository_version_href=self.repo.latest_version_href ) self.assertEqual(latest_version.number, 3) self.assertEqual(latest_version.content_summary.present["file.file"]["count"], 3) self.assertEqual(latest_version.content_summary.added["file.file"]["count"], 3) def test_retain_repo_versions_on_update(self): """Test repo version retention when retain_repo_versions is set.""" self._create_repo_versions() versions = self.version_api.list(file_file_repository_href=self.repo.pulp_href).results self.assertEqual(len(versions), 4) # update retain_repo_versions to 2 result = self.repo_api.partial_update(self.repo.pulp_href, {"retain_repo_versions": 2}) monitor_task(result.task) versions = self.version_api.list(file_file_repository_href=self.repo.pulp_href).results self.assertEqual(len(versions), 2) latest_version = self.version_api.read( file_file_repository_version_href=self.repo.latest_version_href ) self.assertEqual(latest_version.number, 3) self.assertEqual(latest_version.content_summary.present["file.file"]["count"], 3) self.assertEqual(latest_version.content_summary.added["file.file"]["count"], 1) def test_autodistribute(self): """Test repo version retention with autopublish/autodistribute.""" self._create_repo_versions({"retain_repo_versions": 1, "autopublish": True}) # all but the last publication should be gone for publication in self.publications[:-1]: with self.assertRaises(ApiException) as ae: self.publication_api.read(publication.pulp_href) self.assertEqual(404, ae.exception.status) # check that the last publication is distributed manifest = download_content_unit(self.cfg, self.distro.to_dict(), "PULP_MANIFEST") self.assertEqual(manifest.decode("utf-8").count("\n"), len(self.content))
class CRUDLabelTestCase(BaseLabelTestCase): """CRUD labels on repositories.""" @classmethod def setUpClass(cls): """Create class-wide variables.""" cls.cfg = config.get_config() cls.repo = None def setUp(self): """Create an API client.""" self.client = FileApiClient(self.cfg.get_bindings_config()) self.repo_api = RepositoriesFileApi(self.client) def _create_repo(self, labels={}): attrs = {"name": str(uuid4())} if labels: attrs["pulp_labels"] = labels self.repo = self.repo_api.create(attrs) self.addCleanup(self.repo_api.delete, self.repo.pulp_href) def test_create_repo_with_labels(self): """Create repository with labels.""" labels = {"maiar": "mithrandir"} self._create_repo(labels) self.assertEqual(labels, self.repo.pulp_labels) def test_add_repo_labels(self): """Update repository with labels.""" labels = {"maiar": "mithrandir", "valar": "varda"} self._create_repo() resp = self.repo_api.partial_update(self.repo.pulp_href, {"pulp_labels": labels}) monitor_task(resp.task) self.repo = self.repo_api.read(self.repo.pulp_href) self.assertEqual(labels, self.repo.pulp_labels) def test_update_repo_label(self): """Test updating an existing label.""" labels = {"valar": "varda"} self._create_repo(labels) labels["valar"] = "manwe" resp = self.repo_api.partial_update(self.repo.pulp_href, {"pulp_labels": labels}) monitor_task(resp.task) self.repo = self.repo_api.read(self.repo.pulp_href) self.assertEqual(labels, self.repo.pulp_labels) def test_unset_repo_label(self): """Test unsetting a repo label.""" labels = {"maiar": "mithrandir", "valar": "varda"} self._create_repo(labels) labels.pop("valar") resp = self.repo_api.partial_update(self.repo.pulp_href, {"pulp_labels": labels}) monitor_task(resp.task) self.repo = self.repo_api.read(self.repo.pulp_href) self.assertEqual(1, len(self.repo.pulp_labels)) self.assertEqual(labels, self.repo.pulp_labels) def test_remove_all_repo_labels(self): """Test removing all labels.""" labels = {"maiar": "mithrandir", "valar": "varda"} self._create_repo(labels) resp = self.repo_api.partial_update(self.repo.pulp_href, {"pulp_labels": {}}) monitor_task(resp.task) self.repo = self.repo_api.read(self.repo.pulp_href) self.assertEqual(0, len(self.repo.pulp_labels)) self.assertEqual({}, self.repo.pulp_labels) def test_model_partial_update(self): """Test that labels aren't unset accidentially with PATCH calls.""" labels = {"maiar": "mithrandir", "valar": "varda"} self._create_repo(labels) resp = self.repo_api.partial_update(self.repo.pulp_href, {"name": str(uuid4())}) monitor_task(resp.task) self.repo = self.repo_api.read(self.repo.pulp_href) self.assertEqual(labels, self.repo.pulp_labels) def test_invalid_label_type(self): """Test that label doesn't accept non-dicts""" with self.assertRaises(ApiException) as ae: self._create_repo("morgoth") # str instead of dict self.assertEqual(400, ae.exception.status) self.assertTrue("pulp_labels" in json.loads(ae.exception.body)) def test_invalid_labels(self): """Test that label keys and values are validated.""" with self.assertRaises(ApiException) as ae: self._create_repo({"@": "maia"}) self.assertEqual(400, ae.exception.status) self.assertTrue("pulp_labels" in json.loads(ae.exception.body)) with self.assertRaises(ApiException) as ae: self._create_repo({"arda": "eru,illuvata"}) self.assertEqual(400, ae.exception.status) self.assertTrue("pulp_labels" in json.loads(ae.exception.body))