Skip to content

Commit fafbb63

Browse files
mk180007mk180007
authored andcommitted
Update after review
1 parent 15f1ae9 commit fafbb63

File tree

1 file changed

+73
-61
lines changed

1 file changed

+73
-61
lines changed

common/src/stack/report-system/command/report/system/tests/test_sles_patch.py

Lines changed: 73 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,53 @@
55
import json
66

77

8-
# Returns a hash of filename
9-
def Get_A_Hash(filename):
8+
# Returns hash of filename
9+
def get_hash(filename):
1010
hasher = hashlib.md5()
1111
with open(filename, 'rb') as f:
12-
buf = f.read()
13-
hasher.update(buf)
12+
hasher.update(f.read())
1413

1514
return hasher.hexdigest()
1615

17-
# Compare 2 hashes and return boolean
18-
def Hashes_Match(hash1, hash2, filename):
19-
if hash1 == hash2:
20-
return True
21-
else:
22-
print('%s hash did NOT match' % filename)
23-
return False
2416

25-
# Grab all found SLES versions and returns a list
26-
def Get_SLES_Flavors():
17+
# Verify SLES patches are installed
18+
def test_sles_pallet_patched(host, report_output):
19+
total_matched = []
2720
base_dir = '/export/stack/pallets/SLES/'
2821
sles_flavors = os.listdir(base_dir)
29-
return sles_flavors
30-
22+
if not sles_flavors:
23+
pytest.skip('No SLES pallet found - skipping patch check')
3124

32-
# Verify SLES patches are installed
33-
def test_sles_pallet_patched(host):
34-
# Only run on SLES
35-
if not os.path.exists('/etc/SuSE-release'):
36-
print('OS is not SLES, skipping test')
37-
return 0
25+
# Find out where is stack-sles*images*.rpm file(s)
26+
result = host.run('find /export/stack/pallets/stacki/ -name "*stack-sles-*.rpm"')
27+
RPM = result.stdout.splitlines()
28+
assert RPM
3829

39-
error_occurred = False
40-
base_dir = '/export/stack/pallets/SLES/'
41-
sles_flavors = Get_SLES_Flavors()
42-
assert sles_flavors
43-
# Just need a new line for readability
44-
print('')
30+
# If the stack-sles*images*.rpm installed?
31+
result = host.run('rpm -qa | grep stack | grep images')
32+
assert result
4533

4634
# Test every sles flavor found
4735
for sles_flavor in sles_flavors:
4836
# Make sure installed patches match what's under /opt/stack/pallet-patches
4937
patch_dir = '/opt/stack/pallet-patches'
5038
patch_dir_files = []
39+
found_source = False
5140
for (dir_path, dir_names, filenames) in os.walk(patch_dir):
5241
# Only want particular SLES version
5342
if sles_flavor in dir_path:
54-
patch_dir_files += [os.path.join(dir_path, file) for file in filenames if file == 'content']
43+
file_to_check = None
44+
if '15sp' in sles_flavor:
45+
file_to_check = 'CHECKSUMS'
46+
else:
47+
file_to_check = 'content'
48+
patch_dir_files += [os.path.join(dir_path, file) for file in filenames if file == file_to_check]
5549

5650
# We should have non-empty list here
5751
assert patch_dir_files
5852

5953
# Find img file from patch directory in SLES pallet
60-
result = host.run(f'grep .img {patch_dir_files[0]}\n')
54+
result = host.run(f'grep .img {patch_dir_files[0]}')
6155

6256
# Last item is the partial image path
6357
part_img_file = result.stdout.split()[-1]
@@ -73,42 +67,60 @@ def test_sles_pallet_patched(host):
7367
sles_pallet_root = None
7468
for pallet in palinfo[base_dir]:
7569
# Grab the right sles sp level
76-
if pallet['name'] == 'SLES' and pallet['version'] == sles_flavor:
70+
if pallet['version'] == sles_flavor:
7771
assert host.file(f"{pallet['pallet_root']}").is_directory
7872
sles_pallet_root = pallet['pallet_root']
7973
break
8074

