Exemple #1
0
    def test_promptconfig_no_confirm(self, capfd):
        dc = DnstestConfig()
        dc.conf_file = '/foo/bar/baz'

        def prompt_input_se(prompt, default=None, validate_cb=None):
            ret = {
                "Production DNS Server IP": '1.2.3.4',
                "Test/Staging DNS Server IP": '5.6.7.8',
                "Check for reverse DNS by default? [Y|n]": True,
                "Default domain for to append to any input that appears to be less than a FQDN (blank for none)": 'example.com',
                "Ignore difference in TTL when comparing responses? [y|N]": False,
                "Sleep between DNS record tests (seconds)": 0.0,
            }
            return ret[prompt]
        prompt_input_mock = mock.MagicMock(side_effect=prompt_input_se)

        confirm_response_mock = mock.MagicMock()
        confirm_response_mock.return_value = False

        to_string_mock = mock.MagicMock()
        to_string_mock.return_value = 'foo bar baz'

        write_mock = mock.MagicMock()
        write_mock.return_value = True

        with mock.patch('pydnstest.config.DnstestConfig.prompt_input', prompt_input_mock):
            with mock.patch('pydnstest.config.DnstestConfig.to_string', to_string_mock):
                with mock.patch('pydnstest.config.DnstestConfig.confirm_response', confirm_response_mock):
                    with mock.patch('pydnstest.config.DnstestConfig.write', write_mock):
                        with pytest.raises(SystemExit) as excinfo:
                            dc.prompt_config()
        assert excinfo.value.code == "Exiting on user request. No configuration written."
        assert prompt_input_mock.call_count == 6
        assert prompt_input_mock.call_args_list == [
            mock.call("Production DNS Server IP", validate_cb=dc.validate_ipaddr),
            mock.call("Test/Staging DNS Server IP", validate_cb=dc.validate_ipaddr),
            mock.call("Check for reverse DNS by default? [Y|n]", default=True, validate_cb=dc.validate_bool),
            mock.call("Default domain for to append to any input that appears to be less than a FQDN (blank for none)", default=''),
            mock.call("Ignore difference in TTL when comparing responses? [y|N]", default=False, validate_cb=dc.validate_bool),
            mock.call("Sleep between DNS record tests (seconds)", default=0.0, validate_cb=dc.validate_float),
        ]
        assert to_string_mock.call_count == 1
        assert confirm_response_mock.call_count == 1
        assert write_mock.call_count == 0
        out, err = capfd.readouterr()
        assert out == "Configuration:\n#####################\nfoo bar baz\n#####################\n\n"
        assert err == ""
        assert dc.server_prod == '1.2.3.4'
        assert dc.server_test == '5.6.7.8'
        assert dc.have_reverse_dns == True
        assert dc.default_domain == 'example.com'
        assert dc.ignore_ttl == False
        assert dc.sleep == 0.0
Exemple #2
0
def main(options):
    """
    main function - does everything...

    split this out this way for testing...p
    """
    # read in config, set variable
    config = DnstestConfig()
    if options.exampleconf:
        config.set_example_values()
        print(config.to_string())
        raise SystemExit(0)

    if options.promptconfig:
        # interactively build a configuration file
        config.prompt_config()
        raise SystemExit(0)

    if options.config_file:
        conf_file = options.config_file
    else:
        conf_file = config.find_config_file()
    if conf_file is None:
        print(
            "ERROR: no configuration file found. Run with --promptconfig to build one interactively, or --example-config for an example."
        )
        raise SystemExit(1)
    config.load_config(conf_file)

    if options.ignorettl:
        config.ignore_ttl = True

    if options.configprint:
        print("# {fname}".format(fname=config.conf_file))
        print(config.to_string())
        raise SystemExit(0)

    parser = DnstestParser()
    chk = DNStestChecks(config)

    if options.sleep:
        config.sleep = options.sleep
        print("Note - will sleep %g seconds between lines" % options.sleep)

    # if no other options, read from stdin
    if options.testfile:
        if not os.path.exists(options.testfile):
            print("ERROR: test file '%s' does not exist." % options.testfile)
            raise SystemExit(1)
        fh = open(options.testfile, 'r')
    else:
        # read from stdin
        sys.stderr.write(
            "WARNING: reading from STDIN. Run with '-f filename' to read tests from a file.\n"
        )
        fh = sys.stdin

    # read input line by line, handle each line as we're given it
    passed = 0
    failed = 0
    for line in fh:
        line = line.strip()
        if not line:
            continue
        if line[:1] == "#":
            continue
        if options.verify:
            r = run_verify_line(line, parser, chk)
        else:
            r = run_check_line(line, parser, chk)
        if r is False:
            continue
        elif r['result']:
            passed = passed + 1
        else:
            failed = failed + 1
        format_test_output(r)
        if config.sleep is not None and config.sleep > 0.0:
            sleep(config.sleep)

    msg = ""
    if failed == 0:
        msg = "All %d tests passed. (pydnstest %s)" % (passed, VERSION)
    else:
        msg = "%d passed / %d FAILED. (pydnstest %s)" % (passed, failed,
                                                         VERSION)
    print("++++ %s" % msg)

    if options.testfile:
        # we were reading a file, close it
        fh.close()
