From dd11c7386e0b520aed4c7e04bd3b4dc6100040ac Mon Sep 17 00:00:00 2001 From: Aiden Fine Date: Mon, 13 Jul 2026 15:14:52 -0400 Subject: [PATCH 1/7] Update query_db function to support containerized envs --- robottelo/hosts.py | 7 ++++++- tests/foreman/api/test_contentview.py | 9 +++++---- tests/foreman/api/test_notifications.py | 14 +++++++------- tests/foreman/api/test_repository.py | 13 +++++++------ tests/foreman/cli/test_oscap.py | 12 ++---------- tests/foreman/ui/test_host.py | 20 ++++++++++---------- 6 files changed, 37 insertions(+), 38 deletions(-) diff --git a/robottelo/hosts.py b/robottelo/hosts.py index e4dd901f32f..cc6ef53e382 100644 --- a/robottelo/hosts.py +++ b/robottelo/hosts.py @@ -2339,13 +2339,18 @@ def query_db(self, query, db='foreman', output_format='json'): CLIReturnCodeError: If the database query fails """ + from robottelo.enums import InstallMethod + def _execute_db_query(cmd): result = self.execute(cmd) if result.status != 0: raise CLIReturnCodeError(result.status, result.stderr, f'"{cmd}" failed') return result - base_cmd = f'sudo -u postgres psql -d {db}' + if self.install_method == InstallMethod.FOREMANCTL: + base_cmd = f'podman exec postgresql psql -U foreman -d {db}' + else: + base_cmd = f'sudo -u postgres psql -d {db}' if output_format == 'json': cmd = f'{base_cmd} -A -t -c "SELECT json_agg(row_to_json(t)) FROM ({query}) t"' diff --git a/tests/foreman/api/test_contentview.py b/tests/foreman/api/test_contentview.py index ad6e3263ce0..6acccd769c5 100644 --- a/tests/foreman/api/test_contentview.py +++ b/tests/foreman/api/test_contentview.py @@ -2723,11 +2723,12 @@ def test_repository_rpms_id_type(target_sat): :CaseImportance: Medium """ - db_out = target_sat.execute( - 'sudo -u postgres psql -d foreman -c "select * from pg_sequences where sequencename=\'katello_repository_rpms_id_seq\';"' + db_out = target_sat.query_db( + "select * from pg_sequences where sequencename='katello_repository_rpms_id_seq'", + output_format='raw', ) - assert 'bigint' in db_out.stdout - assert 'integer' not in db_out.stdout + assert 'bigint' in db_out + assert 'integer' not in db_out def test_negative_readonly_user_actions( diff --git a/tests/foreman/api/test_notifications.py b/tests/foreman/api/test_notifications.py index 0709932cfe1..d66274c7e53 100644 --- a/tests/foreman/api/test_notifications.py +++ b/tests/foreman/api/test_notifications.py @@ -233,15 +233,15 @@ def long_running_task(target_sat): }, ) sql_date_2_days_ago = "now() - INTERVAL '2 days'" - result = target_sat.execute( - "su - postgres -c \"psql foreman postgres < Date: Mon, 13 Jul 2026 15:15:17 -0400 Subject: [PATCH 2/7] update test_health.py --- tests/foreman/maintain/test_health.py | 33 +++++++++++++-------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tests/foreman/maintain/test_health.py b/tests/foreman/maintain/test_health.py index 5c1082e0b84..221709b82ad 100644 --- a/tests/foreman/maintain/test_health.py +++ b/tests/foreman/maintain/test_health.py @@ -642,6 +642,7 @@ def test_positive_remove_job_file(sat_maintain): assert sat_maintain.execute("ls -l /var/lib/pulp/job1.0.0").status != 0 +@pytest.mark.foreman_installer def test_positive_health_check_corrupted_roles(sat_maintain, request): """Verify corrupted-roles check. @@ -667,10 +668,9 @@ def test_positive_health_check_corrupted_roles(sat_maintain, request): @request.addfinalizer def _finalize(): - resource_type = r"'\''Host'\''" - sat_maintain.execute( - f'''sudo su - postgres -c "psql -d foreman -c 'UPDATE permissions SET - resource_type = {resource_type} WHERE name = {permission_name};'"''' + sat_maintain.query_db( + "UPDATE permissions SET resource_type = 'Host' WHERE name = 'console_hosts'", + output_format='raw', ) sat_maintain.cli.Role.delete(options={'name': role_name}) @@ -678,18 +678,18 @@ def _finalize(): sat_maintain.cli.Filter.create( options={'role': role_name, 'permissions': ['view_hosts', 'console_hosts']} ) - permission_name = r"'\''console_hosts'\''" - resource_type = rf"'\''{resource_type}'\''" - setup = sat_maintain.execute( - f'''sudo su - postgres -c "psql -d foreman -c 'UPDATE permissions SET - resource_type = {resource_type} WHERE name = {permission_name};'"''' + sat_maintain.query_db( + f"UPDATE permissions SET resource_type = '{resource_type}' " + f"WHERE name = 'console_hosts'", + output_format='raw', ) - assert setup.status == 0 result = sat_maintain.cli.Filter.list(options={'search': role_name}, output_format='yaml') # Shows the filter id which comprises of role id hence asserting 2 here assert result.count('Id') == 2 # Run corrupted-roles check. - result = sat_maintain.cli.Health.check(options={'label': 'corrupted-roles', 'assumeyes': True}) + result = sat_maintain.cli.Health.check( + options={'label': 'corrupted-roles', 'assumeyes': True} + ) assert result.status == 0 assert 'FAIL' in result.stdout # Verify corrupted roles are fixed and new filter is created for updated resource_type. @@ -738,6 +738,7 @@ def _finalize(): assert 'WARNING' in result.stdout +@pytest.mark.foreman_installer def test_positive_health_check_duplicate_permissions(sat_maintain): """Verify duplicate-permissions check @@ -758,13 +759,11 @@ def test_positive_health_check_duplicate_permissions(sat_maintain): :BZ: 1849110, 1884024 """ # Verify if check failed because of duplicate permissions - name = r"'\''view_ansible_variables'\''" - resource_type = r"'\''AnsibleVariable'\''" - result = sat_maintain.execute( - f'''sudo su - postgres -c "psql -d foreman -c 'INSERT INTO permissions(name, resource_type) - VALUES({name}, {resource_type});'"''' + sat_maintain.query_db( + "INSERT INTO permissions(name, resource_type) " + "VALUES('view_ansible_variables', 'AnsibleVariable')", + output_format='raw', ) - assert result.status == 0 result = sat_maintain.cli.Health.check({'label': 'duplicate-permissions', 'assumeyes': True}) assert result.status == 0 assert 'FAIL' in result.stdout From dbf6e731dc7b803f3656a2c8663aa8a3f2ac58bd Mon Sep 17 00:00:00 2001 From: Aiden Fine Date: Mon, 13 Jul 2026 15:21:10 -0400 Subject: [PATCH 3/7] ruff format --- tests/foreman/api/test_repository.py | 2 +- tests/foreman/maintain/test_health.py | 7 ++----- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/tests/foreman/api/test_repository.py b/tests/foreman/api/test_repository.py index 48e47e883a9..0f00e51ae0e 100644 --- a/tests/foreman/api/test_repository.py +++ b/tests/foreman/api/test_repository.py @@ -1377,7 +1377,7 @@ def test_positive_sync_repo_null_contents_changed(self, module_sca_manifest_org, releasever=None, ) target_sat.api.Repository(id=repo_id).sync() - assert( + assert ( target_sat.query_db( "select class,execution_plan_uuid,input " "from dynflow_actions where input LIKE '%\"contents_changed\":null%'" diff --git a/tests/foreman/maintain/test_health.py b/tests/foreman/maintain/test_health.py index 221709b82ad..ffaa452a5a4 100644 --- a/tests/foreman/maintain/test_health.py +++ b/tests/foreman/maintain/test_health.py @@ -679,17 +679,14 @@ def _finalize(): options={'role': role_name, 'permissions': ['view_hosts', 'console_hosts']} ) sat_maintain.query_db( - f"UPDATE permissions SET resource_type = '{resource_type}' " - f"WHERE name = 'console_hosts'", + f"UPDATE permissions SET resource_type = '{resource_type}' WHERE name = 'console_hosts'", output_format='raw', ) result = sat_maintain.cli.Filter.list(options={'search': role_name}, output_format='yaml') # Shows the filter id which comprises of role id hence asserting 2 here assert result.count('Id') == 2 # Run corrupted-roles check. - result = sat_maintain.cli.Health.check( - options={'label': 'corrupted-roles', 'assumeyes': True} - ) + result = sat_maintain.cli.Health.check(options={'label': 'corrupted-roles', 'assumeyes': True}) assert result.status == 0 assert 'FAIL' in result.stdout # Verify corrupted roles are fixed and new filter is created for updated resource_type. From c7c42b0ffa7c78025302aab48e67f0671bd7d2cb Mon Sep 17 00:00:00 2001 From: Aiden Fine Date: Thu, 16 Jul 2026 11:25:43 -0400 Subject: [PATCH 4/7] move Install method import to top level --- robottelo/hosts.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/robottelo/hosts.py b/robottelo/hosts.py index cc6ef53e382..eb34ae3ae6c 100644 --- a/robottelo/hosts.py +++ b/robottelo/hosts.py @@ -54,7 +54,7 @@ RHSSO_USER_UPDATE, SATELLITE_VERSION, ) -from robottelo.enums import NetworkType +from robottelo.enums import InstallMethod, NetworkType from robottelo.exceptions import ( CapsuleHostError, CLIFactoryError, @@ -2271,7 +2271,6 @@ def install_satellite( :param foremanctl_parameters: Parameters list for foremanctl deploy :return: Installation result """ - from robottelo.enums import InstallMethod from robottelo.utils.installer import InstallerCommand # Determine method @@ -2339,8 +2338,6 @@ def query_db(self, query, db='foreman', output_format='json'): CLIReturnCodeError: If the database query fails """ - from robottelo.enums import InstallMethod - def _execute_db_query(cmd): result = self.execute(cmd) if result.status != 0: From 9223dcce33ae8c7311a669bce28c3a0320fd969f Mon Sep 17 00:00:00 2001 From: Aiden Fine Date: Thu, 16 Jul 2026 11:27:40 -0400 Subject: [PATCH 5/7] remove local imports --- robottelo/hosts.py | 2 -- tests/foreman/maintain/test_health.py | 2 -- 2 files changed, 4 deletions(-) diff --git a/robottelo/hosts.py b/robottelo/hosts.py index eb34ae3ae6c..e7dd16a05ec 100644 --- a/robottelo/hosts.py +++ b/robottelo/hosts.py @@ -1805,7 +1805,6 @@ def detect_install_method(self): :return: InstallMethod enum value :rtype: InstallMethod """ - from robottelo.enums import InstallMethod # Runtime override if hasattr(self, '_install_method_override'): @@ -1855,7 +1854,6 @@ def get_service_names(self): :rtype: list """ from robottelo.constants import InstallationServices - from robottelo.enums import InstallMethod if self.install_method == InstallMethod.FOREMANCTL: return InstallationServices.FOREMANCTL_SERVICES diff --git a/tests/foreman/maintain/test_health.py b/tests/foreman/maintain/test_health.py index ffaa452a5a4..714e43bbb6e 100644 --- a/tests/foreman/maintain/test_health.py +++ b/tests/foreman/maintain/test_health.py @@ -642,7 +642,6 @@ def test_positive_remove_job_file(sat_maintain): assert sat_maintain.execute("ls -l /var/lib/pulp/job1.0.0").status != 0 -@pytest.mark.foreman_installer def test_positive_health_check_corrupted_roles(sat_maintain, request): """Verify corrupted-roles check. @@ -735,7 +734,6 @@ def _finalize(): assert 'WARNING' in result.stdout -@pytest.mark.foreman_installer def test_positive_health_check_duplicate_permissions(sat_maintain): """Verify duplicate-permissions check From 1feb0e70c2e5722509fb6d05b644c3cde20196e6 Mon Sep 17 00:00:00 2001 From: Aiden Fine Date: Wed, 22 Jul 2026 21:48:33 -0400 Subject: [PATCH 6/7] add db_user arg to query_db() --- robottelo/hosts.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/robottelo/hosts.py b/robottelo/hosts.py index e7dd16a05ec..46897316527 100644 --- a/robottelo/hosts.py +++ b/robottelo/hosts.py @@ -2321,13 +2321,14 @@ def install_satellite( return result - def query_db(self, query, db='foreman', output_format='json'): + def query_db(self, query, db='foreman', output_format='json', db_user='foreman'): """Execute a PostgreSQL query and return the result. Args: query: SQL query to execute db: Database name (default: 'foreman') output_format: Output format - 'json' for JSON array, raw output otherwise + db_user: Database user (default: 'foreman') Returns: list of dicts if output_format='json', str otherwise @@ -2343,7 +2344,7 @@ def _execute_db_query(cmd): return result if self.install_method == InstallMethod.FOREMANCTL: - base_cmd = f'podman exec postgresql psql -U foreman -d {db}' + base_cmd = f'podman exec postgresql psql -U {db_user} -d {db}' else: base_cmd = f'sudo -u postgres psql -d {db}' From a7fcecd4d95abdcf00255612ef9fa90187cd2312 Mon Sep 17 00:00:00 2001 From: Aiden Fine Date: Thu, 23 Jul 2026 09:16:59 -0400 Subject: [PATCH 7/7] replace with self.install_method with settings.server.install_method --- robottelo/hosts.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/robottelo/hosts.py b/robottelo/hosts.py index 46897316527..81635c3bbbb 100644 --- a/robottelo/hosts.py +++ b/robottelo/hosts.py @@ -2343,7 +2343,7 @@ def _execute_db_query(cmd): raise CLIReturnCodeError(result.status, result.stderr, f'"{cmd}" failed') return result - if self.install_method == InstallMethod.FOREMANCTL: + if settings.server.install_method == InstallMethod.FOREMANCTL: base_cmd = f'podman exec postgresql psql -U {db_user} -d {db}' else: base_cmd = f'sudo -u postgres psql -d {db}'