From 176f5abe3caac419370d5a66539e55b1c6bc4dd0 Mon Sep 17 00:00:00 2001 From: Adarsh dubey Date: Tue, 31 Oct 2023 15:55:52 +0530 Subject: [PATCH] Discovery coverage for rule priority/limit/provisioning (#12962) * Discovery coverage for rule priority/limit/provisioning * Adding parametrization for broker workflow --- pytest_fixtures/component/provision_pxe.py | 27 +++++ tests/foreman/api/test_discoveredhost.py | 45 ++++++-- tests/foreman/api/test_discoveryrule.py | 122 ++++++++++----------- 3 files changed, 118 insertions(+), 76 deletions(-) diff --git a/pytest_fixtures/component/provision_pxe.py b/pytest_fixtures/component/provision_pxe.py index 1fc0e47397b..90e7c1798ff 100644 --- a/pytest_fixtures/component/provision_pxe.py +++ b/pytest_fixtures/component/provision_pxe.py @@ -237,6 +237,33 @@ def provisioning_host(module_ssh_key_file, pxe_loader): prov_host.blank = getattr(prov_host, 'blank', False) +@pytest.fixture +def provision_multiple_hosts(module_ssh_key_file, pxe_loader, request): + """Fixture to check out two blank VMs""" + vlan_id = settings.provisioning.vlan_id + cd_iso = ( + "" # TODO: Make this an optional fixture parameter (update vm_firmware when adding this) + ) + # Keeping the default value to 2 + count = request.param if request.param is not None else 2 + + with Broker( + workflow="deploy-configure-pxe-provisioning-host-rhv", + host_class=ContentHost, + _count=count, + target_vlan_id=vlan_id, + target_vm_firmware=pxe_loader.vm_firmware, + target_vm_cd_iso=cd_iso, + blank=True, + target_memory='6GiB', + auth=module_ssh_key_file, + ) as hosts: + yield hosts + + for prov_host in hosts: + prov_host.blank = getattr(prov_host, 'blank', False) + + @pytest.fixture def provisioning_hostgroup( module_provisioning_sat, diff --git a/tests/foreman/api/test_discoveredhost.py b/tests/foreman/api/test_discoveredhost.py index 0db7c486bcf..e61bf069b0a 100644 --- a/tests/foreman/api/test_discoveredhost.py +++ b/tests/foreman/api/test_discoveredhost.py @@ -339,7 +339,7 @@ def test_positive_auto_provision_all( @pytest.mark.stubbed @pytest.mark.tier3 - def test_positive_refresh_facts_pxe_host(self): + def test_positive_refresh_facts_pxe_host(self, module_target_sat): """Refresh the facts of pxe based discovered hosts by adding a new NIC :id: 413fb608-cd5c-441d-af86-fd2d40346d96 @@ -354,14 +354,12 @@ def test_positive_refresh_facts_pxe_host(self): :expectedresults: Added Fact should be displayed on refreshing the facts - :CaseAutomation: NotAutomated - :CaseImportance: High """ @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.parametrize('pxe_loader', ['uefi'], indirect=True) @pytest.mark.rhel_ver_match('9') @pytest.mark.tier3 def test_positive_reboot_pxe_host( @@ -394,6 +392,7 @@ def test_positive_reboot_pxe_host( 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] @@ -402,25 +401,51 @@ def test_positive_reboot_pxe_host( result = sat.api.DiscoveredHost(id=discovered_host.id).reboot() assert 'Unable to perform reboot' not in result - @pytest.mark.stubbed + @pytest.mark.on_premises_provisioning + @pytest.mark.parametrize('module_provisioning_sat', ['discovery'], indirect=True) + @pytest.mark.parametrize('pxe_loader', ['bios'], indirect=True) + @pytest.mark.rhel_ver_match('9') + @pytest.mark.parametrize('provision_multiple_hosts', [2]) @pytest.mark.tier3 - def test_positive_reboot_all_pxe_hosts(self): + def test_positive_reboot_all_pxe_hosts( + self, + module_provisioning_rhel_content, + module_discovery_sat, + provision_multiple_hosts, + provisioning_hostgroup, + pxe_loader, + count, + ): """Rebooting all pxe-based discovered hosts :id: 69c807f8-5646-4aa6-8b3c-5ecdb69560ed :parametrized: yes - :Setup: Provisioning should be configured and a hosts should be discovered via PXE boot. + :Setup: Provisioning should be configured and hosts should be discovered via PXE boot. :Steps: PUT /api/v2/discovered_hosts/reboot_all - :expectedresults: All disdcovered host should be rebooted successfully - - :CaseAutomation: Automated + :expectedresults: All discovered hosst should be rebooted successfully :CaseImportance: Medium """ + sat = module_discovery_sat.sat + for host in provision_multiple_hosts: + host.power_control(ensure=False) + mac = 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().reboot_all() + assert 'Discovered hosts are rebooting now' in result['message'] class TestFakeDiscoveryTests: diff --git a/tests/foreman/api/test_discoveryrule.py b/tests/foreman/api/test_discoveryrule.py index f55a287d675..51ac5fec162 100644 --- a/tests/foreman/api/test_discoveryrule.py +++ b/tests/foreman/api/test_discoveryrule.py @@ -16,36 +16,16 @@ :Upstream: No """ -from fauxfactory import gen_choice, gen_integer, gen_string -from nailgun import entities +from fauxfactory import gen_choice, gen_integer import pytest from requests.exceptions import HTTPError from robottelo.utils.datafactory import valid_data_list -@pytest.fixture(scope="module") -def module_hostgroup(module_org): - module_hostgroup = entities.HostGroup(organization=[module_org]).create() - yield module_hostgroup - module_hostgroup.delete() - - -@pytest.fixture(scope="module") -def module_location(module_location): - yield module_location - module_location.delete() - - -@pytest.fixture(scope="module") -def module_org(module_org): - yield module_org - module_org.delete() - - @pytest.mark.tier1 @pytest.mark.e2e -def test_positive_end_to_end_crud(module_org, module_location, module_hostgroup): +def test_positive_end_to_end_crud(module_org, module_location, module_hostgroup, target_sat): """Create a new discovery rule with several attributes, update them and delete the rule itself. @@ -67,7 +47,7 @@ def test_positive_end_to_end_crud(module_org, module_location, module_hostgroup) name = gen_choice(list(valid_data_list().values())) search = gen_choice(searches) hostname = 'myhost-<%= rand(99999) %>' - discovery_rule = entities.DiscoveryRule( + discovery_rule = target_sat.api.DiscoveryRule( name=name, search_=search, hostname=hostname, @@ -103,23 +83,10 @@ def test_positive_end_to_end_crud(module_org, module_location, module_hostgroup) discovery_rule.read() -@pytest.mark.tier1 -def test_negative_create_with_invalid_host_limit_and_priority(): - """Create a discovery rule with invalid host limit and priority - - :id: e3c7acb1-ac56-496b-ac04-2a83f66ec290 - - :expectedresults: Validation error should be raised - """ - with pytest.raises(HTTPError): - entities.DiscoveryRule(max_count=gen_string('alpha')).create() - with pytest.raises(HTTPError): - entities.DiscoveryRule(priority=gen_string('alpha')).create() - - -@pytest.mark.stubbed @pytest.mark.tier3 -def test_positive_provision_with_rule_priority(): +def test_positive_update_and_provision_with_rule_priority( + module_target_sat, module_discovery_hostgroup, discovery_location, discovery_org +): """Create multiple discovery rules with different priority and check rule with highest priority executed first @@ -130,44 +97,67 @@ def test_positive_provision_with_rule_priority(): :expectedresults: Host with lower count have higher priority and that rule should be executed first - :CaseAutomation: NotAutomated - :CaseImportance: High """ + discovered_host = module_target_sat.api_factory.create_discovered_host() + + prio_rule = module_target_sat.api.DiscoveryRule( + max_count=5, + hostgroup=module_discovery_hostgroup, + search_=f'name = {discovered_host["name"]}', + location=[discovery_location], + organization=[discovery_org], + priority=1, + ).create() + rule = module_target_sat.api.DiscoveryRule( + max_count=5, + hostgroup=module_discovery_hostgroup, + search_=f'name = {discovered_host["name"]}', + location=[discovery_location], + organization=[discovery_org], + priority=10, + ).create() -@pytest.mark.stubbed -@pytest.mark.tier3 -def test_positive_multi_provision_with_rule_limit(): - """Create a discovery rule (CPU_COUNT = 2) with host limit 1 and - provision more than 2 hosts with same rule - - :id: 553c8ebf-d1c1-4ac2-7948-d3664a5b450b - - :Setup: Hosts with two CPUs should already be discovered - - :expectedresults: Rule should only be applied to 2 discovered hosts - and the rule should already be skipped for the 3rd one. - - :CaseAutomation: NotAutomated + result = module_target_sat.api.DiscoveredHost(id=discovered_host['id']).auto_provision() + assert f'provisioned with rule {prio_rule.name}' in result['message'] - :CaseImportance: High - """ + # Delete discovery rule + for _ in rule, prio_rule: + _.delete() + with pytest.raises(HTTPError): + _.read() -@pytest.mark.stubbed @pytest.mark.tier3 -def test_positive_provision_with_updated_discovery_rule(): - """Update an existing rule and provision a host with it. +def test_positive_multi_provision_with_rule_limit( + module_target_sat, module_discovery_hostgroup, discovery_location, discovery_org +): + """Create a discovery rule with certain host limit and try to provision more than the passed limit - :id: 3fb20f0f-02e9-4158-9744-f583308c4e89 - - :Setup: Host should already be discovered + :id: 553c8ebf-d1c1-4ac2-7948-d3664a5b450b - :expectedresults: User should be able to update the rule and it should - be applied on discovered host + :Setup: Hosts should already be discovered - :CaseAutomation: NotAutomated + :expectedresults: Rule should only be applied to the number of the hosts passed as limit in the rule :CaseImportance: High """ + for _ in range(2): + 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=[discovery_location], + organization=[discovery_org], + priority=1000, + ).create() + result = module_target_sat.api.DiscoveredHost().auto_provision_all() + assert '1 discovered hosts were provisioned' in result['message'] + + # Delete discovery rule + rule.delete() + with pytest.raises(HTTPError): + rule.read()