Skip to content

Commit

Permalink
pytest conversion for tests/foreman/cli/test_oscap.py (#8011)
Browse files Browse the repository at this point in the history
* pytest conversion for tests/foreman/cli/test_oscap.py

* Add ansible, oscap, smartproxy and user component fixtures

* Add few more fixtures and resolve jyejare

* rename fixture module_viewer_user

Co-authored-by: Jameer Pathan <[email protected]>
  • Loading branch information
jameerpathan111 and Jameer Pathan authored Oct 23, 2020
1 parent 4ff06d4 commit d8131a0
Show file tree
Hide file tree
Showing 12 changed files with 336 additions and 354 deletions.
4 changes: 4 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@
# Component Fixtures
"pytest_fixtures.satellite_auth",
"pytest_fixtures.templatesync_fixtures",
"pytest_fixtures.ansible_fixtures",
"pytest_fixtures.oscap_fixtures",
"pytest_fixtures.smartproxy_fixtures",
"pytest_fixtures.user_fixtures",
]
10 changes: 10 additions & 0 deletions pytest_fixtures/ansible_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from robottelo.cli.ansible import Ansible


@pytest.fixture(scope="session")
def import_ansible_roles(default_smart_proxy):
""" Import ansible roles to default_smart_proxy for tests"""
Ansible.roles_import({'proxy-id': default_smart_proxy.id})
Ansible.variables_import({'proxy-id': default_smart_proxy.id})
33 changes: 0 additions & 33 deletions pytest_fixtures/api_fixtures.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
# Module-wide Nailgun Entity Fixtures to be used by API, CLI and UI Tests
import os

import pytest
from fauxfactory import gen_string
from nailgun import entities
from wrapanapi import AzureSystem
from wrapanapi import GoogleCloudSystem

from robottelo import ssh
from robottelo.api.utils import publish_puppet_module
from robottelo.constants import AZURERM_RG_DEFAULT
from robottelo.constants import AZURERM_RHEL7_FT_BYOS_IMG_URN
Expand All @@ -27,7 +24,6 @@
from robottelo.constants.repos import CUSTOM_PUPPET_REPO
from robottelo.decorators import skip_if
from robottelo.helpers import download_gce_cert
from robottelo.helpers import file_downloader
from robottelo.test import settings

# Global Satellite Entities
Expand Down Expand Up @@ -81,16 +77,6 @@ def module_compute_profile():
return entities.ComputeProfile().create()


@pytest.fixture(scope='session')
def default_smart_proxy():
smart_proxy = (
entities.SmartProxy()
.search(query={'search': 'name={0}'.format(settings.server.hostname)})[0]
.read()
)
return entities.SmartProxy(id=smart_proxy.id).read()


@pytest.fixture(scope='session')
def default_domain(default_smart_proxy):
domain_name = settings.server.hostname.partition('.')[-1]
Expand Down Expand Up @@ -458,25 +444,6 @@ def module_cv_with_puppet_module(module_org):
)


@pytest.fixture(scope="session")
def tailoring_file_path():
""" Return Tailoring file path."""
local = file_downloader(file_url=settings.oscap.tailoring_path)[0]
satellite = file_downloader(
file_url=settings.oscap.tailoring_path, hostname=settings.server.hostname
)[0]
return {'local': local, 'satellite': satellite}


@pytest.fixture(scope="session")
def oscap_content_path():
""" Download scap content from satellite and return local path of it."""
_, file_name = os.path.split(settings.oscap.content_path)
local_file = f"/tmp/{file_name}"
ssh.download_file(settings.oscap.content_path, local_file)
return local_file


@pytest.fixture(scope='session')
def default_pxetemplate():
pxe_template = entities.ProvisioningTemplate().search(query={'search': DEFAULT_PXE_TEMPLATE})
Expand Down
66 changes: 66 additions & 0 deletions pytest_fixtures/oscap_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os

import pytest
from fauxfactory import gen_string
from nailgun import entities

from robottelo import ssh
from robottelo.cli.factory import make_scapcontent
from robottelo.constants import OSCAP_PROFILE
from robottelo.helpers import file_downloader
from robottelo.test import settings


@pytest.fixture(scope="session")
def tailoring_file_path():
""" Return Tailoring file path."""
local = file_downloader(file_url=settings.oscap.tailoring_path)[0]
satellite = file_downloader(
file_url=settings.oscap.tailoring_path, hostname=settings.server.hostname
)[0]
return {'local': local, 'satellite': satellite}


@pytest.fixture(scope="session")
def oscap_content_path():
""" Download scap content from satellite and return local path of it."""
_, file_name = os.path.split(settings.oscap.content_path)
local_file = f"/tmp/{file_name}"
ssh.download_file(settings.oscap.content_path, local_file)
return local_file


@pytest.fixture(scope="module")
def scap_content(import_ansible_roles, import_puppet_classes):
title = f"rhel-content-{gen_string('alpha')}"
scap_info = make_scapcontent({'title': title, 'scap-file': f'{settings.oscap.content_path}'})
scap_id = scap_info['id']
scap_info = entities.ScapContents(id=scap_id).read()

scap_profile_id = [
profile['id']
for profile in scap_info['scap-content-profiles']
if OSCAP_PROFILE['security7'] in profile['title']
][0]
return {
"title": title,
"scap_id": scap_id,
"scap_profile_id": scap_profile_id,
}


