Esempio n. 1
0
    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)
Esempio n. 2
0
 def test_delete_first_version(self):
     """Delete the first repository version."""
     delete_version(self.repo, self.repo_version_hrefs[0])
     with self.assertRaises(HTTPError):
         get_content(self.repo, self.repo_version_hrefs[0])
     for repo_version_href in self.repo_version_hrefs[1:]:
         artifact_paths = get_artifact_paths(self.repo, repo_version_href)
         self.assertIn(self.content[0]['artifact'], artifact_paths)
Esempio n. 3
0
 def test_delete_first_version(self):
     """Delete the first repository version."""
     delete_version(self.repo, self.repo_version_hrefs[0])
     with self.assertRaises(HTTPError):
         get_content(self.repo, self.repo_version_hrefs[0])
     for repo_version_href in self.repo_version_hrefs[1:]:
         artifact_paths = get_artifact_paths(self.repo, repo_version_href)
         self.assertIn(self.content[0]['artifact'], artifact_paths)
Esempio n. 4
0
 def test_delete_middle_version(self):
     """Delete a middle version."""
     index = randint(1, len(self.repo_version_hrefs) - 2)
     delete_version(self.repo, self.repo_version_hrefs[index])
     with self.assertRaises(HTTPError):
         get_content(self.repo, self.repo_version_hrefs[index])
     for repo_version_href in self.repo_version_hrefs[index + 1:]:
         artifact_paths = get_artifact_paths(self.repo, repo_version_href)
         self.assertIn(self.content[index]['artifact'], artifact_paths)
Esempio n. 5
0
 def test_delete_middle_version(self):
     """Delete a middle version."""
     index = randint(1, len(self.repo_version_hrefs) - 2)
     delete_version(self.repo, self.repo_version_hrefs[index])
     with self.assertRaises(HTTPError):
         get_content(self.repo, self.repo_version_hrefs[index])
     for repo_version_href in self.repo_version_hrefs[index + 1:]:
         artifact_paths = get_artifact_paths(self.repo, repo_version_href)
         self.assertIn(self.content[index]['artifact'], artifact_paths)
Esempio n. 6
0
    def test_delete_all_versions(self):
        """Attempt to delete all versions."""
        for repo_version_href in self.repo_version_hrefs[:-1]:
            delete_version(self.repo, repo_version_href)

        with self.assertRaises(TaskReportError) as ctx:
            delete_version(self.repo, self.repo_version_hrefs[-1])

        self.assertIn("Cannot delete repository version.",
                      ctx.exception.task["error"]["description"])
Esempio n. 7
0
    def test_delete_publication(self):
        """Delete a publication.

        Delete a repository version, and verify the associated publication is
        also deleted.
        """
        publication = create_file_publication(self.cfg, self.repo)
        delete_version(self.repo)

        with self.assertRaises(HTTPError):
            self.client.get(publication["pulp_href"])
Esempio n. 8
0
    def test_delete_publication(self):
        """Delete a publication.

        Delete a repository version, and verify the associated publication is
        also deleted.
        """
        publisher = self.client.post(FILE_PUBLISHER_PATH, gen_publisher())
        self.addCleanup(self.client.delete, publisher['_href'])
        publication = publish(self.cfg, publisher, self.repo)
        delete_version(self.repo)
        with self.assertRaises(HTTPError):
            self.client.get(publication['_href'])
Esempio n. 9
0
    def test_delete_publication(self):
        """Delete a publication.

        Delete a repository version, and verify the associated publication is
        also deleted.
        """
        publisher = self.client.post(FILE_PUBLISHER_PATH, gen_publisher())
        self.addCleanup(self.client.delete, publisher['_href'])
        publication = publish(self.cfg, publisher, self.repo)
        delete_version(self.repo)
        with self.assertRaises(HTTPError):
            self.client.get(publication['_href'])
