Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module is the python counterpart to test_uploadimage.robot. |
| 5 | """ |
| 6 | |
| 7 | import os |
| 8 | import sys |
| 9 | import re |
| 10 | import string |
| 11 | import tarfile |
| 12 | import time |
| 13 | |
| 14 | robot_pgm_dir_path = os.path.dirname(__file__) + os.sep |
| 15 | repo_lib_path = re.sub('/extended/', '/lib', robot_pgm_dir_path) |
| 16 | repo_data_path = re.sub('/extended/', '/data', robot_pgm_dir_path) |
| 17 | sys.path.append(repo_lib_path) |
| 18 | sys.path.append(repo_data_path) |
| 19 | |
| 20 | import gen_robot_keyword as grk |
| 21 | import gen_print as gp |
| 22 | import variables as var |
| 23 | from robot.libraries.BuiltIn import BuiltIn |
| 24 | |
| 25 | |
| 26 | ############################################################################### |
| 27 | def get_latest_file(dir_path): |
| 28 | |
| 29 | r""" |
| 30 | Get the path to the latest uploaded file. |
| 31 | |
| 32 | Description of argument(s): |
| 33 | dir_path Path to the dir from which the name of the last |
| 34 | updated file or folder will be returned to the |
| 35 | calling function. |
| 36 | """ |
| 37 | |
| 38 | grk.run_key_u("Open Connection And Log In") |
| 39 | status, ret_values =\ |
| 40 | grk.run_key("Execute Command On BMC cd " + dir_path |
| 41 | + "; stat -c '%Y %n' * | sort -k1,1nr | head -n 1", ignore=1) |
| 42 | return ret_values.split(" ")[-1] |
| 43 | |
| 44 | ############################################################################### |
| 45 | |
| 46 | |
| 47 | ############################################################################### |
| 48 | def get_version_tar(tar_file_path): |
| 49 | |
| 50 | r""" |
| 51 | Read the image version from the MANIFEST inside the tarball. |
| 52 | |
| 53 | Description of argument(s): |
| 54 | tar_file_path The path to a tar file that holds the image |
| 55 | version inside the MANIFEST. |
| 56 | """ |
| 57 | |
| 58 | tar = tarfile.open(tar_file_path) |
| 59 | for member in tar.getmembers(): |
| 60 | f=tar.extractfile(member) |
| 61 | content=f.read() |
| 62 | if "version=" in content: |
| 63 | content = content.split("\n") |
| 64 | content = [x for x in content if "version=" in x] |
| 65 | version = content[0].split("=")[-1] |
| 66 | break |
| 67 | tar.close() |
| 68 | return version |
| 69 | |
| 70 | ############################################################################### |
| 71 | |
| 72 | |
| 73 | ############################################################################### |
| 74 | def get_image_version(file_path): |
| 75 | |
| 76 | r""" |
| 77 | Read the file for a version object. |
| 78 | |
| 79 | Description of argument(s): |
| 80 | file_path The path to a file that holds the image version. |
| 81 | """ |
| 82 | |
| 83 | grk.run_key_u("Open Connection And Log In") |
| 84 | status, ret_values =\ |
| 85 | grk.run_key("Execute Command On BMC cat " |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 86 | + file_path + " | grep \"version=\"", ignore=1) |
| 87 | return (ret_values.split("\n")[0]).split("=")[-1] |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 88 | |
| 89 | ############################################################################### |
| 90 | |
| 91 | |
| 92 | ############################################################################### |
| 93 | def get_image_purpose(file_path): |
| 94 | |
| 95 | r""" |
| 96 | Read the file for a purpose object. |
| 97 | |
| 98 | Description of argument(s): |
| 99 | file_path The path to a file that holds the image purpose. |
| 100 | """ |
| 101 | |
| 102 | grk.run_key_u("Open Connection And Log In") |
| 103 | status, ret_values =\ |
| 104 | grk.run_key("Execute Command On BMC cat " |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 105 | + file_path + " | grep \"purpose=\"", ignore=1) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 106 | return ret_values.split("=")[-1] |
| 107 | |
| 108 | ############################################################################### |
| 109 | |
| 110 | |
| 111 | ############################################################################### |
| 112 | def get_image_path(image_version): |
| 113 | |
| 114 | r""" |
| 115 | Query the upload image dir for the presence of image matching |
| 116 | the version that was read from the MANIFEST before uploading |
| 117 | the image. Based on the purpose verify the activation object |
| 118 | exists and is either READY or INVALID. |
| 119 | |
| 120 | Description of argument(s): |
| 121 | image_version The version of the image that should match one |
| 122 | of the images in the upload dir. |
| 123 | """ |
| 124 | |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 125 | upload_dir = BuiltIn().get_variable_value("${upload_dir_path}") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 126 | grk.run_key_u("Open Connection And Log In") |
| 127 | status, image_list =\ |
| 128 | grk.run_key("Execute Command On BMC ls -d " + upload_dir |
| 129 | + "*/") |
| 130 | |
| 131 | image_list = image_list.split("\n") |
| 132 | retry = 0 |
| 133 | while (retry < 10): |
| 134 | for i in range(0, len(image_list)): |
| 135 | version = get_image_version(image_list[i] + "MANIFEST") |
| 136 | if (version == image_version): |
| 137 | return image_list[i] |
| 138 | time.sleep(10) |
| 139 | retry += 1 |
| 140 | |
| 141 | ############################################################################### |
| 142 | |
| 143 | |
| 144 | ############################################################################### |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 145 | def verify_image_upload(timeout=3): |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 146 | |
| 147 | r""" |
| 148 | Verify the image was uploaded correctly and that it created |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 149 | a valid d-bus object. If the first check for the image |
| 150 | fails, try again until we reach the timeout. |
| 151 | |
| 152 | Description of argument(s): |
| 153 | timeout How long, in minutes, to keep trying to find the |
| 154 | image on the BMC. Default is 3 minutes. |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 155 | """ |
| 156 | |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 157 | image_version = BuiltIn().get_variable_value("${image_version}") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 158 | image_path = get_image_path(image_version) |
| 159 | image_version_id = image_path.split("/")[-2] |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 160 | BuiltIn().set_global_variable("${version_id}", image_version_id) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 161 | |
| 162 | grk.run_key_u("Open Connection And Log In") |
| 163 | image_purpose = get_image_purpose(image_path + "MANIFEST") |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 164 | if (image_purpose == var.VERSION_PURPOSE_BMC or |
| 165 | image_purpose == var.VERSION_PURPOSE_HOST): |
George Keishing | ff1e3ec | 2017-07-20 01:58:21 -0500 | [diff] [blame] | 166 | uri = var.SOFTWARE_VERSION_URI + image_version_id |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 167 | ret_values = "" |
| 168 | for itr in range(timeout * 2): |
| 169 | status, ret_values = \ |
| 170 | grk.run_key("Read Attribute " + uri + " Activation") |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 171 | |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 172 | if ((ret_values == var.READY) or (ret_values == var.INVALID) |
| 173 | or (ret_values == var.ACTIVE)): |
| 174 | return True |
| 175 | else: |
| 176 | time.sleep(30) |
| 177 | |
| 178 | # If we exit the for loop, the timeout has been reached |
| 179 | gp.print_var(ret_values) |
| 180 | return False |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 181 | else: |
Saqib Khan | bb8b63f | 2017-05-24 10:58:01 -0500 | [diff] [blame] | 182 | gp.print_var(image_purpose) |
Saqib Khan | fb1f6ae | 2017-04-26 13:24:41 -0500 | [diff] [blame] | 183 | return False |
| 184 | |
| 185 | ############################################################################### |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 186 | |
| 187 | |
| 188 | ############################################################################### |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 189 | def verify_image_not_in_bmc_uploads_dir(image_version, timeout=3): |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 190 | |
| 191 | r""" |
| 192 | Check that an image with the given version is not unpacked inside of the |
| 193 | BMCs image uploads directory. If no image is found, retry every 30 seconds |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 194 | until the given timeout is hit, in case the BMC takes time |
| 195 | unpacking the image. |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 196 | |
| 197 | Description of argument(s): |
| 198 | image_version The version of the image to look for on the BMC. |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 199 | timeout How long, in minutes, to try to find an image on the BMC. |
| 200 | Default is 3 minutes. |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 201 | """ |
| 202 | |
| 203 | grk.run_key('Open Connection And Log In') |
Charles P. Hofer | 346c045 | 2017-07-14 15:29:50 -0500 | [diff] [blame] | 204 | upload_dir_path = BuiltIn().get_variable_value("${upload_dir_path}") |
Charles Paul Hofer | 6b97268 | 2017-07-20 11:36:56 -0500 | [diff] [blame] | 205 | for i in range(timeout * 2): |
Charles P. Hofer | 1d20acd | 2017-07-05 15:24:40 -0500 | [diff] [blame] | 206 | stat, grep_res = grk.run_key('Execute Command On BMC ' |
| 207 | + 'ls ' + upload_dir_path + '*/MANIFEST 2>/dev/null ' |
| 208 | + '| xargs grep -rl "version=' + image_version + '"') |
| 209 | image_dir = os.path.dirname(grep_res.split('\n')[0]) |
| 210 | if '' != image_dir: |
| 211 | grk.run_key('Execute Command On BMC rm -rf ' + image_dir) |
| 212 | BuiltIn().fail('Found invalid BMC Image: ' + image_dir) |
| 213 | time.sleep(30) |
| 214 | |
| 215 | ############################################################################### |