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,12 +120,10 @@ 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
@@ -135,35 +132,76 @@ def test_file_workflow_archive_update_host_info(insights_client, external_invent
135
132
the correct host information, such as hostname, in the Inventory
136
133
:tags: Tier 1
137
134
: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
142
141
:expectedresults:
143
- 1. The system is registered and the archive is uploaded successfully
142
+ 1. The system is registered successfully
144
143
2. The system hostname is updated successfully
145
144
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
147
146
"""
148
147
insights_client .register ()
149
148
assert conftest .loop_until (lambda : insights_client .is_registered )
150
- current_hostname = subprocess .check_output ("hostname" , shell = True ).decode ().strip ()
151
149
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" ]
156
194
157
- insights_client .run ()
158
- sleep (30 ) # Wait for data to get reflected in inventory
195
+ insights_client .run (* cmd_options )
159
196
host_data = external_inventory .this_system ()
160
197
logging .debug (f"Host data from inventory: { host_data } " )
161
198
162
- # New hostname matches the one in Inventory
199
+ # The new hostname matches the one in Inventory
163
200
assert host_data .get ("fqdn" ) == fqdn
164
201
165
202
# Reset to the original hostname
166
- set_hostname (current_hostname )
203
+ if "container" not in os .environ .keys ():
204
+ set_hostname (current_hostname )
167
205
168
206
169
207
def modify_archive (
@@ -190,10 +228,14 @@ def modify_archive(
190
228
191
229
def set_hostname (hostname = None ):
192
230
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 ()
197
232
198
233
subprocess .run (["hostnamectl" , "set-hostname" , hostname ], check = True )
199
234
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