From 45f151acffd43ac8da167676b2c1b09060a26530 Mon Sep 17 00:00:00 2001 From: omkarkhatavkar Date: Wed, 8 Nov 2023 09:39:02 +0530 Subject: [PATCH 1/2] recording ui-session-id for report portal logging --- pytest_fixtures/core/ui.py | 3 ++- robottelo/hosts.py | 28 +++++++++++++++++++-------- tests/foreman/destructive/conftest.py | 24 ++++++++++++++++++++++- tests/foreman/ui/conftest.py | 23 ++++++++++++++++++++++ 4 files changed, 68 insertions(+), 10 deletions(-) diff --git a/pytest_fixtures/core/ui.py b/pytest_fixtures/core/ui.py index 7edbd39b731..f87f4cec0ad 100644 --- a/pytest_fixtures/core/ui.py +++ b/pytest_fixtures/core/ui.py @@ -49,7 +49,8 @@ def test_foo(session): session.architecture.create({'name': 'bar'}) """ - return target_sat.ui_session(test_name, ui_user.login, ui_user.password) + with target_sat.ui_session(test_name, ui_user.login, ui_user.password) as session: + yield session @pytest.fixture diff --git a/robottelo/hosts.py b/robottelo/hosts.py index 42edd4c446e..a73518e9857 100644 --- a/robottelo/hosts.py +++ b/robottelo/hosts.py @@ -1726,6 +1726,7 @@ def __init__(self, hostname=None, **kwargs): # create dummy classes for later population self._api = type('api', (), {'_configured': False}) self._cli = type('cli', (), {'_configured': False}) + self.record_property = None def _swap_nailgun(self, new_version): """Install a different version of nailgun from GitHub and invalidate the module cache.""" @@ -1814,6 +1815,7 @@ def omit_credentials(self): yield self.omitting_credentials = False + @contextmanager def ui_session(self, testname=None, user=None, password=None, url=None, login=True): """Initialize an airgun Session object and store it as self.ui_session""" @@ -1826,14 +1828,24 @@ def get_caller(): if frame.function.startswith('test_'): return frame.function - return Session( - session_name=testname or get_caller(), - user=user or settings.server.admin_username, - password=password or settings.server.admin_password, - url=url, - hostname=self.hostname, - login=login, - ) + try: + ui_session = Session( + session_name=testname or get_caller(), + user=user or settings.server.admin_username, + password=password or settings.server.admin_password, + url=url, + hostname=self.hostname, + login=login, + ) + yield ui_session + except Exception: + raise + finally: + video_url = settings.ui.grid_url.replace( + ':4444', f'/videos/{ui_session.ui_session_id}.mp4' + ) + self.record_property('video_url', video_url) + self.record_property('session_id', ui_session.ui_session_id) @property def satellite(self): diff --git a/tests/foreman/destructive/conftest.py b/tests/foreman/destructive/conftest.py index f83f6a6313e..aa7afa3e7f5 100644 --- a/tests/foreman/destructive/conftest.py +++ b/tests/foreman/destructive/conftest.py @@ -1,6 +1,7 @@ import pytest from robottelo.constants import DEFAULT_ORG +from robottelo.hosts import Satellite @pytest.fixture @@ -22,4 +23,25 @@ def test_foo(session): session.architecture.create({'name': 'bar'}) """ - return module_target_sat.ui_session(test_name, ui_user.login, ui_user.password) + with module_target_sat.ui_session(test_name, ui_user.login, ui_user.password) as session: + yield session + + +@pytest.fixture(autouse=True) +def ui_session_record_property(request, record_property): + """ + Autouse fixture to set the record_property attribute for Satellite instances in the test. + + This fixture iterates over all fixtures in the current test node + (excluding the current fixture) and sets the record_property attribute + for instances of the Satellite class. + + Args: + request: The pytest request object. + record_property: The value to set for the record_property attribute. + """ + for fixture in request.node.fixturenames: + if request.fixturename != fixture: + if isinstance(request.getfixturevalue(fixture), Satellite): + sat = request.getfixturevalue(fixture) + sat.record_property = record_property diff --git a/tests/foreman/ui/conftest.py b/tests/foreman/ui/conftest.py index e69de29bb2d..da0a7fac764 100644 --- a/tests/foreman/ui/conftest.py +++ b/tests/foreman/ui/conftest.py @@ -0,0 +1,23 @@ +import pytest + +from robottelo.hosts import Satellite + + +@pytest.fixture(autouse=True) +def ui_session_record_property(request, record_property): + """ + Autouse fixture to set the record_property attribute for Satellite instances in the test. + + This fixture iterates over all fixtures in the current test node + (excluding the current fixture) and sets the record_property attribute + for instances of the Satellite class. + + Args: + request: The pytest request object. + record_property: The value to set for the record_property attribute. + """ + for fixture in request.node.fixturenames: + if request.fixturename != fixture: + if isinstance(request.getfixturevalue(fixture), Satellite): + sat = request.getfixturevalue(fixture) + sat.record_property = record_property From 60e4eab057fea00cbb6f324a85b566c3a0a43cb1 Mon Sep 17 00:00:00 2001 From: omkarkhatavkar Date: Thu, 23 Nov 2023 17:41:52 +0530 Subject: [PATCH 2/2] moving the fixture to common location and adding fspath check --- tests/foreman/conftest.py | 25 +++++++++++++++++++++++++ tests/foreman/destructive/conftest.py | 21 --------------------- tests/foreman/ui/conftest.py | 23 ----------------------- 3 files changed, 25 insertions(+), 44 deletions(-) diff --git a/tests/foreman/conftest.py b/tests/foreman/conftest.py index 4e3aca8a77c..1e11a276863 100644 --- a/tests/foreman/conftest.py +++ b/tests/foreman/conftest.py @@ -1,6 +1,8 @@ """Configurations for py.test runner""" + import pytest +from robottelo.hosts import Satellite from robottelo.logging import collection_logger @@ -40,3 +42,26 @@ def pytest_collection_modifyitems(session, items, config): config.hook.pytest_deselected(items=deselected_items) items[:] = [item for item in items if item not in deselected_items] + + +@pytest.fixture(autouse=True) +def ui_session_record_property(request, record_property): + """ + Autouse fixture to set the record_property attribute for Satellite instances in the test. + + This fixture iterates over all fixtures in the current test node + (excluding the current fixture) and sets the record_property attribute + for instances of the Satellite class. + + Args: + request: The pytest request object. + record_property: The value to set for the record_property attribute. + """ + test_directories = ['tests/foreman/destructive', 'tests/foreman/ui'] + test_file_path = request.node.fspath.strpath + if any(directory in test_file_path for directory in test_directories): + for fixture in request.node.fixturenames: + if request.fixturename != fixture: + if isinstance(request.getfixturevalue(fixture), Satellite): + sat = request.getfixturevalue(fixture) + sat.record_property = record_property diff --git a/tests/foreman/destructive/conftest.py b/tests/foreman/destructive/conftest.py index aa7afa3e7f5..190bc2cabe0 100644 --- a/tests/foreman/destructive/conftest.py +++ b/tests/foreman/destructive/conftest.py @@ -1,7 +1,6 @@ import pytest from robottelo.constants import DEFAULT_ORG -from robottelo.hosts import Satellite @pytest.fixture @@ -25,23 +24,3 @@ def test_foo(session): """ with module_target_sat.ui_session(test_name, ui_user.login, ui_user.password) as session: yield session - - -@pytest.fixture(autouse=True) -def ui_session_record_property(request, record_property): - """ - Autouse fixture to set the record_property attribute for Satellite instances in the test. - - This fixture iterates over all fixtures in the current test node - (excluding the current fixture) and sets the record_property attribute - for instances of the Satellite class. - - Args: - request: The pytest request object. - record_property: The value to set for the record_property attribute. - """ - for fixture in request.node.fixturenames: - if request.fixturename != fixture: - if isinstance(request.getfixturevalue(fixture), Satellite): - sat = request.getfixturevalue(fixture) - sat.record_property = record_property diff --git a/tests/foreman/ui/conftest.py b/tests/foreman/ui/conftest.py index da0a7fac764..e69de29bb2d 100644 --- a/tests/foreman/ui/conftest.py +++ b/tests/foreman/ui/conftest.py @@ -1,23 +0,0 @@ -import pytest - -from robottelo.hosts import Satellite - - -@pytest.fixture(autouse=True) -def ui_session_record_property(request, record_property): - """ - Autouse fixture to set the record_property attribute for Satellite instances in the test. - - This fixture iterates over all fixtures in the current test node - (excluding the current fixture) and sets the record_property attribute - for instances of the Satellite class. - - Args: - request: The pytest request object. - record_property: The value to set for the record_property attribute. - """ - for fixture in request.node.fixturenames: - if request.fixturename != fixture: - if isinstance(request.getfixturevalue(fixture), Satellite): - sat = request.getfixturevalue(fixture) - sat.record_property = record_property