Esempio n. 10
0
    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 = self.api_client.post(FILE_REPO_PATH, gen_repo())
        self.addCleanup(self.api_client.delete, repo["pulp_href"])

        body = gen_file_remote()
        remote = self.api_client.post(FILE_REMOTE_PATH, body)
        self.addCleanup(self.api_client.delete, remote["pulp_href"])

        sync(self.cfg, remote, repo)
        repo = self.api_client.get(repo["pulp_href"])
        content = choice(get_content(repo)[FILE_CONTENT_NAME])

        # Create an orphan content unit.
        modify_repo(self.cfg, repo, remove_units=[content])

        if settings.DEFAULT_FILE_STORAGE == "pulpcore.app.models.storage.FileSystem":
            # Verify that the artifact is present on disk.
            artifact_path = os.path.join(
                MEDIA_PATH,
                self.api_client.get(content["artifact"])["file"])
            cmd = ("ls", artifact_path)
            self.cli_client.run(cmd, sudo=True)

        # Delete first repo version. The previous removed content unit will be
        # an orphan.
        delete_version(repo, get_versions(repo)[1]["pulp_href"])
        content_units = self.api_client.get(FILE_CONTENT_PATH)["results"]
        self.assertIn(content, content_units)

        delete_orphans()
        content_units = self.api_client.get(FILE_CONTENT_PATH)["results"]
        self.assertNotIn(content, content_units)

        if settings.DEFAULT_FILE_STORAGE == "pulpcore.app.models.storage.FileSystem":
            # Verify that the artifact was removed from disk.
            with self.assertRaises(CalledProcessError):
                self.cli_client.run(cmd)
Esempio n. 11
0
    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 = self.api_client.post(REPO_PATH, gen_repo())
        self.addCleanup(self.api_client.delete, repo['pulp_href'])

        body = gen_file_remote()
        remote = self.api_client.post(FILE_REMOTE_PATH, body)
        self.addCleanup(self.api_client.delete, remote['pulp_href'])

        sync(self.cfg, remote, repo)
        repo = self.api_client.get(repo['pulp_href'])
        content = choice(get_content(repo)[FILE_CONTENT_NAME])

        # Create an orphan content unit.
        self.api_client.post(repo['versions_href'],
                             {'remove_content_units': [content['pulp_href']]})

        # Verify that the artifact is present on disk.
        artifact_path = os.path.join(
            MEDIA_PATH,
            self.api_client.get(content['artifact'])['file'])
        cmd = ('ls', artifact_path)
        self.cli_client.run(cmd, sudo=True)

        # Delete first repo version. The previous removed content unit will be
        # an orphan.
        delete_version(repo, get_versions(repo)[0]['pulp_href'])
        content_units = self.api_client.get(FILE_CONTENT_PATH)['results']
        self.assertIn(content, content_units)

        delete_orphans()
        content_units = self.api_client.get(FILE_CONTENT_PATH)['results']
        self.assertNotIn(content, content_units)

        # Verify that the artifact was removed from disk.
        with self.assertRaises(CalledProcessError):
            self.cli_client.run(cmd)
Esempio n. 12
0
    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 = self.api_client.post(REPO_PATH, gen_repo())
        self.addCleanup(self.api_client.delete, repo['_href'])

        body = gen_file_remote()
        remote = self.api_client.post(FILE_REMOTE_PATH, body)
        self.addCleanup(self.api_client.delete, remote['_href'])

        sync(self.cfg, remote, repo)
        repo = self.api_client.get(repo['_href'])
        content = choice(get_content(repo)[FILE_CONTENT_NAME])

        # Create an orphan content unit.
        self.api_client.post(
            repo['_versions_href'],
            {'remove_content_units': [content['_href']]}
        )

        # Verify that the artifact is present on disk.
        artifact_path = self.api_client.get(content['_artifact'])['file']
        cmd = ('ls', artifact_path)
        self.cli_client.run(cmd, sudo=True)

        # Delete first repo version. The previous removed content unit will be
        # an orphan.
        delete_version(repo, get_versions(repo)[0]['_href'])
        content_units = self.api_client.get(FILE_CONTENT_PATH)['results']
        self.assertIn(content, content_units)

        delete_orphans()
        content_units = self.api_client.get(FILE_CONTENT_PATH)['results']
        self.assertNotIn(content, content_units)

        # Verify that the artifact was removed from disk.
        with self.assertRaises(CalledProcessError):
            self.cli_client.run(cmd)
