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 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