Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 78 additions & 29 deletions integration-tests/test_file_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import string
import subprocess
import tarfile
from time import sleep
import pytest
from constants import HOST_DETAILS
from constants import MACHINE_ID_FILE
Expand Down Expand Up @@ -57,7 +56,7 @@ def test_file_workflow_with_an_archive_with_only_one_canonical_fact(
"/data/insights_commands/subscription-manager_identity",
]
machine_id = open(MACHINE_ID_FILE, "r").read()
remove_files_from_archive(archive_name, files_to_remove, modified_archive)
modify_archive(archive_name, modified_archive, files_to_remove)
upload_result = insights_client.run(
f"--payload={modified_archive}", "--content-type=gz", check=False
)
Expand Down Expand Up @@ -114,19 +113,17 @@ def test_file_workflow_with_an_archive_without_canonical_facts(
"/data/sys/class/net/lo/address",
]

remove_files_from_archive(archive_name, files_to_remove, modified_archive)
modify_archive(archive_name, modified_archive, files_to_remove)
upload_result = insights_client.run(
f"--payload={modified_archive}", "--content-type=gz", check=False
)
assert "Successfully uploaded report" in upload_result.stdout


@pytest.mark.skipif(
"container" in os.environ.keys(),
reason="Containers cannot change hostnames",
)
@pytest.mark.tier1
def test_file_workflow_archive_update_host_info(insights_client, external_inventory):
def test_file_workflow_archive_update_host_info(
insights_client, external_inventory, tmp_path
):
"""
:id: 336abff9-4263-4f1d-9448-2cd05d40a371
:title: Verify Insights Archive Updates Host Information
Expand All @@ -135,42 +132,87 @@ def test_file_workflow_archive_update_host_info(insights_client, external_invent
the correct host information, such as hostname, in the Inventory
:tags: Tier 1
:steps:
1. Register the system with insights-client and confirm data upload
2. Change the system hostname
3. Collect and upload the new archive
4. Retrieve host data from Inventory and verify fqdn
1. Register the system with insights-client
2. In a non-container environment, change the system hostname and collect
a new archive. In a container environment create a modified archive
with new hostname files
3. Upload the new archive
4. Retrieve host data from Inventory and verify FQDN
:expectedresults:
1. The system is registered and the archive is uploaded successfully
1. The system is registered successfully
2. The system hostname is updated successfully
3. A new archive is collected and uploaded successfully
4. The fqdn in the Inventory matches the updated hostname
4. The FQDN in the Inventory matches the updated hostname
"""
insights_client.register()
assert conftest.loop_until(lambda: insights_client.is_registered)
current_hostname = subprocess.check_output("hostname", shell=True).decode().strip()

# Set a new hostname
new_hostname = set_hostname()
fqdn = subprocess.check_output("hostname -f", shell=True).decode().strip()
logging.debug(f"Assigned hostname: {new_hostname}, FQDN: {fqdn}")
if "container" not in os.environ.keys():
current_hostname = (
subprocess.check_output("hostname", shell=True).decode().strip()
)

# Set a new hostname
new_hostname = set_hostname()
fqdn = subprocess.check_output("hostname -f", shell=True).decode().strip()
logging.debug(f"Assigned hostname: {new_hostname}, FQDN: {fqdn}")
cmd_options = []

else:
# In a container environment, we cannot change the hostname
# So we will just simulate the upload of an archive with a different hostname
fqdn = new_hostname = generate_random_hostname()
logging.debug(f"Assigned hostname: {new_hostname}")
original_archive = tmp_path / "archive.tar.gz"
modified_archive = tmp_path / "archive_modified.tar.gz"

insights_client.run(f"--output-file={original_archive}")
hostname_file = tmp_path / "hostname"
hostname_f_file = tmp_path / "hostname_-f"
with open(hostname_file, "w") as hf:
hf.write(new_hostname + "\n")
with open(hostname_f_file, "w") as hff:
hff.write(new_hostname + "\n")

# Create a modified archive with new hostname files
modify_archive(
original_archive,
modified_archive,
files_to_remove=[
"/data/insights_commands/hostname",
"/data/insights_commands/hostname_-f",
],
files_to_add=[
(hostname_file, "/data/insights_commands/hostname"),
(hostname_f_file, "/data/insights_commands/hostname_-f"),
],
)

os.remove(hostname_file)
os.remove(hostname_f_file)
cmd_options = ["--payload", modified_archive, "--content-type", "gz"]

insights_client.run()
sleep(30) # Wait for data to get reflected in inventory
insights_client.run(*cmd_options)
host_data = external_inventory.this_system()
logging.debug(f"Host data from inventory: {host_data}")

# New hostname matches the one in Inventory
# The new hostname matches the one in Inventory
assert host_data.get("fqdn") == fqdn

# Reset to the original hostname
set_hostname(current_hostname)
if "container" not in os.environ.keys():
set_hostname(current_hostname)


def remove_files_from_archive(original_archive, files_to_remove, modified_archive):
def modify_archive(
original_archive, modified_archive, files_to_remove=None, files_to_add=None
):
files_to_add = files_to_add or []
files_to_remove = files_to_remove or []
with tarfile.open(original_archive, "r:gz") as tar:
file_list = tar.getnames()
dir_name = tar.getnames()[0]
# append dirname to create absolute path names
# Append dirname to create absolute path names
files_to_remove = [dir_name + item for item in files_to_remove]
# Remove the specified files from the list
files_to_keep = [f for f in file_list if f not in files_to_remove]
Expand All @@ -179,14 +221,21 @@ def remove_files_from_archive(original_archive, files_to_remove, modified_archiv
for member in tar.getmembers():
if member.name in files_to_keep:
new_tar.addfile(member, tar.extractfile(member))
# Add new files
for file_path, arcname in files_to_add:
new_tar.add(file_path, arcname=f"{dir_name}/{arcname}")


def set_hostname(hostname=None):
if hostname is None:
hostname = (
"".join(random.choices(string.ascii_lowercase + string.digits, k=10))
+ "-new-hostname.example.com"
)
hostname = generate_random_hostname()

subprocess.run(["hostnamectl", "set-hostname", hostname], check=True)
return hostname


def generate_random_hostname():
return (
"".join(random.choices(string.ascii_lowercase + string.digits, k=10))
+ "-container-host.example.com"
)