def test_second_unit_replaces_the_first(self): """Create a duplicate content unit with different ``artifacts`` and same ``relative_path``. Artifacts are unique by ``relative_path`` and ``file``. """ delete_orphans() content_api = ContentFilesApi(self.client) repo_api = RepositoriesFileApi(self.client) versions_api = RepositoriesFileVersionsApi(self.client) repo = repo_api.create(gen_repo()) self.addCleanup(repo_api.delete, repo.pulp_href) artifact = gen_artifact() # create first content unit. content_attrs = gen_file_content_attrs(artifact) content_attrs["repository"] = repo.pulp_href response = content_api.create(**content_attrs) monitor_task(response.task) artifact = gen_artifact(file=__file__) # create second content unit. second_content_attrs = gen_file_content_attrs(artifact) second_content_attrs["repository"] = repo.pulp_href second_content_attrs["relative_path"] = content_attrs["relative_path"] response = content_api.create(**second_content_attrs) monitor_task(response.task) repo_latest_version = versions_api.read( repo_api.read(repo.pulp_href).latest_version_href) self.assertEqual( repo_latest_version.content_summary.present["file.file"]["count"], 1)
# Create a Repository repository_data = FileFileRepository(name="foo25") repository = filerepositories.create(repository_data) pprint(repository) # Sync a Repository repository_sync_data = RepositorySyncURL(remote=file_remote.pulp_href) sync_response = filerepositories.sync(repository.pulp_href, repository_sync_data) pprint(sync_response) # Monitor the sync task created_resources = monitor_task(sync_response.task) repository_version_1 = filerepoversions.read(created_resources[0]) pprint(repository_version_1) # Create an artifact from a local file file_path = os.path.join(os.environ["GITHUB_WORKSPACE"], ".ci/assets/bindings/test_bindings.py") artifact = artifacts.create(file=file_path) pprint(artifact) # 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)
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))