George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | r""" |
| 4 | This module contains keyword functions to supplement robot's built in |
| 5 | functions and use in test where generic robot keywords don't support. |
| 6 | |
| 7 | """ |
| 8 | import time |
| 9 | from robot.libraries.BuiltIn import BuiltIn |
| 10 | from robot.libraries import DateTime |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 11 | import re |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 12 | |
| 13 | ############################################################################### |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 14 | |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 15 | |
| 16 | def run_until_keyword_fails(retry, retry_interval, name, *args): |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 17 | r""" |
| 18 | Execute a robot keyword repeatedly until it either fails or the timeout |
| 19 | value is exceeded. |
| 20 | Note: Opposite of robot keyword "Wait Until Keyword Succeeds". |
| 21 | |
| 22 | Description of argument(s): |
| 23 | retry Max timeout time in hour(s). |
| 24 | retry_interval Time interval in minute(s) for looping. |
| 25 | name Robot keyword to execute. |
| 26 | args Robot keyword arguments. |
| 27 | """ |
| 28 | |
| 29 | # Convert the retry time in seconds |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 30 | retry_seconds = DateTime.convert_time(retry) |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 31 | timeout = time.time() + int(retry_seconds) |
| 32 | |
| 33 | # Convert the interval time in seconds |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 34 | interval_seconds = DateTime.convert_time(retry_interval) |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 35 | interval = int(interval_seconds) |
| 36 | |
| 37 | BuiltIn().log(timeout) |
| 38 | BuiltIn().log(interval) |
| 39 | |
| 40 | while True: |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 41 | status = BuiltIn().run_keyword_and_return_status(name, *args) |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 42 | |
| 43 | # Return if keywords returns as failure. |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 44 | if status is False: |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 45 | BuiltIn().log("Failed as expected") |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 46 | return False |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 47 | # Return if retry timeout as success. |
| 48 | elif time.time() > timeout > 0: |
| 49 | BuiltIn().log("Max retry timeout") |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 50 | return True |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 51 | time.sleep(interval) |
| 52 | BuiltIn().log(time.time()) |
| 53 | |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 54 | return True |
| 55 | ############################################################################### |
| 56 | |
| 57 | |
| 58 | ############################################################################### |
| 59 | def htx_error_log_to_list(htx_error_log_output): |
| 60 | |
| 61 | r""" |
| 62 | Parse htx error log output string and return list of strings in the form |
| 63 | "<field name>:<field value>". |
| 64 | The output of this function may be passed to the build_error_dict function. |
| 65 | |
| 66 | Description of argument(s): |
| 67 | htx_error_log_output Error entry string containing the stdout |
| 68 | generated by "htxcmdline -geterrlog". |
| 69 | |
| 70 | Example of htx_error_log_output contents: |
| 71 | |
| 72 | ######################## Result Starts Here ############################### |
| 73 | Currently running ECG/MDT : /usr/lpp/htx/mdt/mdt.whit |
| 74 | =========================== |
| 75 | --------------------------------------------------------------------- |
| 76 | Device id:/dev/nvidia0 |
| 77 | Timestamp:Mar 29 19:41:54 2017 |
| 78 | err=00000027 |
| 79 | sev=1 |
| 80 | Exerciser Name:hxenvidia |
| 81 | Serial No:Not Available |
| 82 | Part No:Not Available |
| 83 | Location:Not Available |
| 84 | FRU Number:Not Available |
| 85 | Device:Not Available |
| 86 | Error Text:cudaEventSynchronize for stopEvent returned err = 0039 from file |
| 87 | , line 430. |
| 88 | --------------------------------------------------------------------- |
| 89 | --------------------------------------------------------------------- |
| 90 | Device id:/dev/nvidia0 |
| 91 | Timestamp:Mar 29 19:41:54 2017 |
| 92 | err=00000027 |
| 93 | sev=1 |
| 94 | Exerciser Name:hxenvidia |
| 95 | Serial No:Not Available |
| 96 | Part No:Not Available |
| 97 | Location:Not Available |
| 98 | FRU Number:Not Available |
| 99 | Device:Not Available |
| 100 | Error Text:Hardware Exerciser stopped on error |
| 101 | --------------------------------------------------------------------- |
| 102 | ######################### Result Ends Here ################################ |
| 103 | |
| 104 | Example output: |
| 105 | Returns the lists of error string per entry |
| 106 | ['Device id:/dev/nvidia0', |
| 107 | 'Timestamp:Mar 29 19:41:54 2017', |
| 108 | 'err=00000027', |
| 109 | 'sev=1', |
| 110 | 'Exerciser Name:hxenvidia', |
| 111 | 'Serial No:Not Available', |
| 112 | 'Part No:Not Available', |
| 113 | 'Location:Not Available', |
| 114 | 'FRU Number:Not Available', |
| 115 | 'Device:Not Available', |
| 116 | 'Error Text:cudaEventSynchronize for stopEvent returned err = 0039 |
| 117 | from file , line 430.'] |
| 118 | """ |
| 119 | |
| 120 | # List which will hold all the list of entries. |
| 121 | error_list = [] |
| 122 | |
| 123 | temp_error_list = [] |
| 124 | parse_walk = False |
| 125 | |
| 126 | for line in htx_error_log_output.splitlines(): |
| 127 | # Skip lines starting with "#" |
| 128 | if line.startswith("#"): |
| 129 | continue |
| 130 | |
| 131 | # Mark line starting with "-" and set parse flag. |
| 132 | if line.startswith("-") and parse_walk is False: |
| 133 | parse_walk = True |
| 134 | continue |
| 135 | # Mark line starting with "-" and reset parse flag. |
| 136 | # Set temp error list to EMPTY. |
| 137 | elif line.startswith("-"): |
| 138 | error_list.append(temp_error_list) |
| 139 | parse_walk = False |
| 140 | temp_error_list = [] |
| 141 | # Add entry to list if line is not emtpy |
| 142 | elif parse_walk: |
| 143 | temp_error_list.append(str(line)) |
| 144 | |
| 145 | return error_list |
| 146 | ############################################################################### |
| 147 | |
| 148 | |
| 149 | ############################################################################### |
| 150 | def build_error_dict(htx_error_log_output): |
| 151 | |
| 152 | r""" |
| 153 | Builds error list into a list of dictionary entries. |
| 154 | |
| 155 | Description of argument(s): |
| 156 | error_list Error list entries. |
| 157 | |
| 158 | Example output dictionary: |
| 159 | { |
| 160 | 0: |
| 161 | { |
| 162 | 'sev': '1', |
| 163 | 'err': '00000027', |
| 164 | 'Timestamp': 'Mar 29 19:41:54 2017', |
| 165 | 'Part No': 'Not Available', |
| 166 | 'Serial No': 'Not Available', |
| 167 | 'Device': 'Not Available', |
| 168 | 'FRU Number': 'Not Available', |
| 169 | 'Location': 'Not Available', |
| 170 | 'Device id': '/dev/nvidia0', |
| 171 | 'Error Text': 'cudaEventSynchronize for stopEvent returned err = 0039 |
| 172 | from file , line 430.', |
| 173 | 'Exerciser Name': 'hxenvidia' |
| 174 | }, |
| 175 | 1: |
| 176 | { |
| 177 | 'sev': '1', |
| 178 | 'err': '00000027', |
| 179 | 'Timestamp': 'Mar 29 19:41:54 2017', |
| 180 | 'Part No': 'Not Available', |
| 181 | 'Serial No': 'Not Available', |
| 182 | 'Device': 'Not Available', |
| 183 | 'FRU Number': 'Not Available', |
| 184 | 'Location': 'Not Available', |
| 185 | 'Device id': '/dev/nvidia0', |
| 186 | 'Error Text': 'Hardware Exerciser stopped on error', |
| 187 | 'Exerciser Name': 'hxenvidia' |
| 188 | } |
| 189 | }, |
| 190 | |
| 191 | """ |
| 192 | |
| 193 | # List which will hold all the list of entries. |
| 194 | error_list = [] |
| 195 | error_list = htx_error_log_to_list(htx_error_log_output) |
| 196 | |
| 197 | # dictionary which holds the error dictionry entry. |
| 198 | error_dict = {} |
| 199 | |
| 200 | temp_error_dict = {} |
| 201 | error_index = 0 |
| 202 | |
| 203 | # Loop through the error list. |
| 204 | for entry_list in error_list: |
| 205 | # Loop through the first error list entry. |
| 206 | for entry in entry_list: |
| 207 | # Split string into list for key value update. |
| 208 | # Example: 'Device id:/dev/nvidia0' |
| 209 | # Example: 'err=00000027' |
| 210 | parm_split = re.split("[:=]", entry) |
| 211 | # Populate temp dictionary with key value pair data. |
| 212 | temp_error_dict[str(parm_split[0])] = parm_split[1] |
| 213 | |
| 214 | # Update the master dictionary per entry index. |
| 215 | error_dict[error_index] = temp_error_dict |
| 216 | # Reset temp dict to EMPTY and increment index count. |
| 217 | temp_error_dict = {} |
| 218 | error_index += 1 |
| 219 | |
| 220 | return error_dict |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 221 | |
| 222 | ############################################################################### |