How to use report_failures method in Testify

Best Python code snippet using Testify_python

highstate_return.py

Source:highstate_return.py Github

copy

Full Screen

1"""2Return the results of a highstate (or any other state function that returns3data in a compatible format) via an HTML email or HTML file.4.. versionadded:: 2017.7.05Similar results can be achieved by using the smtp returner with a custom template,6except an attempt at writing such a template for the complex data structure7returned by highstate function had proven to be a challenge, not to mention8that the smtp module doesn't support sending HTML mail at the moment.9The main goal of this returner was to produce an easy to read email similar10to the output of highstate outputter used by the CLI.11This returner could be very useful during scheduled executions,12but could also be useful for communicating the results of a manual execution.13Returner configuration is controlled in a standard fashion either via14highstate group or an alternatively named group.15.. code-block:: bash16 salt '*' state.highstate --return highstate17To use the alternative configuration, append '--return_config config-name'18.. code-block:: bash19 salt '*' state.highstate --return highstate --return_config simple20Here is an example of what the configuration might look like:21.. code-block:: yaml22 simple.highstate:23 report_failures: True24 report_changes: True25 report_everything: False26 failure_function: pillar.items27 success_function: pillar.items28 report_format: html29 report_delivery: smtp30 smtp_success_subject: 'success minion {id} on host {host}'31 smtp_failure_subject: 'failure minion {id} on host {host}'32 smtp_server: smtp.example.com33 smtp_recipients: saltusers@example.com, devops@example.com34 smtp_sender: salt@example.com35The *report_failures*, *report_changes*, and *report_everything* flags provide36filtering of the results. If you want an email to be sent every time, then37*report_everything* is your choice. If you want to be notified only when38changes were successfully made use *report_changes*. And *report_failures* will39generate an email if there were failures.40The configuration allows you to run a salt module function in case of41success (*success_function*) or failure (*failure_function*).42Any salt function, including ones defined in the _module folder of your salt43repo, could be used here and its output will be displayed under the 'extra'44heading of the email.45Supported values for *report_format* are html, json, and yaml. The latter two46are typically used for debugging purposes, but could be used for applying47a template at some later stage.48The values for *report_delivery* are smtp or file. In case of file delivery49the only other applicable option is *file_output*.50In case of smtp delivery, smtp_* options demonstrated by the example above51could be used to customize the email.52As you might have noticed, the success and failure subjects contain {id} and {host}53values. Any other grain name could be used. As opposed to using54{{grains['id']}}, which will be rendered by the master and contain master's55values at the time of pillar generation, these will contain minion values at56the time of execution.57"""58import html59import io60import logging61import smtplib62from email.mime.text import MIMEText63import salt.returners64import salt.utils.files65import salt.utils.json66import salt.utils.stringutils67import salt.utils.yaml68log = logging.getLogger(__name__)69__virtualname__ = "highstate"70def __virtual__():71 """72 Return our name73 """74 return __virtualname__75def _get_options(ret):76 """77 Return options78 """79 attrs = {80 "report_everything": "report_everything",81 "report_changes": "report_changes",82 "report_failures": "report_failures",83 "failure_function": "failure_function",84 "success_function": "success_function",85 "report_format": "report_format",86 "report_delivery": "report_delivery",87 "file_output": "file_output",88 "smtp_sender": "smtp_sender",89 "smtp_recipients": "smtp_recipients",90 "smtp_failure_subject": "smtp_failure_subject",91 "smtp_success_subject": "smtp_success_subject",92 "smtp_server": "smtp_server",93 }94 _options = salt.returners.get_returner_options(95 __virtualname__, ret, attrs, __salt__=__salt__, __opts__=__opts__96 )97 return _options98#99# Most email readers to not support <style> tag.100# The following dict and a function provide a primitive styler101# sufficient for our needs.102#103_STYLES = {104 "_table": "border-collapse:collapse;width:100%;",105 "_td": "vertical-align:top;"106 "font-family:Helvetica,Arial,sans-serif;font-size:9pt;",107 "unchanged": "color:blue;",108 "changed": "color:green",109 "failed": "color:red;",110 "first": "border-top:0;border-left:1px solid #9e9e9e;",111 "first_first": "border-top:0;border-left:0;",112 "notfirst_first": "border-left:0;border-top:1px solid #9e9e9e;",113 "other": "border-top:1px solid #9e9e9e;border-left:1px solid #9e9e9e;",114 "name": "width:70pt;",115 "container": "padding:0;",116}117def _lookup_style(element, names):118 """119 Lookup style by either element name or the list of classes120 """121 return _STYLES.get("_" + element, "") + "".join(122 [_STYLES.get(name, "") for name in names]123 )124def _generate_html_table(data, out, level=0, extra_style=""):125 """126 Generate a single table of data127 """128 print(129 '<table style="{}">'.format(_lookup_style("table", ["table" + str(level)])),130 file=out,131 )132 firstone = True133 row_style = "row" + str(level)134 cell_style = "cell" + str(level)135 for subdata in data:136 first_style = "first_first" if firstone else "notfirst_first"137 second_style = "first" if firstone else "other"138 if isinstance(subdata, dict):139 if "__style__" in subdata:140 new_extra_style = subdata["__style__"]141 del subdata["__style__"]142 else:143 new_extra_style = extra_style144 if len(subdata) == 1:145 name, value = next(iter(subdata.items()))146 print(147 '<tr style="{}">'.format(_lookup_style("tr", [row_style])),148 file=out,149 )150 print(151 '<td style="{}">{}</td>'.format(152 _lookup_style(153 "td", [cell_style, first_style, "name", new_extra_style]154 ),155 name,156 ),157 file=out,158 )159 if isinstance(value, list):160 print(161 '<td style="{}">'.format(162 _lookup_style(163 "td",164 [165 cell_style,166 second_style,167 "container",168 new_extra_style,169 ],170 )171 ),172 file=out,173 )174 _generate_html_table(value, out, level + 1, new_extra_style)175 print("</td>", file=out)176 else:177 print(178 '<td style="{}">{}</td>'.format(179 _lookup_style(180 "td",181 [cell_style, second_style, "value", new_extra_style],182 ),183 html.escape(str(value)),184 ),185 file=out,186 )187 print("</tr>", file=out)188 elif isinstance(subdata, list):189 print('<tr style="{}">'.format(_lookup_style("tr", [row_style])), file=out)190 print(191 '<td style="{}">'.format(192 _lookup_style(193 "td", [cell_style, first_style, "container", extra_style]194 )195 ),196 file=out,197 )198 _generate_html_table(subdata, out, level + 1, extra_style)199 print("</td>", file=out)200 print("</tr>", file=out)201 else:202 print('<tr style="{}">'.format(_lookup_style("tr", [row_style])), file=out)203 print(204 '<td style="{}">{}</td>'.format(205 _lookup_style(206 "td", [cell_style, first_style, "value", extra_style]207 ),208 html.escape(str(subdata)),209 ),210 file=out,211 )212 print("</tr>", file=out)213 firstone = False214 print("</table>", file=out)215def _generate_html(data, out):216 """217 Generate report data as HTML218 """219 print("<html>", file=out)220 print("<body>", file=out)221 _generate_html_table(data, out, 0)222 print("</body>", file=out)223 print("</html>", file=out)224def _dict_to_name_value(data):225 """226 Convert a dictionary to a list of dictionaries to facilitate ordering227 """228 if isinstance(data, dict):229 sorted_data = sorted(data.items(), key=lambda s: s[0])230 result = []231 for name, value in sorted_data:232 if isinstance(value, dict):233 result.append({name: _dict_to_name_value(value)})234 else:235 result.append({name: value})236 else:237 result = data238 return result239def _generate_states_report(sorted_data):240 """241 Generate states report242 """243 states = []244 for state, data in sorted_data:245 module, stateid, name, function = state.split("_|-")246 module_function = ".".join((module, function))247 result = data.get("result", "")248 single = [249 {"function": module_function},250 {"name": name},251 {"result": result},252 {"duration": data.get("duration", 0.0)},253 {"comment": data.get("comment", "")},254 ]255 if not result:256 style = "failed"257 else:258 changes = data.get("changes", {})259 if changes and isinstance(changes, dict):260 single.append({"changes": _dict_to_name_value(changes)})261 style = "changed"262 else:263 style = "unchanged"264 started = data.get("start_time", "")265 if started:266 single.append({"started": started})267 states.append({stateid: single, "__style__": style})268 return states269def _generate_report(ret, setup):270 """271 Generate report dictionary272 """273 retdata = ret.get("return", {})274 sorted_data = sorted(retdata.items(), key=lambda s: s[1].get("__run_num__", 0))275 total = 0276 failed = 0277 changed = 0278 duration = 0.0279 # gather stats280 for _, data in sorted_data:281 if not data.get("result", True):282 failed += 1283 total += 1284 try:285 duration += float(data.get("duration", 0.0))286 except ValueError:287 pass288 if data.get("changes", {}):289 changed += 1290 unchanged = total - failed - changed291 log.debug("highstate total: %s", total)292 log.debug("highstate failed: %s", failed)293 log.debug("highstate unchanged: %s", unchanged)294 log.debug("highstate changed: %s", changed)295 # generate report if required296 if (297 setup.get("report_everything", False)298 or (setup.get("report_changes", True) and changed != 0)299 or (setup.get("report_failures", True) and failed != 0)300 ):301 report = [302 {303 "stats": [304 {"total": total},305 {"failed": failed, "__style__": "failed"},306 {"unchanged": unchanged, "__style__": "unchanged"},307 {"changed": changed, "__style__": "changed"},308 {"duration": duration},309 ]310 },311 {312 "job": [313 {"function": ret.get("fun", "")},314 {"arguments": ret.get("fun_args", "")},315 {"jid": ret.get("jid", "")},316 {"success": ret.get("success", True)},317 {"retcode": ret.get("retcode", 0)},318 ]319 },320 {"states": _generate_states_report(sorted_data)},321 ]322 if failed:323 function = setup.get("failure_function", None)324 else:325 function = setup.get("success_function", None)326 if function:327 func_result = __salt__[function]()328 report.insert(0, {"extra": [{function: _dict_to_name_value(func_result)}]})329 else:330 report = []331 return report, failed332def _sprinkle(config_str):333 """334 Sprinkle with grains of salt, that is335 convert 'test {id} test {host} ' types of strings336 """337 parts = [x for sub in config_str.split("{") for x in sub.split("}")]338 for i in range(1, len(parts), 2):339 parts[i] = str(__grains__.get(parts[i], ""))340 return "".join(parts)341def _produce_output(report, failed, setup):342 """343 Produce output from the report dictionary generated by _generate_report344 """345 report_format = setup.get("report_format", "yaml")346 log.debug("highstate output format: %s", report_format)347 if report_format == "json":348 report_text = salt.utils.json.dumps(report)349 elif report_format == "yaml":350 string_file = io.StringIO()351 salt.utils.yaml.safe_dump(report, string_file, default_flow_style=False)352 string_file.seek(0)353 report_text = string_file.read()354 else:355 string_file = io.StringIO()356 _generate_html(report, string_file)357 string_file.seek(0)358 report_text = string_file.read()359 report_delivery = setup.get("report_delivery", "file")360 log.debug("highstate report_delivery: %s", report_delivery)361 if report_delivery == "file":362 output_file = _sprinkle(setup.get("file_output", "/tmp/test.rpt"))363 with salt.utils.files.fopen(output_file, "w") as out:364 out.write(salt.utils.stringutils.to_str(report_text))365 else:366 msg = MIMEText(report_text, report_format)367 sender = setup.get("smtp_sender", "")368 recipients = setup.get("smtp_recipients", "")369 if failed:370 subject = setup.get("smtp_failure_subject", "Installation failure")371 else:372 subject = setup.get("smtp_success_subject", "Installation success")373 subject = _sprinkle(subject)374 msg["Subject"] = subject375 msg["From"] = sender376 msg["To"] = recipients377 smtp = smtplib.SMTP(host=setup.get("smtp_server", ""))378 smtp.sendmail(379 sender, [x.strip() for x in recipients.split(",")], msg.as_string()380 )381 smtp.quit()382def returner(ret):383 """384 Check highstate return information and possibly fire off an email385 or save a file.386 """387 setup = _get_options(ret)388 log.debug("highstate setup %s", setup)389 report, failed = _generate_report(ret, setup)390 if report:391 _produce_output(report, failed, setup)392def __test_html():393 """394 HTML generation test only used when called from the command line:395 python ./highstate.py396 Typical options for generating the report file:397 highstate:398 report_format: yaml399 report_delivery: file400 file_output: '/srv/salt/_returners/test.rpt'401 """402 with salt.utils.files.fopen("test.rpt", "r") as input_file:403 data_text = salt.utils.stringutils.to_unicode(input_file.read())404 data = salt.utils.yaml.safe_load(data_text)405 string_file = io.StringIO()406 _generate_html(data, string_file)407 string_file.seek(0)408 result = string_file.read()409 with salt.utils.files.fopen("test.html", "w") as output:410 output.write(salt.utils.stringutils.to_str(result))411if __name__ == "__main__":...

