|
| 1 | +import pytest |
| 2 | +import os |
| 3 | +import stack.api |
| 4 | +import hashlib |
| 5 | +import json |
| 6 | + |
| 7 | + |
| 8 | +# Returns hash of filename |
| 9 | +def get_hash(filename, sles): |
| 10 | + # SLES15 uses sha256 |
| 11 | + if '15sp' in sles: |
| 12 | + hasher = hashlib.sha256() |
| 13 | + else: |
| 14 | + hasher = hashlib.md5() |
| 15 | + |
| 16 | + with open(filename, 'rb') as f: |
| 17 | + hasher.update(f.read()) |
| 18 | + |
| 19 | + return hasher.hexdigest() |
| 20 | + |
| 21 | + |
| 22 | +# Verify SLES patches are installed |
| 23 | +def test_sles_pallet_patched(host, report_output): |
| 24 | + total_matched = [] |
| 25 | + base_dir = '/export/stack/pallets/SLES/' |
| 26 | + if not os.path.isdir(base_dir): |
| 27 | + pytest.skip('No SLES pallet found - skipping patch check') |
| 28 | + sles_flavors = os.listdir(base_dir) |
| 29 | + assert sles_flavors |
| 30 | + |
| 31 | + # Find out where is stack-sles*images*.rpm file(s) |
| 32 | + result = host.run('find /export/stack/pallets/stacki/ -name "*stack-sles-*.rpm"') |
| 33 | + RPM = result.stdout.splitlines() |
| 34 | + assert RPM |
| 35 | + |
| 36 | + # If the stack-sles*images*.rpm installed? |
| 37 | + result = host.run('rpm -qa | grep stack | grep images') |
| 38 | + assert result |
| 39 | + |
| 40 | + # Test every sles flavor found |
| 41 | + for sles_flavor in sles_flavors: |
| 42 | + # Make sure installed patches match what's under /opt/stack/pallet-patches |
| 43 | + patch_dir = '/opt/stack/pallet-patches' |
| 44 | + patch_dir_files = [] |
| 45 | + found_source = False |
| 46 | + for (dir_path, dir_names, filenames) in os.walk(patch_dir): |
| 47 | + # Only want particular SLES version |
| 48 | + if sles_flavor in dir_path: |
| 49 | + file_to_check = None |
| 50 | + if '15sp' in sles_flavor: |
| 51 | + file_to_check = 'CHECKSUMS' |
| 52 | + else: |
| 53 | + file_to_check = 'content' |
| 54 | + patch_dir_files += [os.path.join(dir_path, file) for file in filenames if file == file_to_check] |
| 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]}') |
| 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['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 | + # Verify all the stack-sles-* rpm packages |
| 85 | + for rpm in RPM: |
| 86 | + # Make sure we found pm file |
| 87 | + assert '.rpm' in rpm |
| 88 | + |
| 89 | + result = host.run(f'rpm -qp --dump {rpm}') |
| 90 | + # We don't want file under /opt/stack/images to be included in this list |
| 91 | + patch_files = [line.split() for line in result.stdout.splitlines() if sles_flavor in line and '/images/' not in line] |
| 92 | + |
| 93 | + matched_list = [] |
| 94 | + # Verify file(s) from images RPM actually exist |
| 95 | + for this_list in patch_files: |
| 96 | + file_to_check = this_list[0] |
| 97 | + assert os.path.exists(file_to_check) |
| 98 | + hash_value = this_list[3] |
| 99 | + |
| 100 | + # Grab hash for this file in patch directory and 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(file_to_check,sles_flavor) == hash_value and get_hash(temp_path, sles_flavor) == hash_value: |
| 105 | + matched_list.append(file_to_check) |
| 106 | + |
| 107 | + # Check if we found hash match as expected. Should be 4 files. |
| 108 | + if matched_list and len(matched_list) == 4: |
| 109 | + found_source = True |
| 110 | + # trim the string |
| 111 | + temp_path = rpm.split('stacki/')[1] |
| 112 | + temp_path_list = temp_path.split('/') |
| 113 | + version = temp_path_list[0] |
| 114 | + os_type = temp_path_list[1] |
| 115 | + version_string = 'stacki-' + version + '-' + os_type + '-sles-x86_64' |
| 116 | + # Don't want duplicate entry |
| 117 | + if version_string not in total_matched: |
| 118 | + total_matched.append(version_string) |
| 119 | + |
| 120 | + # Didn't find stacki source for this os |
| 121 | + assert found_source == True |
| 122 | + |
| 123 | + for match in total_matched: |
| 124 | + report_output('SLES pallet patch source', match) |
0 commit comments