Esempio n. 13
0
    def test_clean_specific_orphans(self):
        """Test whether the `content_hrefs` param removes specific orphans but not others"""
        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)

        # Create two orphaned content units.
        content_a = get_content(
            repo.to_dict())[FILE_CONTENT_NAME][0]["pulp_href"]
        content_b = get_content(
            repo.to_dict())[FILE_CONTENT_NAME][1]["pulp_href"]
        content_to_remove = dict(remove_content_units=[content_a, content_b])
        repo_api.modify(repo.pulp_href, content_to_remove)

        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_a, content_units_href)
        self.assertIn(content_b, content_units_href)

        content_hrefs_dict = {"content_hrefs": [content_a]}
        orphans_response = self.orphans_cleanup_api.cleanup(content_hrefs_dict)
        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_a, content_units_href)
        self.assertIn(content_b, content_units_href)
Esempio n. 14
0
    def test_delete_last_version(self):
        """Delete the last repository version.

        Create a new repository version from the second-to-last repository
        version. Verify that the content unit from the old last repository
        version is not in the new last repository version.
        """
        # Delete the last repo version.
        delete_version(self.repo, self.repo_version_hrefs[-1])
        with self.assertRaises(HTTPError):
            get_content(self.repo, self.repo_version_hrefs[-1])

        # Make new repo version from new last repo version.
        self.client.post(self.repo['_versions_href'],
                         {'add_content_units': [self.content[-1]['_href']]})
        self.repo = self.client.get(self.repo['_href'])
        artifact_paths = get_artifact_paths(self.repo)
        self.assertNotIn(self.content[-2]['artifact'], artifact_paths)
        self.assertIn(self.content[-1]['artifact'], artifact_paths)
Esempio n. 15
0
    def test_delete_last_version(self):
        """Delete the last repository version.

        Create a new repository version from the second-to-last repository
        version. Verify that the content unit from the old last repository
        version is not in the new last repository version.
        """
        # Delete the last repo version.
        delete_version(self.repo, self.repo_version_hrefs[-1])
        with self.assertRaises(HTTPError):
            get_content(self.repo, self.repo_version_hrefs[-1])

        # Make new repo version from new last repo version.
        self.client.post(
            self.repo['_versions_href'],
            {'add_content_units': [self.content[-1]['_href']]}
        )
        self.repo = self.client.get(self.repo['_href'])
        artifact_paths = get_artifact_paths(self.repo)
        self.assertNotIn(self.content[-2]['artifact'], artifact_paths)
        self.assertIn(self.content[-1]['artifact'], artifact_paths)
Esempio n. 16
0
 def test_delete_first_version(self):
     """Attempt to delete the first repository version."""
     with self.assertRaises(HTTPError):
         delete_version(self.repo, self.repo_version_hrefs[0])
Esempio n. 17
0
 def test_deleted_version_filter(self):
     """Delete a repository version and filter by its number."""
     numbers = self.get_repo_versions_attr('number')
     delete_version(self.repo)
     versions = get_versions(self.repo, {'number': numbers[-1]})
     self.assertEqual(len(versions), 0, versions)
Esempio n. 18
0
 def test_delete_first_version(self):
     """Delete the first repository version (version 0)."""
     delete_version(self.repo, self.repo_version_hrefs[0])
Esempio n. 19
0
 def test_deleted_version_filter(self):
     """Delete a repository version and filter by its number."""
     numbers = self.get_repo_versions_attr("number")
     delete_version(self.repo)
     versions = get_versions(self.repo, {"number": numbers[-1]})
     self.assertEqual(len(versions), 0, versions)
Esempio n. 20
0
    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)