Exemplo n.º 1
0
def test_log_exception_and_exit(capfd, caplog):
    # GIVEN the expected output to logging
    expected_log_start = "Unhandled exception:"
    eel = len(expected_log_start)
    # WHEN a ops_utils.Fatal is raised and
    #      the exception is logged via ops_utils.log_exception_and_exit and
    #      the output is captured
    with pytest.raises(SystemExit) as e:
        try:
            raise ops_utils.Fatal("test_log_exception_and_exit", 3)
        except:
            ops_utils.log_exception_and_exit()
    out, err = capfd.readouterr()
    # THEN a SystemExit exception should be raised and
    #      the exit status should be 2 and
    #      there should be nothing written to stderr and
    #      there should be a single log message and
    #      the log level of the message should be ERROR and
    #      the beginning of log message should match the expected text
    assert int(str(e.value)) == 1
    assert err == ""
    assert len(caplog.records) == 1
    assert caplog.records[0].levelno == logging.ERROR
    assert caplog.records[0].message[0:eel] == expected_log_start
Exemplo n.º 2
0
def test_log_exception_and_exit(capfd, caplog):
    # GIVEN the expected output to logging
    expected_log_start = "Unhandled exception:"
    eel = len(expected_log_start)
    # WHEN a ops_utils.Fatal is raised and
    #      the exception is logged via ops_utils.log_exception_and_exit and
    #      the output is captured
    with pytest.raises(SystemExit) as e:
        try:
            raise ops_utils.Fatal("test_log_exception_and_exit", 3)
        except:
            ops_utils.log_exception_and_exit()
    out, err = capfd.readouterr()
    # THEN a SystemExit exception should be raised and
    #      the exit status should be 2 and
    #      there should be nothing written to stderr and
    #      there should be a single log message and
    #      the log level of the message should be ERROR and
    #      the beginning of log message should match the expected text
    assert int(str(e.value)) == 1
    assert err == ""
    assert len(caplog.records) == 1
    assert caplog.records[0].levelno == logging.ERROR
    assert caplog.records[0].message[0:eel] == expected_log_start
    """
    add_args = {"config": True, "EMAIL": True, "dryrun": True,
                "verbosity": True}
    cap = ops_config.OpsConfigArgParse(description=__doc__, add_args=add_args)
    logger = ops_logging.OpScriptsLogging(cap.prog)
    args = ops_config.parse_args(cap)
    logger.set_log_level(args.verbosity)
    logger.dryrun(args.dryrun)
    return args


def main():
    args = setup()
    subject = "Test - disregard"
    body = "This is a test message"
    message = ops_notify_email.Message(args.program_name, subject, body)
    message.send(args.email_from, args.email_to, dryrun=args.dryrun)


if __name__ == "__main__":
    try:
        main()
    except SystemExit as e:
        sys.exit(e.code)
    except KeyboardInterrupt:
        ops_utils.log_ctrlc_and_exit()
    except ops_utils.Fatal:
        ops_utils.log_fatal_and_exit()
    except:
        ops_utils.log_exception_and_exit()
Exemplo n.º 4
0
        "dryrun": True,
        "verbosity": True
    }
    cap = ops_config.OpsConfigArgParse(description=__doc__, add_args=add_args)
    logger = ops_logging.OpScriptsLogging(cap.prog)
    args = ops_config.parse_args(cap)
    logger.set_log_level(args.verbosity)
    logger.dryrun(args.dryrun)
    return args


def main():
    args = setup()
    subject = "Test - disregard"
    body = "This is a test message"
    message = ops_notify_email.Message(args.program_name, subject, body)
    message.send(args.email_from, args.email_to, dryrun=args.dryrun)


if __name__ == "__main__":
    try:
        main()
    except SystemExit as e:
        sys.exit(e.code)
    except KeyboardInterrupt:
        ops_utils.log_ctrlc_and_exit()
    except ops_utils.Fatal:
        ops_utils.log_fatal_and_exit()
    except:
        ops_utils.log_exception_and_exit()