Skip to content

Commit

Permalink
Add a contenthost_factory to perform post-deploy actions
Browse files Browse the repository at this point in the history
The intent of this change is to enable us to add post-deploy actions
when requesting specific types of content hosts, like fips-enabled.
Users can define a set of host_post_configs in conf/content_host.yaml
file. They can then add the post config action under the host
definition.
  • Loading branch information
JacobCallahan committed Nov 21, 2024
1 parent b24d3d4 commit 0c6809c
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 121 deletions.
80 changes: 0 additions & 80 deletions conf/content_host.yaml.template

This file was deleted.

86 changes: 52 additions & 34 deletions pytest_fixtures/core/contenthosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
All functions in this module will be treated as fixtures that apply the contenthost mark
"""

from contextlib import contextmanager

from broker import Broker
import pytest

Expand Down Expand Up @@ -41,93 +43,115 @@ def host_conf(request):
return conf


def host_post_config(hosts, config_name):
"""A function that runs a specified post config on a list of content hosts."""
broker_args = settings.content_host.host_post_configs.get(config_name).to_dict()
for host in hosts:
for key, val in broker_args.items():
if "{" in val:
broker_args[key] = val.format(host=host)
Broker(**broker_args).execute()


@contextmanager
def contenthost_factory(request, **kwargs):
"""A factory function that checks out and (optionally) configures a content host."""
host_params = host_conf(request)
post_configs = host_params.pop("post_configs", [])
host_class = kwargs.pop("host_class", ContentHost)
with Broker(**host_params, host_class=host_class, **kwargs) as host:
if post_configs:
hosts = host if isinstance(host, list) else [host]
for config_name in post_configs:
host_post_config(hosts, config_name)
yield host


@pytest.fixture
def rhel_contenthost(request):
"""A function-level fixture that provides a content host object parametrized"""
# Request should be parametrized through pytest_fixtures.fixture_markers
# unpack params dict
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(scope='module')
def module_rhel_contenthost(request):
"""A module-level fixture that provides a content host object parametrized"""
# Request should be parametrized through pytest_fixtures.fixture_markers
# unpack params dict
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(params=[{'rhel_version': '7'}])
def rhel7_contenthost(request):
"""A function-level fixture that provides a rhel7 content host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(scope="class", params=[{'rhel_version': '7'}])
def rhel7_contenthost_class(request):
"""A fixture for use with unittest classes. Provides a rhel7 Content Host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(scope='module', params=[{'rhel_version': '7'}])
def rhel7_contenthost_module(request):
"""A module-level fixture that provides a rhel7 content host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(params=[{'rhel_version': '8'}])
def rhel8_contenthost(request):
"""A fixture that provides a rhel8 content host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(scope='module', params=[{'rhel_version': '8'}])
def rhel8_contenthost_module(request):
"""A module-level fixture that provides a rhel8 content host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(params=[{'rhel_version': 6}])
def rhel6_contenthost(request):
"""A function-level fixture that provides a rhel6 content host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(params=[{'rhel_version': '9'}])
def rhel9_contenthost(request):
"""A fixture that provides a rhel9 content host object"""
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture
def content_hosts(request):
"""A function-level fixture that provides two rhel content hosts object"""
with Broker(**host_conf(request), host_class=ContentHost, _count=2) as hosts:
with contenthost_factory(request=request, _count=2) as hosts:
hosts[0].set_infrastructure_type('physical')
yield hosts


@pytest.fixture(scope='module')
def mod_content_hosts(request):
"""A module-level fixture that provides two rhel content hosts object"""
with Broker(**host_conf(request), host_class=ContentHost, _count=2) as hosts:
with contenthost_factory(request=request, _count=2) as hosts:
hosts[0].set_infrastructure_type('physical')
yield hosts


@pytest.fixture
def registered_hosts(request, target_sat, module_org, module_ak_with_cv):
"""Fixture that registers content hosts to Satellite, based on rh_cloud setup"""
with Broker(**host_conf(request), host_class=ContentHost, _count=2) as hosts:
with contenthost_factory(request=request, _count=2) as hosts:
for vm in hosts:
repo = settings.repos['SATCLIENT_REPO'][f'RHEL{vm.os_version.major}']
vm.register(
Expand Down Expand Up @@ -171,7 +195,7 @@ def cockpit_host(class_target_sat, class_org, rhel_contenthost):
@pytest.fixture
def rex_contenthost(request, module_org, target_sat, module_ak_with_cv):
request.param['no_containers'] = True
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
repo = settings.repos['SATCLIENT_REPO'][f'RHEL{host.os_version.major}']
host.register(
module_org, None, module_ak_with_cv.name, target_sat, repo_data=f'repo={repo}'
Expand All @@ -182,7 +206,7 @@ def rex_contenthost(request, module_org, target_sat, module_ak_with_cv):
@pytest.fixture
def rex_contenthosts(request, module_org, target_sat, module_ak_with_cv):
request.param['no_containers'] = True
with Broker(**host_conf(request), host_class=ContentHost, _count=2) as hosts:
with contenthost_factory(request=request, _count=2) as hosts:
for host in hosts:
repo = settings.repos['SATCLIENT_REPO'][f'RHEL{host.os_version.major}']
host.register(
Expand All @@ -193,8 +217,7 @@ def rex_contenthosts(request, module_org, target_sat, module_ak_with_cv):

@pytest.fixture
def katello_host_tools_tracer_host(rex_contenthost, target_sat):
"""Install katello-host-tools-tracer, create custom
repositories on the host"""
"""Install katello-host-tools-tracer, create custom repositories on the host"""
# create a custom, rhel version-specific OS repo
rhelver = rex_contenthost.os_version.major
if rhelver > 7:
Expand All @@ -215,7 +238,7 @@ def module_container_contenthost(request, module_target_sat, module_org, module_
"distro": "rhel",
"no_containers": True,
}
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
host.register_to_cdn()
for client in constants.CONTAINER_CLIENTS:
assert (
Expand All @@ -239,7 +262,7 @@ def centos_host(request, version):
"distro": "centos",
"no_containers": True,
}
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


Expand All @@ -250,16 +273,14 @@ def oracle_host(request, version):
"distro": "oracle",
"no_containers": True,
}
with Broker(**host_conf(request), host_class=ContentHost) as host:
with contenthost_factory(request=request) as host:
yield host


@pytest.fixture(scope='module', params=[{'rhel_version': 8, 'no_containers': True}])
def external_puppet_server(request):
deploy_args = host_conf(request)
deploy_args['target_cores'] = 2
deploy_args['target_memory'] = '4GiB'
with Broker(**deploy_args, host_class=ContentHost) as host:
request.param.update({'target_cores': 2, 'target_memory': '4GiB'})
with contenthost_factory(request=request) as host:
host.register_to_cdn()
# Install puppet packages
assert (
Expand Down Expand Up @@ -290,21 +311,18 @@ def external_puppet_server(request):


@pytest.fixture(scope="module")
def sat_upgrade_chost():
def sat_upgrade_chost(request): # This leaks! Be sure to clean up manually.
"""A module-level fixture that provides a UBI_8 content host for upgrade scenario testing"""
return Broker(
container_host=settings.content_host.rhel8.container.container_host, host_class=ContentHost
).checkout()
request.param = {"container_host": settings.content_host.ubi8.container.container_host}
return contenthost_factory(request=request)


@pytest.fixture
def custom_host(request):
"""A rhel content host that passes custom host config through request.param"""
deploy_args = request.param
# if 'deploy_rhel_version' is not set, let's default to what's in content_host.yaml
deploy_args['deploy_rhel_version'] = deploy_args.get(
request.param['deploy_rhel_version'] = request.param.get(
'deploy_rhel_version', settings.content_host.default_rhel_version
)
deploy_args['workflow'] = 'deploy-rhel'
with Broker(**deploy_args, host_class=Satellite) as host:
request.param['workflow'] = 'deploy-rhel'
with contenthost_factory(request=request, host_class=Satellite) as host:
yield host
10 changes: 5 additions & 5 deletions tests/new_upgrades/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@


@pytest.fixture
def custom_repo_check_setup(sat_upgrade_chost, content_upgrade_shared_satellite, upgrade_action):
def custom_repo_check_setup(rhel9_contenthost, content_upgrade_shared_satellite, upgrade_action):
"""This is pre-upgrade scenario test to verify if we can create a
custom repository and consume it via content host.
Expand All @@ -53,7 +53,7 @@ def custom_repo_check_setup(sat_upgrade_chost, content_upgrade_shared_satellite,
test_data = Box(
{
'target_sat': target_sat,
'rhel_client': sat_upgrade_chost,
'rhel_client': rhel9_contenthost,
'lce': None,
'repo': None,
'content_view': None,
Expand Down Expand Up @@ -85,11 +85,11 @@ def custom_repo_check_setup(sat_upgrade_chost, content_upgrade_shared_satellite,
query={'search': f'name={product.name}'}
)[0]
ak.add_subscriptions(data={'subscription_id': subscription.id})
sat_upgrade_chost.api_register(
rhel9_contenthost.api_register(
target_sat, organization=org, activation_keys=[ak.name], location=None
)
sat_upgrade_chost.execute('subscription-manager repos --enable=* && yum clean all')
result = sat_upgrade_chost.execute(f'yum install -y {FAKE_0_CUSTOM_PACKAGE_NAME}')
rhel9_contenthost.execute('subscription-manager repos --enable=* && yum clean all')
result = rhel9_contenthost.execute(f'yum install -y {FAKE_0_CUSTOM_PACKAGE_NAME}')
assert result.status == 0
sat_upgrade.ready()
target_sat._session = None
Expand Down
10 changes: 8 additions & 2 deletions tests/upgrades/test_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"""

from broker import Broker
import pytest

from robottelo import constants
Expand Down Expand Up @@ -175,7 +176,7 @@ def test_pre_scenario_custom_repo_check(self, target_sat, sat_upgrade_chost, sav
)

@pytest.mark.post_upgrade(depend_on=test_pre_scenario_custom_repo_check)
def test_post_scenario_custom_repo_check(self, target_sat, pre_upgrade_data):
def test_post_scenario_custom_repo_check(self, request, target_sat, pre_upgrade_data):
"""This is post-upgrade scenario test to verify if we can alter the
created custom repository and satellite will be able to sync back
the repo.
Expand All @@ -192,6 +193,12 @@ def test_post_scenario_custom_repo_check(self, target_sat, pre_upgrade_data):
"""
client_hostname = pre_upgrade_data.get('rhel_client')
rhel_client = ContentHost.get_host_by_hostname(client_hostname)

@request.addfinalizer
def _cleanup():
Broker(hosts=[rhel_client]).checkin()

content_view_name = pre_upgrade_data.get('content_view_name')
lce_id = pre_upgrade_data.get('lce_id')
repo_name = pre_upgrade_data.get('repo_name')
Expand All @@ -208,7 +215,6 @@ def test_post_scenario_custom_repo_check(self, target_sat, pre_upgrade_data):
data={'environment_ids': lce_id}
)

rhel_client = ContentHost.get_host_by_hostname(client_hostname)
result = rhel_client.execute(f'yum install -y {FAKE_4_CUSTOM_PACKAGE_NAME}')
assert result.status == 0

Expand Down

0 comments on commit 0c6809c

Please sign in to comment.