@pytest.fixture(scope="module")
def tailoring_file(module_org, module_location, tailoring_file_path):
""" Create Tailoring file."""
tailoring_file_name = f"tailoring-file-{gen_string('alpha')}"
tf_info = entities.TailoringFile(
name=f"{tailoring_file_name}",
scap_file=f"{tailoring_file_path['local']}",
organization=[module_org],
location=[module_location],
).create()
return {
"name": tailoring_file_name,
"tailoring_file_id": tf_info['id'],
"tailoring_file_profile_id": tf_info.tailoring_file_profiles[0]['id'],
}
19 changes: 19 additions & 0 deletions pytest_fixtures/smartproxy_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import pytest
from nailgun import entities

from robottelo.test import settings


@pytest.fixture(scope='session')
def default_smart_proxy():
smart_proxy = (
entities.SmartProxy()
.search(query={'search': f'name={settings.server.hostname}'})[0]
.read()
)
return entities.SmartProxy(id=smart_proxy.id).read()


@pytest.fixture(scope='session')
def import_puppet_classes(default_smart_proxy):
default_smart_proxy.import_puppetclasses(environment='production')
22 changes: 22 additions & 0 deletions pytest_fixtures/user_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import pytest
from fauxfactory import gen_string
from nailgun import entities


@pytest.fixture(scope='module')
def default_viewer_role(module_org, default_location):
"""Custom user with viewer role for tests validating visibility of entities or fields created
by some other user. Created only when accessed, unlike `module_user`.
"""
viewer_role = entities.Role().search(query={'search': 'name="Viewer"'})[0]
custom_password = gen_string('alphanumeric')
custom_user = entities.User(
admin=False,
default_organization=module_org,
location=[default_location],
organization=[module_org],
role=[viewer_role],
password=custom_password,
).create()
custom_user.password = custom_password
return custom_user
60 changes: 7 additions & 53 deletions tests/foreman/api/test_oscappolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
from fauxfactory import gen_string
from nailgun import entities

from robottelo.cli.ansible import Ansible
from robottelo.constants import OSCAP_PROFILE
from robottelo.decorators import tier1


Expand All @@ -36,50 +34,6 @@ def module_org(module_org):
module_org.delete()


@pytest.fixture(scope="module")
def scap_content(module_org, module_location, oscap_content_path):
""" Import Ansible roles, Ansible variables, create Scap content."""
# Import Ansible roles and variables.
Ansible.roles_import({'proxy-id': 1})
Ansible.variables_import({'proxy-id': 1})
sc_title = gen_string('alpha')
entity = entities.ScapContents().search(query={'search': f'title="{sc_title}"'})
# Create Scap content.
if not entity:
result = entities.ScapContents(
title=f"{sc_title}",
scap_file=f"{oscap_content_path}",
organization=[module_org],
location=[module_location],
).create()
else:
result = entities.ScapContents(id=entity[0].id).read()
scap_profile_id_rhel7 = [
profile['id']
for profile in result.scap_content_profiles
if OSCAP_PROFILE['security7'] in profile['title']
][0]
return (result, scap_profile_id_rhel7)


@pytest.fixture(scope="module")
def tailoring_file(module_org, module_location, tailoring_file_path):
""" Create Tailoring file."""
tf_name = gen_string('alpha')
entity = entities.TailoringFile().search(query={'search': f'name="{tf_name}"'})
if not entity:
result = entities.TailoringFile(
name=f"{tf_name}",
scap_file=f"{tailoring_file_path['local']}",
organization=[module_org],
location=[module_location],
).create()
else:
result = entities.TailoringFile(id=entity[0].id).read()
tailor_profile_id = result.tailoring_file_profiles[0]['id']
return (result, tailor_profile_id)


class TestOscapPolicy:
"""Implements Oscap Policy tests in API."""

Expand Down Expand Up @@ -108,10 +62,10 @@ def test_positive_crud_scap_policy(
name=name,
deploy_by='puppet',
description=description,
scap_content_id=scap_content[0].id,
scap_content_profile_id=scap_content[1],
tailoring_file_id=tailoring_file[0].id,
tailoring_file_profile_id=tailoring_file[1],
scap_content_id=scap_content["scap_id"],
scap_content_profile_id=scap_content["scap_profile_id"],
tailoring_file_id=tailoring_file["tailoring_file_id"],
tailoring_file_profile_id=tailoring_file["tailoring_file_profile_id"],
period="monthly",
day_of_month="5",
hostgroup=[hostgroup],
Expand All @@ -123,9 +77,9 @@ def test_positive_crud_scap_policy(
assert policy.deploy_by == 'puppet'
assert policy.name == name
assert policy.description == description
assert policy.scap_content_id == scap_content[0].id
assert policy.scap_content_profile_id == scap_content[1]
assert policy.tailoring_file_id == tailoring_file[0].id
assert policy.scap_content_id == scap_content["scap_id"]
assert policy.scap_content_profile_id == scap_content["scap_profile_id"]
assert policy.tailoring_file_id == tailoring_file["tailoring_file_id"]
assert policy.period == "monthly"
assert policy.day_of_month == 5
assert policy.hostgroup[0].id == hostgroup.id
Expand Down
Loading

0 comments on commit d8131a0

Please sign in to comment.