Skip to content

Commit

Permalink
[6.12.z] addFinalizer in test body (#14447)
Browse files Browse the repository at this point in the history
addFinalizer in test body (#14036)

* reordered addFinalizer in test body

* Update tests/foreman/cli/test_errata.py



* Update tests/foreman/api/test_discoveryrule.py



* Update tests/foreman/cli/test_errata.py



* Update tests/foreman/destructive/test_capsule_loadbalancer.py



---------

Co-authored-by: Gaurav Talreja <[email protected]>
  • Loading branch information
pondrejk and Gauravtalreja1 authored Mar 19, 2024
1 parent 36d5591 commit 3b3bcfe
Show file tree
Hide file tree
Showing 19 changed files with 222 additions and 149 deletions.
13 changes: 5 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 @@ -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': ''}))
Expand Down
67 changes: 67 additions & 0 deletions tests/foreman/api/test_parameters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
"""Tests for parameters
:Requirement: Parameters
:CaseAutomation: Automated
:CaseComponent: Parameters
:Team: Rocket
:CaseImportance: Critical
"""
from fauxfactory import gen_string
import pytest


@pytest.mark.tier1
@pytest.mark.e2e
@pytest.mark.upgrade
def test_positive_parameter_precedence_impact(
request, module_org, module_location, module_target_sat
):
"""Check parameter precedences for Global, Hostgroup, and Host parameters
:id: 8dd6c4e8-4ec9-4bee-8a04-f5788960979b
:steps:
1. Create Global Parameter
2. Create host and verify global parameter is assigned
3. Create Host Group with parameter
4. Assign hostgroup to host created above and verify hostgroup parameter is assigned.
5. Add parameter on the host directly, and verify that this should take precedence
over Host group and Global Parameter
:expectedresults: Host parameter take precedence over hostgroup and global parameter,
and hostgroup take precedence over global parameter when there are no host parameters
"""
param_name = gen_string('alpha')
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'

hg = module_target_sat.api.HostGroup(
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]
assert result[0]['name'] == param_name
assert result[0]['associated_type'] != 'global'
assert result[0]['associated_type'] == 'host group'

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]
assert result[0]['name'] == param_name
assert result[0]['associated_type'] != 'global'
assert result[0]['associated_type'] != 'host group'
assert result[0]['associated_type'] == 'host'
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
15 changes: 6 additions & 9 deletions tests/foreman/cli/test_errata.py
Original file line number Diff line number Diff line change
Expand Up @@ -758,16 +758,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(
Expand Down Expand Up @@ -945,17 +944,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(
{
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 @@ -131,6 +131,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 @@ -149,12 +156,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


def test_positive_check_debug_log_levels(target_sat):
"""Enabling debug log level in candlepin via hammer logging
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 @@ -1988,6 +1988,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 @@ -2004,7 +2005,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
63 changes: 33 additions & 30 deletions tests/foreman/maintain/test_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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


def test_positive_advanced_run_packages(request, sat_maintain):
"""Packages install/downgrade/check-update/update using advanced procedure run
Expand All @@ -129,6 +130,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(
Expand Down Expand Up @@ -158,11 +165,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',
Expand Down Expand Up @@ -305,6 +307,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 = []
Expand All @@ -313,16 +316,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'})
Expand All @@ -333,6 +326,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):
Expand Down
Loading

0 comments on commit 3b3bcfe

Please sign in to comment.