def test_parse_should_succeed(self, line, parsed_dict): foo = None try: p = DnstestParser() foo = p.parse_line(line) except ParseException: # assert will fail, no need to do anything here pass assert foo == parsed_dict
def test_get_grammar(self): p = DnstestParser() expected = ['add (record|name|entry)? <hostname_or_fqdn> (with ?)(value|address|target)? <hostname_fqdn_or_ip>', 'remove (record|name|entry)? <hostname_or_fqdn>', 'rename (record|name|entry)? <hostname_or_fqdn> (with ?)(value ?) <value> to <hostname_or_fqdn>', 'change (record|name|entry)? <hostname_or_fqdn> to <hostname_fqdn_or_ip>', 'confirm (record|name|entry)? <hostname_or_fqdn>', ] result = p.get_grammar() assert result == expected
def test_get_grammar(self): p = DnstestParser() expected = [ 'add (record|name|entry)? <hostname_or_fqdn> (with ?)(value|address|target)? <hostname_fqdn_or_ip>', 'remove (record|name|entry)? <hostname_or_fqdn>', 'rename (record|name|entry)? <hostname_or_fqdn> (with ?)(value ?) <value> to <hostname_or_fqdn>', 'change (record|name|entry)? <hostname_or_fqdn> to <hostname_fqdn_or_ip>', 'confirm (record|name|entry)? <hostname_or_fqdn>', ] result = p.get_grammar() assert result == expected
def parse_opts(): """ Runs OptionParser and calls main() with the resulting options. """ usage = "%prog [-h|--help] [--version] [-c|--config path_to_config] [-f|--file path_to_test_file] [-V|--verify]" usage += "\n\npydnstest %s - <https://github.com/jantman/pydnstest/>" % VERSION usage += "\nlicensed under the GNU Affero General Public License - see LICENSE.txt" usage += "\nGrammar:\n\n" parser = DnstestParser() for s in parser.get_grammar(): usage += "{s}\n".format(s=s) p = optparse.OptionParser(usage=usage, version="pydnstest %s" % VERSION) p.add_option('-c', '--config', dest='config_file', help='path to config file (default looks for ./dnstest.ini or ~/.dnstest.ini)') p.add_option('-f', '--file', dest='testfile', help='path to file listing tests (default reads from STDIN)') p.add_option('-V', '--verify', dest='verify', default=False, action='store_true', help='verify changes against PROD server once they\'re live (default False)') p.add_option('-s', '--sleep', dest='sleep', action='store', type='float', help='optionally, a decimal number of seconds to sleep between queries') p.add_option('-t', '--ignore-ttl', dest='ignorettl', default=False, action='store_true', help='when comparing responses, ignore the TTL value') p.add_option('--example-config', dest='exampleconf', default=False, action='store_true', help='print an example configuration file and exit') p.add_option('--configprint', dest='configprint', default=False, action='store_true', help='print the current configuration and exit') p.add_option('--promptconfig', dest='promptconfig', default=False, action='store_true', help='interactively build a configuration file through a series of prompts') options, args = p.parse_args() main(options)
def setup_parser_return_unknown_op(self): """ Sets up test environment for tests of check methods, including redefining resolve_name and lookup_reverse to the appropriate methods in this class """ config = DnstestConfig() config.server_test = "test" config.server_prod = "prod" config.default_domain = ".example.com" config.have_reverse_dns = True parser = DnstestParser() # mock the parser function to just return None parser.parse_line = self.parser_return_unknown_op pydnstest.parser = parser chk = DNStestChecks(config) # stub chk.DNS.resolve_name = self.stub_resolve_name # stub chk.DNS.lookup_reverse = self.stub_lookup_reverse return (parser, chk)
def setup_verifies(self): """ Sets up test environment for tests of verify methods, including redefining resolve_name and lookup_reverse to the appropriate methods in this class """ config = DnstestConfig() config.server_test = "test" config.server_prod = "prod" config.default_domain = ".example.com" config.have_reverse_dns = True parser = DnstestParser() pydnstest.parser = parser chk = DNStestChecks(config) # stub chk.DNS.resolve_name = self.stub_resolve_name_verify # stub chk.DNS.lookup_reverse = self.stub_lookup_reverse_verify return (parser, chk)
def setup_checks(self): global config global chk global parser config = DnstestConfig() config.server_test = "test_server_stub" config.server_prod = "prod_server_stub" config.default_domain = ".example.com" config.have_reverse_dns = True pydnstest.config = config parser = DnstestParser() pydnstest.parser = parser chk = DNStestChecks(config) # stub chk.DNS.resolve_name = self.stub_resolve_name # stub chk.DNS.lookup_reverse = self.stub_lookup_reverse pydnstest.chk = chk return (parser, chk)
def test_parse_should_raise_exception(self, line): with pytest.raises(ParseException): p = DnstestParser() p.parse_line(line)
def parse_opts(): """ Runs OptionParser and calls main() with the resulting options. """ usage = "%prog [-h|--help] [--version] [-c|--config path_to_config] [-f|--file path_to_test_file] [-V|--verify]" usage += "\n\npydnstest %s - <https://github.com/jantman/pydnstest/>" % VERSION usage += "\nlicensed under the GNU Affero General Public License - see LICENSE.txt" usage += "\nGrammar:\n\n" parser = DnstestParser() for s in parser.get_grammar(): usage += "{s}\n".format(s=s) p = optparse.OptionParser(usage=usage, version="pydnstest %s" % VERSION) p.add_option( '-c', '--config', dest='config_file', help= 'path to config file (default looks for ./dnstest.ini or ~/.dnstest.ini)' ) p.add_option('-f', '--file', dest='testfile', help='path to file listing tests (default reads from STDIN)') p.add_option( '-V', '--verify', dest='verify', default=False, action='store_true', help= 'verify changes against PROD server once they\'re live (default False)' ) p.add_option( '-s', '--sleep', dest='sleep', action='store', type='float', help='optionally, a decimal number of seconds to sleep between queries' ) p.add_option('-t', '--ignore-ttl', dest='ignorettl', default=False, action='store_true', help='when comparing responses, ignore the TTL value') p.add_option('--example-config', dest='exampleconf', default=False, action='store_true', help='print an example configuration file and exit') p.add_option('--configprint', dest='configprint', default=False, action='store_true', help='print the current configuration and exit') p.add_option( '--promptconfig', dest='promptconfig', default=False, action='store_true', help= 'interactively build a configuration file through a series of prompts') options, args = p.parse_args() main(options)
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()