From 28b706efbf27225cec9b67c6358eb3b4bab8c164 Mon Sep 17 00:00:00 2001 From: Vladimir Sedmik Date: Mon, 13 Nov 2023 15:07:50 +0100 Subject: [PATCH 1/3] Add test for SCA subscription report --- tests/foreman/api/test_reporttemplates.py | 86 +++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/tests/foreman/api/test_reporttemplates.py b/tests/foreman/api/test_reporttemplates.py index 7d6bf4d2e1d..bdc65fd2592 100644 --- a/tests/foreman/api/test_reporttemplates.py +++ b/tests/foreman/api/test_reporttemplates.py @@ -16,6 +16,8 @@ :Upstream: No """ +import re + from broker import Broker from fauxfactory import gen_string import pytest @@ -24,6 +26,7 @@ from robottelo.config import settings from robottelo.constants import ( + DEFAULT_ARCHITECTURE, DEFAULT_SUBSCRIPTION_NAME, FAKE_1_CUSTOM_PACKAGE, FAKE_1_CUSTOM_PACKAGE_NAME, @@ -836,3 +839,86 @@ def test_positive_installable_errata( installable_errata = report[0] assert FAKE_1_CUSTOM_PACKAGE_NAME in installable_errata['Packages'] assert installable_errata['Erratum'] == ERRATUM_ID + + +@pytest.mark.tier2 +@pytest.mark.rhel_ver_match(r'^(?!6$)\d+$') +def test_positive_installed_products( + target_sat, + rhel_contenthost, + default_location, + function_sca_manifest_org, +): + """Generate 'Host - Installed Products' report for an SCA host. + + :id: d290daa2-aaba-4f4d-8eee-d8a540415320 + + :parametrized: yes + + :setup: + 1. RH content published in a CV, promoted to LCE, AK created. + All inside an SCA-enabled organization. + 2. A RHEL content host. + + :steps: + 1. Register the content host using the AK. + 2. Generate 'Host - Installed Products' report. + 3. Verify the report generated from the template. + + :expectedresults: + 1. Report is generated with correct values. + + :CaseImportance: Medium + """ + org = function_sca_manifest_org + lce_name = gen_string('alpha') + cv_name = gen_string('alpha') + + rh_repo = { + 'basearch': DEFAULT_ARCHITECTURE, + 'product': REPOS['rhae2']['product'], + 'name': REPOS['rhae2']['name'], + 'reposet': REPOS['rhae2']['reposet'], + 'releasever': None, + } + repo_id = target_sat.api_factory.enable_sync_redhat_repo(rh_repo, org.id) + cv = target_sat.api_factory.cv_publish_promote(cv_name, lce_name, repo_id, org.id) + ak = target_sat.api.ActivationKey( + content_view=cv, organization=org, environment=cv.environment[-1] + ).create() + + rhel_contenthost.register(org, default_location, ak.name, target_sat) + assert rhel_contenthost.subscribed, 'Host registration failed.' + + input_data = { + 'organization_id': org.id, + 'report_format': "json", + 'input_values': { + 'hosts': rhel_contenthost.hostname, + }, + } + report = ( + target_sat.api.ReportTemplate() + .search(query={'search': 'name="Host - Installed Products"'})[0] + .read() + .generate(data=input_data) + ) + assert report + assert report[0]['Host Name'] == rhel_contenthost.hostname, 'Incorrect host was reported.' + assert report[0]['Organization'] == org.name, 'Incorrect org was reported.' + assert report[0]['Lifecycle Environment'] == lce_name, 'Incorrect LCE was reported.' + assert report[0]['Content View'] == cv_name, 'Incorrect CV was reported.' + + # Get the installed products via rake and compare them with report + rake = target_sat.execute( + f'echo "Host.find_by(name: \'{rhel_contenthost.hostname}\').' + 'subscription_facet.installed_products" | foreman-rake console' + ) + assert rake.status == 0, f'Rake call failed with this output:\n({rake.stdout}).' + + pattern = re.compile(r'name: "(.*?)".*?cp_product_id: "(.*?)"') + matches = pattern.findall(rake.stdout) + products = [f"{match[0]} ({match[1]})" for match in matches] + assert len(products), 'No installed products to compare.' + + assert set(products) == set(report[0]['Products']), 'Reported products do not match.' From 4aa0d485c40105bb225648ef8ff9d7df79c91609 Mon Sep 17 00:00:00 2001 From: Vladimir Sedmik Date: Tue, 14 Nov 2023 18:41:15 +0100 Subject: [PATCH 2/3] Add checks for Role and Usage syspurpose tags --- tests/foreman/api/test_reporttemplates.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/foreman/api/test_reporttemplates.py b/tests/foreman/api/test_reporttemplates.py index bdc65fd2592..b8f42245150 100644 --- a/tests/foreman/api/test_reporttemplates.py +++ b/tests/foreman/api/test_reporttemplates.py @@ -873,6 +873,12 @@ def test_positive_installed_products( org = function_sca_manifest_org lce_name = gen_string('alpha') cv_name = gen_string('alpha') + sys_tags = {'role': gen_string('alpha'), 'usage': gen_string('alpha')} + + for key, val in sys_tags.items(): + assert ( + rhel_contenthost.execute(f'subscription-manager {key} --set {val}').status == 0 + ), f'Setting of {key} failed.' rh_repo = { 'basearch': DEFAULT_ARCHITECTURE, @@ -903,11 +909,13 @@ def test_positive_installed_products( .read() .generate(data=input_data) ) - assert report + assert report, 'No report generated.' assert report[0]['Host Name'] == rhel_contenthost.hostname, 'Incorrect host was reported.' assert report[0]['Organization'] == org.name, 'Incorrect org was reported.' assert report[0]['Lifecycle Environment'] == lce_name, 'Incorrect LCE was reported.' assert report[0]['Content View'] == cv_name, 'Incorrect CV was reported.' + assert report[0]['Role'] == sys_tags['role'], 'Incorrect role was reported.' + assert report[0]['Usage'] == sys_tags['usage'], 'Incorrect usage was reported.' # Get the installed products via rake and compare them with report rake = target_sat.execute( From c57e16869b48429c63cfa2a986cda137ff6fb45a Mon Sep 17 00:00:00 2001 From: Vladimir Sedmik Date: Wed, 15 Nov 2023 11:25:47 +0100 Subject: [PATCH 3/3] Address comments --- tests/foreman/api/test_reporttemplates.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/foreman/api/test_reporttemplates.py b/tests/foreman/api/test_reporttemplates.py index b8f42245150..610f9cf6eea 100644 --- a/tests/foreman/api/test_reporttemplates.py +++ b/tests/foreman/api/test_reporttemplates.py @@ -861,9 +861,10 @@ def test_positive_installed_products( 2. A RHEL content host. :steps: - 1. Register the content host using the AK. - 2. Generate 'Host - Installed Products' report. - 3. Verify the report generated from the template. + 1. Set syspurpose tags of the content host via subman. + 2. Register the content host using the AK. + 3. Generate 'Host - Installed Products' report. + 4. Verify the report generated from the template. :expectedresults: 1. Report is generated with correct values. @@ -917,7 +918,7 @@ def test_positive_installed_products( assert report[0]['Role'] == sys_tags['role'], 'Incorrect role was reported.' assert report[0]['Usage'] == sys_tags['usage'], 'Incorrect usage was reported.' - # Get the installed products via rake and compare them with report + # Get the installed products via rake and compare them with generated report rake = target_sat.execute( f'echo "Host.find_by(name: \'{rhel_contenthost.hostname}\').' 'subscription_facet.installed_products" | foreman-rake console' @@ -926,7 +927,7 @@ def test_positive_installed_products( pattern = re.compile(r'name: "(.*?)".*?cp_product_id: "(.*?)"') matches = pattern.findall(rake.stdout) - products = [f"{match[0]} ({match[1]})" for match in matches] + products = [f'{match[0]} ({match[1]})' for match in matches] assert len(products), 'No installed products to compare.' assert set(products) == set(report[0]['Products']), 'Reported products do not match.'