Skip to content

Commit

Permalink
Capsule N-Minus testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jyejare committed Nov 29, 2023
1 parent a9ffccd commit 985982a
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
2 changes: 2 additions & 0 deletions conf/capsule.yaml.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
CAPSULE:
# Capsule hostname for N-minus testing
HOSTNAME:
VERSION:
# The full release version (6.9.2)
RELEASE: # populate with capsule version
Expand Down
1 change: 1 addition & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
'pytest_plugins.factory_collection',
'pytest_plugins.requirements.update_requirements',
'pytest_plugins.sanity_plugin',
'pytest_plugins.capsule_n-minus',
# Fixtures
'pytest_fixtures.core.broker',
'pytest_fixtures.core.sat_cap_factory',
Expand Down
23 changes: 16 additions & 7 deletions pytest_fixtures/core/sat_cap_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,17 @@ def _target_satellite_host(request, satellite_factory):

@contextmanager
def _target_capsule_host(request, capsule_factory):
if 'sanity' not in request.config.option.markexpr:
if 'sanity' not in request.config.option.markexpr and not request.config.option.n_minus:
new_cap = capsule_factory()
yield new_cap
new_cap.teardown()
Broker(hosts=[new_cap]).checkin()
elif request.config.option.n_minus:
if not settings.capsule.hostname:
hosts = Capsule.get_hosts_from_inventory(filter="'cap' in @inv.name")
settings.capsule.hostname = hosts[0].hostname
caps = Capsule(hostname=settings.capsule.hostname)
yield caps
else:
yield

Expand Down Expand Up @@ -148,9 +154,10 @@ def session_capsule_host(request, capsule_factory):


@pytest.fixture
def capsule_configured(capsule_host, target_sat):
def capsule_configured(request, capsule_host, target_sat):
"""Configure the capsule instance with the satellite from settings.server.hostname"""
capsule_host.capsule_setup(sat_host=target_sat)
if not request.config.option.n_minus:
capsule_host.capsule_setup(sat_host=target_sat)
return capsule_host


Expand All @@ -162,16 +169,18 @@ def large_capsule_configured(large_capsule_host, target_sat):


@pytest.fixture(scope='module')
def module_capsule_configured(module_capsule_host, module_target_sat):
def module_capsule_configured(request, module_capsule_host, module_target_sat):
"""Configure the capsule instance with the satellite from settings.server.hostname"""
module_capsule_host.capsule_setup(sat_host=module_target_sat)
if not request.config.option.n_minus:
module_capsule_host.capsule_setup(sat_host=module_target_sat)
return module_capsule_host


@pytest.fixture(scope='session')
def session_capsule_configured(session_capsule_host, session_target_sat):
def session_capsule_configured(request, session_capsule_host, session_target_sat):
"""Configure the capsule instance with the satellite from settings.server.hostname"""
session_capsule_host.capsule_setup(sat_host=session_target_sat)
if not request.config.option.n_minus:
session_capsule_host.capsule_setup(sat_host=session_target_sat)
return session_capsule_host


Expand Down
43 changes: 43 additions & 0 deletions pytest_plugins/capsule_n-minus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Collection of Capsule Factory fixture tests
# No destructive tests
# Adjust capsule host and capsule_configured host behavior for n_minus testing
# Calculate capsule hostname from inventory just as we do in xDist.py

def pytest_addoption(parser):
"""Add options for pytest to collect tests based on fixtures its using"""
help_text = '''
Collects tests based on capsule fixtures used by tests and uncollect destructive tests
Usage: --n-minus
example: pytest --n-minus tests/foreman
'''
parser.addoption(
"--n-minus", action='store_true', default=False,
help=help_text)


def pytest_collection_modifyitems(items, config):

if not config.getoption('n_minus', False):
return

selected = []
deselected = []

for item in items:
is_destructive = item.get_closest_marker('destructive')
# Deselect Destructive tests and tests without capsule_factory fixture
if 'capsule_factory' not in item.fixturenames or is_destructive:
deselected.append(item)
continue
if 'session_puppet_enabled_sat' in item.fixturenames:
deselected.append(item)
continue
if 'sat_maintain' in item.fixturenames and 'satellite' in item.callspec.params.values():
deselected.append(item)
continue
selected.append(item)

config.hook.pytest_deselected(items=deselected)
items[:] = selected

0 comments on commit 985982a

Please sign in to comment.