|
11 | 11 | import os |
12 | 12 | import logging |
13 | 13 | from fnmatch import fnmatch |
| 14 | +from subprocess import check_call, CalledProcessError, TimeoutExpired |
14 | 15 | from common_tasks import ( |
15 | 16 | run_check_call, |
16 | 17 | process_glob_string, |
|
20 | 21 |
|
21 | 22 | root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), "..", "..", "..")) |
22 | 23 |
|
| 24 | +""" |
| 25 | +Some samples may "run forever" or need to be timed out after a period of time. Add them here in the following format: |
| 26 | +TIMEOUT_SAMPLES = { |
| 27 | + "<package-name>": { |
| 28 | + "<sample_file_name.py>": (<timeout (seconds)>, <pass if timeout? (bool)>) |
| 29 | + } |
| 30 | +} |
| 31 | +""" |
| 32 | +TIMEOUT_SAMPLES = { |
| 33 | + "azure-eventhub": { |
| 34 | + "receive_batch_with_checkpoint.py": (3, True) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +# Add your library + sample file if you do not want a particular sample to be run |
23 | 40 | IGNORED_SAMPLES = { |
24 | 41 | "azure-eventgrid": [ |
25 | 42 | "__init__.py", |
|
29 | 46 | "sample_publish_events_to_a_topic_using_sas_credential.py", |
30 | 47 | "sample_publish_events_to_a_topic_using_sas_credential_async.py"], |
31 | 48 | "azure-eventhub": [ |
| 49 | + "send.py", |
| 50 | + "send_async.py", |
| 51 | + "send_stream.py", |
| 52 | + "send_stream_async.py", |
| 53 | + "recv_for_period_async.py", |
| 54 | + "client_creation_async.py", |
| 55 | + "connection_string_authentication.py", |
| 56 | + "connection_string_authentication_async.py", |
| 57 | + "client_identity_authentication_async.py", |
| 58 | + "client_creation.py", |
| 59 | + "client_identity_authentication.py", |
32 | 60 | "authenticate_with_sas_token.py", |
33 | 61 | "connection_to_custom_endpoint_address.py", |
34 | 62 | "proxy.py", |
35 | | - "receive_batch_with_checkpoint.py", |
36 | 63 | "recv.py", |
37 | 64 | "recv_track_last_enqueued_event_prop.py", |
38 | 65 | "recv_with_checkpoint_by_event_count.py", |
|
87 | 114 | } |
88 | 115 |
|
89 | 116 |
|
| 117 | +def run_check_call_with_timeout( |
| 118 | + command_array, |
| 119 | + working_directory, |
| 120 | + acceptable_return_codes=[], |
| 121 | + always_exit=False, |
| 122 | + timeout=None, |
| 123 | + pass_if_timeout=False |
| 124 | +): |
| 125 | + """This is copied from common_tasks.py with some additions. |
| 126 | + Don't want to break anyone that's using the original code. |
| 127 | + """ |
| 128 | + try: |
| 129 | + logging.info( |
| 130 | + "Command Array: {0}, Target Working Directory: {1}".format( |
| 131 | + command_array, working_directory |
| 132 | + ) |
| 133 | + ) |
| 134 | + check_call(command_array, cwd=working_directory, timeout=timeout) |
| 135 | + except CalledProcessError as err: |
| 136 | + if err.returncode not in acceptable_return_codes: |
| 137 | + logging.error(err) # , file = sys.stderr |
| 138 | + if always_exit: |
| 139 | + exit(1) |
| 140 | + else: |
| 141 | + return err |
| 142 | + except TimeoutExpired as err: |
| 143 | + if pass_if_timeout: |
| 144 | + logging.info( |
| 145 | + "Sample timed out successfully" |
| 146 | + ) |
| 147 | + else: |
| 148 | + logging.info( |
| 149 | + "Fail: Sample timed out" |
| 150 | + ) |
| 151 | + return err |
| 152 | + |
| 153 | + |
| 154 | +def execute_sample(sample, samples_errors, timed): |
| 155 | + if isinstance(sample, tuple): |
| 156 | + sample, timeout, pass_if_timeout = sample |
| 157 | + |
| 158 | + if sys.version_info < (3, 5) and sample.endswith("_async.py"): |
| 159 | + return |
| 160 | + |
| 161 | + logging.info( |
| 162 | + "Testing {}".format(sample) |
| 163 | + ) |
| 164 | + command_array = [sys.executable, sample] |
| 165 | + |
| 166 | + if not timed: |
| 167 | + errors = run_check_call(command_array, root_dir) |
| 168 | + else: |
| 169 | + errors = run_check_call_with_timeout( |
| 170 | + command_array, root_dir, timeout=timeout, pass_if_timeout=pass_if_timeout |
| 171 | + ) |
| 172 | + |
| 173 | + sample_name = os.path.basename(sample) |
| 174 | + if errors: |
| 175 | + samples_errors.append(sample_name) |
| 176 | + logging.info( |
| 177 | + "ERROR: {}".format(sample_name) |
| 178 | + ) |
| 179 | + else: |
| 180 | + logging.info( |
| 181 | + "SUCCESS: {}.".format(sample_name) |
| 182 | + ) |
| 183 | + |
| 184 | + |
90 | 185 | def run_samples(targeted_package): |
91 | 186 | logging.info("running samples for {}".format(targeted_package)) |
92 | 187 |
|
93 | 188 | samples_errors = [] |
94 | 189 | sample_paths = [] |
| 190 | + timed_sample_paths = [] |
| 191 | + |
95 | 192 | samples_dir_path = os.path.abspath(os.path.join(targeted_package, "samples")) |
96 | 193 | package_name = os.path.basename(targeted_package) |
| 194 | + samples_need_timeout = TIMEOUT_SAMPLES.get(package_name, {}) |
97 | 195 |
|
98 | 196 | for path, subdirs, files in os.walk(samples_dir_path): |
99 | 197 | for name in files: |
100 | | - if fnmatch(name, "*.py") and name not in IGNORED_SAMPLES.get(package_name, []): |
| 198 | + if fnmatch(name, "*.py") and name in samples_need_timeout: |
| 199 | + timeout, pass_if_timeout = samples_need_timeout[name] |
| 200 | + timed_sample_paths.append((os.path.abspath(os.path.join(path, name)), timeout, pass_if_timeout)) |
| 201 | + elif fnmatch(name, "*.py") and name not in IGNORED_SAMPLES.get(package_name, []): |
101 | 202 | sample_paths.append(os.path.abspath(os.path.join(path, name))) |
102 | 203 |
|
103 | | - if not sample_paths: |
| 204 | + if not sample_paths and not timed_sample_paths: |
104 | 205 | logging.info( |
105 | 206 | "No samples found in {}".format(targeted_package) |
106 | 207 | ) |
107 | 208 | exit(0) |
108 | 209 |
|
109 | 210 | for sample in sample_paths: |
110 | | - if sys.version_info < (3, 5) and sample.endswith("_async.py"): |
111 | | - continue |
| 211 | + execute_sample(sample, samples_errors, timed=False) |
112 | 212 |
|
113 | | - logging.info( |
114 | | - "Testing {}".format(sample) |
115 | | - ) |
116 | | - command_array = [sys.executable, sample] |
117 | | - errors = run_check_call(command_array, root_dir, always_exit=False) |
118 | | - |
119 | | - sample_name = os.path.basename(sample) |
120 | | - if errors: |
121 | | - samples_errors.append(sample_name) |
122 | | - logging.info( |
123 | | - "ERROR: {}".format(sample_name) |
124 | | - ) |
125 | | - else: |
126 | | - logging.info( |
127 | | - "SUCCESS: {}.".format(sample_name) |
128 | | - ) |
| 213 | + for sample in timed_sample_paths: |
| 214 | + execute_sample(sample, samples_errors, timed=True) |
129 | 215 |
|
130 | 216 | if samples_errors: |
131 | 217 | logging.error("Sample(s) that ran with errors: {}".format(samples_errors)) |
|
0 commit comments