# Create a FileContent from the artifact filecontent_response = filecontent.create(relative_path="foo.tar.gz", artifact=artifact.pulp_href) created_resources = monitor_task(filecontent_response.task) # Add the new FileContent to a repository version repo_version_data = {"add_content_units": [created_resources[0]]} repo_version_response = filerepositories.modify(repository.pulp_href, repo_version_data) # Monitor the repo version creation task created_resources = monitor_task(repo_version_response.task) repository_version_2 = filerepoversions.read(created_resources[0]) pprint(repository_version_2) # List all the repository versions filerepoversions.list(repository.pulp_href) # Create a publication from the latest version of the repository publish_data = FileFilePublication(repository=repository.pulp_href) publish_response = filepublications.create(publish_data) # Monitor the publish task created_resources = monitor_task(publish_response.task) publication_href = created_resources[0] distribution_data = FileFileDistribution( name="baz25", base_path="foo25", publication=publication_href ) distribution = filedistributions.create(distribution_data) pprint(distribution)
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))