|
12 | 12 | import string
|
13 | 13 | import subprocess
|
14 | 14 | import tarfile
|
15 |
| -from time import sleep |
16 | 15 | import pytest
|
17 | 16 | from constants import HOST_DETAILS
|
18 | 17 | from constants import MACHINE_ID_FILE
|
@@ -121,46 +120,93 @@ def test_file_workflow_with_an_archive_without_canonical_facts(
|
121 | 120 | assert "Successfully uploaded report" in upload_result.stdout
|
122 | 121 |
|
123 | 122 |
|
124 |
| -@pytest.mark.skipif( |
125 |
| - "container" in os.environ.keys(), |
126 |
| - reason="Containers cannot change hostnames", |
127 |
| -) |
128 | 123 | @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 | +): |
130 | 127 | """
|
131 | 128 | :id: 336abff9-4263-4f1d-9448-2cd05d40a371
|
132 | 129 | :title: Verify Insights Archive Updates Host Information
|
133 | 130 | :description:
|
134 |
| - Ensure that updating files within an Insights Archive reflects |
135 |
| - the correct host information, such as hostname, in the Inventory |
| 131 | + Ensure that updating files within an Insights Archive works correctly. |
| 132 | + In regular systems, test hostname changes in Inventory. |
| 133 | + In containers, test archive modification and upload workflow. |
136 | 134 | :tags: Tier 1
|
137 | 135 | :steps:
|
138 | 136 | 1. Register the system with insights-client and confirm data upload
|
139 |
| - 2. Change the system hostname |
| 137 | + 2. Change the system hostname (non-containers) or modify archive files |
| 138 | + (containers) |
140 | 139 | 3. Collect and upload the new archive
|
141 |
| - 4. Retrieve host data from Inventory and verify fqdn |
| 140 | + 4. Retrieve host data from Inventory and verify changes (non-containers) |
| 141 | + or successful processing (containers) |
142 | 142 | :expectedresults:
|
143 | 143 | 1. The system is registered and the archive is uploaded successfully
|
144 |
| - 2. The system hostname is updated successfully |
| 144 | + 2. The system hostname is updated (non-containers) or archive files are |
| 145 | + modified (containers) |
145 | 146 | 3. A new archive is collected and uploaded successfully
|
146 |
| - 4. The fqdn in the Inventory matches the updated hostname |
| 147 | + 4. In non-containers, the FQDN in Inventory matches the updated hostname. |
| 148 | + In containers, the archive upload and processing succeeds. |
147 | 149 | """
|
148 | 150 | insights_client.register()
|
149 | 151 | assert conftest.loop_until(lambda: insights_client.is_registered)
|
150 | 152 | current_hostname = subprocess.check_output("hostname", shell=True).decode().strip()
|
151 | 153 |
|
152 | 154 | # Set a new hostname
|
153 | 155 | 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}") |
156 | 156 |
|
157 |
| - insights_client.run() |
158 |
| - sleep(30) # Wait for data to get reflected in inventory |
159 |
| - host_data = external_inventory.this_system() |
160 |
| - logging.debug(f"Host data from inventory: {host_data}") |
| 157 | + if "container" not in os.environ.keys(): |
| 158 | + fqdn = subprocess.check_output("hostname -f", shell=True).decode().strip() |
| 159 | + logging.debug(f"Assigned hostname: {new_hostname}, FQDN: {fqdn}") |
| 160 | + |
| 161 | + insights_client.run() |
| 162 | + host_data = external_inventory.this_system() |
| 163 | + logging.debug(f"Host data from inventory: {host_data}") |
| 164 | + |
| 165 | + # New hostname matches the one in Inventory |
| 166 | + assert host_data.get("fqdn") == fqdn |
| 167 | + else: |
| 168 | + # Container-specific approach: collect data, modify archive, and upload manually |
| 169 | + # Let insights-client collect data into a directory |
| 170 | + archive_name = tmp_path / "archive.tar.gz" |
| 171 | + insights_client.run(f"--output-file={archive_name}") |
| 172 | + |
| 173 | + # Extract archive to modify hostname files |
| 174 | + extract_dir = tmp_path / "extracted" |
| 175 | + extract_dir.mkdir() |
| 176 | + with tarfile.open(archive_name, "r:gz") as tar: |
| 177 | + tar.extractall(extract_dir, filter="data") |
| 178 | + dir_name = tar.getnames()[0] |
| 179 | + |
| 180 | + # Overwrite hostname file content in the archive |
| 181 | + hostname_file = ( |
| 182 | + extract_dir / dir_name / "data" / "insights_commands" / "hostname" |
| 183 | + ) |
| 184 | + hostname_f_file = ( |
| 185 | + extract_dir / dir_name / "data" / "insights_commands" / "hostname_-f" |
| 186 | + ) |
| 187 | + |
| 188 | + # Update hostname files with new hostname |
| 189 | + hostname_file.parent.mkdir(parents=True, exist_ok=True) |
| 190 | + hostname_file.write_text(new_hostname + "\n") |
| 191 | + hostname_f_file.parent.mkdir(parents=True, exist_ok=True) |
| 192 | + hostname_f_file.write_text(new_hostname + "\n") |
| 193 | + |
| 194 | + # Recreate the modified archive |
| 195 | + modified_archive = tmp_path / "modified_archive.tar.gz" |
| 196 | + with tarfile.open(modified_archive, "w:gz") as tar: |
| 197 | + tar.add(extract_dir / dir_name, arcname=dir_name) |
| 198 | + |
| 199 | + # Upload using --payload and --content-type |
| 200 | + upload_result = insights_client.run( |
| 201 | + f"--payload={modified_archive}", "--content-type=gz", check=False |
| 202 | + ) |
161 | 203 |
|
162 |
| - # New hostname matches the one in Inventory |
163 |
| - assert host_data.get("fqdn") == fqdn |
| 204 | + # Verify the files were modified and upload was successful |
| 205 | + assert hostname_file.exists() |
| 206 | + assert hostname_f_file.exists() |
| 207 | + assert hostname_file.read_text().strip() == new_hostname |
| 208 | + assert hostname_f_file.read_text().strip() == new_hostname |
| 209 | + assert "Successfully uploaded report" in upload_result.stdout |
164 | 210 |
|
165 | 211 | # Reset to the original hostname
|
166 | 212 | set_hostname(current_hostname)
|
@@ -188,5 +234,6 @@ def set_hostname(hostname=None):
|
188 | 234 | + "-new-hostname.example.com"
|
189 | 235 | )
|
190 | 236 |
|
191 |
| - subprocess.run(["hostnamectl", "set-hostname", hostname], check=True) |
| 237 | + if "container" not in os.environ.keys(): |
| 238 | + subprocess.run(["hostnamectl", "set-hostname", hostname], check=True) |
192 | 239 | return hostname
|
0 commit comments