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): |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 62 | r""" |
| 63 | Parse htx error log output string and return list of strings in the form |
| 64 | "<field name>:<field value>". |
| 65 | The output of this function may be passed to the build_error_dict function. |
| 66 | |
| 67 | Description of argument(s): |
| 68 | htx_error_log_output Error entry string containing the stdout |
| 69 | generated by "htxcmdline -geterrlog". |
| 70 | |
| 71 | Example of htx_error_log_output contents: |
| 72 | |
| 73 | ######################## Result Starts Here ############################### |
| 74 | Currently running ECG/MDT : /usr/lpp/htx/mdt/mdt.whit |
| 75 | =========================== |
| 76 | --------------------------------------------------------------------- |
| 77 | Device id:/dev/nvidia0 |
| 78 | Timestamp:Mar 29 19:41:54 2017 |
| 79 | err=00000027 |
| 80 | sev=1 |
| 81 | Exerciser Name:hxenvidia |
| 82 | Serial No:Not Available |
| 83 | Part No:Not Available |
| 84 | Location:Not Available |
| 85 | FRU Number:Not Available |
| 86 | Device:Not Available |
| 87 | Error Text:cudaEventSynchronize for stopEvent returned err = 0039 from file |
| 88 | , line 430. |
| 89 | --------------------------------------------------------------------- |
| 90 | --------------------------------------------------------------------- |
| 91 | Device id:/dev/nvidia0 |
| 92 | Timestamp:Mar 29 19:41:54 2017 |
| 93 | err=00000027 |
| 94 | sev=1 |
| 95 | Exerciser Name:hxenvidia |
| 96 | Serial No:Not Available |
| 97 | Part No:Not Available |
| 98 | Location:Not Available |
| 99 | FRU Number:Not Available |
| 100 | Device:Not Available |
| 101 | Error Text:Hardware Exerciser stopped on error |
| 102 | --------------------------------------------------------------------- |
| 103 | ######################### Result Ends Here ################################ |
| 104 | |
| 105 | Example output: |
| 106 | Returns the lists of error string per entry |
| 107 | ['Device id:/dev/nvidia0', |
| 108 | 'Timestamp:Mar 29 19:41:54 2017', |
| 109 | 'err=00000027', |
| 110 | 'sev=1', |
| 111 | 'Exerciser Name:hxenvidia', |
| 112 | 'Serial No:Not Available', |
| 113 | 'Part No:Not Available', |
| 114 | 'Location:Not Available', |
| 115 | 'FRU Number:Not Available', |
| 116 | 'Device:Not Available', |
| 117 | 'Error Text:cudaEventSynchronize for stopEvent returned err = 0039 |
| 118 | from file , line 430.'] |
| 119 | """ |
| 120 | |
| 121 | # List which will hold all the list of entries. |
| 122 | error_list = [] |
| 123 | |
| 124 | temp_error_list = [] |
| 125 | parse_walk = False |
| 126 | |
| 127 | for line in htx_error_log_output.splitlines(): |
| 128 | # Skip lines starting with "#" |
| 129 | if line.startswith("#"): |
| 130 | continue |
| 131 | |
| 132 | # Mark line starting with "-" and set parse flag. |
| 133 | if line.startswith("-") and parse_walk is False: |
| 134 | parse_walk = True |
| 135 | continue |
| 136 | # Mark line starting with "-" and reset parse flag. |
| 137 | # Set temp error list to EMPTY. |
| 138 | elif line.startswith("-"): |
| 139 | error_list.append(temp_error_list) |
| 140 | parse_walk = False |
| 141 | temp_error_list = [] |
| 142 | # Add entry to list if line is not emtpy |
| 143 | elif parse_walk: |
| 144 | temp_error_list.append(str(line)) |
| 145 | |
| 146 | return error_list |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 147 | |
| 148 | |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 149 | def build_error_dict(htx_error_log_output): |
George Keishing | 4bbf520 | 2017-05-18 06:55:53 -0500 | [diff] [blame] | 150 | r""" |
| 151 | Builds error list into a list of dictionary entries. |
| 152 | |
| 153 | Description of argument(s): |
| 154 | error_list Error list entries. |
| 155 | |
| 156 | Example output dictionary: |
| 157 | { |
| 158 | 0: |
| 159 | { |
| 160 | 'sev': '1', |
| 161 | 'err': '00000027', |
| 162 | 'Timestamp': 'Mar 29 19:41:54 2017', |
| 163 | 'Part No': 'Not Available', |
| 164 | 'Serial No': 'Not Available', |
| 165 | 'Device': 'Not Available', |
| 166 | 'FRU Number': 'Not Available', |
| 167 | 'Location': 'Not Available', |
| 168 | 'Device id': '/dev/nvidia0', |
| 169 | 'Error Text': 'cudaEventSynchronize for stopEvent returned err = 0039 |
| 170 | from file , line 430.', |
| 171 | 'Exerciser Name': 'hxenvidia' |
| 172 | }, |
| 173 | 1: |
| 174 | { |
| 175 | 'sev': '1', |
| 176 | 'err': '00000027', |
| 177 | 'Timestamp': 'Mar 29 19:41:54 2017', |
| 178 | 'Part No': 'Not Available', |
| 179 | 'Serial No': 'Not Available', |
| 180 | 'Device': 'Not Available', |
| 181 | 'FRU Number': 'Not Available', |
| 182 | 'Location': 'Not Available', |
| 183 | 'Device id': '/dev/nvidia0', |
| 184 | 'Error Text': 'Hardware Exerciser stopped on error', |
| 185 | 'Exerciser Name': 'hxenvidia' |
| 186 | } |
| 187 | }, |
| 188 | |
| 189 | """ |
| 190 | |
| 191 | # List which will hold all the list of entries. |
| 192 | error_list = [] |
| 193 | error_list = htx_error_log_to_list(htx_error_log_output) |
| 194 | |
| 195 | # dictionary which holds the error dictionry entry. |
| 196 | error_dict = {} |
| 197 | |
| 198 | temp_error_dict = {} |
| 199 | error_index = 0 |
| 200 | |
| 201 | # Loop through the error list. |
| 202 | for entry_list in error_list: |
| 203 | # Loop through the first error list entry. |
| 204 | for entry in entry_list: |
| 205 | # Split string into list for key value update. |
| 206 | # Example: 'Device id:/dev/nvidia0' |
| 207 | # Example: 'err=00000027' |
| 208 | parm_split = re.split("[:=]", entry) |
| 209 | # Populate temp dictionary with key value pair data. |
| 210 | temp_error_dict[str(parm_split[0])] = parm_split[1] |
| 211 | |
| 212 | # Update the master dictionary per entry index. |
| 213 | error_dict[error_index] = temp_error_dict |
| 214 | # Reset temp dict to EMPTY and increment index count. |
| 215 | temp_error_dict = {} |
| 216 | error_index += 1 |
| 217 | |
| 218 | return error_dict |