def test_clean_orphan_artifact(self): """Test whether orphan artifacts units can be clean up.""" repo_api = RepositoriesFileApi(self.api_client) repo = repo_api.create(gen_repo()) self.addCleanup(repo_api.delete, repo.pulp_href) artifacts_api = ArtifactsApi(core_client) artifact = artifacts_api.create(file=__file__) if self.storage == "pulpcore.app.models.storage.FileSystem": cmd = ("ls", os.path.join(self.media_root, artifact.file)) self.cli_client.run(cmd, sudo=True) orphans_response = self.orphans_cleanup_api.cleanup({"orphan_protection_time": 10}) monitor_task(orphans_response.task) # assert artifact was not removed artifacts = artifacts_api.list().count self.assertEqual(artifacts, 1) orphans_response = self.orphans_cleanup_api.cleanup({"orphan_protection_time": 0}) monitor_task(orphans_response.task) with self.assertRaises(ApiException): artifacts_api.read(artifact.pulp_href) if self.storage == "pulpcore.app.models.storage.FileSystem": with self.assertRaises(CalledProcessError): self.cli_client.run(cmd)
def test_clean_orphan_content_unit(self): """Test whether orphaned content units can be cleaned up.""" repo_api = RepositoriesFileApi(self.api_client) remote_api = RemotesFileApi(self.api_client) repo = repo_api.create(gen_repo()) self.addCleanup(repo_api.delete, repo.pulp_href) body = gen_file_remote() remote = remote_api.create(body) self.addCleanup(remote_api.delete, remote.pulp_href) # Sync the repository. self.assertEqual(repo.latest_version_href, f"{repo.pulp_href}versions/0/") repository_sync_data = RepositorySyncURL(remote=remote.pulp_href) sync_response = repo_api.sync(repo.pulp_href, repository_sync_data) monitor_task(sync_response.task) repo = repo_api.read(repo.pulp_href) content = choice(get_content(repo.to_dict())[FILE_CONTENT_NAME]) # Create an orphan content unit. repo_api.modify(repo.pulp_href, dict(remove_content_units=[content["pulp_href"]])) artifacts_api = ArtifactsApi(core_client) if self.storage == "pulpcore.app.models.storage.FileSystem": # Verify that the artifact is present on disk. relative_path = artifacts_api.read(content["artifact"]).file artifact_path = os.path.join(self.media_root, relative_path) cmd = ("ls", artifact_path) self.cli_client.run(cmd, sudo=True) file_contents_api = ContentFilesApi(self.api_client) # Delete first repo version. The previous removed content unit will be # an orphan. delete_version(repo, get_versions(repo.to_dict())[1]["pulp_href"]) content_units = file_contents_api.list().to_dict()["results"] content_units_href = [c["pulp_href"] for c in content_units] self.assertIn(content["pulp_href"], content_units_href) content_before_cleanup = file_contents_api.list().count orphans_response = self.orphans_cleanup_api.cleanup({"orphan_protection_time": 10}) monitor_task(orphans_response.task) # assert content was not removed content_after_cleanup = file_contents_api.list().count self.assertEqual(content_after_cleanup, content_before_cleanup) orphans_response = self.orphans_cleanup_api.cleanup({"orphan_protection_time": 0}) monitor_task(orphans_response.task) content_units = file_contents_api.list().to_dict()["results"] content_units_href = [c["pulp_href"] for c in content_units] self.assertNotIn(content["pulp_href"], content_units_href) if self.storage == "pulpcore.app.models.storage.FileSystem": # Verify that the artifact was removed from disk. with self.assertRaises(CalledProcessError): self.cli_client.run(cmd)
def test_clean_orphan_artifact(self): """Test whether orphan artifacts units can be clean up.""" repo_api = RepositoriesFileApi(self.api_client) repo = repo_api.create(gen_repo()) self.addCleanup(repo_api.delete, repo.pulp_href) artifacts_api = ArtifactsApi(core_client) artifact = artifacts_api.create(file=__file__) if self.storage == "pulpcore.app.models.storage.FileSystem": cmd = ("ls", os.path.join(MEDIA_PATH, artifact.file)) self.cli_client.run(cmd, sudo=True) delete_orphans() with self.assertRaises(ApiException): artifacts_api.read(artifact.pulp_href) if self.storage == "pulpcore.app.models.storage.FileSystem": with self.assertRaises(CalledProcessError): self.cli_client.run(cmd)
def test_clean_orphan_content_unit(self): """Test whether orphan content units can be clean up. Do the following: 1. Create, and sync a repo. 2. Remove a content unit from the repo. This will create a second repository version, and create an orphan content unit. 3. Assert that content unit that was removed from the repo and its artifact are present on disk. 4. Delete orphans. 5. Assert that the orphan content unit was cleaned up, and its artifact is not present on disk. """ repo_api = RepositoriesFileApi(self.api_client) remote_api = RemotesFileApi(self.api_client) repo = repo_api.create(gen_repo()) self.addCleanup(repo_api.delete, repo.pulp_href) body = gen_file_remote() remote = remote_api.create(body) self.addCleanup(remote_api.delete, remote.pulp_href) # Sync the repository. self.assertEqual(repo.latest_version_href, f"{repo.pulp_href}versions/0/") repository_sync_data = RepositorySyncURL(remote=remote.pulp_href) sync_response = repo_api.sync(repo.pulp_href, repository_sync_data) monitor_task(sync_response.task) repo = repo_api.read(repo.pulp_href) content = choice(get_content(repo.to_dict())[FILE_CONTENT_NAME]) # Create an orphan content unit. repo_api.modify(repo.pulp_href, dict(remove_content_units=[content["pulp_href"]])) artifacts_api = ArtifactsApi(core_client) if self.storage == "pulpcore.app.models.storage.FileSystem": # Verify that the artifact is present on disk. artifact_path = os.path.join( MEDIA_PATH, artifacts_api.read(content["artifact"]).file) cmd = ("ls", artifact_path) self.cli_client.run(cmd, sudo=True) file_contents_api = ContentFilesApi(self.api_client) # Delete first repo version. The previous removed content unit will be # an orphan. delete_version(repo, get_versions(repo.to_dict())[1]["pulp_href"]) content_units = file_contents_api.list().to_dict()["results"] content_units_href = [c["pulp_href"] for c in content_units] self.assertIn(content["pulp_href"], content_units_href) delete_orphans() content_units = file_contents_api.list().to_dict()["results"] content_units_href = [c["pulp_href"] for c in content_units] self.assertNotIn(content["pulp_href"], content_units_href) if self.storage == "pulpcore.app.models.storage.FileSystem": # Verify that the artifact was removed from disk. with self.assertRaises(CalledProcessError): self.cli_client.run(cmd)