-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathtest_convert2rhel.py
More file actions
471 lines (404 loc) · 17.1 KB
/
Copy pathtest_convert2rhel.py
File metadata and controls
471 lines (404 loc) · 17.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
"""Test class for converting to RHEL from API
:Requirement: Convert2rhel
:CaseAutomation: Automated
:CaseComponent: Conversionsappliance
:CaseImportance: Critical
:Team: Rocket
"""
import json
import pytest
import requests
from robottelo.config import settings
from robottelo.constants import DEFAULT_ARCHITECTURE, REPOS
from robottelo.content_info import get_baseurl_by_repofile
from robottelo.utils.issue_handlers import is_open
def create_repo(sat, org, repo_url, ssl_cert=None):
"""Create and sync repository"""
product = sat.api.Product(organization=org).create()
options = {
'organization': org,
'product': product,
'content_type': 'yum',
'url': repo_url,
'ssl_ca_cert': ssl_cert,
'unprotected': True,
'verify_ssl_on_sync': False,
}
repo = sat.api.Repository(**options).create()
repo.product = product
repo.sync()
return repo
def update_cv(sat, cv, lce, repos):
"""Update and publish Content view with repos"""
cv = sat.api.ContentView(id=cv.id, repository=repos).update(['repository'])
cv.publish()
cv = cv.read()
cv.version.sort(key=lambda version: version.id)
cv.version[-1].promote(data={'environment_ids': lce.id, 'force': False})
return cv
@pytest.fixture(scope='module')
def ssl_cert(module_target_sat, module_els_sca_manifest_org):
"""Create credential with SSL cert for Oracle Linux"""
res = requests.get(settings.repos.convert2rhel.ssl_cert_oracle)
res.raise_for_status()
return module_target_sat.api.ContentCredential(
content=res.text, organization=module_els_sca_manifest_org, content_type='cert'
).create()
@pytest.fixture
def activation_key_rhel(
module_target_sat, module_els_sca_manifest_org, module_lce, module_promoted_cv
):
"""Create activation key that will be used after conversion for registration"""
return module_target_sat.api.ActivationKey(
organization=module_els_sca_manifest_org,
content_view=module_promoted_cv,
environment=module_lce,
).create()
@pytest.fixture(scope='module')
def enable_rhel_subscriptions(module_target_sat, module_els_sca_manifest_org, version):
"""Enable and sync RHEL rpms repos"""
major = int(version.split('.')[0])
minor = ''
if major <= 7 and module_target_sat.is_fips_enabled():
pytest.skip('CentOS7/OEL7 conversion is not supported with FIPS-enabled Sat')
if major == 8:
repo_names = ['rhel8_bos', 'rhel8_aps']
minor = version[1:]
else:
repo_names = ['rhel7_els']
rh_repos = []
tasks = []
for name in repo_names:
rh_repo_id = module_target_sat.api_factory.enable_rhrepo_and_fetchid(
basearch=DEFAULT_ARCHITECTURE,
org_id=module_els_sca_manifest_org.id,
product=REPOS[name]['product'],
repo=REPOS[name]['name'] + minor,
reposet=REPOS[name]['reposet'],
releasever=REPOS[name]['releasever'] + minor,
)
# Sync step because repo is not synced by default
rh_repo = module_target_sat.api.Repository(id=rh_repo_id).read()
task = rh_repo.sync(synchronous=False)
tasks.append(task)
rh_repos.append(rh_repo)
for task in tasks:
module_target_sat.wait_for_tasks(
search_query=(f'id = {task["id"]}'),
poll_timeout=2500,
search_rate=20,
max_tries=10,
)
task_status = module_target_sat.api.ForemanTask(id=task['id']).poll()
assert task_status['result'] == 'success'
return rh_repos
@pytest.fixture
def centos(
module_target_sat,
centos_host,
module_els_sca_manifest_org,
smart_proxy_location,
module_promoted_cv,
module_lce,
version,
enable_rhel_subscriptions,
):
"""Deploy and register Centos host"""
major = int(version.split('.')[0])
if major <= 7 and module_target_sat.is_fips_enabled():
pytest.skip('CentOS7 conversion is not supported with FIPS-enabled Sat')
centos_host.enable_ipv6_dnf_proxy()
assert centos_host.execute('yum -y update').status == 0
repofile_url = settings.repos.convert2rhel.convert_to_rhel_repofile.format(major)
repo_url = get_baseurl_by_repofile(repofile_url)
repo = create_repo(module_target_sat, module_els_sca_manifest_org, repo_url)
cv = update_cv(
module_target_sat, module_promoted_cv, module_lce, enable_rhel_subscriptions + [repo]
)
ak = module_target_sat.api.ActivationKey(
organization=module_els_sca_manifest_org,
content_view=cv,
environment=module_lce,
).create()
# Ensure C2R repo is enabled in the activation key
all_content = ak.product_content(data={'content_access_mode_all': '1'})['results']
repo_label = [content['label'] for content in all_content if content['name'] == repo.name][0]
ak.content_override(data={'content_overrides': [{'content_label': repo_label, 'value': '1'}]})
# Register CentOS host with Satellite
result = centos_host.api_register(
module_target_sat,
organization=module_els_sca_manifest_org,
activation_keys=[ak.name],
location=smart_proxy_location,
)
assert result.status == 0, f'Failed to register host: {result.stderr}'
if centos_host.execute('needs-restarting -r').status == 1:
centos_host.power_control(state='reboot')
yield centos_host
# close ssh session before teardown, because of reboot in conversion it may cause problems
centos_host.close()
@pytest.fixture
def oracle(
module_target_sat,
oracle_host,
module_els_sca_manifest_org,
smart_proxy_location,
module_promoted_cv,
module_lce,
ssl_cert,
version,
enable_rhel_subscriptions,
):
"""Deploy and register Oracle host"""
major = int(version.split('.')[0])
if major <= 7 and module_target_sat.is_fips_enabled():
pytest.skip('OEL7 conversion is not supported with FIPS-enabled Sat')
oracle_host.enable_ipv6_dnf_proxy()
# disable rhn-client-tools because it obsoletes the subscription manager package
oracle_host.execute('echo "exclude=rhn-client-tools" >> /etc/yum.conf')
# Install and set correct RHEL compatible kernel and using non-UEK kernel, based on C2R docs
assert (
oracle_host.execute(
'yum install -y kernel && '
'grubby --set-default /boot/vmlinuz-'
'`rpm -q --qf "%{BUILDTIME}\t%{EVR}.%{ARCH}\n" kernel | sort -nr | head -1 | cut -f2`'
).status
== 0
)
assert oracle_host.execute('yum -y update').status == 0
if major == 8:
# needs-restarting missing in OEL8
assert oracle_host.execute('dnf install -y yum-utils').status == 0
# Fix inhibitor CHECK_FIREWALLD_AVAILABILITY::FIREWALLD_MODULES_CLEANUP_ON_EXIT_CONFIG -
# Firewalld is set to cleanup modules after exit
result = oracle_host.execute(
'sed -i -- "s/CleanupModulesOnExit=yes/CleanupModulesOnExit=no/g" '
'/etc/firewalld/firewalld.conf && firewall-cmd --reload'
)
assert result.status == 0
# Set RHEL kernel to be used during boot
oracle_host.execute("mkdir -p /boot/loader/entries/backup")
oracle_host.execute("mv /boot/loader/entries/*uek*.conf /boot/loader/entries/backup/")
# Needs reboot to reflect the changes
oracle_host.power_control(state='reboot')
assert oracle_host.execute("grubby --default-kernel | grep uek").status != 0
assert oracle_host.execute("uname -r | grep uek").status != 0
# Fix inhibitor TAINTED_KMODS::TAINTED_KMODS_DETECTED - Tainted kernel modules detected
blacklist_cfg = '/etc/modprobe.d/blacklist.conf'
assert oracle_host.execute('modprobe -r nvme_tcp').status == 0
assert oracle_host.execute(f'echo "blacklist nvme_tcp" >> {blacklist_cfg}').status == 0
assert (
oracle_host.execute(f'echo "install nvme_tcp /bin/false" >> {blacklist_cfg}').status
== 0
)
if oracle_host.execute('needs-restarting -r').status == 1:
oracle_host.power_control(state='reboot')
repofile_url = settings.repos.convert2rhel.convert_to_rhel_repofile.format(major)
repo_url = get_baseurl_by_repofile(repofile_url)
repo = create_repo(module_target_sat, module_els_sca_manifest_org, repo_url, ssl_cert)
cv = update_cv(
module_target_sat, module_promoted_cv, module_lce, enable_rhel_subscriptions + [repo]
)
ak = module_target_sat.api.ActivationKey(
organization=module_els_sca_manifest_org,
content_view=cv,
environment=module_lce,
).create()
# Ensure C2R repo is enabled in the activation key
all_content = ak.product_content(data={'content_access_mode_all': '1'})['results']
repo_label = [content['label'] for content in all_content if content['name'] == repo.name][0]
ak.content_override(data={'content_overrides': [{'content_label': repo_label, 'value': '1'}]})
# UBI repo required for subscription-manager packages on Oracle
ubi_url = settings.repos.convert2rhel.ubi7 if major == 7 else settings.repos.convert2rhel.ubi8
# Register Oracle host with Satellite
result = oracle_host.api_register(
module_target_sat,
organization=module_els_sca_manifest_org,
activation_keys=[ak.name],
location=smart_proxy_location,
repo=ubi_url,
)
assert result.status == 0, f'Failed to register host: {result.stderr}'
yield oracle_host
# close ssh session before teardown, because of reboot in conversion it may cause problems
oracle_host.close()
@pytest.fixture(scope='module')
def version(request):
"""Version of converted OS"""
return settings.content_host.get(request.param).vm.deploy_rhel_version
@pytest.mark.e2e
@pytest.mark.parametrize('version', ['oracle7', 'oracle8'], indirect=True)
def test_convert2rhel_oracle_with_pre_conversion_template_check(
module_target_sat, oracle, activation_key_rhel, version
):
"""Convert Oracle linux to RHEL
:id: 7fd393f0-551a-4de0-acdd-7f026b485f79
:steps:
0. Have host registered to Satellite
1. Check for operating system
2. Convert host to RHEL
:expectedresults: Host is converted to RHEL with correct os facts
and subscription status
:parametrized: yes
:Verifies: SAT-24654, SAT-24655, SAT-26076
"""
major = int(version.split('.')[0])
if major <= 7 and module_target_sat.is_fips_enabled():
pytest.skip('OEL7 conversion is not supported with FIPS-enabled Sat')
host_content = module_target_sat.api.Host(id=oracle.hostname).read_json()
assert host_content['operatingsystem_name'] == f'OracleLinux {version}'
# Pre-conversion template job
template_id = (
module_target_sat.api.JobTemplate()
.search(query={'search': 'name="Convert2RHEL analyze"'})[0]
.id
)
job = module_target_sat.api.JobInvocation().run(
synchronous=False,
data={
'job_template_id': template_id,
'targeting_type': 'static_query',
'search_query': f'name = {oracle.hostname}',
'inputs': {
'ELS': 'yes' if major <= 7 else 'no',
},
},
)
# wait for job to complete
module_target_sat.wait_for_tasks(
f'resource_type = JobInvocation and resource_id = {job["id"]}',
poll_timeout=5500,
search_rate=20,
)
result = module_target_sat.api.JobInvocation(id=job['id']).read()
assert result.succeeded == 1
# execute job 'Convert 2 RHEL' on host
template_id = (
module_target_sat.api.JobTemplate().search(query={'search': 'name="Convert to RHEL"'})[0].id
)
job = module_target_sat.api.JobInvocation().run(
synchronous=False,
data={
'job_template_id': template_id,
'inputs': {
'Activation Key': activation_key_rhel.id,
'Restart': 'yes',
'ELS': 'yes' if major <= 7 else 'no',
},
'targeting_type': 'static_query',
'search_query': f'name = {oracle.hostname}',
},
)
# wait for job to complete
module_target_sat.wait_for_tasks(
f'resource_type = JobInvocation and resource_id = {job["id"]}', poll_timeout=2500
)
result = module_target_sat.api.JobInvocation(id=job['id']).read()
assert result.succeeded == 1
# check facts: correct os and valid subscription status
host_content = module_target_sat.api.Host(id=oracle.hostname).read_json()
# workaround for BZ 2080347
assert (
host_content['operatingsystem_name'].startswith(f'RHEL Server {version}')
or host_content['operatingsystem_name'].startswith(f'RedHat {version}')
or host_content['operatingsystem_name'].startswith(f'RHEL {version}')
)
# Wait for the host to be rebooted and SSH daemon to be started.
oracle.wait_for_connection()
# Verify convert2rhel facts are generated, and verify fact conversions.success is true
assert host_content['facts']['conversions::success'] == 'true'
convert2rhel_facts = json.loads(oracle.execute('cat /etc/rhsm/facts/convert2rhel.facts').stdout)
assert convert2rhel_facts['conversions.env.CONVERT2RHEL_THROUGH_FOREMAN'] == '1'
# https://issues.redhat.com/browse/RHELC-1737
target_os_name = 'Oracle Linux Server' if is_open('RHELC-1737') else 'Red Hat'
assert target_os_name in convert2rhel_facts['conversions.target_os.name']
assert convert2rhel_facts['conversions.success'] is True
@pytest.mark.e2e
@pytest.mark.parametrize('version', ['centos7', 'centos8'], indirect=True)
def test_convert2rhel_centos_with_pre_conversion_template_check(
module_target_sat, centos, activation_key_rhel, version
):
"""Convert CentOS linux to RHEL
:id: 6f698440-7d85-4deb-8dd9-363ea9003b92
:steps:
0. Have host registered to Satellite
1. Check for operating system
2. Convert host to RHEL
:expectedresults: Host is converted to RHEL with correct os facts
and subscription status
:parametrized: yes
:Verifies: SAT-24654, SAT-24655, SAT-26076
"""
major = int(version.split('.')[0])
if major <= 7 and module_target_sat.is_fips_enabled():
pytest.skip('CentOS7 conversion is not supported with FIPS-enabled Sat')
host_content = module_target_sat.api.Host(id=centos.hostname).read_json()
assert host_content['operatingsystem_name'] == f'CentOS {major}'
# Pre-conversion template job
template_id = (
module_target_sat.api.JobTemplate()
.search(query={'search': 'name="Convert2RHEL analyze"'})[0]
.id
)
job = module_target_sat.api.JobInvocation().run(
synchronous=False,
data={
'job_template_id': template_id,
'targeting_type': 'static_query',
'search_query': f'name = {centos.hostname}',
'inputs': {
'ELS': 'yes' if major <= 7 else 'no',
},
},
)
# wait for job to complete
module_target_sat.wait_for_tasks(
f'resource_type = JobInvocation and resource_id = {job["id"]}',
poll_timeout=5500,
search_rate=20,
)
result = module_target_sat.api.JobInvocation(id=job['id']).read()
assert result.succeeded == 1
# execute job 'Convert 2 RHEL' on host
template_id = (
module_target_sat.api.JobTemplate().search(query={'search': 'name="Convert to RHEL"'})[0].id
)
job = module_target_sat.api.JobInvocation().run(
synchronous=False,
data={
'job_template_id': template_id,
'inputs': {
'Activation Key': activation_key_rhel.id,
'Restart': 'yes',
'ELS': 'yes' if major <= 7 else 'no',
},
'targeting_type': 'static_query',
'search_query': f'name = {centos.hostname}',
},
)
# wait for job to complete
module_target_sat.wait_for_tasks(
f'resource_type = JobInvocation and resource_id = {job["id"]}',
poll_timeout=2500,
search_rate=20,
)
result = module_target_sat.api.JobInvocation(id=job['id']).read()
assert result.succeeded == 1
# check facts: correct os and valid subscription status
host_content = module_target_sat.api.Host(id=centos.hostname).read_json()
# workaround for BZ 2080347
assert (
host_content['operatingsystem_name'].startswith(f'RHEL Server {version}')
or host_content['operatingsystem_name'].startswith(f'RedHat {version}')
or host_content['operatingsystem_name'].startswith(f'RHEL {version}')
)
# Wait for the host to be rebooted and SSH daemon to be started.
centos.wait_for_connection()
# Verify convert2rhel facts are generated, and verify fact conversions.success is true
assert host_content['facts']['conversions::success'] == 'true'
convert2rhel_facts = json.loads(centos.execute('cat /etc/rhsm/facts/convert2rhel.facts').stdout)
assert convert2rhel_facts['conversions.env.CONVERT2RHEL_THROUGH_FOREMAN'] == '1'
# https://issues.redhat.com/browse/RHELC-1737
target_os_name = 'CentOS Linux' if is_open('RHELC-1737') else 'Red Hat'
assert target_os_name in convert2rhel_facts['conversions.target_os.name']
assert convert2rhel_facts['conversions.success'] is True