Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate test_positive_ansible_variables_installed_with_collection #16489

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions robottelo/cli/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ def variables_info(cls, options=None):
"""Information about ansible variables"""
cls.command_sub = 'variables info'
return cls.execute(cls._construct_command(options), output_format='csv')

@classmethod
def variables_list(cls, options=None):
"""Information about ansible variables"""
cls.command_sub = 'variables list'
return cls.execute(cls._construct_command(options), output_format='csv')
19 changes: 18 additions & 1 deletion robottelo/hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,8 @@ def register(
force=False,
insecure=True,
hostgroup=None,
auth_username=None,
auth_password=None,
):
"""Registers content host to the Satellite or Capsule server
using a global registration template.
Expand All @@ -642,6 +644,8 @@ def register(
:param force: Register the content host even if it's already registered.
:param insecure: Don't verify server authenticity.
:param hostgroup: hostgroup to register with
:param auth_username: username required if non-admin user
:param auth_password: password required if non-admin user
:return: SSHCommandResult instance filled with the result of the registration
"""
options = {
Expand Down Expand Up @@ -692,7 +696,20 @@ def register(
options['force'] = str(force).lower()

self._satellite = target.satellite
cmd = target.satellite.cli.HostRegistration.generate_command(options)
if auth_username and auth_password:
user = target.satellite.cli.User.list({'search': f'login={auth_username}'})
if user:
register_role = target.satellite.cli.Role.info({'name': 'Register hosts'})
target.satellite.cli.User.add_role(
{'id': user[0]['id'], 'role-id': register_role['id']}
)
cmd = target.satellite.cli.HostRegistration.with_user(
auth_username, auth_password
).generate_command(options)
else:
raise CLIFactoryError(f'User {auth_username} doesn\'t exist')
else:
cmd = target.satellite.cli.HostRegistration.generate_command(options)
return self.execute(cmd.strip('\n'))

def api_register(self, target, **kwargs):
Expand Down
166 changes: 165 additions & 1 deletion tests/foreman/cli/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,14 @@

from fauxfactory import gen_string
import pytest
import yaml

from robottelo.config import settings
from robottelo.config import (
robottelo_tmp_dir,
settings,
)
from robottelo.exceptions import CLIFactoryError
from robottelo.utils.issue_handlers import is_open


def assert_job_invocation_result(
Expand Down Expand Up @@ -186,6 +191,61 @@ def test_add_and_remove_ansible_role_hostgroup(self, target_sat):
)
assert 'Ansible role has been disassociated.' in result[0]['message']

@pytest.mark.tier3
@pytest.mark.rhel_ver_list([settings.content_host.default_rhel_version])
def test_positive_ansible_variables_installed_with_collection(
self, request, target_sat, module_org, module_ak_with_cv, rhel_contenthost
):
"""Verify that installing an Ansible collection also imports
any variables associated with the roles avaialble in the collection

:id: 7ff88022-fe9b-482f-a6bb-3922036a1e1c

:steps:
Gauravtalreja1 marked this conversation as resolved.
Show resolved Hide resolved
1. Register a content host with Satellite
2. Install a ansible collection with roles from ansible-galaxy
3. Import any role with variables from installed ansible collection
4. Assert that the role is imported along with associated variables
5. Assign that role to a host and verify the role assigned to the host

:expectedresults: Verify variables associated to role from collection are also imported along with roles

:bz: 1982753
"""
SELECTED_COLLECTION = 'oasis_roles.system'
SELECTED_ROLE = 'oasis_roles.system.sshd'
SELECTED_VAR = 'sshd_allow_password_login'

@request.addfinalizer
def _finalize():
result = target_sat.cli.Ansible.roles_delete({'name': SELECTED_ROLE})
assert f'Ansible role [{SELECTED_ROLE}] was deleted.' in result[0]['message']

result = rhel_contenthost.register(
module_org,
None,
module_ak_with_cv.name,
target_sat,
)
assert result.status == 0, f'Failed to register host: {result.stderr}'
target_host = rhel_contenthost.nailgun_host
proxy_id = target_sat.nailgun_smart_proxy.id

for path in ['/etc/ansible/collections', '/usr/share/ansible/collections']:
target_sat.execute(f'ansible-galaxy collection install -p {path} {SELECTED_COLLECTION}')
target_sat.cli.Ansible.roles_sync({'role-names': SELECTED_ROLE, 'proxy-id': proxy_id})
result = target_sat.cli.Host.ansible_roles_assign(
{'id': target_host.id, 'ansible-roles': f'{SELECTED_ROLE}'}
)
assert 'Ansible roles were assigned to the host' in result[0]['message']

result = target_sat.cli.Ansible.variables_list(
{'search': f'ansible_role="{SELECTED_ROLE}"'}
)
assert result[0]['variable'] == SELECTED_VAR
# Remove the ansible collection from collection_paths
target_sat.execute(f'rm -rf {path}/ansible_collections/')


@pytest.mark.tier3
@pytest.mark.upgrade
Expand Down Expand Up @@ -561,3 +621,107 @@ def test_positive_install_ansible_collection(
assert result['success'] == '1'
collection_path = client.execute('ls ~/ansible_collections').stdout
assert 'oasis_roles' in collection_path

@pytest.mark.tier3
@pytest.mark.no_containers
@pytest.mark.rhel_ver_list([settings.content_host.default_rhel_version])
@pytest.mark.parametrize('auth_type', ['admin', 'non-admin'])
def test_positive_ansible_variables_imported_with_roles(
self, request, auth_type, target_sat, module_org, module_ak_with_cv, rhel_contenthost
):
"""Verify that, when Ansible roles are imported, their variables are imported simultaneously

:id: 107c53e8-5a8a-4291-bbde-fbd66a0bb85e

:steps:
1. Register a content host with satellite
2. Create a custom role with variables and import it into satellite
3. Assert that the role is imported along with associated variables
4. Assign that role to a host and verify the role assigned to the host
5. Run the Ansible role

:expectedresults: Verify variables associated to role are also imported along with roles

:verifies: SAT-28198
"""
username = settings.server.admin_username
password = settings.server.admin_password
if auth_type == 'non-admin':
username = gen_string('alpha')
user = target_sat.cli_factory.user(
{
'admin': False,
'login': username,
'password': password,
'organization-ids': module_org.id,
}
)
target_sat.cli.User.add_role(
{'id': user['id'], 'login': username, 'role': 'Ansible Roles Manager'}
)

@request.addfinalizer
def _finalize():
result = target_sat.cli.Ansible.roles_delete({'name': SELECTED_ROLE})
assert f'Ansible role [{SELECTED_ROLE}] was deleted.' in result[0]['message']
target_sat.execute(f'rm -rvf /etc/ansible/roles/{SELECTED_ROLE}')

SELECTED_ROLE = gen_string('alphanumeric')
playbook = f'{robottelo_tmp_dir}/playbook.yml'
vars = f'{robottelo_tmp_dir}/vars.yml'
target_sat.execute(f'ansible-galaxy init --init-path /etc/ansible/roles/ {SELECTED_ROLE}')
tasks_file = f'/etc/ansible/roles/{SELECTED_ROLE}/tasks/main.yml'
vars_file = f'/etc/ansible/roles/{SELECTED_ROLE}/{"defaults" if is_open("SAT-28198") else "vars"}/main.yml'
tasks_main = [
{
'name': 'Copy SSH keys',
'copy': {
'src': '/var/lib/foreman-proxy/ssh/{{ item }}',
'dest': '/root/.ssh',
'owner': 'root',
'group': 'root',
'mode': '0400',
},
'loop': '{{ ssh_keys }}',
}
]
vars_main = {'ssh_keys': ['id_rsa_foreman_proxy.pub', 'id_rsa_foreman_proxy']}
with open(playbook, 'w') as f:
yaml.dump(tasks_main, f, sort_keys=False, default_flow_style=False)
with open(vars, 'w') as f:
yaml.dump(vars_main, f, sort_keys=False, default_flow_style=False)
target_sat.put(playbook, tasks_file)
target_sat.put(vars, vars_file)

result = rhel_contenthost.register(
module_org,
None,
module_ak_with_cv.name,
target_sat,
auth_username=username,
auth_password=password,
)
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.cli.Ansible.with_user(username, password).roles_sync(
{'role-names': SELECTED_ROLE, 'proxy-id': proxy_id}
)
result = target_sat.cli.Host.with_user(username, password).ansible_roles_assign(
{'id': target_host.id, 'ansible-roles': f'{SELECTED_ROLE}'}
)
assert 'Ansible roles were assigned to the host' in result[0]['message']

result = target_sat.cli.Ansible.with_user(username, password).variables_list(
{'search': f'ansible_role="{SELECTED_ROLE}"'}
)
assert result[0]['variable'] == 'ssh_keys'

job_id = target_sat.cli.Host.ansible_roles_play({'name': rhel_contenthost.hostname})[0].get(
'id'
)
target_sat.wait_for_tasks(
f'resource_type = JobInvocation and resource_id = {job_id} and action ~ "hosts job"'
)
result = target_sat.cli.JobInvocation.info({'id': job_id})['success']
assert result == '1'
56 changes: 0 additions & 56 deletions tests/foreman/ui/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,44 +228,6 @@ def _finalize():
(v['Name'], v['Ansible role'], v['Type'], v['Value']) for v in variable_table
]

@pytest.mark.stubbed
@pytest.mark.tier2
def test_positive_role_variable_information(self):
"""Create and assign variables to an Ansible Role and verify that the information in
the new UI is displayed correctly

:id: 4ab2813a-6b83-4907-b104-0473465814f5

:steps:
1. Register a RHEL host to Satellite.
2. Import all roles available by default.
3. Create a host group and assign one of the Ansible roles to the host group.
4. Assign the host to the host group.
5. Assign one roles to the RHEL host.
6. Create a variable and associate it with the role assigned to the Host.
7. Create a variable and associate it with the role assigned to the Hostgroup.
8. Navigate to the new UI for the given Host.
9. Select the 'Ansible' tab, then the 'Variables' sub-tab.

:expectedresults: The variables information for the given Host is visible.
"""

@pytest.mark.stubbed
@pytest.mark.tier3
def test_positive_ansible_variables_imported_with_roles(self):
"""Verify that, when Ansible roles are imported, their variables are imported simultaneously

:id: 107c53e8-5a8a-4291-bbde-fbd66a0bb85e

:steps:
1. Import Ansible roles
2. Navigate to Configure > Variables

:expectedresults: Verify that any variables in the role were also imported to Satellite

:CaseAutomation: NotAutomated
"""

@pytest.mark.stubbed
@pytest.mark.tier3
def test_positive_ansible_roles_ignore_list(self):
Expand All @@ -282,24 +244,6 @@ def test_positive_ansible_roles_ignore_list(self):
:CaseAutomation: NotAutomated
"""

@pytest.mark.stubbed
@pytest.mark.tier3
def test_positive_ansible_variables_installed_with_collection(self):
"""Verify that installing an Ansible collection also imports
any variables associated with the collection

:id: 7ff88022-fe9b-482f-a6bb-3922036a1e1c

:steps:
1. Install an Ansible collection
2. Navigate to Configure > Variables

:expectedresults: Verify that any variables associated with the collection
are present on Configure > Variables

:CaseAutomation: NotAutomated
"""

@pytest.mark.stubbed
@pytest.mark.tier3
def test_positive_set_ansible_role_order_per_host(self):
Expand Down