From eb65b8fa6eb33d7e84c4611b17071e195b42c29a Mon Sep 17 00:00:00 2001 From: Adarsh Dubey Date: Wed, 4 Oct 2023 19:20:38 +0530 Subject: [PATCH 1/3] Coverage for autoprovision and reboot of discovered host --- tests/foreman/api/test_discoveredhost.py | 70 +++++++++++++++++++----- tests/foreman/api/test_repositories.py | 4 +- 2 files changed, 58 insertions(+), 16 deletions(-) diff --git a/tests/foreman/api/test_discoveredhost.py b/tests/foreman/api/test_discoveredhost.py index 9b4a5ae5e22..28b2e82e2d3 100644 --- a/tests/foreman/api/test_discoveredhost.py +++ b/tests/foreman/api/test_discoveredhost.py @@ -25,6 +25,22 @@ from robottelo.utils.datafactory import valid_data_list +@pytest.fixture(scope='module') +def module_discovery_hostgroup(default_org, default_location, module_target_sat): + host = module_target_sat.api.Host(organization=default_org, location=default_location) + host.create_missing() + return module_target_sat.api.HostGroup( + organization=[default_org], + location=[default_location], + medium=host.medium, + root_pass=gen_string('alpha'), + operatingsystem=host.operatingsystem, + ptable=host.ptable, + domain=host.domain, + architecture=host.architecture, + ).create() + + class HostNotDiscoveredException(Exception): """Raised when host is not discovered""" @@ -247,8 +263,6 @@ def test_positive_provision_pxe_less_host( :expectedresults: Host should be provisioned successfully - :CaseAutomation: NotAutomated - :CaseImportance: Critical """ sat = module_discovery_sat.sat @@ -274,9 +288,10 @@ def test_positive_provision_pxe_less_host( assert not sat.api.Host().search(query={"search": f'name={host.name}'}) pxeless_discovery_host.blank = True - @pytest.mark.stubbed @pytest.mark.tier3 - def test_positive_auto_provision_pxe_host(self): + def test_positive_auto_provision_pxe_host( + self, module_discovery_hostgroup, module_target_sat, default_org, default_location + ): """Auto provision a pxe-based host by executing discovery rules :id: c93fd7c9-41ef-4eb5-8042-f72e87e67e10 @@ -290,12 +305,20 @@ def test_positive_auto_provision_pxe_host(self): :expectedresults: Selected Host should be auto-provisioned successfully - :CaseAutomation: Automated - :CaseImportance: Critical """ + discovered_host = module_target_sat.api_factory.create_discovered_host() + + rule = module_target_sat.api.DiscoveryRule( + max_count=1, + hostgroup=module_discovery_hostgroup, + search_=f'name = {discovered_host["name"]}', + location=[default_location], + organization=[default_org], + ).create() + result = module_target_sat.api.DiscoveredHost(id=discovered_host['id']).auto_provision() + assert f'provisioned with rule {rule.name}' in result['message'] - @pytest.mark.stubbed @pytest.mark.tier3 def test_positive_auto_provision_all(self): """Auto provision all host by executing discovery rules @@ -337,9 +360,19 @@ def test_positive_refresh_facts_pxe_host(self): :CaseImportance: High """ - @pytest.mark.stubbed + @pytest.mark.on_premises_provisioning + @pytest.mark.parametrize('module_provisioning_sat', ['discovery'], indirect=True) + @pytest.mark.parametrize('pxe_loader', ['bios', 'uefi'], indirect=True) + @pytest.mark.rhel_ver_match('9') @pytest.mark.tier3 - def test_positive_reboot_pxe_host(self): + def test_positive_reboot_pxe_host( + self, + module_provisioning_rhel_content, + module_discovery_sat, + provisioning_host, + provisioning_hostgroup, + pxe_loader, + ): """Rebooting a pxe based discovered host :id: 69c807f8-5646-4aa6-8b3c-5ecab69560fc @@ -352,10 +385,23 @@ def test_positive_reboot_pxe_host(self): :expectedresults: Selected host should be rebooted successfully - :CaseAutomation: Automated - :CaseImportance: Medium """ + sat = module_discovery_sat.sat + provisioning_host.power_control(ensure=False) + mac = provisioning_host._broker_args['provisioning_nic_mac_addr'] + wait_for( + lambda: sat.api.DiscoveredHost().search(query={'mac': mac}) != [], + timeout=240, + delay=20, + ) + discovered_host = sat.api.DiscoveredHost().search(query={'mac': mac})[0] + discovered_host.hostgroup = provisioning_hostgroup + discovered_host.location = provisioning_hostgroup.location[0] + discovered_host.organization = provisioning_hostgroup.organization[0] + discovered_host.build = True + result = sat.api.DiscoveredHost(id=discovered_host.id).reboot() + assert 'Unable to perform reboot' not in result @pytest.mark.stubbed @pytest.mark.tier3 @@ -381,8 +427,6 @@ def test_positive_reboot_all_pxe_hosts(self): class TestFakeDiscoveryTests: """Tests that use fake discovered host. - :CaseAutomation: Automated - :CaseImportance: High """ diff --git a/tests/foreman/api/test_repositories.py b/tests/foreman/api/test_repositories.py index a7c4b1a5340..58cbd9792b2 100644 --- a/tests/foreman/api/test_repositories.py +++ b/tests/foreman/api/test_repositories.py @@ -25,9 +25,7 @@ from robottelo import constants from robottelo.cli.base import CLIReturnCodeError from robottelo.config import settings -from robottelo.constants import DEFAULT_ARCHITECTURE -from robottelo.constants import MIRRORING_POLICIES -from robottelo.constants import REPOS +from robottelo.constants import DEFAULT_ARCHITECTURE, MIRRORING_POLICIES, REPOS from robottelo.utils.datafactory import parametrized From 0beabd4d6d3f43458638f05c351e247ef34ab254 Mon Sep 17 00:00:00 2001 From: Adarsh Dubey Date: Mon, 9 Oct 2023 16:16:14 +0530 Subject: [PATCH 2/3] Added one more test case for autoprovision of multiple discoveredhosts --- tests/foreman/api/test_discoveredhost.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/tests/foreman/api/test_discoveredhost.py b/tests/foreman/api/test_discoveredhost.py index 28b2e82e2d3..3fafd1cc423 100644 --- a/tests/foreman/api/test_discoveredhost.py +++ b/tests/foreman/api/test_discoveredhost.py @@ -320,7 +320,9 @@ def test_positive_auto_provision_pxe_host( assert f'provisioned with rule {rule.name}' in result['message'] @pytest.mark.tier3 - def test_positive_auto_provision_all(self): + def test_positive_auto_provision_all( + self, module_discovery_hostgroup, module_target_sat, default_org, default_location + ): """Auto provision all host by executing discovery rules :id: 954d3688-62d9-47f7-9106-a4fff8825ffa @@ -337,6 +339,19 @@ def test_positive_auto_provision_all(self): :CaseImportance: High """ + module_target_sat.api.DiscoveryRule( + max_count=25, + hostgroup=module_discovery_hostgroup, + search_='location = "Default Location"', + location=[default_location], + organization=[default_org], + ).create() + + for _ in range(2): + module_target_sat.api_factory.create_discovered_host() + + result = module_target_sat.api.DiscoveredHost().auto_provision_all() + assert '2 discovered hosts were provisioned' in result['message'] @pytest.mark.stubbed @pytest.mark.tier3 From fdea4f08568ba2a89704325625d7a6ef8c9ffa4e Mon Sep 17 00:00:00 2001 From: Adarsh Dubey Date: Tue, 10 Oct 2023 12:58:44 +0530 Subject: [PATCH 3/3] Making a discovery component helper module --- conftest.py | 1 + pytest_fixtures/component/discovery.py | 31 ++++++++++++++++++++++++ tests/foreman/api/test_discoveredhost.py | 30 ++++++----------------- 3 files changed, 39 insertions(+), 23 deletions(-) create mode 100644 pytest_fixtures/component/discovery.py diff --git a/conftest.py b/conftest.py index 91eb0d54ae9..cba26ace014 100644 --- a/conftest.py +++ b/conftest.py @@ -37,6 +37,7 @@ 'pytest_fixtures.component.computeprofile', 'pytest_fixtures.component.contentview', 'pytest_fixtures.component.domain', + 'pytest_fixtures.component.discovery', 'pytest_fixtures.component.host', 'pytest_fixtures.component.hostgroup', 'pytest_fixtures.component.http_proxy', diff --git a/pytest_fixtures/component/discovery.py b/pytest_fixtures/component/discovery.py new file mode 100644 index 00000000000..e05b3859f2f --- /dev/null +++ b/pytest_fixtures/component/discovery.py @@ -0,0 +1,31 @@ +from fauxfactory import gen_string +import pytest + + +@pytest.fixture(scope='module') +def module_discovery_hostgroup(module_org, module_location, module_target_sat): + host = module_target_sat.api.Host(organization=module_org, location=module_location).create() + return module_target_sat.api.HostGroup( + organization=[module_org], + location=[module_location], + medium=host.medium, + root_pass=gen_string('alpha'), + operatingsystem=host.operatingsystem, + ptable=host.ptable, + domain=host.domain, + architecture=host.architecture, + ).create() + + +@pytest.fixture(scope='module') +def discovery_org(module_org, module_target_sat): + discovery_org = module_target_sat.update_setting('discovery_organization', module_org.name) + yield module_org + module_target_sat.update_setting('discovery_organization', discovery_org) + + +@pytest.fixture(scope='module') +def discovery_location(module_location, module_target_sat): + discovery_loc = module_target_sat.update_setting('discovery_location', module_location.name) + yield module_location + module_target_sat.update_setting('discovery_location', discovery_loc) diff --git a/tests/foreman/api/test_discoveredhost.py b/tests/foreman/api/test_discoveredhost.py index 3fafd1cc423..773175812cf 100644 --- a/tests/foreman/api/test_discoveredhost.py +++ b/tests/foreman/api/test_discoveredhost.py @@ -25,22 +25,6 @@ from robottelo.utils.datafactory import valid_data_list -@pytest.fixture(scope='module') -def module_discovery_hostgroup(default_org, default_location, module_target_sat): - host = module_target_sat.api.Host(organization=default_org, location=default_location) - host.create_missing() - return module_target_sat.api.HostGroup( - organization=[default_org], - location=[default_location], - medium=host.medium, - root_pass=gen_string('alpha'), - operatingsystem=host.operatingsystem, - ptable=host.ptable, - domain=host.domain, - architecture=host.architecture, - ).create() - - class HostNotDiscoveredException(Exception): """Raised when host is not discovered""" @@ -290,7 +274,7 @@ def test_positive_provision_pxe_less_host( @pytest.mark.tier3 def test_positive_auto_provision_pxe_host( - self, module_discovery_hostgroup, module_target_sat, default_org, default_location + self, module_discovery_hostgroup, module_target_sat, discovery_org, discovery_location ): """Auto provision a pxe-based host by executing discovery rules @@ -313,15 +297,15 @@ def test_positive_auto_provision_pxe_host( max_count=1, hostgroup=module_discovery_hostgroup, search_=f'name = {discovered_host["name"]}', - location=[default_location], - organization=[default_org], + location=[discovery_location], + organization=[discovery_org], ).create() result = module_target_sat.api.DiscoveredHost(id=discovered_host['id']).auto_provision() assert f'provisioned with rule {rule.name}' in result['message'] @pytest.mark.tier3 def test_positive_auto_provision_all( - self, module_discovery_hostgroup, module_target_sat, default_org, default_location + self, module_discovery_hostgroup, module_target_sat, discovery_org, discovery_location ): """Auto provision all host by executing discovery rules @@ -342,9 +326,9 @@ def test_positive_auto_provision_all( module_target_sat.api.DiscoveryRule( max_count=25, hostgroup=module_discovery_hostgroup, - search_='location = "Default Location"', - location=[default_location], - organization=[default_org], + search_=f'location = "{discovery_location.name}"', + location=[discovery_location], + organization=[discovery_org], ).create() for _ in range(2):