Skip to content

Commit

Permalink
codecoverage bug2173870 (SatelliteQE#14273)
Browse files Browse the repository at this point in the history
* codecoverage bug2173870

* update the code for f-format and comments

* fix conflict and format issue

* update to fix conflict

* update to fix conflict

* use nested list comprehensions to replace loop

* replace double quotes with single quotes

* remove the duplicated \n

* Add the description of the methods

* Update the pre-commit for description

* Update robottelo/utils/virtwho.py

Co-authored-by: Gaurav Talreja <[email protected]>

* resolve conflict

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: Gaurav Talreja <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
3 people authored and jyejare committed Oct 19, 2024
1 parent 6b1bf47 commit 927f6eb
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
73 changes: 73 additions & 0 deletions robottelo/utils/virtwho.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,33 @@ def hypervisor_json_create(hypervisors, guests):
return {"hypervisors": hypervisors_list}


def hypervisor_fake_json_create(hypervisors, guests):
"""
Create a hypervisor guest json data for fake config usages. For example:
{'hypervisors': [{'uuid': '820b5143-3885-4dba-9358-4ce8c30d934e',
'guests': [{'guestId': 'afb91b1f-8438-46f5-bc67-d7ab328ef782', 'state': 1,
'attributes': {'active': 1, 'virtWhoType': 'esx'}}]}]}
:param hypervisors: how many hypervisors will be created
:param guests: how many guests will be created
"""
hypervisors_list = [
{
'guests': [
{
"guestId": str(uuid.uuid4()),
"state": 1,
"attributes": {"active": 1, "virtWhoType": "esx"},
}
for _ in range(guests)
],
'name': str(uuid.uuid4()),
'uuid': str(uuid.uuid4()),
}
for _ in range(hypervisors)
]
return {"hypervisors": hypervisors_list}


def create_fake_hypervisor_content(org_label, hypervisors, guests):
"""
Post the fake hypervisor content to satellite server
Expand Down Expand Up @@ -527,3 +554,49 @@ def get_configure_command_option(deploy_type, args, org=DEFAULT_ORG):
if deploy_type == 'name':
return f"hammer -u {username} -p {password} virt-who-config deploy --name {args['name']} --organization '{org}' "
return None


def vw_fake_conf_create(
owner,
rhsm_hostname,
rhsm_username,
rhsm_encrypted_password,
fake_conf_file,
json_file,
is_hypervisor=True,
):
"""Create fake config file
:param owner: Name of the Owner
:param rhsm_hostname: Name of the rhsm_hostname
:param rhsm_username: Name of the rhsm_username
:param rhsm_encrypted_password: Value of the rhsm_encrypted_password
:param fake_conf_file: Name of the fake_conf_file
:param json_file: Name of the json_file
:param is_hypervisor: Default ir True
:return:
"""
conf_name = fake_conf_file.split("/")[-1].split(".")[0]
file = f'{fake_conf_file}\n'
title = f'[{conf_name}]\n'
type = 'type=fake\n'
json = f'file={json_file}\n'
is_hypervisor = f'is_hypervisor={is_hypervisor}\n'
owner = f'owner={owner}\n'
env = 'env = Library\n'
rhsm_hostname = f'rhsm_hostname={rhsm_hostname}\n'
rhsm_username = f'rhsm_username={rhsm_username}\n'
rhsm_encrypted_password = f'rhsm_encrypted_password={rhsm_encrypted_password}\n'
rhsm_prefix = 'rhsm_prefix=/rhsm\n'
rhsm_port = 'rhsm_port=443\n'
cmd = f'cat <<EOF > {file}{title}{type}{json}{is_hypervisor}{owner}{env}{rhsm_hostname}{rhsm_username}{rhsm_encrypted_password}{rhsm_prefix}{rhsm_port}EOF'
runcmd(cmd)


def vw_run_option(option):
"""virt who run by option
:param option: -d, --debug -o, --one-shot -i INTERVAL, --interval INTERVAL -p, --print -c CONFIGS, --config CONFIGS --version
:ruturn:
"""
runcmd('systemctl stop virt-who')
runcmd('pkill -9 virt-who')
runcmd(f'virt-who -{option}')
90 changes: 90 additions & 0 deletions tests/foreman/virtwho/cli/test_esx_sca.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"""

import json
from pathlib import Path
import re

from fauxfactory import gen_string
Expand All @@ -26,10 +28,13 @@
get_configure_command,
get_configure_file,
get_configure_option,
hypervisor_fake_json_create,
hypervisor_json_create,
restart_virtwho_service,
runcmd,
virtwho_package_locked,
vw_fake_conf_create,
vw_run_option,
)


