|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +import stack.api |
| 4 | +import hashlib |
| 5 | +import json |
| 6 | + |
| 7 | + |
| 8 | +# Returns a hash of filename |
| 9 | +def Get_A_Hash(filename): |
| 10 | + hasher = hashlib.md5() |
| 11 | + with open(filename, 'rb') as f: |
| 12 | + buf = f.read() |
| 13 | + hasher.update(buf) |
| 14 | + |
| 15 | + return hasher.hexdigest() |
| 16 | + |
| 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 |
| 24 | + |
| 25 | +# Grab all found SLES versions and returns a list |
| 26 | +def Get_SLES_Flavors(): |
| 27 | + base_dir = '/export/stack/pallets/SLES/' |
| 28 | + sles_flavors = os.listdir(base_dir) |
| 29 | + return sles_flavors |
| 30 | + |
| 31 | + |
| 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 |
| 38 | + |
| 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('') |
| 45 | + |
| 46 | + # Test every sles flavor found |
| 47 | + for sles_flavor in sles_flavors: |
| 48 | + # Make sure installed patches match what's under /opt/stack/pallet-patches |
| 49 | + patch_dir = '/opt/stack/pallet-patches' |
| 50 | + patch_dir_files = [] |
| 51 | + for (dir_path, dir_names, filenames) in os.walk(patch_dir): |
| 52 | + # Only want particular SLES version |
| 53 | + if sles_flavor in dir_path: |
| 54 | + patch_dir_files += [os.path.join(dir_path, file) for file in filenames if file == 'content'] |
| 55 | + |
| 56 | + # We should have non-empty list here |
| 57 | + assert patch_dir_files |
| 58 | + |
| 59 | + # Find img file from patch directory in SLES pallet |
| 60 | + result = host.run(f'grep .img {patch_dir_files[0]}\n') |
| 61 | + |
| 62 | + # Last item is the partial image path |
| 63 | + part_img_file = result.stdout.split()[-1] |
| 64 | + |
| 65 | + # Find full path to /export/stack/pallets/SLES/? |
| 66 | + result = host.run(f'probepal {base_dir}') |
| 67 | + assert result.rc == 0 |
| 68 | + palinfo = json.loads(result.stdout) |
| 69 | + |
| 70 | + # It shouldn't be empty |
| 71 | + assert palinfo[base_dir] |
| 72 | + |
| 73 | + sles_pallet_root = None |
| 74 | + for pallet in palinfo[base_dir]: |
| 75 | + # Grab the right sles sp level |
| 76 | + if pallet['name'] == 'SLES' and pallet['version'] == sles_flavor: |
| 77 | + assert host.file(f"{pallet['pallet_root']}").is_directory |
| 78 | + sles_pallet_root = pallet['pallet_root'] |
| 79 | + break |
| 80 | + |
| 81 | + # .img file should exist in sles pallet directory in relative path |
| 82 | + assert os.path.exists(sles_pallet_root + '/' + part_img_file) |
| 83 | + |
| 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 |
0 commit comments