Skip to content
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
11 changes: 11 additions & 0 deletions development/playbooks/test/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,14 @@
args:
chdir: "{{ inventory_dir }}/../"
changed_when: false
register: pytest_result
ignore_errors: true

- name: Display pytest results
ansible.builtin.debug:
msg: "{{ pytest_result.stdout }}"

- name: Fail if tests did not pass
ansible.builtin.fail:
msg: "Tests failed. See output above for details."
when: pytest_result.rc != 0
5 changes: 5 additions & 0 deletions tests/client_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import pytest


def test_foreman_content_view(client_environment, activation_key, organization, foremanapi, client):
client.run('dnf install -y subscription-manager')
rcmd = foremanapi.create('registration_commands', {'organization_id': organization['id'], 'insecure': True, 'activation_keys': [activation_key['name']], 'force': True})
Expand All @@ -9,6 +12,8 @@ def test_foreman_content_view(client_environment, activation_key, organization,
client.run('subscription-manager unregister')
client.run('subscription-manager clean')

@pytest.mark.feature("remote-execution")
@pytest.mark.feature("foreman-proxy")
def test_foreman_rex(client_environment, activation_key, organization, foremanapi, client, client_fqdn):
client.run('dnf install -y subscription-manager')
rcmd = foremanapi.create('registration_commands', {'organization_id': organization['id'], 'insecure': True, 'activation_keys': [activation_key['name']], 'force': True})
Expand Down
91 changes: 69 additions & 22 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import pathlib
import uuid

import apypie
Expand All @@ -6,7 +8,6 @@
import pytest
import testinfra
import yaml
import os

from jinja2 import Environment, FileSystemLoader, select_autoescape

Expand Down Expand Up @@ -83,11 +84,20 @@ def ssh_config(server_hostname):


@pytest.fixture(scope="module")
def foremanapi(ssh_config, server_fqdn):
def foreman_admin_password():
test_dir = os.path.dirname(os.path.abspath(__file__))
foremanctl_dir = os.path.dirname(test_dir)
passwd_file = os.path.join(foremanctl_dir, '.var', 'lib', 'foremanctl', 'foreman-admin-init-passwd')
with open(passwd_file) as f:
Comment on lines +87 to +91

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I also ran into this. I'd love to see it in a dedicated commit/PR. Please then also use rootpath

Suggested change
def foreman_admin_password():
test_dir = os.path.dirname(os.path.abspath(__file__))
foremanctl_dir = os.path.dirname(test_dir)
passwd_file = os.path.join(foremanctl_dir, '.var', 'lib', 'foremanctl', 'foreman-admin-init-passwd')
with open(passwd_file) as f:
def foreman_admin_password(pytestconfig):
passwd_file = pytestconfig.rootpath / '.var' / 'lib' / 'foremanctl' / 'foreman-admin-init-passwd'
with passwd_file.open() as f:

return f.read().strip()


@pytest.fixture(scope="module")
def foremanapi(ssh_config, server_fqdn, foreman_admin_password):
api = apypie.ForemanApi(
uri=f'https://{ssh_config["hostname"]}',
username='admin',
password='changeme',
password=foreman_admin_password,
verify_ssl=False,
)
api._session.headers['Host'] = server_fqdn
Expand Down Expand Up @@ -174,31 +184,68 @@ def wait_for_metadata_generate(foremanapi):
wait_for_tasks(foremanapi, 'label = Actions::Katello::Repository::MetadataGenerate')


def is_iop_enabled():
test_dir = os.path.dirname(os.path.abspath(__file__))
foremanctl_dir = os.path.dirname(test_dir)
params_file = os.path.join(foremanctl_dir, '.var', 'lib', 'foremanctl', 'parameters.yaml')
class UserParameters:
_enabled_features = None

if os.path.exists(params_file):
with open(params_file, 'r') as f:
params = yaml.safe_load(f)
features = params.get('features', [])
if isinstance(features, str):
features = features.split()
return 'iop' in features
@classmethod
def enabled_features(cls):
if cls._enabled_features is None:
cls._enabled_features = cls._load()
return cls._enabled_features

return False
@classmethod
def has_feature(cls, name):
return name in cls.enabled_features()

@classmethod
def _load(cls):
test_dir = os.path.dirname(os.path.abspath(__file__))
foremanctl_dir = os.path.dirname(test_dir)
params_file = os.path.join(foremanctl_dir, '.var', 'lib', 'foremanctl', 'parameters.yaml')
defaults_file = os.path.join(foremanctl_dir, 'src', 'vars', 'defaults.yml')

params = {}
if os.path.exists(params_file):
with open(params_file, 'r') as f:
params = yaml.safe_load(f) or {}

defaults = {}
if os.path.exists(defaults_file):
with open(defaults_file, 'r') as f:
defaults = yaml.safe_load(f) or {}

flavor = params.get('flavor', defaults.get('flavor', 'katello'))
flavor_file = os.path.join(foremanctl_dir, 'src', 'vars', 'flavors', f'{flavor}.yml')

flavor_features = []
if os.path.exists(flavor_file):
with open(flavor_file, 'r') as f:
flavor_data = yaml.safe_load(f) or {}
flavor_features = flavor_data.get('flavor_features', [])

features = params.get('features', [])
if isinstance(features, str):
features = features.split()

return set(flavor_features + features)


def pytest_configure(config):
config.addinivalue_line("markers", "iop: tests requiring IOP to be enabled")
config.addinivalue_line("markers", "feature(name): skip test if named feature is not enabled")


def pytest_collection_modifyitems(config, items):
if is_iop_enabled():
return

skip_iop = pytest.mark.skip(reason="IOP not enabled - skipping IOP tests ('iop' not in enabled_features)")
feature_dir = pathlib.Path(__file__).parent / 'feature'
for item in items:
if "iop" in item.keywords:
item.add_marker(skip_iop)
try:
rel = pathlib.Path(item.fspath).relative_to(feature_dir)
item.add_marker(pytest.mark.feature(rel.parts[0]))
except ValueError:
pass


def pytest_runtest_setup(item):
for marker in item.iter_markers("feature"):
feature = marker.args[0]
if not UserParameters.has_feature(feature):
pytest.skip(f"{feature} not enabled")
17 changes: 17 additions & 0 deletions tests/feature/hammer/test_foreman_compute_resources.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

FOREMAN_HOST = 'localhost'
FOREMAN_PORT = 3000

@pytest.mark.parametrize("compute_resource", [
pytest.param('AzureRm', marks=pytest.mark.feature('azure-rm')),
pytest.param('EC2'),
pytest.param('GCE', marks=pytest.mark.feature('google')),
pytest.param('Libvirt'),
pytest.param('Openstack'),
pytest.param('Vmware'),
])
def test_foreman_compute_resources(server, compute_resource):
hammer = server.run("hammer compute-resource create --help | grep provider")
assert hammer.succeeded
assert compute_resource in hammer.stdout
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_advisor_backend_api_service(server):
service = server.service("iop-service-advisor-backend-api")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_advisor_frontend_assets_directory(server):
assets_dir = server.file("/var/www/iop/assets/apps/advisor")
assert assets_dir.exists
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_cvemap_download_script(server):
script = server.file("/usr/local/bin/iop-cvemap-download.sh")
assert script.exists
Expand Down
5 changes: 0 additions & 5 deletions tests/iop/test_engine.py → tests/feature/iop/test_engine.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_engine_service(server):
service = server.service("iop-core-engine")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_gateway_service(server):
service = server.service("iop-core-gateway")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_ingress_service(server):
service = server.service("iop-core-ingress")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_iop_core_kafka_service(server):
service = server.service("iop-core-kafka")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_inventory_migrate_service(server):
service = server.service("iop-core-host-inventory-migrate")
assert service.is_enabled
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_inventory_frontend_assets_directory(server):
assets_dir = server.file("/var/www/iop/assets/apps/inventory")
assert assets_dir.exists
Expand Down
5 changes: 0 additions & 5 deletions tests/iop/test_kafka.py → tests/feature/iop/test_kafka.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_kafka_service(server):
service = server.service("iop-core-kafka")
assert service.is_running
Expand Down
5 changes: 0 additions & 5 deletions tests/iop/test_puptoo.py → tests/feature/iop/test_puptoo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_puptoo_service(server):
service = server.service("iop-core-puptoo")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_remediation_api_service(server):
service = server.service("iop-service-remediations-api")
assert service.is_running
Expand Down
5 changes: 0 additions & 5 deletions tests/iop/test_vmaas.py → tests/feature/iop/test_vmaas.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_vmaas_reposcan_service(server):
service = server.service("iop-service-vmaas-reposcan")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_vulnerability_manager_service(server):
service = server.service("iop-service-vuln-manager")
assert service.is_running
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_vulnerability_frontend_assets_directory(server):
assets_dir = server.file("/var/www/iop/assets/apps/vulnerability")
assert assets_dir.exists
Expand Down
5 changes: 0 additions & 5 deletions tests/iop/test_yuptoo.py → tests/feature/iop/test_yuptoo.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
import pytest

pytestmark = pytest.mark.iop


def test_yuptoo_service(server):
service = server.service("iop-core-yuptoo")
assert service.is_running
Expand Down
10 changes: 0 additions & 10 deletions tests/foreman_compute_resources_test.py

This file was deleted.

5 changes: 4 additions & 1 deletion tests/foreman_plugins_test.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import pytest

@pytest.mark.parametrize("foreman_plugin", ['foreman_azure_rm', 'foreman_google'])
@pytest.mark.parametrize("foreman_plugin", [
pytest.param('foreman_azure_rm', marks=pytest.mark.feature('azure-rm')),
pytest.param('foreman_google', marks=pytest.mark.feature('google')),
])
def test_foreman_compute_resources(foremanapi, foreman_plugin):
plugins = [plugin['name'] for plugin in foremanapi.list('plugins')]
assert foreman_plugin in plugins