Skip to content

Commit

Permalink
reordered addFinalizer in test body
Browse files Browse the repository at this point in the history
  • Loading branch information
pondrejk committed Feb 14, 2024
1 parent d9c5ea8 commit 7fe1635
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 186 deletions.
17 changes: 9 additions & 8 deletions tests/foreman/api/test_computeresource_libvirt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -241,6 +241,12 @@ def test_negative_update_same_name(request, module_target_sat, module_org, modul
:CaseImportance: High
"""

@request.addfinalizer
def _finalize():
compresource.delete()
new_compresource.delete()

name = gen_string('alphanumeric')
compresource = module_target_sat.api.LibvirtComputeResource(
location=[module_location], name=name, organization=[module_org], url=LIBVIRT_URL
Expand All @@ -253,11 +259,6 @@ def test_negative_update_same_name(request, module_target_sat, module_org, modul
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': ''}))
Expand Down
18 changes: 9 additions & 9 deletions tests/foreman/api/test_discoveryrule.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,15 @@ def test_positive_multi_provision_with_rule_limit(
:CaseImportance: High
"""

@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()

discovered_host1 = module_target_sat.api_factory.create_discovered_host()
discovered_host2 = module_target_sat.api_factory.create_discovered_host()
rule = module_target_sat.api.DiscoveryRule(
Expand All @@ -183,12 +192,3 @@ def test_positive_multi_provision_with_rule_limit(
).create()
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()
9 changes: 5 additions & 4 deletions tests/foreman/api/test_http_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,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', '')
Expand Down Expand Up @@ -312,7 +317,3 @@ def test_positive_sync_proxy_with_certificate(request, target_sat, module_org, m
assert response.get('errors') is None
assert repo.read().last_sync is not None
assert repo.read().content_counts['rpm'] >= 1

@request.addfinalizer
def _finalize():
target_sat.custom_certs_cleanup()
13 changes: 7 additions & 6 deletions tests/foreman/api/test_parameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ def test_positive_parameter_precedence_impact(
:expectedresults: Host parameter take precedence over hostgroup and global parameter,
and hostgroup take precedence over global parameter when there are no host parameters
"""

@request.addfinalizer
def _finalize():
host.delete()
hg.delete()
cp.delete()

param_name = gen_string('alpha')
param_value = gen_string('alpha')

Expand All @@ -55,12 +62,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]
Expand Down
2 changes: 1 addition & 1 deletion tests/foreman/cli/test_classparameters.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion tests/foreman/cli/test_computeresource_osp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]))

Expand All @@ -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):
Expand Down
8 changes: 2 additions & 6 deletions tests/foreman/cli/test_discoveredhost.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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',
Expand Down
42 changes: 21 additions & 21 deletions tests/foreman/cli/test_errata.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,17 @@ def test_positive_list_affected_chosts_by_erratum_restrict_flag(
:CaseAutomation: Automated
"""

@request.addfinalizer
def cleanup():
cv_filter_cleanup(
target_sat,
cv_filter['filter-id'],
module_cv,
module_sca_manifest_org,
module_lce,
)

# Uninstall package so that only the first errata applies.
for host in errata_hosts:
host.execute(f'yum erase -y {REPO_WITH_ERRATA["errata"][1]["package_name"]}')
Expand Down Expand Up @@ -689,16 +700,6 @@ def test_positive_list_affected_chosts_by_erratum_restrict_flag(
}
)

@request.addfinalizer
def cleanup():
cv_filter_cleanup(
target_sat,
cv_filter['filter-id'],
module_cv,
module_sca_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(
{
Expand Down Expand Up @@ -765,6 +766,16 @@ def test_host_errata_search_commands(
:expectedresults: The hosts are correctly listed for security and bugfix advisories.
"""

@request.addfinalizer
def cleanup():
cv_filter_cleanup(
target_sat,
cv_filter['filter-id'],
module_cv,
module_sca_manifest_org,
module_lce,
)

errata = REPO_WITH_ERRATA['errata']
# Update package on first host so that the security advisory doesn't apply.
result = errata_hosts[0].execute(f'yum update -y {errata[0]["new_package"]}')
Expand Down Expand Up @@ -861,17 +872,6 @@ def test_host_errata_search_commands(
'inclusion': 'false',
}
)

@request.addfinalizer
def cleanup():
cv_filter_cleanup(
target_sat,
cv_filter['filter-id'],
module_cv,
module_sca_manifest_org,
module_lce,
)

# Make rule to exclude the specified bugfix package
target_sat.cli_factory.content_view_filter_rule(
{
Expand Down
13 changes: 7 additions & 6 deletions tests/foreman/cli/test_hammer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}
)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/foreman/cli/test_satellitesync.py
Original file line number Diff line number Diff line change
Expand Up @@ -2265,6 +2265,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.
Expand All @@ -2281,7 +2282,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}')

Expand Down
4 changes: 2 additions & 2 deletions tests/foreman/cli/test_subnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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'])
Expand Down
9 changes: 5 additions & 4 deletions tests/foreman/destructive/test_capsule_loadbalancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,11 @@ def test_loadbalancer_install_package(
registration.
"""

@request.addfinalizer
def _finalize():
registered_to_capsule.power_control(state=VmState.RUNNING, ensure=True)

# Register content host
result = rhel7_contenthost.register(
org=module_org,
Expand Down Expand Up @@ -231,10 +236,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
Expand Down
Loading

0 comments on commit 7fe1635

Please sign in to comment.