Skip to content

Commit

Permalink
[6.14.z] Add test for add/remove ansible_role to host/hostgroup via A…
Browse files Browse the repository at this point in the history
…PI (#11623)

Add test for add/remove ansible_role to host/hostgroup via API (#11545)

Signed-off-by: Gaurav Talreja <[email protected]>
(cherry picked from commit 707e671)

Co-authored-by: Gaurav Talreja <[email protected]>
  • Loading branch information
Satellite-QE and Gauravtalreja1 authored Jun 7, 2023
1 parent 6363720 commit 8831478
Showing 1 changed file with 68 additions and 10 deletions.
78 changes: 68 additions & 10 deletions tests/foreman/api/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
:Upstream: No
"""
import pytest
from fauxfactory import gen_string

from robottelo.config import settings

Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand All @@ -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

0 comments on commit 8831478

Please sign in to comment.