From 9424b9e8aacdde9e72cdcc679236c82467247d0f Mon Sep 17 00:00:00 2001 From: Peter Ondrejka Date: Fri, 15 Mar 2024 14:44:09 +0100 Subject: [PATCH] addFinalizer in test body (#14036) * reordered addFinalizer in test body * Update tests/foreman/cli/test_errata.py Co-authored-by: Gaurav Talreja * Update tests/foreman/api/test_discoveryrule.py Co-authored-by: Gaurav Talreja * Update tests/foreman/cli/test_errata.py Co-authored-by: Gaurav Talreja * Update tests/foreman/destructive/test_capsule_loadbalancer.py Co-authored-by: Gaurav Talreja --------- Co-authored-by: Gaurav Talreja --- .../api/test_computeresource_libvirt.py | 13 +-- tests/foreman/api/test_discoveryrule.py | 13 +-- tests/foreman/api/test_http_proxy.py | 5 + tests/foreman/api/test_parameters.py | 9 +- tests/foreman/cli/test_classparameters.py | 2 +- tests/foreman/cli/test_computeresource_osp.py | 2 +- tests/foreman/cli/test_discoveredhost.py | 8 +- tests/foreman/cli/test_errata.py | 15 ++- tests/foreman/cli/test_hammer.py | 13 +-- tests/foreman/cli/test_satellitesync.py | 2 +- tests/foreman/cli/test_subnet.py | 4 +- .../destructive/test_capsule_loadbalancer.py | 8 +- tests/foreman/maintain/test_advanced.py | 63 +++++++------ tests/foreman/maintain/test_health.py | 91 ++++++++++--------- .../foreman/maintain/test_maintenance_mode.py | 9 +- tests/foreman/maintain/test_packages.py | 28 +++--- tests/foreman/ui/test_ansible.py | 13 +-- tests/foreman/ui/test_computeresource_gce.py | 31 ++++--- .../ui/test_computeresource_libvirt.py | 4 +- tests/upgrades/test_contentview.py | 2 +- tests/upgrades/test_host.py | 2 +- tests/upgrades/test_provisioningtemplate.py | 2 +- tests/upgrades/test_subscription.py | 2 +- tests/upgrades/test_usergroup.py | 4 +- 24 files changed, 174 insertions(+), 171 deletions(-) diff --git a/tests/foreman/api/test_computeresource_libvirt.py b/tests/foreman/api/test_computeresource_libvirt.py index 5189af9d445..ad95fb9f202 100644 --- a/tests/foreman/api/test_computeresource_libvirt.py +++ b/tests/foreman/api/test_computeresource_libvirt.py @@ -113,9 +113,9 @@ def test_positive_create_with_name_description( location=[module_location], url=LIBVIRT_URL, ).create() + request.addfinalizer(compresource.delete) assert compresource.name == name assert compresource.description == name - request.addfinalizer(compresource.delete) @pytest.mark.tier2 @@ -134,9 +134,9 @@ def test_positive_create_with_orgs_and_locs(request, module_target_sat): compresource = module_target_sat.api.LibvirtComputeResource( location=locs, organization=orgs, url=LIBVIRT_URL ).create() + request.addfinalizer(compresource.delete) assert {org.name for org in orgs} == {org.read().name for org in compresource.organization} assert {loc.name for loc in locs} == {loc.read().name for loc in compresource.location} - request.addfinalizer(compresource.delete) @pytest.mark.tier2 @@ -175,8 +175,8 @@ def test_negative_create_with_same_name(request, module_target_sat, module_org, cr = module_target_sat.api.LibvirtComputeResource( location=[module_location], name=name, organization=[module_org], url=LIBVIRT_URL ).create() - assert cr.name == name request.addfinalizer(cr.delete) + assert cr.name == name with pytest.raises(HTTPError): module_target_sat.api.LibvirtComputeResource( name=name, @@ -245,19 +245,16 @@ def test_negative_update_same_name(request, module_target_sat, module_org, modul compresource = module_target_sat.api.LibvirtComputeResource( location=[module_location], name=name, organization=[module_org], url=LIBVIRT_URL ).create() + request.addfinalizer(compresource.delete) new_compresource = module_target_sat.api.LibvirtComputeResource( location=[module_location], organization=[module_org], url=LIBVIRT_URL ).create() + request.addfinalizer(new_compresource.delete) new_compresource.name = name with pytest.raises(HTTPError): new_compresource.update(['name']) assert new_compresource.read().name != name - @request.addfinalizer - def _finalize(): - compresource.delete() - new_compresource.delete() - @pytest.mark.tier2 @pytest.mark.parametrize('url', **parametrized({'random': gen_string('alpha'), 'empty': ''})) diff --git a/tests/foreman/api/test_discoveryrule.py b/tests/foreman/api/test_discoveryrule.py index f5dffd5be14..3d1c1f4bf48 100644 --- a/tests/foreman/api/test_discoveryrule.py +++ b/tests/foreman/api/test_discoveryrule.py @@ -171,8 +171,11 @@ def test_positive_multi_provision_with_rule_limit( :CaseImportance: High """ + discovered_host1 = module_target_sat.api_factory.create_discovered_host() + request.addfinalizer(module_target_sat.api.Host(id=discovered_host1['id']).delete) discovered_host2 = module_target_sat.api_factory.create_discovered_host() + request.addfinalizer(module_target_sat.api.DiscoveredHost(id=discovered_host2['id']).delete) rule = module_target_sat.api.DiscoveryRule( max_count=1, hostgroup=module_discovery_hostgroup, @@ -181,14 +184,6 @@ def test_positive_multi_provision_with_rule_limit( organization=[discovery_org], priority=1000, ).create() + request.addfinalizer(rule.delete) result = module_target_sat.api.DiscoveredHost().auto_provision_all() assert '1 discovered hosts were provisioned' in result['message'] - - # Delete discovery rule - @request.addfinalizer - def _finalize(): - rule.delete() - module_target_sat.api.Host(id=discovered_host1['id']).delete() - module_target_sat.api.DiscoveredHost(id=discovered_host2['id']).delete() - with pytest.raises(HTTPError): - rule.read() diff --git a/tests/foreman/api/test_http_proxy.py b/tests/foreman/api/test_http_proxy.py index d69fc7e61d5..51ceb485361 100644 --- a/tests/foreman/api/test_http_proxy.py +++ b/tests/foreman/api/test_http_proxy.py @@ -295,6 +295,11 @@ def test_positive_sync_proxy_with_certificate(request, target_sat, module_org, m :customerscenario: true """ + + @request.addfinalizer + def _finalize(): + target_sat.custom_certs_cleanup() + # Cleanup any existing certs that may conflict target_sat.custom_certs_cleanup() proxy_host = settings.http_proxy.auth_proxy_url.replace('http://', '').replace(':3128', '') diff --git a/tests/foreman/api/test_parameters.py b/tests/foreman/api/test_parameters.py index 1e415d15dc8..5d70374a546 100644 --- a/tests/foreman/api/test_parameters.py +++ b/tests/foreman/api/test_parameters.py @@ -39,7 +39,9 @@ def test_positive_parameter_precedence_impact( param_value = gen_string('alpha') cp = module_target_sat.api.CommonParameter(name=param_name, value=param_value).create() + request.addfinalizer(cp.delete) host = module_target_sat.api.Host(organization=module_org, location=module_location).create() + request.addfinalizer(host.delete) result = [res for res in host.all_parameters if res['name'] == param_name] assert result[0]['name'] == param_name assert result[0]['associated_type'] == 'global' @@ -48,6 +50,7 @@ def test_positive_parameter_precedence_impact( organization=[module_org], group_parameters_attributes=[{'name': param_name, 'value': param_value}], ).create() + request.addfinalizer(hg.delete) host.hostgroup = hg host = host.update(['hostgroup']) result = [res for res in host.all_parameters if res['name'] == param_name] @@ -55,12 +58,6 @@ def test_positive_parameter_precedence_impact( assert result[0]['associated_type'] != 'global' assert result[0]['associated_type'] == 'host group' - @request.addfinalizer - def _finalize(): - host.delete() - hg.delete() - cp.delete() - host.host_parameters_attributes = [{'name': param_name, 'value': param_value}] host = host.update(['host_parameters_attributes']) result = [res for res in host.all_parameters if res['name'] == param_name] diff --git a/tests/foreman/cli/test_classparameters.py b/tests/foreman/cli/test_classparameters.py index a4e75c1f76d..022ceed0aac 100644 --- a/tests/foreman/cli/test_classparameters.py +++ b/tests/foreman/cli/test_classparameters.py @@ -86,8 +86,8 @@ def test_positive_list( location=module_puppet_loc.id, environment=module_puppet['env'].name, ).create() - host.add_puppetclass(data={'puppetclass_id': module_puppet['class']['id']}) request.addfinalizer(host.delete) + host.add_puppetclass(data={'puppetclass_id': module_puppet['class']['id']}) hostgroup = session_puppet_enabled_sat.cli_factory.hostgroup( { 'puppet-environment-id': module_puppet['env'].id, diff --git a/tests/foreman/cli/test_computeresource_osp.py b/tests/foreman/cli/test_computeresource_osp.py index d88daaf1ad8..ff6779056ae 100644 --- a/tests/foreman/cli/test_computeresource_osp.py +++ b/tests/foreman/cli/test_computeresource_osp.py @@ -76,6 +76,7 @@ def test_crud_and_duplicate_name(self, request, id_type, osp_version, target_sat 'url': osp_version, } ) + request.addfinalizer(lambda: self.cr_cleanup(compute_resource['id'], id_type, target_sat)) assert compute_resource['name'] == name assert target_sat.cli.ComputeResource.exists(search=(id_type, compute_resource[id_type])) @@ -102,7 +103,6 @@ def test_crud_and_duplicate_name(self, request, id_type, osp_version, target_sat else: compute_resource = target_sat.cli.ComputeResource.info({'id': compute_resource['id']}) assert new_name == compute_resource['name'] - request.addfinalizer(lambda: self.cr_cleanup(compute_resource['id'], id_type, target_sat)) @pytest.mark.tier3 def test_negative_create_osp_with_url(self, target_sat): diff --git a/tests/foreman/cli/test_discoveredhost.py b/tests/foreman/cli/test_discoveredhost.py index 2cd1297224e..e578f35e1ac 100644 --- a/tests/foreman/cli/test_discoveredhost.py +++ b/tests/foreman/cli/test_discoveredhost.py @@ -70,10 +70,8 @@ def test_rhel_pxe_discovery_provisioning( assert 'Host created' in result[0]['message'] host = sat.api.Host().search(query={"search": f'id={discovered_host.id}'})[0] - assert host - - # teardown request.addfinalizer(lambda: sat.provisioning_cleanup(host.name)) + assert host wait_for( lambda: host.read().build_status_label != 'Pending installation', @@ -131,10 +129,8 @@ def test_rhel_pxeless_discovery_provisioning( ) assert 'Host created' in result[0]['message'] host = sat.api.Host().search(query={"search": f'id={discovered_host.id}'})[0] - assert host - - # teardown request.addfinalizer(lambda: sat.provisioning_cleanup(host.name)) + assert host wait_for( lambda: host.read().build_status_label != 'Pending installation', diff --git a/tests/foreman/cli/test_errata.py b/tests/foreman/cli/test_errata.py index 51f25746302..3f5047a5781 100644 --- a/tests/foreman/cli/test_errata.py +++ b/tests/foreman/cli/test_errata.py @@ -753,16 +753,15 @@ def test_positive_list_affected_chosts_by_erratum_restrict_flag( 'inclusion': 'false', } ) - - @request.addfinalizer - def cleanup(): - cv_filter_cleanup( + request.addfinalizer( + lambda: cv_filter_cleanup( target_sat, cv_filter['filter-id'], module_cv, module_entitlement_manifest_org, module_lce, ) + ) # Make rule to hide the RPM that creates the need for the installable erratum target_sat.cli_factory.content_view_filter_rule( @@ -940,17 +939,15 @@ def test_host_errata_search_commands( 'inclusion': 'false', } ) - - @request.addfinalizer - def cleanup(): - cv_filter_cleanup( + request.addfinalizer( + lambda: cv_filter_cleanup( target_sat, cv_filter['filter-id'], module_cv, module_entitlement_manifest_org, module_lce, ) - + ) # Make rule to exclude the specified bugfix package target_sat.cli_factory.content_view_filter_rule( { diff --git a/tests/foreman/cli/test_hammer.py b/tests/foreman/cli/test_hammer.py index 44a7c5c5dba..54a35f833c1 100644 --- a/tests/foreman/cli/test_hammer.py +++ b/tests/foreman/cli/test_hammer.py @@ -136,6 +136,13 @@ def test_positive_disable_hammer_defaults(request, function_product, target_sat) :BZ: 1640644, 1368173 """ + + @request.addfinalizer + def _finalize(): + target_sat.cli.Defaults.delete({'param-name': 'organization_id'}) + result = target_sat.execute('hammer defaults list') + assert str(function_product.organization.id) not in result.stdout + target_sat.cli.Defaults.add( {'param-name': 'organization_id', 'param-value': function_product.organization.id} ) @@ -154,12 +161,6 @@ def test_positive_disable_hammer_defaults(request, function_product, target_sat) assert result.status == 0 assert function_product.name in result.stdout - @request.addfinalizer - def _finalize(): - target_sat.cli.Defaults.delete({'param-name': 'organization_id'}) - result = target_sat.execute('hammer defaults list') - assert str(function_product.organization.id) not in result.stdout - @pytest.mark.upgrade def test_positive_check_debug_log_levels(target_sat): diff --git a/tests/foreman/cli/test_satellitesync.py b/tests/foreman/cli/test_satellitesync.py index bab092a3ea7..1391b895334 100644 --- a/tests/foreman/cli/test_satellitesync.py +++ b/tests/foreman/cli/test_satellitesync.py @@ -2270,6 +2270,7 @@ def test_positive_custom_cdn_with_credential( meta_file = 'metadata.json' crt_file = 'source.crt' pub_dir = '/var/www/html/pub/repos' + request.addfinalizer(lambda: target_sat.execute(f'rm -rf {pub_dir}')) # Export the repository in syncable format and move it # to /var/www/html/pub/repos to mimic custom CDN. @@ -2286,7 +2287,6 @@ def test_positive_custom_cdn_with_credential( exp_dir = exp_dir[0].replace(meta_file, '') assert target_sat.execute(f'mv {exp_dir} {pub_dir}').status == 0 - request.addfinalizer(lambda: target_sat.execute(f'rm -rf {pub_dir}')) target_sat.execute(f'semanage fcontext -a -t httpd_sys_content_t "{pub_dir}(/.*)?"') target_sat.execute(f'restorecon -R {pub_dir}') diff --git a/tests/foreman/cli/test_subnet.py b/tests/foreman/cli/test_subnet.py index 12272ed57c2..74a6e1727fc 100644 --- a/tests/foreman/cli/test_subnet.py +++ b/tests/foreman/cli/test_subnet.py @@ -199,8 +199,8 @@ def test_negative_update_attributes(request, options, module_target_sat): :CaseImportance: Medium """ subnet = module_target_sat.cli_factory.make_subnet() - options['id'] = subnet['id'] request.addfinalizer(lambda: module_target_sat.cli.Subnet.delete({'id': subnet['id']})) + options['id'] = subnet['id'] with pytest.raises(CLIReturnCodeError, match='Could not update the subnet:'): module_target_sat.cli.Subnet.update(options) # check - subnet is not updated @@ -223,8 +223,8 @@ def test_negative_update_address_pool(request, options, module_target_sat): :CaseImportance: Medium """ subnet = module_target_sat.cli_factory.make_subnet() - opts = {'id': subnet['id']} request.addfinalizer(lambda: module_target_sat.cli.Subnet.delete({'id': subnet['id']})) + opts = {'id': subnet['id']} # generate pool range from network address for key, val in options.items(): opts[key] = re.sub(r'\d+$', str(val), subnet['network-addr']) diff --git a/tests/foreman/destructive/test_capsule_loadbalancer.py b/tests/foreman/destructive/test_capsule_loadbalancer.py index 38be248de98..6879cb567af 100644 --- a/tests/foreman/destructive/test_capsule_loadbalancer.py +++ b/tests/foreman/destructive/test_capsule_loadbalancer.py @@ -190,6 +190,7 @@ def test_loadbalancer_install_package( registration. """ + # Register content host result = rhel7_contenthost.register( org=module_org, @@ -218,6 +219,9 @@ def test_loadbalancer_install_package( if loadbalancer_setup['setup_capsules']['capsule_1'].hostname in result.stdout else loadbalancer_setup['setup_capsules']['capsule_2'] ) + request.addfinalizer( + lambda: registered_to_capsule.power_control(state=VmState.RUNNING, ensure=True) + ) # Remove the packages from the client result = rhel7_contenthost.execute('yum remove -y tree') @@ -230,10 +234,6 @@ def test_loadbalancer_install_package( result = rhel7_contenthost.execute('yum install -y tree') assert result.status == 0 - @request.addfinalizer - def _finalize(): - registered_to_capsule.power_control(state=VmState.RUNNING, ensure=True) - @pytest.mark.rhel_ver_match('[^6]') @pytest.mark.tier1 diff --git a/tests/foreman/maintain/test_advanced.py b/tests/foreman/maintain/test_advanced.py index 6a66c9aefcf..68b00e5c071 100644 --- a/tests/foreman/maintain/test_advanced.py +++ b/tests/foreman/maintain/test_advanced.py @@ -75,6 +75,22 @@ def test_positive_advanced_run_hammer_setup(request, sat_maintain): :BZ: 1830355 """ + + @request.addfinalizer + def _finalize(): + result = sat_maintain.execute( + f'hammer -u admin -p admin user update --login admin --password {default_admin_pass}' + ) + assert result.status == 0 + # Make default admin creds available in MAINTAIN_HAMMER_YML + assert sat_maintain.cli.Advanced.run_hammer_setup().status == 0 + # Make sure default password available in MAINTAIN_HAMMER_YML + result = sat_maintain.execute( + f"grep -i ':password: {default_admin_pass}' {MAINTAIN_HAMMER_YML}" + ) + assert result.status == 0 + assert default_admin_pass in result.stdout + default_admin_pass = settings.server.admin_password result = sat_maintain.execute( f'hammer -u admin -p {default_admin_pass} user update --login admin --password admin' @@ -100,21 +116,6 @@ def test_positive_advanced_run_hammer_setup(request, sat_maintain): assert result.status == 0 assert 'admin' in result.stdout - @request.addfinalizer - def _finalize(): - result = sat_maintain.execute( - f'hammer -u admin -p admin user update --login admin --password {default_admin_pass}' - ) - assert result.status == 0 - # Make default admin creds available in MAINTAIN_HAMMER_YML - assert sat_maintain.cli.Advanced.run_hammer_setup().status == 0 - # Make sure default password available in MAINTAIN_HAMMER_YML - result = sat_maintain.execute( - f"grep -i ':password: {default_admin_pass}' {MAINTAIN_HAMMER_YML}" - ) - assert result.status == 0 - assert default_admin_pass in result.stdout - @pytest.mark.e2e @pytest.mark.upgrade @@ -131,6 +132,12 @@ def test_positive_advanced_run_packages(request, sat_maintain): :expectedresults: packages should install/downgrade/check-update/update. """ + + @request.addfinalizer + def _finalize(): + assert sat_maintain.execute('dnf remove -y walrus').status == 0 + sat_maintain.execute('rm -rf /etc/yum.repos.d/custom_repo.repo') + # Setup custom_repo and install walrus package sat_maintain.create_custom_repos(custom_repo=settings.repos.yum_0.url) result = sat_maintain.cli.Advanced.run_packages_install( @@ -160,11 +167,6 @@ def test_positive_advanced_run_packages(request, sat_maintain): assert result.status == 0 assert 'walrus-5.21-1' in result.stdout - @request.addfinalizer - def _finalize(): - assert sat_maintain.execute('dnf remove -y walrus').status == 0 - sat_maintain.execute('rm -rf /etc/yum.repos.d/custom_repo.repo') - @pytest.mark.parametrize( 'tasks_state', @@ -250,6 +252,7 @@ def test_positive_sync_plan_with_hammer_defaults(request, sat_maintain, module_o :customerscenario: true """ + sat_maintain.cli.Defaults.add({'param-name': 'organization_id', 'param-value': module_org.id}) sync_plans = [] @@ -258,16 +261,6 @@ def test_positive_sync_plan_with_hammer_defaults(request, sat_maintain, module_o sat_maintain.api.SyncPlan(enabled=True, name=name, organization=module_org).create() ) - result = sat_maintain.cli.Advanced.run_sync_plans_disable() - assert 'FAIL' not in result.stdout - assert result.status == 0 - - sync_plans[0].delete() - - result = sat_maintain.cli.Advanced.run_sync_plans_enable() - assert 'FAIL' not in result.stdout - assert result.status == 0 - @request.addfinalizer def _finalize(): sat_maintain.cli.Defaults.delete({'param-name': 'organization_id'}) @@ -278,6 +271,16 @@ def _finalize(): if sync_plan: sync_plans[0].delete() + result = sat_maintain.cli.Advanced.run_sync_plans_disable() + assert 'FAIL' not in result.stdout + assert result.status == 0 + + sync_plans[0].delete() + + result = sat_maintain.cli.Advanced.run_sync_plans_enable() + assert 'FAIL' not in result.stdout + assert result.status == 0 + @pytest.mark.e2e def test_positive_satellite_repositories_setup(sat_maintain): diff --git a/tests/foreman/maintain/test_health.py b/tests/foreman/maintain/test_health.py index c45c565dc38..672cdde1550 100644 --- a/tests/foreman/maintain/test_health.py +++ b/tests/foreman/maintain/test_health.py @@ -200,6 +200,13 @@ def test_negative_health_check_upstream_repository(sat_maintain, request): :expectedresults: check-upstream-repository health check should fail. """ + + @request.addfinalizer + def _finalize(): + for name in upstream_url: + sat_maintain.execute(f'rm -fr /etc/yum.repos.d/{name}.repo') + sat_maintain.execute('dnf clean all') + for name, url in upstream_url.items(): sat_maintain.create_custom_repos(**{name: url}) result = sat_maintain.cli.Health.check( @@ -215,12 +222,6 @@ def test_negative_health_check_upstream_repository(sat_maintain, request): elif name in ['foreman_repo', 'puppet_repo']: assert 'enabled=0' in result.stdout - @request.addfinalizer - def _finalize(): - for name in upstream_url: - sat_maintain.execute(f'rm -fr /etc/yum.repos.d/{name}.repo') - sat_maintain.execute('dnf clean all') - def test_positive_health_check_available_space(sat_maintain): """Verify available-space check @@ -259,15 +260,16 @@ def test_positive_hammer_defaults_set(sat_maintain, request): :customerscenario: true """ - sat_maintain.cli.Defaults.add({'param-name': 'organization_id', 'param-value': 1}) - result = sat_maintain.cli.Health.check(options={'assumeyes': True}) - assert result.status == 0 - assert 'FAIL' not in result.stdout @request.addfinalizer def _finalize(): sat_maintain.cli.Defaults.delete({'param-name': 'organization_id'}) + sat_maintain.cli.Defaults.add({'param-name': 'organization_id', 'param-value': 1}) + result = sat_maintain.cli.Health.check(options={'assumeyes': True}) + assert result.status == 0 + assert 'FAIL' not in result.stdout + @pytest.mark.include_capsule def test_positive_health_check_hotfix_installed(sat_maintain, request): @@ -287,6 +289,13 @@ def test_positive_health_check_hotfix_installed(sat_maintain, request): :expectedresults: check-hotfix-installed check should detect modified file and installed hotfix. """ + + @request.addfinalizer + def _finalize(): + sat_maintain.execute('rm -fr /etc/yum.repos.d/custom_repo.repo') + sat_maintain.execute('dnf remove -y hotfix-package') + assert sat_maintain.execute(f'sed -i "/#modifying_file/d" {fpath.stdout}').status == 0 + # Verify check-hotfix-installed without hotfix package. result = sat_maintain.cli.Health.check(options={'label': 'check-hotfix-installed'}) assert result.status == 0 @@ -306,12 +315,6 @@ def test_positive_health_check_hotfix_installed(sat_maintain, request): assert 'WARNING' in result.stdout assert 'hotfix-package' in result.stdout - @request.addfinalizer - def _finalize(): - sat_maintain.execute('rm -fr /etc/yum.repos.d/custom_repo.repo') - sat_maintain.execute('dnf remove -y hotfix-package') - assert sat_maintain.execute(f'sed -i "/#modifying_file/d" {fpath.stdout}').status == 0 - @pytest.mark.include_capsule def test_positive_health_check_validate_yum_config(sat_maintain): @@ -364,6 +367,11 @@ def test_negative_health_check_epel_repository(request, sat_maintain): :expectedresults: check-non-redhat-repository health check should fail. """ + + @request.addfinalizer + def _finalize(): + assert sat_maintain.execute('dnf remove -y epel-release').status == 0 + epel_repo = 'https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm' sat_maintain.execute(f'dnf install -y {epel_repo}') result = sat_maintain.cli.Health.check(options={'label': 'check-non-redhat-repository'}) @@ -371,10 +379,6 @@ def test_negative_health_check_epel_repository(request, sat_maintain): assert result.status == 1 assert 'FAIL' in result.stdout - @request.addfinalizer - def _finalize(): - assert sat_maintain.execute('dnf remove -y epel-release').status == 0 - def test_positive_health_check_old_foreman_tasks(sat_maintain): """Verify check-old-foreman-tasks. @@ -470,6 +474,14 @@ def test_positive_health_check_tftp_storage(sat_maintain, request): :expectedresults: check-tftp-storage health check should pass. """ + + @request.addfinalizer + def _finalize(): + sat_maintain.cli.Settings.set({'name': 'token_duration', 'value': '360'}) + assert ( + sat_maintain.cli.Settings.list({'search': 'name=token_duration'})[0]['value'] == '360' + ) + sat_maintain.cli.Settings.set({'name': 'token_duration', 'value': '2'}) assert sat_maintain.cli.Settings.list({'search': 'name=token_duration'})[0]['value'] == '2' files_to_delete = [ @@ -503,13 +515,6 @@ def test_positive_health_check_tftp_storage(sat_maintain, request): assert result.status == 0 assert 'FAIL' not in result.stdout - @request.addfinalizer - def _finalize(): - sat_maintain.cli.Settings.set({'name': 'token_duration', 'value': '360'}) - assert ( - sat_maintain.cli.Settings.list({'search': 'name=token_duration'})[0]['value'] == '360' - ) - @pytest.mark.include_capsule def test_positive_health_check_env_proxy(sat_maintain): @@ -619,10 +624,20 @@ def test_positive_health_check_corrupted_roles(sat_maintain, request): :BZ: 1703041, 1908846 """ - # Check the filter created to verify the role, resource type, and permissions assigned. role_name = 'test_role' resource_type = gen_string("alpha") sat_maintain.cli.Role.create(options={'name': role_name}) + + @request.addfinalizer + def _finalize(): + resource_type = r"'\''Host'\''" + sat_maintain.execute( + f'''sudo su - postgres -c "psql -d foreman -c 'UPDATE permissions SET + resource_type = {resource_type} WHERE name = {permission_name};'"''' + ) + sat_maintain.cli.Role.delete(options={'name': role_name}) + + # Check the filter created to verify the role, resource type, and permissions assigned. sat_maintain.cli.Filter.create( options={'role': role_name, 'permissions': ['view_hosts', 'console_hosts']} ) @@ -644,15 +659,6 @@ def test_positive_health_check_corrupted_roles(sat_maintain, request): result = sat_maintain.cli.Filter.list(options={'search': role_name}, output_format='yaml') assert result.count('Id') == 4 - @request.addfinalizer - def _finalize(): - resource_type = r"'\''Host'\''" - sat_maintain.execute( - f'''sudo su - postgres -c "psql -d foreman -c 'UPDATE permissions SET - resource_type = {resource_type} WHERE name = {permission_name};'"''' - ) - sat_maintain.cli.Role.delete(options={'name': role_name}) - @pytest.mark.include_capsule def test_positive_health_check_non_rh_packages(sat_maintain, request): @@ -677,6 +683,12 @@ def test_positive_health_check_non_rh_packages(sat_maintain, request): :CaseImportance: High """ + + @request.addfinalizer + def _finalize(): + assert sat_maintain.execute('dnf remove -y walrus').status == 0 + assert sat_maintain.execute('rm -fr /etc/yum.repos.d/custom_repo.repo').status == 0 + sat_maintain.create_custom_repos(custom_repo=settings.repos.yum_0.url) assert ( sat_maintain.cli.Packages.install(packages='walrus', options={'assumeyes': True}).status @@ -688,11 +700,6 @@ def test_positive_health_check_non_rh_packages(sat_maintain, request): assert result.status == 78 assert 'WARNING' in result.stdout - @request.addfinalizer - def _finalize(): - assert sat_maintain.execute('dnf remove -y walrus').status == 0 - assert sat_maintain.execute('rm -fr /etc/yum.repos.d/custom_repo.repo').status == 0 - def test_positive_health_check_duplicate_permissions(sat_maintain): """Verify duplicate-permissions check diff --git a/tests/foreman/maintain/test_maintenance_mode.py b/tests/foreman/maintain/test_maintenance_mode.py index 50d130ac55a..b8d5024e1b1 100644 --- a/tests/foreman/maintain/test_maintenance_mode.py +++ b/tests/foreman/maintain/test_maintenance_mode.py @@ -47,6 +47,11 @@ def test_positive_maintenance_mode(request, sat_maintain, setup_sync_plan): to disable/enable sync-plan, stop/start crond.service and is able to add FOREMAN_MAINTAIN_TABLE rule in nftables. """ + + @request.addfinalizer + def _finalize(): + assert sat_maintain.cli.MaintenanceMode.stop().status == 0 + enable_sync_ids = setup_sync_plan data_yml_path = '/var/lib/foreman-maintain/data.yml' local_data_yml_path = f'{robottelo_tmp_dir}/data.yml' @@ -142,7 +147,3 @@ def test_positive_maintenance_mode(request, sat_maintain, setup_sync_plan): assert 'OK' in result.stdout assert result.status == 1 assert 'Maintenance mode is Off' in result.stdout - - @request.addfinalizer - def _finalize(): - assert sat_maintain.cli.MaintenanceMode.stop().status == 0 diff --git a/tests/foreman/maintain/test_packages.py b/tests/foreman/maintain/test_packages.py index a4c56eac531..f0ae22d6d10 100644 --- a/tests/foreman/maintain/test_packages.py +++ b/tests/foreman/maintain/test_packages.py @@ -160,6 +160,15 @@ def test_positive_fm_packages_install(request, sat_maintain): :expectedresults: Packages get install/update when lock/unlocked. """ + + @request.addfinalizer + def _finalize(): + assert sat_maintain.execute('dnf remove -y zsh').status == 0 + if sat_maintain.__class__.__name__ == 'Satellite': + result = sat_maintain.install(InstallerCommand('lock-package-versions')) + assert result.status == 0 + assert 'Success!' in result.stdout + # Test whether packages are locked or not result = sat_maintain.install(InstallerCommand('lock-package-versions')) assert result.status == 0 @@ -216,14 +225,6 @@ def test_positive_fm_packages_install(request, sat_maintain): assert result.status == 0 assert 'Use foreman-maintain packages install/update ' not in result.stdout - @request.addfinalizer - def _finalize(): - assert sat_maintain.execute('dnf remove -y zsh').status == 0 - if sat_maintain.__class__.__name__ == 'Satellite': - result = sat_maintain.install(InstallerCommand('lock-package-versions')) - assert result.status == 0 - assert 'Success!' in result.stdout - @pytest.mark.include_capsule def test_positive_fm_packages_update(request, sat_maintain): @@ -244,6 +245,12 @@ def test_positive_fm_packages_update(request, sat_maintain): :customerscenario: true """ + + @request.addfinalizer + def _finalize(): + assert sat_maintain.execute('dnf remove -y walrus').status == 0 + sat_maintain.execute('rm -rf /etc/yum.repos.d/custom_repo.repo') + # Setup custom_repo and packages update sat_maintain.create_custom_repos(custom_repo=settings.repos.yum_0.url) disableplugin = '--disableplugin=foreman-protector' @@ -263,8 +270,3 @@ def test_positive_fm_packages_update(request, sat_maintain): result = sat_maintain.execute('rpm -qa walrus') assert result.status == 0 assert 'walrus-5.21-1' in result.stdout - - @request.addfinalizer - def _finalize(): - assert sat_maintain.execute('dnf remove -y walrus').status == 0 - sat_maintain.execute('rm -rf /etc/yum.repos.d/custom_repo.repo') diff --git a/tests/foreman/ui/test_ansible.py b/tests/foreman/ui/test_ansible.py index 82ffd5bf187..89eba62ff56 100644 --- a/tests/foreman/ui/test_ansible.py +++ b/tests/foreman/ui/test_ansible.py @@ -178,6 +178,13 @@ def test_positive_ansible_custom_role(target_sat, session, module_org, rhel_cont :CaseComponent: Ansible-RemoteExecution """ + + @request.addfinalizer + def _finalize(): + result = target_sat.cli.Ansible.roles_delete({'name': SELECTED_ROLE}) + assert f'Ansible role [{SELECTED_ROLE}] was deleted.' in result[0]['message'] + target_sat.execute('rm -rvf /etc/ansible/roles/custom_role') + SELECTED_ROLE = 'custom_role' playbook = f'{robottelo_tmp_dir}/playbook.yml' data = { @@ -231,12 +238,6 @@ def test_positive_ansible_custom_role(target_sat, session, module_org, rhel_cont session.configreport.delete(rhel_contenthost.hostname) assert len(session.configreport.read()['table']) == 0 - @request.addfinalizer - def _finalize(): - result = target_sat.cli.Ansible.roles_delete({'name': SELECTED_ROLE}) - assert f'Ansible role [{SELECTED_ROLE}] was deleted.' in result[0]['message'] - target_sat.execute('rm -rvf /etc/ansible/roles/custom_role') - @pytest.mark.tier2 def test_positive_host_role_information(target_sat, function_host): diff --git a/tests/foreman/ui/test_computeresource_gce.py b/tests/foreman/ui/test_computeresource_gce.py index 73f4fb11daf..a25edb1da44 100644 --- a/tests/foreman/ui/test_computeresource_gce.py +++ b/tests/foreman/ui/test_computeresource_gce.py @@ -161,8 +161,17 @@ def test_positive_gce_provision_end_to_end( :expectedresults: Host is provisioned successfully """ + name = f'test{gen_string("alpha", 4).lower()}' hostname = f'{name}.{gce_domain.name}' + + @request.addfinalizer + def _finalize(): + gcehost = sat_gce.api.Host().search(query={'search': f'name={hostname}'}) + if gcehost: + gcehost[0].delete() + googleclient.disconnect() + gceapi_vmname = hostname.replace('.', '-') root_pwd = gen_string('alpha', 15) storage = [{'size': 20}] @@ -214,13 +223,6 @@ def test_positive_gce_provision_end_to_end( # 2.2 GCE Backend Assertions assert gceapi_vm.is_stopping or gceapi_vm.is_stopped - @request.addfinalizer - def _finalize(): - gcehost = sat_gce.api.Host().search(query={'search': f'name={hostname}'}) - if gcehost: - gcehost[0].delete() - googleclient.disconnect() - @pytest.mark.tier4 @pytest.mark.upgrade @@ -247,6 +249,14 @@ def test_positive_gce_cloudinit_provision_end_to_end( """ name = f'test{gen_string("alpha", 4).lower()}' hostname = f'{name}.{gce_domain.name}' + + @request.addfinalizer + def _finalize(): + gcehost = sat_gce.api.Host().search(query={'search': f'name={hostname}'}) + if gcehost: + gcehost[0].delete() + googleclient.disconnect() + gceapi_vmname = hostname.replace('.', '-') storage = [{'size': 20}] root_pwd = gen_string('alpha', random.choice([8, 15])) @@ -290,10 +300,3 @@ def test_positive_gce_cloudinit_provision_end_to_end( assert not sat_gce.api.Host().search(query={'search': f'name="{hostname}"'}) # 2.2 GCE Backend Assertions assert gceapi_vm.is_stopping or gceapi_vm.is_stopped - - @request.addfinalizer - def _finalize(): - gcehost = sat_gce.api.Host().search(query={'search': f'name={hostname}'}) - if gcehost: - gcehost[0].delete() - googleclient.disconnect() diff --git a/tests/foreman/ui/test_computeresource_libvirt.py b/tests/foreman/ui/test_computeresource_libvirt.py index 518e26817b8..42477b8d46a 100644 --- a/tests/foreman/ui/test_computeresource_libvirt.py +++ b/tests/foreman/ui/test_computeresource_libvirt.py @@ -171,10 +171,8 @@ def test_positive_provision_end_to_end( } ) name = f'{hostname}.{module_libvirt_provisioning_sat.domain.name}' - assert session.host.search(name)[0]['Name'] == name - - # teardown request.addfinalizer(lambda: sat.provisioning_cleanup(name)) + assert session.host.search(name)[0]['Name'] == name # Check on Libvirt, if VM exists result = sat.execute( diff --git a/tests/upgrades/test_contentview.py b/tests/upgrades/test_contentview.py index 9b7fa90bb56..2b581297b1e 100644 --- a/tests/upgrades/test_contentview.py +++ b/tests/upgrades/test_contentview.py @@ -87,6 +87,7 @@ def test_cv_postupgrade_scenario(self, request, target_sat, pre_upgrade_data): cv = target_sat.api.ContentView(organization=org.id).search( query={'search': f'name="{cv_name}"'} )[0] + request.addfinalizer(cv.delete) yum_repo = target_sat.api.Repository(organization=org.id).search( query={'search': f'name="{pre_test_name}_yum_repo"'} )[0] @@ -95,7 +96,6 @@ def test_cv_postupgrade_scenario(self, request, target_sat, pre_upgrade_data): query={'search': f'name="{pre_test_name}_file_repo"'} )[0] request.addfinalizer(file_repo.delete) - request.addfinalizer(cv.delete) cv.repository = [] cv.update(['repository']) assert len(cv.read_json()['repositories']) == 0 diff --git a/tests/upgrades/test_host.py b/tests/upgrades/test_host.py index 5d0e9fda247..b60585d63bd 100644 --- a/tests/upgrades/test_host.py +++ b/tests/upgrades/test_host.py @@ -161,6 +161,7 @@ def test_post_create_gce_cr_and_host( pre_upgrade_host = sat_gce.api.Host().search( query={'search': f'name={pre_upgrade_data.provision_host_name}'} )[0] + request.addfinalizer(pre_upgrade_host.delete) org = sat_gce.api.Organization(id=pre_upgrade_host.organization.id).read() loc = sat_gce.api.Location(id=pre_upgrade_host.location.id).read() domain = sat_gce.api.Domain(id=pre_upgrade_host.domain.id).read() @@ -185,7 +186,6 @@ def test_post_create_gce_cr_and_host( image=image, root_pass=gen_string('alphanumeric'), ).create() - request.addfinalizer(pre_upgrade_host.delete) request.addfinalizer(host.delete) assert host.name == f"{self.hostname.lower()}.{domain.name}" assert host.build_status_label == 'Installed' diff --git a/tests/upgrades/test_provisioningtemplate.py b/tests/upgrades/test_provisioningtemplate.py index 9d681a56ead..7c603c30a53 100644 --- a/tests/upgrades/test_provisioningtemplate.py +++ b/tests/upgrades/test_provisioningtemplate.py @@ -105,6 +105,7 @@ def test_post_scenario_provisioning_templates( pre_upgrade_host = module_target_sat.api.Host().search( query={'search': f'id={pre_upgrade_data.provision_host_id}'} )[0] + request.addfinalizer(pre_upgrade_host.delete) org = module_target_sat.api.Organization(id=pre_upgrade_host.organization.id).read() loc = module_target_sat.api.Location(id=pre_upgrade_host.location.id).read() domain = module_target_sat.api.Domain(id=pre_upgrade_host.domain.id).read() @@ -129,7 +130,6 @@ def test_post_scenario_provisioning_templates( root_pass=settings.provisioning.host_root_password, pxe_loader=pxe_loader, ).create() - request.addfinalizer(pre_upgrade_host.delete) request.addfinalizer(new_host.delete) for kind in provisioning_template_kinds: diff --git a/tests/upgrades/test_subscription.py b/tests/upgrades/test_subscription.py index 116d8956120..dc6d33b84b2 100644 --- a/tests/upgrades/test_subscription.py +++ b/tests/upgrades/test_subscription.py @@ -173,5 +173,5 @@ def test_post_subscription_scenario_auto_attach(self, request, target_sat, pre_u sub.delete_manifest(data={'organization_id': org.id}) assert len(sub.search()) == 0 manifester = Manifester(manifest_category=settings.manifest.entitlement) - manifester.allocation_uuid = pre_upgrade_data.allocation_uuid request.addfinalizer(manifester.delete_subscription_allocation) + manifester.allocation_uuid = pre_upgrade_data.allocation_uuid diff --git a/tests/upgrades/test_usergroup.py b/tests/upgrades/test_usergroup.py index 0832602dbb3..11ac95e2af8 100644 --- a/tests/upgrades/test_usergroup.py +++ b/tests/upgrades/test_usergroup.py @@ -102,16 +102,16 @@ def test_post_verify_user_group_membership( user_group = target_sat.api.UserGroup().search( query={'search': f'name={pre_upgrade_data["user_group_name"]}'} ) + request.addfinalizer(user_group[0].delete) auth_source = target_sat.api.AuthSourceLDAP().search( query={'search': f'name={pre_upgrade_data["auth_source_name"]}'} )[0] request.addfinalizer(auth_source.delete) - request.addfinalizer(user_group[0].delete) user = target_sat.api.User().search(query={'search': f'login={ad_data["ldap_user_name"]}'})[ 0 ] - assert user.read().id == user_group[0].read().user[0].id request.addfinalizer(user.delete) + assert user.read().id == user_group[0].read().user[0].id role_list = target_sat.cli.Role.with_user( username=ad_data['ldap_user_name'], password=ad_data['ldap_user_passwd'] ).list()