Exemple #3
0
def main(options):
    """
    main function - does everything...

    split this out this way for testing...p
    """
    # read in config, set variable
    config = DnstestConfig()
    if options.exampleconf:
        config.set_example_values()
        print(config.to_string())
        raise SystemExit(0)

    if options.promptconfig:
        # interactively build a configuration file
        config.prompt_config()
        raise SystemExit(0)

    if options.config_file:
        conf_file = options.config_file
    else:
        conf_file = config.find_config_file()
    if conf_file is None:
        print("ERROR: no configuration file found. Run with --promptconfig to build one interactively, or --example-config for an example.")
        raise SystemExit(1)
    config.load_config(conf_file)

    if options.ignorettl:
        config.ignore_ttl = True

    if options.configprint:
        print("# {fname}".format(fname=config.conf_file))
        print(config.to_string())
        raise SystemExit(0)

    parser = DnstestParser()
    chk = DNStestChecks(config)

    if options.sleep:
        config.sleep = options.sleep
        print("Note - will sleep %g seconds between lines" % options.sleep)

    # if no other options, read from stdin
    if options.testfile:
        if not os.path.exists(options.testfile):
            print("ERROR: test file '%s' does not exist." % options.testfile)
            raise SystemExit(1)
        fh = open(options.testfile, 'r')
    else:
        # read from stdin
        sys.stderr.write("WARNING: reading from STDIN. Run with '-f filename' to read tests from a file.\n")
        fh = sys.stdin

    # read input line by line, handle each line as we're given it
    passed = 0
    failed = 0
    for line in fh:
        line = line.strip()
        if not line:
            continue
        if line[:1] == "#":
            continue
        if options.verify:
            r = run_verify_line(line, parser, chk)
        else:
            r = run_check_line(line, parser, chk)
        if r is False:
            continue
        elif r['result']:
            passed = passed + 1
        else:
            failed = failed + 1
        format_test_output(r)
        if config.sleep is not None and config.sleep > 0.0:
            sleep(config.sleep)

    msg = ""
    if failed == 0:
        msg = "All %d tests passed. (pydnstest %s)" % (passed, VERSION)
    else:
        msg = "%d passed / %d FAILED. (pydnstest %s)" % (passed, failed, VERSION)
    print("++++ %s" % msg)

    if options.testfile:
        # we were reading a file, close it
        fh.close()
Exemple #4
0
    def test_promptconfig_no_confirm(self, capfd):
        dc = DnstestConfig()
        dc.conf_file = '/foo/bar/baz'

        def prompt_input_se(prompt, default=None, validate_cb=None):
            ret = {
                "Production DNS Server IP":
                '1.2.3.4',
                "Test/Staging DNS Server IP":
                '5.6.7.8',
                "Check for reverse DNS by default? [Y|n]":
                True,
                "Default domain for to append to any input that appears to be less than a FQDN (blank for none)":
                'example.com',
                "Ignore difference in TTL when comparing responses? [y|N]":
                False,
                "Sleep between DNS record tests (seconds)":
                0.0,
            }
            return ret[prompt]

        prompt_input_mock = mock.MagicMock(side_effect=prompt_input_se)

        confirm_response_mock = mock.MagicMock()
        confirm_response_mock.return_value = False

        to_string_mock = mock.MagicMock()
        to_string_mock.return_value = 'foo bar baz'

        write_mock = mock.MagicMock()
        write_mock.return_value = True

        with mock.patch('pydnstest.config.DnstestConfig.prompt_input',
                        prompt_input_mock):
            with mock.patch('pydnstest.config.DnstestConfig.to_string',
                            to_string_mock):
                with mock.patch(
                        'pydnstest.config.DnstestConfig.confirm_response',
                        confirm_response_mock):
                    with mock.patch('pydnstest.config.DnstestConfig.write',
                                    write_mock):
                        with pytest.raises(SystemExit) as excinfo:
                            dc.prompt_config()
        assert excinfo.value.code == "Exiting on user request. No configuration written."
        assert prompt_input_mock.call_count == 6
        assert prompt_input_mock.call_args_list == [
            mock.call("Production DNS Server IP",
                      validate_cb=dc.validate_ipaddr),
            mock.call("Test/Staging DNS Server IP",
                      validate_cb=dc.validate_ipaddr),
            mock.call("Check for reverse DNS by default? [Y|n]",
                      default=True,
                      validate_cb=dc.validate_bool),
            mock.call(
                "Default domain for to append to any input that appears to be less than a FQDN (blank for none)",
                default=''),
            mock.call(
                "Ignore difference in TTL when comparing responses? [y|N]",
                default=False,
                validate_cb=dc.validate_bool),
            mock.call("Sleep between DNS record tests (seconds)",
                      default=0.0,
                      validate_cb=dc.validate_float),
        ]
        assert to_string_mock.call_count == 1
        assert confirm_response_mock.call_count == 1
        assert write_mock.call_count == 0
        out, err = capfd.readouterr()
        assert out == "Configuration:\n#####################\nfoo bar baz\n#####################\n\n"
        assert err == ""
        assert dc.server_prod == '1.2.3.4'
        assert dc.server_test == '5.6.7.8'
        assert dc.have_reverse_dns == True
        assert dc.default_domain == 'example.com'
        assert dc.ignore_ttl == False
        assert dc.sleep == 0.0