Skip to content

Commit

Permalink
codecoverage bug2173870
Browse files Browse the repository at this point in the history
  • Loading branch information
yanpliu committed Mar 7, 2024
1 parent cc8227d commit e0c4606
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 0 deletions.
72 changes: 72 additions & 0 deletions robottelo/utils/virtwho.py
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,33 @@ def hypervisor_json_create(hypervisors, guests):
return mapping


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 = []
for _ in range(hypervisors):
guest_list = []
for _ in range(guests):
guest_list.append(
{
"guestId": str(uuid.uuid4()),
"state": 1,
"attributes": {"active": 1, "virtWhoType": "esx"},
}
)
name = str(uuid.uuid4())
hypervisor = {"guests": guest_list, "name": name, "uuid": name}
hypervisors_list.append(hypervisor)
mapping = {"hypervisors": hypervisors_list}
return mapping


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


def vw_fake_conf_create(
owner,
rhsm_hostname,
rhsm_username,
rhsm_encrypted_password,
fake_conf_file,
json_file,
is_hypervisor=True,
):
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 = ('cat <<EOF > {}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' '{}' 'EOF').format(
file,
title,
type,
json,
is_hypervisor,
owner,
env,
rhsm_hostname,
rhsm_username,
rhsm_encrypted_password,
rhsm_prefix,
rhsm_port,
)
runcmd(cmd)


def vw_run_option(option):
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 @@ -9,6 +9,8 @@
:Team: Phoenix
"""
import json
from pathlib import Path
import re

from fauxfactory import gen_string
Expand All @@ -18,14 +20,18 @@
from robottelo.config import settings
from robottelo.utils.virtwho import (
ETC_VIRTWHO_CONFIG,
check_message_in_rhsm_log,
create_http_proxy,
deploy_configure_by_command,
deploy_configure_by_command_check,
get_configure_command,
get_configure_file,
get_configure_option,
hypervisor_fake_json_create,
hypervisor_json_create,
virtwho_package_locked,
vw_fake_conf_create,
vw_run_option,
)


Expand Down Expand Up @@ -502,3 +508,87 @@ def test_positive_remove_env_option(
env_warning = f"Ignoring unknown configuration option \"{option}\""
result = target_sat.execute(f'grep "{env_warning}" /var/log/messages')
assert result.status == 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 3 hypersiors and each have 3 guests json file
json_file = Path("/tmp/fake.json")
data = hypervisor_fake_json_create(hypervisors=1, guests=1)
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,
)
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 e0c4606

Please sign in to comment.