Skip to content

Commit 86951b4

Browse files
committed
fix: Fix file workflow test for container environments
* Card ID: CCT-994 The test for updating info in the archive was failing in containers, which are not in charge of their own hostnames. This commit resolves the test failures by simulating hostname updates through archive file modification.
1 parent 09e701b commit 86951b4

File tree

1 file changed

+67
-25
lines changed

1 file changed

+67
-25
lines changed

integration-tests/test_file_workflow.py

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
import string
1313
import subprocess
1414
import tarfile
15-
from time import sleep
1615
import pytest
1716
from constants import HOST_DETAILS
1817
from constants import MACHINE_ID_FILE
@@ -121,12 +120,10 @@ def test_file_workflow_with_an_archive_without_canonical_facts(
121120
assert "Successfully uploaded report" in upload_result.stdout
122121

123122

124-
@pytest.mark.skipif(
125-
"container" in os.environ.keys(),
126-
reason="Containers cannot change hostnames",
127-
)
128123
@pytest.mark.tier1
129-
def test_file_workflow_archive_update_host_info(insights_client, external_inventory):
124+
def test_file_workflow_archive_update_host_info(
125+
insights_client, external_inventory, tmp_path
126+
):
130127
"""
131128
:id: 336abff9-4263-4f1d-9448-2cd05d40a371
132129
:title: Verify Insights Archive Updates Host Information
@@ -135,35 +132,76 @@ def test_file_workflow_archive_update_host_info(insights_client, external_invent
135132
the correct host information, such as hostname, in the Inventory
136133
:tags: Tier 1
137134
:steps:
138-
1. Register the system with insights-client and confirm data upload
139-
2. Change the system hostname
140-
3. Collect and upload the new archive
141-
4. Retrieve host data from Inventory and verify fqdn
135+
1. Register the system with insights-client
136+
2. In a non-container environment, change the system hostname and collect
137+
a new archive. In a container environment create a modified archive
138+
with new hostname files
139+
3. Upload the new archive
140+
4. Retrieve host data from Inventory and verify FQDN
142141
:expectedresults:
143-
1. The system is registered and the archive is uploaded successfully
142+
1. The system is registered successfully
144143
2. The system hostname is updated successfully
145144
3. A new archive is collected and uploaded successfully
146-
4. The fqdn in the Inventory matches the updated hostname
145+
4. The FQDN in the Inventory matches the updated hostname
147146
"""
148147
insights_client.register()
149148
assert conftest.loop_until(lambda: insights_client.is_registered)
150-
current_hostname = subprocess.check_output("hostname", shell=True).decode().strip()
151149

152-
# Set a new hostname
153-
new_hostname = set_hostname()
154-
fqdn = subprocess.check_output("hostname -f", shell=True).decode().strip()
155-
logging.debug(f"Assigned hostname: {new_hostname}, FQDN: {fqdn}")
150+
if "container" not in os.environ.keys():
151+
current_hostname = (
152+
subprocess.check_output("hostname", shell=True).decode().strip()
153+
)
154+
155+
# Set a new hostname
156+
new_hostname = set_hostname()
157+
fqdn = subprocess.check_output("hostname -f", shell=True).decode().strip()
158+
logging.debug(f"Assigned hostname: {new_hostname}, FQDN: {fqdn}")
159+
cmd_options = []
160+
161+
else:
162+
# In a container environment, we cannot change the hostname
163+
# So we will just simulate the upload of an archive with a different hostname
164+
fqdn = new_hostname = generate_random_hostname()
165+
logging.debug(f"Assigned hostname: {new_hostname}")
166+
original_archive = tmp_path / "archive.tar.gz"
167+
modified_archive = tmp_path / "archive_modified.tar.gz"
168+
169+
insights_client.run(f"--output-file={original_archive}")
170+
hostname_file = tmp_path / "hostname"
171+
hostname_f_file = tmp_path / "hostname_-f"
172+
with open(hostname_file, "w") as hf:
173+
hf.write(new_hostname + "\n")
174+
with open(hostname_f_file, "w") as hff:
175+
hff.write(new_hostname + "\n")
176+
177+
# Create a modified archive with new hostname files
178+
modify_archive(
179+
original_archive,
180+
modified_archive,
181+
files_to_remove=[
182+
"/data/insights_commands/hostname",
183+
"/data/insights_commands/hostname_-f",
184+
],
185+
files_to_add=[
186+
(hostname_file, "/data/insights_commands/hostname"),
187+
(hostname_f_file, "/data/insights_commands/hostname_-f"),
188+
],
189+
)
190+
191+
os.remove(hostname_file)
192+
os.remove(hostname_f_file)
193+
cmd_options = ["--payload", modified_archive, "--content-type", "gz"]
156194

157-
insights_client.run()
158-
sleep(30) # Wait for data to get reflected in inventory
195+
insights_client.run(*cmd_options)
159196
host_data = external_inventory.this_system()
160197
logging.debug(f"Host data from inventory: {host_data}")
161198

162-
# New hostname matches the one in Inventory
199+
# The new hostname matches the one in Inventory
163200
assert host_data.get("fqdn") == fqdn
164201

165202
# Reset to the original hostname
166-
set_hostname(current_hostname)
203+
if "container" not in os.environ.keys():
204+
set_hostname(current_hostname)
167205

168206

169207
def modify_archive(
@@ -190,10 +228,14 @@ def modify_archive(
190228

191229
def set_hostname(hostname=None):
192230
if hostname is None:
193-
hostname = (
194-
"".join(random.choices(string.ascii_lowercase + string.digits, k=10))
195-
+ "-new-hostname.example.com"
196-
)
231+
hostname = generate_random_hostname()
197232

198233
subprocess.run(["hostnamectl", "set-hostname", hostname], check=True)
199234
return hostname
235+
236+
237+
def generate_random_hostname():
238+
return (
239+
"".join(random.choices(string.ascii_lowercase + string.digits, k=10))
240+
+ "-container-host.example.com"
241+
)

0 commit comments

Comments
 (0)