From 7c2fdf7ce0bb1130be4e6b6b6352ba3f3d374967 Mon Sep 17 00:00:00 2001 From: David Moore Date: Tue, 7 Nov 2023 18:08:12 -0500 Subject: [PATCH] Cleanup between parameterized runs --- robottelo/cli/factory.py | 5 ++- robottelo/host_helpers/cli_factory.py | 5 ++- tests/foreman/ui/test_errata.py | 62 ++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 15 deletions(-) diff --git a/robottelo/cli/factory.py b/robottelo/cli/factory.py index 958588ed41d..7d1dd5b612a 100644 --- a/robottelo/cli/factory.py +++ b/robottelo/cli/factory.py @@ -1692,9 +1692,10 @@ def setup_org_for_a_custom_repo(options=None): raise CLIFactoryError(f'Failed to publish new version of content view\n{err.msg}') # Get the content view info cv_info = ContentView.info({'id': cv_id}) - lce_promoted = cv_info['lifecycle-environments'] assert len(cv_info['versions']) > 0 - cvv = sorted(cvv['id'] for cvv in cv_info['versions'])[-1] + cv_info['versions'].sort(key=lambda version: version['id']) + cvv = cv_info['versions'][-1] + lce_promoted = cv_info['lifecycle-environments'] # Promote version to next env try: if env_id not in [int(lce['id']) for lce in lce_promoted]: diff --git a/robottelo/host_helpers/cli_factory.py b/robottelo/host_helpers/cli_factory.py index 0201ab4e103..d27e27ce6e4 100644 --- a/robottelo/host_helpers/cli_factory.py +++ b/robottelo/host_helpers/cli_factory.py @@ -631,14 +631,15 @@ def setup_org_for_a_custom_repo(self, options=None): # Get the version id cv_info = self._satellite.cli.ContentView.info({'id': cv_id}) assert len(cv_info['versions']) > 0 - cvv_id = sorted(cvv['id'] for cvv in cv_info['versions'])[-1] + cv_info['versions'].sort(key=lambda version: version['id']) + cvv = cv_info['versions'][-1] lce_promoted = cv_info['lifecycle-environments'] # Promote version to next env try: if env_id not in [int(lce['id']) for lce in lce_promoted]: self._satellite.cli.ContentView.version_promote( { - 'id': cvv_id, + 'id': cvv['id'], 'organization-id': org_id, 'to-lifecycle-environment-id': env_id, } diff --git a/tests/foreman/ui/test_errata.py b/tests/foreman/ui/test_errata.py index 86338fcefca..9a1a8b8befa 100644 --- a/tests/foreman/ui/test_errata.py +++ b/tests/foreman/ui/test_errata.py @@ -174,7 +174,7 @@ def registered_contenthost( rhel_contenthost, module_org, module_lce, - module_cv, + module_published_cv, module_target_sat, request, repos=[CUSTOM_REPO_URL], @@ -185,8 +185,6 @@ def registered_contenthost( :param repos: list of upstream URLs for custom repositories, default to CUSTOM_REPO_URL """ - module_cv.environment = [module_lce] - module_cv.update(['environment']) activation_key = module_target_sat.api.ActivationKey( organization=module_org, environment=module_lce, @@ -196,20 +194,25 @@ def registered_contenthost( custom_repos = [] for repo_url in repos: rhel_contenthost.create_custom_repos(custom_repo=repo_url) - # Publishes and promotes a new cvv to lce - # Associate org, ak, cv, with custom repo: + # Publishes a new cvv, associates org, ak, cv, with custom repo: custom_repo_info = module_target_sat.cli_factory.setup_org_for_a_custom_repo( { 'url': repo_url, 'organization-id': module_org.id, 'lifecycle-environment-id': module_lce.id, 'activationkey-id': activation_key.id, - 'content-view-id': module_cv.id, + 'content-view-id': module_published_cv.id, } ) custom_products.append(custom_repo_info['product-id']) custom_repos.append(custom_repo_info['repository-id']) + # Promote newest version with all content + module_published_cv = module_published_cv.read() + module_published_cv.version.sort(key=lambda version: version.id) + module_published_cv.version[-1].promote(data={'environment_ids': module_lce.id}) + module_published_cv = module_published_cv.read() + result = rhel_contenthost.register( activation_keys=activation_key.name, target=module_target_sat, @@ -227,9 +230,47 @@ def registered_contenthost( len(result['errors']) == 0 ), f'Failed to sync custom repository [id: {custom_repo_id}]:\n{str(result["errors"])}' - # TODO: @request.addfinalizer: Cleanup to remove created ak, - # contentview versions, products & repositories. - return rhel_contenthost + yield rhel_contenthost + + @request.addfinalizer + # Cleanup for in between parameterized runs + def cleanup(): + nonlocal rhel_contenthost, module_published_cv, custom_repos, custom_products, activation_key + rhel_contenthost.unregister() + activation_key.delete() + # Remove CV from all lifecycle-environments + module_target_sat.cli.ContentView.remove_from_environment( + { + 'id': module_published_cv.id, + 'organization-id': module_org.id, + 'lifecycle-environment-id': module_lce.id, + } + ) + module_target_sat.cli.ContentView.remove_from_environment( + { + 'id': module_published_cv.id, + 'organization-id': module_org.id, + 'lifecycle-environment': 'Library', + } + ) + # Delete all CV versions + module_published_cv = module_published_cv.read() + for version in module_published_cv.version: + version.delete() + # Remove repos from CV, delete all custom repos and products + for repo_id in custom_repos: + module_target_sat.cli.ContentView.remove_repository( + { + 'id': module_published_cv.id, + 'repository-id': repo_id, + } + ) + module_target_sat.api.Repository(id=repo_id).delete() + for product_id in custom_products: + module_target_sat.api.Product(id=product_id).delete() + # Publish a new CV version with no content + module_published_cv = module_published_cv.read() + module_published_cv.publish() @pytest.mark.e2e @@ -241,7 +282,7 @@ def test_end_to_end( request, module_org, module_lce, - module_cv, + module_published_cv, module_target_sat, registered_contenthost, ): @@ -291,7 +332,6 @@ def test_end_to_end( assert len(product_list) > 0 product_list.sort(key=lambda product: product.id) _product = product_list[-1].read() - # _product = module_target_sat.api.Product(id=product_id).read() assert len(_product.repository) == 1 _repository = _product.repository[0].read() # Remove custom package if present, install outdated version