def do_create_repo_and_sync(self, client, policy): """ Create a repo and remote (fixture_u1) using `policy`. Sync the repo. Verify that a new version was created, the number of downloads and the number of content units. """ repo = client.post(COOKBOOK_REPO_PATH, gen_repo()) self.addCleanup(client.delete, repo["pulp_href"]) body = gen_remote(fixture_u1.url, policy=policy) remote = client.post(COOKBOOK_REMOTE_PATH, body) self.addCleanup(client.delete, remote["pulp_href"]) # Sync the full repository: self.assert_initial_repo(repo) all_cookbook_count = fixture_u1.cookbook_count() task = self.sync_and_inspect_task_report(remote, repo, all_cookbook_count, policy=policy) repo = client.get(repo["pulp_href"]) latest_version_href = repo["latest_version_href"] self.assertIsNotNone(latest_version_href) self.assertEqual(latest_version_href, task["created_resources"][0]) self.verify_counts(repo, all_cookbook_count, all_cookbook_count, 0) return repo, remote
def do_create_repo_and_sync_twice(self, client, policy, second_policy): """ Create a repo and remote (fixture_u1). Sync the repo twice. The first sync happens with policy `policy`, the second sync with policy `second_policy`. Verify that a new version was created, the number of downloads and the number of content units for both syncs. """ all_cookbook_count = fixture_u1.cookbook_count() repo, remote = self.do_create_repo_and_sync(client, policy) latest_version_href = repo["latest_version_href"] body = gen_remote(fixture_u1.url, policy=second_policy) client.patch(remote["pulp_href"], body) # Sync the full repository again using the second policy. exp_download_count = 0 exp_policy = second_policy if policy != "immediate" and second_policy == "immediate": exp_download_count = all_cookbook_count if policy == "immediate": exp_policy = "immediate" report = self.sync_and_inspect_task_report(remote, repo, exp_download_count, policy=second_policy, mirror=True) repo = client.get(repo["pulp_href"]) if exp_download_count: # When we download the actual artifacts, the respective content units will be replaced. # This looks like adding/deleting all cookbooks. self.assertNotEqual(latest_version_href, repo["latest_version_href"]) self.assertListEqual(report["created_resources"], [repo["latest_version_href"]]) self.verify_counts(repo, all_cookbook_count, exp_download_count, exp_download_count) else: # When we don't download anything, no new repo version is created self.assertEqual(latest_version_href, repo["latest_version_href"]) self.assertListEqual(report["created_resources"], []) self.verify_ids_and_artifacts(repo, exp_policy) return repo, remote
def test_sync_repo_isolation(self): """Sync two repositories with same content but different artifacts. The two repo fixtures u1 and u1_diff_digest contain the same cookbooks by name and version, but differ in the digest of the artifact. Ensure that syncing these two remotes to two repos respectively does not mixup cookbooks. """ client = api.Client(self.cfg, api.json_handler) # Create repo u1 and sync partially repo_u1 = client.post(COOKBOOK_REPO_PATH, gen_repo()) self.addCleanup(client.delete, repo_u1["pulp_href"]) body = gen_remote(fixture_u1.url, cookbooks={fixture_u1.example1_name: ""}) remote_u1 = client.post(COOKBOOK_REMOTE_PATH, body) self.addCleanup(client.delete, remote_u1["pulp_href"]) self.assert_initial_repo(repo_u1) example1_count = fixture_u1.cookbook_count([fixture_u1.example1_name]) self.sync_and_inspect_task_report(remote_u1, repo_u1, example1_count) repo_u1 = client.get(repo_u1["pulp_href"]) self.verify_counts(repo_u1, all_count=example1_count, added_count=example1_count, removed_count=0) # Create repo u1_diff_digest and do a full sync repo_u1_diff_digest = client.post(COOKBOOK_REPO_PATH, gen_repo()) self.addCleanup(client.delete, repo_u1_diff_digest["pulp_href"]) body = gen_remote(fixture_u1_diff_digest.url) remote_u1_diff_digest = client.post(COOKBOOK_REMOTE_PATH, body) self.addCleanup(client.delete, remote_u1_diff_digest["pulp_href"]) self.assert_initial_repo(repo_u1_diff_digest) # u1 and u1_diff_digest must not share content: all cookbooks are added cookbook_count = fixture_u1_diff_digest.cookbook_count() self.sync_and_inspect_task_report(remote_u1_diff_digest, repo_u1_diff_digest, cookbook_count) repo_u1_diff_digest = client.get(repo_u1_diff_digest["pulp_href"]) self.verify_counts( repo_u1_diff_digest, all_count=cookbook_count, added_count=cookbook_count, removed_count=0, ) # Full sync u1 client.patch(remote_u1["pulp_href"], {"cookbooks": {}}) all_cookbook_count = fixture_u1.cookbook_count() self.sync_and_inspect_task_report(remote_u1, repo_u1, all_cookbook_count - example1_count) repo_u1 = client.get(repo_u1["pulp_href"]) self.verify_counts( repo_u1, all_count=all_cookbook_count, added_count=all_cookbook_count - example1_count, removed_count=0, ) # Verify that the same cookbooks (w.r.t. name and version) differ by content_id content_u1_diff_digest = get_cookbook_content(repo_u1_diff_digest) self.assertTrue(content_u1_diff_digest) for c_u1 in get_cookbook_content(repo_u1): for c_u1_diff_digest in content_u1_diff_digest: if (c_u1["name"] == c_u1_diff_digest["name"] and c_u1["version"] == c_u1_diff_digest["version"]): artifact_u1 = client.get(c_u1["artifact"]) self.assertEqual(c_u1["content_id"], artifact_u1["sha256"]) artifact_u1_diff_digest = client.get( c_u1_diff_digest["artifact"]) self.assertEqual(c_u1_diff_digest["content_id"], artifact_u1_diff_digest["sha256"]) self.assertNotEqual(c_u1["content_id"], c_u1_diff_digest["content_id"]) break else: self.fail( f"Found no matching cookbook for {c_u1} in u1_diff_digest")
def do_sync_check(self, policy, repo_stores_remote=False): """Sync repository with the cookbook plugin. In order to sync a repository a remote has to be associated within this repository. When a repository is created this version field is set as None. After a sync the repository version is updated. Do the following: 1. Delete orphan content units, create a repository, and an remote. If repo_stores_remote is true, store the remote in the repo 2. Assert that repository version is None 3. Sync the remote. 4. Assert that repository version is not None and the artifact/content counts 5. Add a filter for a single cookbook name to the remote. 6. Sync the remote. 7. Assert artifact/content counts 8. Change the filter for a single cookbook name in the remote. 9. Sync the remote with "mirror=False". 10. Assert artifact/content counts (content must have been added) """ client = api.Client(self.cfg, api.json_handler) repo, remote = self.do_create_repo_and_sync(client, policy) if repo_stores_remote: client.patch(repo["pulp_href"], {"remote": remote["pulp_href"]}) sync_remote_param = None else: sync_remote_param = remote # Sync the repository with a filter (use mirror mode). all_cookbook_count = fixture_u1.cookbook_count() latest_version_href = repo["latest_version_href"] client.patch(remote["pulp_href"], {"cookbooks": { fixture_u1.example1_name: "" }}) self.sync_and_inspect_task_report(sync_remote_param, repo, 0, policy=policy, mirror=True) repo = client.get(repo["pulp_href"]) self.assertNotEqual(latest_version_href, repo["latest_version_href"]) example1_count = fixture_u1.cookbook_count([fixture_u1.example1_name]) self.verify_counts(repo, example1_count, 0, all_cookbook_count - example1_count) # Sync the repository with another filter and add cookbooks (additive mode (default). client.patch(remote["pulp_href"], {"cookbooks": { fixture_u1.example2_name: "" }}) # Although cookbook content is already present, it is not present in the # repository. Thus, it must be downloaded again. example2_count = fixture_u1.cookbook_count([fixture_u1.example2_name]) self.sync_and_inspect_task_report(sync_remote_param, repo, example2_count, policy=policy) repo = client.get(repo["pulp_href"]) self.assertNotEqual(latest_version_href, repo["latest_version_href"]) self.verify_counts( repo, fixture_u1.cookbook_count( [fixture_u1.example1_name, fixture_u1.example2_name]), fixture_u1.cookbook_count([fixture_u1.example2_name]), 0, ) # Verify that cookbook content_id is of the right type (content_type_id # is hidden by the serializer) self.verify_ids_and_artifacts(repo, policy)
def test_sync(self): """Sync repositories with the cookbook plugin. In order to sync a repository a remote has to be associated within this repository. When a repository is created this version field is set as None. After a sync the repository version is updated. Do the following: 1. Delete orphan content units, create a repository, and an remote. 2. Assert that repository version is None 3. Sync the remote. 4. Assert that repository version is not None and the artifact/content counts 5. Sync the remote one more time. 6. Assert that repository version is different from the previous one. Assert that no artifact was downloaded. 7. Add a filter for a single cookbook name to the remote. 8. Sync the remote. 9. Assert artifact/content counts 10. Change the filter for a single cookbook name in the remote. 11. Sync the remote with "mirror=False". 12. Assert artifact/content counts (content must have been added) """ delete_orphans(self.cfg) client = api.Client(self.cfg, api.json_handler) repo = client.post(REPO_PATH, gen_repo()) self.addCleanup(client.delete, repo['_href']) body = gen_remote(fixture_u1.url) remote = client.post(COOKBOOK_REMOTE_PATH, body) self.addCleanup(client.delete, remote['_href']) # Sync the full repository. self.assertIsNone(repo['_latest_version_href']) all_cookbook_count = fixture_u1.cookbook_count() task = self.sync_and_inspect_task_report(remote, repo, all_cookbook_count) repo = client.get(repo['_href']) latest_version_href = repo['_latest_version_href'] self.assertIsNotNone(latest_version_href) self.assertEqual(latest_version_href, task['created_resources'][0]) self.verify_counts(repo, all_cookbook_count, all_cookbook_count, 0) # Sync the full repository again. self.sync_and_inspect_task_report(remote, repo, 0) repo = client.get(repo['_href']) self.assertNotEqual(latest_version_href, repo['_latest_version_href']) self.verify_counts(repo, all_cookbook_count, 0, 0) # Sync the repository with a filter (mirror mode is the default). client.patch(remote['_href'], {'cookbooks': { fixture_u1.example1_name: '' }}) self.sync_and_inspect_task_report(remote, repo, 0) repo = client.get(repo['_href']) self.assertNotEqual(latest_version_href, repo['_latest_version_href']) example1_count = fixture_u1.cookbook_count([fixture_u1.example1_name]) self.verify_counts(repo, example1_count, 0, all_cookbook_count - example1_count) # Sync the repository with another filter and add cookbooks (mirror=False). client.patch(remote['_href'], {'cookbooks': { fixture_u1.example2_name: '' }}) self.sync_and_inspect_task_report(remote, repo, 0, mirror=False) repo = client.get(repo['_href']) self.assertNotEqual(latest_version_href, repo['_latest_version_href']) self.verify_counts( repo, fixture_u1.cookbook_count( [fixture_u1.example1_name, fixture_u1.example2_name]), fixture_u1.cookbook_count([fixture_u1.example2_name]), 0)