Full Screen

Full Screen

finder.py

Source:finder.py Github

copy

Full Screen

...85 content += list(map(str.rstrip, handle.readlines()))86 filtered = list(filter(lambda x: x not in content, doms))87 print('size after the remove:', len(filtered))88 return filtered89def report_failures(d):90 with open('doms_failed.txt', 'a') as h_failed:91 h_failed.write(d + '\n')92def main():93 # d = tri_part_dom()94 # d = three_letter_dom()95 # d = four_letter_dom()96 d = cvcvc_dom()97 domains = eliminate_prior_domains(d)98 # 4. Check list of domains and write to file99 for domain in domains:100 sleep(0.5) # Too many requests lead to incorrect responses101 print(' Checking: ' + domain), # Comma means no newline is printed102 try:103 w = whois.whois(domain)104 print('\tTAKEN')105 with open('doms_checked.txt', 'a') as h_checked:106 h_checked.write(domain + '\n')107 except whois.parser.PywhoisError:108 # Exception means that the domain is free109 print('\tFREE')110 with open('doms_free.txt', 'a') as h_free:111 h_free.write(domain + '\n')112 except ConnectionResetError:113 print('~>~>~> Connection reset (ConnectionResetError) error has been thrown')114 report_failures(domain)115 except socket.timeout:116 print('|>|>|> Time out (socket.timeout) error has been thrown')117 report_failures(domain)118 except ConnectionRefusedError:119 print('+>+>+> Connection refused (ConnectionRefusedError) error has been thrown')120 report_failures(domain)121 except:122 print('Unexpected error has been thrown')123 report_failures(domain)124 print("DONE!")125if __name__ == '__main__':...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Testify automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful