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

Bz1699188 #11688

Merged
merged 1 commit into from
Sep 5, 2023
Merged
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
89 changes: 89 additions & 0 deletions tests/foreman/api/test_ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
"""
import pytest
from fauxfactory import gen_string
from wait_for import wait_for

from robottelo.config import settings
from robottelo.config import user_nailgun_config
from robottelo.utils.issue_handlers import is_open


@pytest.mark.e2e
Expand Down Expand Up @@ -253,3 +256,89 @@ def test_add_and_remove_ansible_role_hostgroup(target_sat):
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


@pytest.fixture(scope='function')
def filtered_user(target_sat, module_org, module_location):
"""
:Steps:
1. Create a role with a host view filtered
2. Create a user with that role
3. Setup a host
"""
api = target_sat.api
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please use this as is instead of making this alias? not sure if it'll reduce any lines in test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api refers to an existing object in the memory. Seeing this line feels weird, but it is readable in the end.
I would not block merging on this one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, it feels weird, but its not good practice to have additional variables for same thing unless it saves some lines of code, and to make consistent it across robottelo I'd still suggest to remove it, thanks!

role = api.Role(
name=gen_string('alpha'), location=[module_location], organization=[module_org]
).create()
# assign view_hosts (with a filter, to test BZ 1699188),
# view_hostgroups, view_facts permissions to the role
permission_hosts = api.Permission().search(query={'search': 'name="view_hosts"'})
permission_hostgroups = api.Permission().search(query={'search': 'name="view_hostgroups"'})
permission_facts = api.Permission().search(query={'search': 'name="view_facts"'})
api.Filter(permission=permission_hosts, search='name != nonexistent', role=role).create()
api.Filter(permission=permission_hostgroups, role=role).create()
api.Filter(permission=permission_facts, role=role).create()

password = gen_string('alpha')
user = api.User(
role=[role], password=password, location=[module_location], organization=[module_org]
).create()

return user, password


@pytest.fixture(scope='function')
def rex_host_in_org_and_loc(target_sat, module_org, module_location, rex_contenthost):
api = target_sat.api
host = api.Host().search(query={'search': f'name={rex_contenthost.hostname}'})[0]
host_id = host.id
api.Host(id=host_id, organization=[module_org.id]).update(['organization'])
api.Host(id=host_id, location=module_location.id).update(['location'])
rplevka marked this conversation as resolved.
Show resolved Hide resolved
return host


@pytest.mark.rhel_ver_match('[78]')
@pytest.mark.tier2
def test_positive_read_facts_with_filter(
target_sat, rex_contenthost, filtered_user, rex_host_in_org_and_loc
):
"""
Read host's Ansible facts as a user with a role that has host filter

:id: 483d5faf-7a4c-4cb7-b14f-369768ad99b0

1. Run Ansible roles on a host
2. Using API, read Ansible facts of that host

:expectedresults: Ansible facts returned

:BZ: 1699188

:customerscenario: true
"""
user, password = filtered_user
host = rex_host_in_org_and_loc

# gather ansible facts by running ansible roles on the host
host.play_ansible_roles()
if is_open('BZ:2216471'):
host_wait = target_sat.api.Host().search(
query={'search': f'name={rex_contenthost.hostname}'}
)[0]
wait_for(
lambda: len(host_wait.get_facts()) > 0,
timeout=30,
delay=2,
)

user_cfg = user_nailgun_config(user.login, password)
host = target_sat.api.Host(server_config=user_cfg).search(
query={'search': f'name={rex_contenthost.hostname}'}
)[0]
# get facts through API
facts = host.get_facts()
assert 'subtotal' in facts
rplevka marked this conversation as resolved.
Show resolved Hide resolved
assert facts['subtotal'] == 1
assert 'results' in facts
rplevka marked this conversation as resolved.
Show resolved Hide resolved
assert rex_contenthost.hostname in facts['results']
assert len(facts['results'][rex_contenthost.hostname]) > 0
Loading