Expand Down Expand Up @@ -593,3 +598,88 @@ def test_positive_rhsm_username_option(
elif index == 1:
restart_virtwho_service()
assert not check_message_in_rhsm_log(messages[1])

@pytest.mark.tier2
def test_positive_post_hypervisors_with_fake_different_org_simultaneous(
self, module_sca_manifest_org, form_data_cli, target_sat
):
"""create many fake conf files in two orgs with specific service account rhsm_username=virt_who_reporter_X post to satellite without task errors"
:id: ee2ec178-01e0-48b9-8c2f-5c3279eef796
:expectedresults:
hypervisor/guest json can be posted and the task is success status
:customerscenario: true
:CaseImportance: Medium
:BZ: 2173870
"""

# create json file for 3 hyperviors, each with 3 guests
json_file = Path("/tmp/fake.json")
data = hypervisor_fake_json_create(hypervisors=3, guests=3)
json_file.write_text(json.dumps(data))
# create 10 fake files in module_sca_manifest_org
virtwho_config_cli = target_sat.cli.VirtWhoConfig.create(form_data_cli)[
'general-information'
]
command = get_configure_command(virtwho_config_cli['id'], module_sca_manifest_org.name)
deploy_configure_by_command(
command, form_data_cli['hypervisor-type'], org=module_sca_manifest_org.label
)
config_file_1 = get_configure_file(virtwho_config_cli['id'])
owner_1 = get_configure_option('owner', config_file_1)
rhsm_hostname_1 = get_configure_option('rhsm_hostname', config_file_1)
rhsm_username_1 = get_configure_option('rhsm_username', config_file_1)
rhsm_encrypted_password_1 = get_configure_option('rhsm_encrypted_password', config_file_1)
# create another org and create fake conf files in this org
ORG_DATA = {'name': f'virtwho_fake_{gen_string("alpha")}'}
org = target_sat.api.Organization(name=ORG_DATA['name']).create()
target_sat.api.Location(organization=[org]).create()
form_data_cli['organization-id'] = org.id
virtwho_config_cli = target_sat.cli.VirtWhoConfig.create(form_data_cli)[
'general-information'
]
command = get_configure_command(virtwho_config_cli['id'], org.name)
deploy_configure_by_command(
command, form_data_cli['hypervisor-type'], debug=True, org=org.label
)
config_file_2 = get_configure_file(virtwho_config_cli['id'])
owner_2 = get_configure_option('owner', config_file_2)
rhsm_hostname_2 = get_configure_option('rhsm_hostname', config_file_2)
rhsm_username_2 = get_configure_option('rhsm_username', config_file_2)
rhsm_encrypted_password_2 = get_configure_option('rhsm_encrypted_password', config_file_2)
for i in range(5):
fake_conf_file = f"/etc/virt-who.d/virt-who-config-fake{i}.conf"
vw_fake_conf_create(
owner_1,
rhsm_hostname_1,
rhsm_username_1,
rhsm_encrypted_password_1,
fake_conf_file,
json_file,
)
for i in range(5, 10):
fake_conf_file = f"/etc/virt-who.d/virt-who-config-fake{i}.conf"
vw_fake_conf_create(
owner_2,
rhsm_hostname_2,
rhsm_username_2,
rhsm_encrypted_password_2,
fake_conf_file,
json_file,
)
# run virt-who with option -d, --debug and -o, --one-shot
vw_run_option("od")
for i in range(10):
fake_conf_file = f"/etc/virt-who.d/virt-who-config-fake{i}.conf"
conf_name = fake_conf_file.split("/")[-1].split(".")[0]
config_str = f'Using configuration "{conf_name}" ("fake" mode)'
check_message_in_rhsm_log(config_str)
check_message_in_rhsm_log(f"Host-to-guest mapping being sent to '{org}'")
task = target_sat.cli.Task.list_tasks({'search': 'label ~ Hyper'})
for item in task:
assert "Job blocked by the following existing jobs" not in item['task-errors']
assert "success" in item['result']

0 comments on commit 927f6eb

Please sign in to comment.