def test_send_result_email(test_name, job_id, report_name, report_title, expected_title): body_in = "<body><h1>hello 😆 😉 😊 😋 😎</h1></body>" result = NotebookResultComplete( job_id=job_id, job_start_time=datetime.datetime.now(), job_finish_time=datetime.datetime.now(), raw_html_resources={}, raw_ipynb_json={}, raw_html=body_in, pdf="", report_name=report_name, report_title=report_title, ) to_email = "∫åññîsté®@test-email.com" with mock.patch("notebooker.utils.notebook_execution.mail") as mail: notebook_execution.send_result_email(result, to_email) email_sent = mail.mock_calls[0][1] assert len(email_sent ) == 4, "mail() was not called with the correct number of args" from_address = email_sent[0] to_address = email_sent[1] title = email_sent[2] body = email_sent[3] assert from_address == "*****@*****.**" assert to_address == to_email assert title == expected_title assert body == [ "Please either activate HTML emails, or see the PDF attachment.", body_in ]
def test_send_result_email_fallback_to_raw_html(): body_in = "<body><h1>hello 😆 😉 😊 😋 😎</h1></body>" to_email = "hello@you" result = NotebookResultComplete( job_id="fallback", job_start_time=datetime.datetime.now(), job_finish_time=datetime.datetime.now(), mailto=to_email, raw_html_resources={}, raw_ipynb_json={}, raw_html=body_in, pdf="", report_name="fallback", report_title="fallback", ) with mock.patch("notebooker.utils.notebook_execution.mail") as mail: notebook_execution.send_result_email(result) email_sent = mail.mock_calls[0][1] assert len(email_sent ) == 4, "mail() was not called with the correct number of args" from_address = email_sent[0] to_address = email_sent[1] title = email_sent[2] body = email_sent[3] assert from_address == "*****@*****.**" assert to_address == to_email assert title == "Notebooker: fallback report completed with status: Checks done!" assert body == [ "Please either activate HTML emails, or see the PDF attachment.", body_in ]
def main( report_name, overrides_as_json, iterate_override_values_of, report_title, n_retries, mongo_db_name, mongo_host, mongo_user, mongo_password, result_collection_name, notebook_kernel_name, job_id, output_base_dir, template_base_dir, mailto, pdf_output, serializer_cls, prepare_notebook_only, ): if report_name is None: raise ValueError("Error! Please provide a --report-name.") mongo_db_name = mongo_db_name or os.environ.get("DATABASE_NAME", "notebooker") mongo_host = mongo_host or os.environ.get("MONGO_HOST", "research") mongo_user = mongo_user if mongo_user is not None else os.environ.get( "MONGO_USER") if mongo_user is None: os.environ.pop("MONGO_USER", None) else: os.environ["MONGO_USER"] = mongo_user mongo_password = mongo_password if mongo_password is not None else os.environ.get( "MONGO_PASSWORD") if mongo_password is None: os.environ.pop("MONGO_PASSWORD", None) else: os.environ[ "MONGO_PASSWORD"] = mongo_password # FIXME: rather insecure.. result_collection_name = result_collection_name or os.environ.get( "RESULT_COLLECTION_NAME", "NOTEBOOK_OUTPUT") if notebook_kernel_name: os.environ["NOTEBOOK_KERNEL_NAME"] = notebook_kernel_name report_title = report_title or report_name output_dir, template_dir, _ = initialise_base_dirs( output_dir=output_base_dir, template_dir=template_base_dir) all_overrides = _get_overrides(overrides_as_json, iterate_override_values_of) start_time = datetime.datetime.now() logger.info("Running a report with these parameters:") logger.info("report_name = %s", report_name) logger.info("overrides_as_json = %s", overrides_as_json) logger.info("iterate_override_values_of = %s", iterate_override_values_of) logger.info("report_title = %s", report_title) logger.info("n_retries = %s", n_retries) logger.info("mongo_db_name = %s", mongo_db_name) logger.info("mongo_host = %s", mongo_host) logger.info("mongo_user = %s", mongo_user) logger.info("mongo_password = %s", "*******") logger.info("result_collection_name = %s", result_collection_name) logger.info("job_id = %s", job_id) logger.info("output_dir = %s", output_dir) logger.info("template_dir = %s", template_dir) logger.info("mailto = %s", mailto) logger.info("pdf_output = %s", pdf_output) logger.info("prepare_notebook_only = %s", prepare_notebook_only) logger.info("Calculated overrides are: %s", str(all_overrides)) result_serializer = get_serializer_from_cls( serializer_cls, database_name=mongo_db_name, mongo_host=mongo_host, result_collection_name=result_collection_name, user=mongo_user, password=mongo_password, ) results = [] for overrides in all_overrides: result = run_report( start_time, report_name, overrides, result_serializer, report_title=report_title, job_id=job_id, output_base_dir=output_dir, template_base_dir=template_dir, attempts_remaining=n_retries - 1, mailto=mailto, generate_pdf_output=pdf_output, prepare_only=prepare_notebook_only, ) if mailto: send_result_email(result, mailto) if isinstance(result, NotebookResultError): logger.warning("Notebook execution failed! Output was:") logger.warning(repr(result)) raise Exception(result.error_info) results.append(result) return results
def execute_notebook_entrypoint( config: BaseConfig, report_name: str, overrides_as_json: str, iterate_override_values_of: Union[List[str], str], report_title: str, n_retries: int, job_id: str, mailto: str, error_mailto: str, email_subject: str, pdf_output: bool, hide_code: bool, prepare_notebook_only: bool, ): report_title = report_title or report_name output_dir, template_dir, _ = initialise_base_dirs( output_dir=config.OUTPUT_DIR, template_dir=config.TEMPLATE_DIR) all_overrides = _get_overrides(overrides_as_json, iterate_override_values_of) notebooker_disable_git = config.NOTEBOOKER_DISABLE_GIT py_template_base_dir = config.PY_TEMPLATE_BASE_DIR py_template_subdir = config.PY_TEMPLATE_SUBDIR start_time = datetime.datetime.now() logger.info("Running a report with these parameters:") logger.info("report_name = %s", report_name) logger.info("overrides_as_json = %s", overrides_as_json) logger.info("iterate_override_values_of = %s", iterate_override_values_of) logger.info("report_title = %s", report_title) logger.info("n_retries = %s", n_retries) logger.info("job_id = %s", job_id) logger.info("output_dir = %s", output_dir) logger.info("template_dir = %s", template_dir) logger.info("mailto = %s", mailto) logger.info("error_mailto = %s", error_mailto) logger.info("email_subject = %s", email_subject) logger.info("pdf_output = %s", pdf_output) logger.info("hide_code = %s", hide_code) logger.info("prepare_notebook_only = %s", prepare_notebook_only) logger.info("notebooker_disable_git = %s", notebooker_disable_git) logger.info("py_template_base_dir = %s", py_template_base_dir) logger.info("py_template_subdir = %s", py_template_subdir) logger.info("serializer_cls = %s", config.SERIALIZER_CLS) logger.info("serializer_config = %s", config.SERIALIZER_CONFIG) logger.info("Calculated overrides are: %s", str(all_overrides)) result_serializer = get_serializer_from_cls(config.SERIALIZER_CLS, **config.SERIALIZER_CONFIG) results = [] for overrides in all_overrides: result = run_report( start_time, report_name, overrides, result_serializer, report_title=report_title, job_id=job_id, output_base_dir=output_dir, template_base_dir=template_dir, attempts_remaining=n_retries - 1, mailto=mailto, error_mailto=error_mailto, email_subject=email_subject, generate_pdf_output=pdf_output, hide_code=hide_code, prepare_only=prepare_notebook_only, notebooker_disable_git=notebooker_disable_git, py_template_base_dir=py_template_base_dir, py_template_subdir=py_template_subdir, ) if result.mailto: send_result_email(result) if isinstance(result, NotebookResultError): logger.warning("Notebook execution failed! Output was:") logger.warning(repr(result)) raise Exception(result.error_info) results.append(result) return results