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. |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 6 | """ |
Steven Sombar | 130a04f | 2017-07-16 10:02:37 -0500 | [diff] [blame] | 7 | |
| 8 | try: |
| 9 | from robot.libraries.BuiltIn import BuiltIn |
| 10 | from robot.libraries import DateTime |
| 11 | except ImportError: |
| 12 | pass |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 13 | import time |
Steven Sombar | 130a04f | 2017-07-16 10:02:37 -0500 | [diff] [blame] | 14 | import os |
Steven Sombar | 130a04f | 2017-07-16 10:02:37 -0500 | [diff] [blame] | 15 | |
| 16 | |
Steven Sombar | 130a04f | 2017-07-16 10:02:37 -0500 | [diff] [blame] | 17 | def run_until_keyword_fails(retry, |
| 18 | retry_interval, |
| 19 | name, |
| 20 | *args): |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 21 | r""" |
| 22 | Execute a robot keyword repeatedly until it either fails or the timeout |
| 23 | value is exceeded. |
| 24 | Note: Opposite of robot keyword "Wait Until Keyword Succeeds". |
| 25 | |
| 26 | Description of argument(s): |
| 27 | retry Max timeout time in hour(s). |
| 28 | retry_interval Time interval in minute(s) for looping. |
| 29 | name Robot keyword to execute. |
| 30 | args Robot keyword arguments. |
| 31 | """ |
| 32 | |
| 33 | # Convert the retry time in seconds |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 34 | retry_seconds = DateTime.convert_time(retry) |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 35 | timeout = time.time() + int(retry_seconds) |
| 36 | |
| 37 | # Convert the interval time in seconds |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 38 | interval_seconds = DateTime.convert_time(retry_interval) |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 39 | interval = int(interval_seconds) |
| 40 | |
| 41 | BuiltIn().log(timeout) |
| 42 | BuiltIn().log(interval) |
| 43 | |
| 44 | while True: |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 45 | status = BuiltIn().run_keyword_and_return_status(name, *args) |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 46 | |
| 47 | # Return if keywords returns as failure. |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 48 | if status is False: |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 49 | BuiltIn().log("Failed as expected") |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 50 | return False |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 51 | # Return if retry timeout as success. |
| 52 | elif time.time() > timeout > 0: |
| 53 | BuiltIn().log("Max retry timeout") |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 54 | return True |
George Keishing | 04d9345 | 2017-05-03 09:14:15 -0500 | [diff] [blame] | 55 | time.sleep(interval) |
| 56 | BuiltIn().log(time.time()) |
| 57 | |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 58 | return True |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 59 | |
| 60 | |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 61 | def htx_error_log_to_list(htx_error_log_output): |
| 62 | |
| 63 | r""" |
| 64 | Parse htx error log output string and return list of strings in the form |
| 65 | "<field name>:<field value>". |
| 66 | The output of this function may be passed to the build_error_dict function. |
| 67 | |
| 68 | Description of argument(s): |
| 69 | htx_error_log_output Error entry string containing the stdout |
| 70 | generated by "htxcmdline -geterrlog". |
| 71 | |
| 72 | Example of htx_error_log_output contents: |
| 73 | |
| 74 | ######################## Result Starts Here ############################### |
| 75 | Currently running ECG/MDT : /usr/lpp/htx/mdt/mdt.whit |
| 76 | =========================== |
| 77 | --------------------------------------------------------------------- |
| 78 | Device id:/dev/nvidia0 |
| 79 | Timestamp:Mar 29 19:41:54 2017 |
| 80 | err=00000027 |
| 81 | sev=1 |
| 82 | Exerciser Name:hxenvidia |
| 83 | Serial No:Not Available |
| 84 | Part No:Not Available |
| 85 | Location:Not Available |
| 86 | FRU Number:Not Available |
| 87 | Device:Not Available |
| 88 | Error Text:cudaEventSynchronize for stopEvent returned err = 0039 from file |
| 89 | , line 430. |
| 90 | --------------------------------------------------------------------- |
| 91 | --------------------------------------------------------------------- |
| 92 | Device id:/dev/nvidia0 |
| 93 | Timestamp:Mar 29 19:41:54 2017 |
| 94 | err=00000027 |
| 95 | sev=1 |
| 96 | Exerciser Name:hxenvidia |
| 97 | Serial No:Not Available |
| 98 | Part No:Not Available |
| 99 | Location:Not Available |
| 100 | FRU Number:Not Available |
| 101 | Device:Not Available |
| 102 | Error Text:Hardware Exerciser stopped on error |
| 103 | --------------------------------------------------------------------- |
| 104 | ######################### Result Ends Here ################################ |
| 105 | |
| 106 | Example output: |
| 107 | Returns the lists of error string per entry |
| 108 | ['Device id:/dev/nvidia0', |
| 109 | 'Timestamp:Mar 29 19:41:54 2017', |
| 110 | 'err=00000027', |
| 111 | 'sev=1', |
| 112 | 'Exerciser Name:hxenvidia', |
| 113 | 'Serial No:Not Available', |
| 114 | 'Part No:Not Available', |
| 115 | 'Location:Not Available', |
| 116 | 'FRU Number:Not Available', |
| 117 | 'Device:Not Available', |
| 118 | 'Error Text:cudaEventSynchronize for stopEvent returned err = 0039 |
| 119 | from file , line 430.'] |
| 120 | """ |
| 121 | |
| 122 | # List which will hold all the list of entries. |
| 123 | error_list = [] |
| 124 | |
| 125 | temp_error_list = [] |
| 126 | parse_walk = False |
| 127 | |
| 128 | for line in htx_error_log_output.splitlines(): |
| 129 | # Skip lines starting with "#" |
| 130 | if line.startswith("#"): |
| 131 | continue |
| 132 | |
| 133 | # Mark line starting with "-" and set parse flag. |
| 134 | if line.startswith("-") and parse_walk is False: |
| 135 | parse_walk = True |
| 136 | continue |
| 137 | # Mark line starting with "-" and reset parse flag. |
| 138 | # Set temp error list to EMPTY. |
| 139 | elif line.startswith("-"): |
| 140 | error_list.append(temp_error_list) |
| 141 | parse_walk = False |
| 142 | temp_error_list = [] |
| 143 | # Add entry to list if line is not emtpy |
| 144 | elif parse_walk: |
| 145 | temp_error_list.append(str(line)) |
| 146 | |
| 147 | return error_list |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 148 | |
| 149 | |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 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 | |