From 8831478c3fd4a97a12c09678c78cc286aa3d0885 Mon Sep 17 00:00:00 2001 From: Satellite QE <115476073+Satellite-QE@users.noreply.github.com> Date: Wed, 7 Jun 2023 14:25:12 -0400 Subject: [PATCH] [6.14.z] Add test for add/remove ansible_role to host/hostgroup via API (#11623) Add test for add/remove ansible_role to host/hostgroup via API (#11545) Signed-off-by: Gaurav Talreja (cherry picked from commit 707e671b81ab579e7b98e1f8ace1d8a30e3c18a4) Co-authored-by: Gaurav Talreja --- tests/foreman/api/test_ansible.py | 78 +++++++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 10 deletions(-) diff --git a/tests/foreman/api/test_ansible.py b/tests/foreman/api/test_ansible.py index 84ffd656528..3a9b8f0a726 100644 --- a/tests/foreman/api/test_ansible.py +++ b/tests/foreman/api/test_ansible.py @@ -17,6 +17,7 @@ :Upstream: No """ import pytest +from fauxfactory import gen_string from robottelo.config import settings @@ -68,7 +69,9 @@ def test_fetch_and_sync_ansible_playbooks(target_sat): @pytest.mark.e2e @pytest.mark.no_containers @pytest.mark.rhel_ver_match('[^6].*') -def test_positive_ansible_job_on_host(target_sat, module_org, rhel_contenthost): +def test_positive_ansible_job_on_host( + target_sat, module_org, module_location, module_ak_with_synced_repo, rhel_contenthost +): """ Test successful execution of Ansible Job on host. @@ -86,6 +89,8 @@ def test_positive_ansible_job_on_host(target_sat, module_org, rhel_contenthost): 1. Host should be assigned the proper role. 2. Job execution must be successful. + :BZ: 2164400 + :CaseAutomation: Automated :CaseImportance: Critical @@ -94,17 +99,19 @@ def test_positive_ansible_job_on_host(target_sat, module_org, rhel_contenthost): if rhel_contenthost.os_version.major <= 7: rhel_contenthost.create_custom_repos(rhel7=settings.repos.rhel7_os) assert rhel_contenthost.execute('yum install -y insights-client').status == 0 - rhel_contenthost.install_katello_ca(target_sat) - rhel_contenthost.register_contenthost(module_org.label, force=True) - assert rhel_contenthost.subscribed - rhel_contenthost.add_rex_key(satellite=target_sat) + result = rhel_contenthost.register( + module_org, module_location, module_ak_with_synced_repo.name, target_sat + ) + assert result.status == 0, f'Failed to register host: {result.stderr}' proxy_id = target_sat.nailgun_smart_proxy.id target_host = rhel_contenthost.nailgun_host target_sat.api.AnsibleRoles().sync(data={'proxy_id': proxy_id, 'role_names': [SELECTED_ROLE]}) - target_sat.cli.Host.ansible_roles_assign({'id': target_host.id, 'ansible-roles': SELECTED_ROLE}) + role_id = target_sat.api.AnsibleRoles().search(query={'search': f'name={SELECTED_ROLE}'})[0].id + target_sat.api.Host(id=target_host.id).add_ansible_role(data={'ansible_role_id': role_id}) host_roles = target_host.list_ansible_roles() assert host_roles[0]['name'] == SELECTED_ROLE assert target_host.name == rhel_contenthost.hostname + template_id = ( target_sat.api.JobTemplate() .search(query={'search': 'name="Ansible Roles - Ansible Default"'})[0] @@ -123,6 +130,9 @@ def test_positive_ansible_job_on_host(target_sat, module_org, rhel_contenthost): ) result = target_sat.api.JobInvocation(id=job['id']).read() assert result.succeeded == 1 + target_sat.api.Host(id=target_host.id).remove_ansible_role(data={'ansible_role_id': role_id}) + host_roles = target_host.list_ansible_roles() + assert len(host_roles) == 0 @pytest.mark.no_containers @@ -163,12 +173,15 @@ def test_positive_ansible_job_on_multiple_host( module_org, module_location, module_ak_with_synced_repo.name, target_sat ) assert result.status == 0, f'Failed to register host: {result.stderr}' - id = target_sat.nailgun_smart_proxy.id + proxy_id = target_sat.nailgun_smart_proxy.id target_host = host.nailgun_host - target_sat.api.AnsibleRoles().sync(data={'proxy_id': id, 'role_names': [SELECTED_ROLE]}) - target_sat.cli.Host.ansible_roles_assign( - {'id': target_host.id, 'ansible-roles': SELECTED_ROLE} + target_sat.api.AnsibleRoles().sync( + data={'proxy_id': proxy_id, 'role_names': [SELECTED_ROLE]} ) + role_id = ( + target_sat.api.AnsibleRoles().search(query={'search': f'name={SELECTED_ROLE}'})[0].id + ) + target_sat.api.Host(id=target_host.id).add_ansible_role(data={'ansible_role_id': role_id}) host_roles = target_host.list_ansible_roles() assert host_roles[0]['name'] == SELECTED_ROLE @@ -195,3 +208,48 @@ def test_positive_ansible_job_on_multiple_host( assert result.succeeded == 2 # SELECTED_ROLE working on rhel8/rhel9 clients assert result.failed == 1 # SELECTED_ROLE failing on rhel7 client assert result.status_label == 'failed' + + +@pytest.mark.e2e +@pytest.mark.tier2 +def test_add_and_remove_ansible_role_hostgroup(target_sat): + """ + Test add and remove functionality for ansible roles in hostgroup via API + + :id: 7672cf86-fa31-11ed-855a-0fd307d2d66b + + :Steps: + 1. Create a hostgroup + 2. Sync few ansible roles + 3. Assign a few ansible roles with the host group + 4. Add some ansible role with the host group + 5. Remove the added ansible roles from the host group + + :expectedresults: + 1. Ansible role assign/add/remove functionality should work as expected in API + + :BZ: 2164400 + """ + ROLE_NAMES = [ + 'theforeman.foreman_scap_client', + 'redhat.satellite.hostgroups', + 'RedHatInsights.insights-client', + ] + hg = target_sat.api.HostGroup(name=gen_string('alpha')).create() + proxy_id = target_sat.nailgun_smart_proxy.id + target_sat.api.AnsibleRoles().sync(data={'proxy_id': proxy_id, 'role_names': ROLE_NAMES}) + ROLES = [ + target_sat.api.AnsibleRoles().search(query={'search': f'name={role}'})[0].id + for role in ROLE_NAMES + ] + target_sat.api.HostGroup(id=hg.id).assign_ansible_roles(data={'ansible_role_ids': ROLES[:2]}) + for r1, r2 in zip(target_sat.api.HostGroup(id=hg.id).list_ansible_roles(), ROLE_NAMES[:2]): + assert r1['name'] == r2 + target_sat.api.HostGroup(id=hg.id).add_ansible_role(data={'ansible_role_id': ROLES[2]}) + for r1, r2 in zip(target_sat.api.HostGroup(id=hg.id).list_ansible_roles(), ROLE_NAMES): + assert r1['name'] == r2 + + for role in ROLES: + target_sat.api.HostGroup(id=hg.id).remove_ansible_role(data={'ansible_role_id': role}) + host_roles = target_sat.api.HostGroup(id=hg.id).list_ansible_roles() + assert len(host_roles) == 0