8175
# .img file should exist in sles pallet directory in relative path
8276
assert os.path.exists(sles_pallet_root + '/' + part_img_file)
8377

84-
# Find out where is stack-sles*images*.rpm file
85-
result = host.run('find / -name "*stack-sles-*.rpm" -print\n')
86-
RPM = result.stdout.splitlines()[0]
87-
88-
# Make sure we found pm file
89-
assert '.rpm' in RPM
90-
91-
result = host.run(f'rpm -qp --dump {RPM}\n')
92-
# We don't want file under /opt/stack/images to be included in this list
93-
patch_files = [line.split() for line in result.stdout.splitlines() if sles_flavor in line and '/images/' not in line]
94-
95-
# Verify file(s) from images RPM actually exist
96-
for this_list in patch_files:
97-
file_to_check = this_list[0]
98-
assert os.path.exists(file_to_check)
99-
hash_value = this_list[3]
100-
# Grab hash for this file in patch directory and compare against hash from img file. They should match
101-
hash_value1 = Get_A_Hash(file_to_check)
102-
if Hashes_Match(hash_value, hash_value1, file_to_check) == False:
103-
error_occurred = True
104-
105-
# Grab hash for this file in SLES pallet directory and compare against hash from img file. They should match
106-
# Need to build the equivalent path first
107-
temp_path_list = file_to_check.split('add-stacki-squashfs')
108-
temp_path = sles_pallet_root + temp_path_list[1]
109-
hash_value1 = Get_A_Hash(temp_path)
110-
if Hashes_Match(hash_value, hash_value1, temp_path) == False:
111-
error_occurred = True
112-
113-
# Overall result for hash check
114-
assert error_occurred == False
78+
# Verify all the stack-sles-* rpm packages
79+
for rpm in RPM:
80+
# Make sure we found pm file
81+
assert '.rpm' in rpm
82+
83+
result = host.run(f'rpm -qp --dump {rpm}')
84+
# We don't want file under /opt/stack/images to be included in this list
85+
patch_files = [line.split() for line in result.stdout.splitlines() if sles_flavor in line and '/images/' not in line]
86+
87+
matched_list = []
88+
not_matched_list = []
89+
# Verify file(s) from images RPM actually exist
90+
for this_list in patch_files:
91+
file_to_check = this_list[0]
92+
assert os.path.exists(file_to_check)
93+
hash_value = this_list[3]
94+
# Grab hash for this file in patch directory and compare against hash from img file. They should match
95+
if get_hash(file_to_check) == hash_value:
96+
matched_list.append(file_to_check)
97+
else:
98+
not_matched_list.append(file_to_check)
99+
100+
# Grab hash for this file in SLES pallet directory and compare against hash from img file. They should match
101+
# Need to build the equivalent path first
102+
temp_path_list = file_to_check.split('add-stacki-squashfs')
103+
temp_path = sles_pallet_root + temp_path_list[1]
104+
if get_hash(temp_path) == hash_value:
105+
matched_list.append(file_to_check)
106+
else:
107+
not_matched_list.append(file_to_check)
108+
109+
# Check if we found hash match as expected
110+
if matched_list and len(matched_list) == len(patch_files)*2:
111+
found_source = True
112+
# trim the string
113+
temp_path = rpm.split('stacki/')[1]
114+
temp_path_list = temp_path.split('/')
115+
version = temp_path_list[0]
116+
os_type = temp_path_list[1]
117+
version_string = 'stacki-' + version + '-' + os_type + '-sles-x86_64'
118+
# Don't want duplicate entry
119+
if version_string not in total_matched:
120+
total_matched.append(version_string)
121+
122+
# Didn't find stacki source for this os
123+
assert found_source == True
124+
125+
for match in total_matched:
126+
report_output('SLES pallet patch source', match)

0 commit comments

Comments
 (0)