def testTarDnsBindFiles(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) # No DNS tree yet self.assertRaises(config_lib.ExporterNoFileError, config_lib_instance.TarDnsBindFiles, 'localhost') tree_exporter_instance.ExportAllBindTrees() audit_id, filename = config_lib_instance.FindNewestDnsTreeFilename() config_lib_instance.UnTarDnsTree(audit_id) # No such DNS server self.assertRaises(config_lib.ServerCheckError, config_lib_instance.TarDnsBindFiles, 'bad') # Invalid tree format f = open('%s/localhost/named/bad.txt' % self.root_config_dir, 'w') f.close() self.assertRaises(config_lib.ExporterListFileError, config_lib_instance.TarDnsBindFiles, 'localhost') os.remove('%s/localhost/named/bad.txt' % self.root_config_dir) # Success config_lib_instance.TarDnsBindFiles('localhost') self.assertTrue(os.path.exists('%s/localhost/localhost.tar.bz2' % self.root_config_dir)) os.mkdir('%s/test/' % self.root_config_dir) shutil.move('%s/localhost/localhost.tar.bz2' % self.root_config_dir, '%s/test/localhost.tar.bz2' % self.root_config_dir) tar_file = tarfile.open('%s/test/localhost.tar.bz2' % self.root_config_dir, 'r:bz2') tarred_files = tar_file.getnames() for tarred in tarred_files: self.assertTrue(os.path.exists('%s/localhost/%s' % (self.root_config_dir, tarred)))
def testUnTarInCorrectLocation(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() config_lib_instance.UnTarDnsTree() self.assertTrue(os.path.exists('%s/localhost' % self.root_config_dir)) config_lib_instance.backup_dir = '%s/%s' % (os.getcwd(), config_lib_instance.backup_dir) config_lib_instance.root_config_dir = 'config_test_dir/config' os.chdir('test_data') config_lib_instance.UnTarDnsTree() os.chdir('../') self.assertTrue(os.path.exists('test_data/config_test_dir/config/localhost')) self.assertTrue(os.path.exists('test_data/config_test_dir/config/localhost/named/named.ca')) shutil.rmtree('test_data/config_test_dir') config_lib_instance.root_config_dir = '../root_config_dir' os.chdir('test_data/named') config_lib_instance.UnTarDnsTree() os.chdir('../../') self.assertTrue(os.path.exists('test_data/root_config_dir/localhost')) shutil.rmtree('test_data/root_config_dir') config_lib_instance.root_config_dir = '%s/test_data/root_config_dir_abs' % os.getcwd() config_lib_instance.UnTarDnsTree() self.assertTrue(os.path.exists('test_data/root_config_dir_abs/localhost')) shutil.rmtree('test_data/root_config_dir_abs')
def testTarringSlaveZone(self): for zone in self.core_instance.ListZones(): self.core_instance.RemoveZone(zone) self.core_instance.MakeDnsServer(u'server1', u'some_user', u'/bind/dir/', u'/test/dir/') self.core_instance.MakeDnsServerSet(u'server_set1') self.core_instance.MakeDnsServerSetAssignments(u'server1', u'server_set1') self.core_instance.MakeDnsServerSetViewAssignments(u'external', 1, u'server_set1') self.core_instance.MakeZone(u'test_zone', u'slave', u'test_zone.', view_name=u'external', make_any=False) self.core_instance.MakeNamedConfGlobalOption(u'server_set1', u'recursion no;') tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) config_lib_instance.UnTarDnsTree() #Make sure we exported the empty 'external' folder self.assertTrue(os.path.exists(os.path.join(self.root_config_dir, 'server1/named/external'))) config_lib_instance.TarDnsBindFiles(u'server1') try: untarred_path = os.path.join(self.root_config_dir, 'server1_untarred') tar_file = tarfile.open( os.path.join(self.root_config_dir, 'server1', 'server1.tar.bz2'), 'r:bz2') tar_file.extractall(path=untarred_path) #Make sure we tarred the empty 'external' folder self.assertTrue(os.path.exists(os.path.join(untarred_path, 'named/external'))) finally: tar_file.close()
def testGetNamedZoneToolArgs(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() config_lib_instance.UnTarDnsTree() self.assertEqual(config_lib_instance.GetNamedZoneToolArgs('localhost', 'external', 'forward_zone.db'), '-i full-sibling -S warn -W warn -k warn -m warn -r warn -M warn')
def testGetDnsServerInfo(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() audit_id, filename = config_lib_instance.FindNewestDnsTreeFilename() config_lib_instance.UnTarDnsTree(audit_id) # This is temporary until DNS tree exporter is committed, at which it will be unnecessary. if( os.path.exists('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) ): os.remove('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) if( os.path.exists('%s/localhost/named/localhost_config' % self.root_config_dir) ): os.remove('%s/localhost/named/localhost_config' % self.root_config_dir) # End of needing to remove # No .info file #self.assertRaises(config_lib.ServerCheckError, config_lib_instance.GetDnsServerInfo, 'localhost') # Invalid .info file invalid_file = open('%s/localhost/localhost.info' % self.root_config_dir, 'w') invalid_file.write('[tools]') invalid_file.close() self.assertRaises(config_lib.ServerCheckError, config_lib_instance.GetDnsServerInfo, 'localhost') server1_information = {'server_info': { 'server_name': 'localhost', 'server_user': SSH_USER, 'ssh_host': '%s@localhost:22' % SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}, 'tools': {}} config_lib_instance.WriteDnsServerInfo(server1_information) server2_information = {'server_info': { 'server_name': '255.254.253.252', 'server_user': SSH_USER, 'ssh_host': '%[email protected]:22' % SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}, 'tools': { 'named-compilezone': True, 'named-checkzone': True, 'named-checkconf': False , 'tar': False}} config_lib_instance.WriteDnsServerInfo(server2_information) config_lib_instance.TarDnsTree(audit_id) # No exported trees self.assertRaises(config_lib.ExporterNoFileError, config_lib_instance.GetDnsServerInfo, 'localhost') config_lib_instance.UnTarDnsTree(audit_id) # Checking for correct read in self.assertEqual(config_lib_instance.GetDnsServerInfo('localhost'), server1_information) self.assertEqual(config_lib_instance.GetDnsServerInfo('255.254.253.252'), server2_information) # No existing server self.assertRaises(config_lib.ServerCheckError, config_lib_instance.GetDnsServerInfo, 'bad_server')
def testUnTarDnsTree(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() self.assertRaises(config_lib.ExporterNoFileError, config_lib_instance.UnTarDnsTree, 96) config_lib_instance.UnTarDnsTree() self.assertTrue(os.path.exists('%s/%s' % (self.root_config_dir, 'localhost')))
def setUp(self): self.port = PickUnusedPort() self.rndc_port = PickUnusedPort() while (self.rndc_port == self.port): self.rndc_port = PickUnusedPort() if (not os.path.exists(BINDDIR)): os.mkdir(BINDDIR) if (not os.path.exists(TESTDIR)): os.mkdir(TESTDIR) rndc_key = open(RNDC_KEY, 'w') rndc_key.write(RNDC_KEY_DATA) rndc_key.close() rndc_conf = open(RNDC_CONF, 'w') rndc_conf.write(RNDC_CONF_DATA) rndc_conf.close() fabric_api.env.warn_only = True fabric_state.output['everything'] = False fabric_state.output['warnings'] = False fabric_api.env.host_string = "%s@%s" % (SSH_USER, TEST_DNS_SERVER) self.config_instance = roster_core.Config(file_name=CONFIG_FILE) self.root_config_dir = self.config_instance.config_file['exporter'][ 'root_config_dir'].rstrip('/').lstrip('./') self.backup_dir = self.config_instance.config_file['exporter'][ 'backup_dir'].rstrip('/').lstrip('./') self.tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.lockfile = self.config_instance.config_file['server']['lock_file'] database_server = self.config_instance.config_file['database'][ 'server'] traceroute_output = os.popen( 'dig +trace www.google.com | grep Received').read().strip('\n') traceroute_lines = traceroute_output.split('\n') self.real_dns_server = traceroute_lines[0].split(' ')[5].split('#')[0] db_instance = self.config_instance.GetDb() db_instance.CreateRosterDatabase() data = open(DATA_FILE, 'r').read() db_instance.StartTransaction() db_instance.cursor.execute(data) db_instance.EndTransaction() db_instance.close() self.db_instance = db_instance self.core_instance = roster_core.Core(u'sharrell', self.config_instance) self.core_instance.RemoveZone(u'cs.university.edu') self.core_instance.RemoveZone(u'bio.university.edu') self.core_instance.RemoveZone(u'eas.university.edu') self.config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) if (not os.path.exists(TESTDIR)): os.system('mkdir %s' % TESTDIR)
def testTarDnsTree(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() self.assertRaises(config_lib.ExporterAuditIdError, config_lib_instance.TarDnsTree, None) self.assertRaises(config_lib.ExporterListFileError, config_lib_instance.TarDnsTree, '16') os.mkdir(self.root_config_dir) config_lib_instance.UnTarDnsTree('18') self.assertEqual(config_lib_instance.TarDnsTree('16'), None)
def testGetZoneList(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.assertRaises(config_lib.ServerCheckError, config_lib_instance.GetZoneList, 'bad') tree_exporter_instance.ExportAllBindTrees() config_lib_instance.UnTarDnsTree() f = open('%s/localhost/named/external/local.db' % self.root_config_dir, 'wb') f.close() self.assertRaises(errors.FunctionError, config_lib_instance.GetZoneList, 'localhost') os.remove('%s/localhost/named/external/local.db' % self.root_config_dir) self.assertEqual(config_lib_instance.GetZoneList('localhost'), {'external':{'university.lcl':'forward_zone.db'}})
def testFindDnsTreeFilename(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() file_name = 'dns_tree_%s-18.tar.bz2' % datetime.datetime.now().strftime('%d_%m_%yT%H_%M') self.assertEqual(config_lib_instance.FindDnsTreeFilename('18'), file_name) self.assertRaises(config_lib.ExporterAuditIdError, config_lib_instance.FindDnsTreeFilename, None) config_lib_instance.backup_dir = '/bad/dir/' self.assertRaises(config_lib.ExporterFileError, config_lib_instance.FindDnsTreeFilename, '16') config_lib_instance.backup_dir = self.backup_dir self.assertEqual(config_lib_instance.FindDnsTreeFilename('16'), None) os.rename('%s/%s' % (self.backup_dir, file_name), '%s/dns_tree_fail_file.tar.bz2' % self.backup_dir) self.assertRaises(config_lib.ExporterFileError, config_lib_instance.FindDnsTreeFilename, '16')
def testMultipleThreadedConnectionsWithDifferentUsers(self): data_exporter = tree_exporter.BindTreeExport(CONFIG_FILE) self.daemon_instance.core_die_time = 7200 roster_client_lib.RunFunction(u'MakeZone', USERNAME, credstring=self.credential, args=['new_zone', 'master', 'zone.com.'], server_name=self.server_name, password=PASSWORD) cred_dict = {} for user_number in range(40): roster_client_lib.RunFunction(u'MakeUser', USERNAME, credstring=self.credential, args=['user%s' % user_number, 128], server_name=self.server_name) cred_dict['user%s' % user_number] = roster_client_lib.GetCredentials( 'user%s' % user_number, 'tost', server_name=self.server_name) client_threads = [] for record_number in range(5): for user_number in range(40): new_client_thread = ClientRecordModifyThread( 'user%s' % user_number, '192.168.%s.%s' % (user_number, record_number), 'host%s-%s' % (user_number, record_number), cred_dict['user%s' % user_number], self) new_client_thread.daemon = True new_client_thread.start() client_threads.append(new_client_thread) data_exporter.db_instance.StartTransaction() try: data_exporter.db_instance.LockDb() try: data_exporter.GetRawData() finally: data_exporter.db_instance.UnlockDb() finally: data_exporter.db_instance.EndTransaction() for old_thread in client_threads: old_thread.join()
def setUp(self): self.config_instance = roster_core.Config(file_name=CONFIG_FILE) db_instance = self.config_instance.GetDb() db_instance.CreateRosterDatabase() data = open(DATA_FILE, 'r').read() db_instance.StartTransaction() db_instance.cursor.execute(data) db_instance.EndTransaction() db_instance.close() self.core_instance = roster_core.Core(u'sharrell', self.config_instance) self.tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.db_instance = db_instance if (os.path.exists(self.tree_exporter_instance.backup_dir)): shutil.rmtree(self.tree_exporter_instance.backup_dir)
def testFindNewestDnsTreeFilename(self): config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) os.mkdir(self.backup_dir) self.assertRaises(config_lib.ExporterNoFileError, config_lib_instance.FindNewestDnsTreeFilename) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() file_name = 'dns_tree_%s-18.tar.bz2' % datetime.datetime.now().strftime('%d_%m_%yT%H_%M') audit_id, filename = config_lib_instance.FindNewestDnsTreeFilename() self.assertEqual(filename, file_name) self.assertEqual(audit_id, 18) os.rename('%s/%s' % (self.backup_dir, file_name), '%s/dns_tree_fail.tar.bz2' % self.backup_dir) self.assertRaises(config_lib.ExporterFileNameError, config_lib_instance.FindNewestDnsTreeFilename) os.remove('%s/dns_tree_fail.tar.bz2' % self.backup_dir) self.assertRaises(config_lib.ExporterNoFileError, config_lib_instance.FindNewestDnsTreeFilename) config_lib_instance.backup_dir = '/bad/directory' self.assertRaises(config_lib.ExporterListFileError, config_lib_instance.FindNewestDnsTreeFilename)
def setUp(self): self.config_instance = roster_core.Config(file_name=CONFIG_FILE) db_instance = self.config_instance.GetDb() db_instance.CreateRosterDatabase() data = open(DATA_FILE, 'r').read() db_instance.StartTransaction() db_instance.cursor.execute(data) db_instance.EndTransaction() db_instance.close() self.core_instance = roster_core.Core(u'sharrell', self.config_instance) self.db_recovery_instance = db_recovery.Recover( u'sharrell', self.config_instance) self.tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.db_instance = db_instance self.core_instance.RemoveZone(u'eas.university.edu') self.core_instance.RemoveZone(u'bio.university.edu') self.core_instance.RemoveZone(u'cs.university.edu')
def setUp(self): self.config_instance = roster_core.Config(file_name=CONFIG_FILE) self.config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) self.root_config_dir = self.config_instance.config_file['exporter'][ 'root_config_dir'].lstrip('./').rstrip('/') self.backup_dir = self.config_instance.config_file['exporter'][ 'backup_dir'].lstrip('./').rstrip('/') self.bind_config_dir = os.path.expanduser(self.root_config_dir) self.lockfile = self.config_instance.config_file['server']['lock_file'] self.tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) db_instance = self.config_instance.GetDb() db_instance.CreateRosterDatabase() data = open(DATA_FILE, 'r').read() db_instance.StartTransaction() db_instance.cursor.execute(data) db_instance.EndTransaction() db_instance.close() self.db_instance = db_instance if (not os.path.exists(TEST_DIR)): os.system('mkdir %s' % TEST_DIR) self.core_instance = roster_core.Core(USERNAME, self.config_instance) self.core_instance.RemoveZone(u'cs.university.edu') self.core_instance.RemoveZone(u'bio.university.edu') self.core_instance.RemoveZone(u'eas.university.edu') self.core_instance.MakeView(u'test_view') self.core_instance.MakeZone(u'sub.university.lcl', u'master', u'sub.university.lcl.', view_name=u'test_view')
def setUp(self): def PickUnusedPort(): s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((TEST_DNS_SERVER, 0)) addr, port = s.getsockname() s.close() return port self.port = PickUnusedPort() self.rndc_port = PickUnusedPort() while (self.rndc_port == self.port): self.rndc_port = PickUnusedPort() if (not os.path.exists(BINDDIR)): os.mkdir(BINDDIR) rndc_key = open(RNDC_KEY, 'w') rndc_key.write(RNDC_KEY_DATA) rndc_key.close() rndc_conf = open(RNDC_CONF, 'w') rndc_conf.write(RNDC_CONF_DATA) rndc_conf.close() fabric_api.env.warn_only = True fabric_state.output['everything'] = False fabric_state.output['warnings'] = False fabric_api.env.host_string = "%s@%s" % (SSH_USER, TEST_DNS_SERVER) self.config_instance = roster_core.Config(file_name=CONFIG_FILE) db_instance = self.config_instance.GetDb() db_instance.CreateRosterDatabase() self.root_config_dir = os.path.expanduser( self.config_instance.config_file['exporter']['root_config_dir']) self.backup_dir = os.path.expanduser( self.config_instance.config_file['exporter']['backup_dir']) self.tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.lockfile = self.config_instance.config_file['server']['lock_file'] data = open(DATA_FILE, 'r').read() db_instance.StartTransaction() db_instance.cursor.execute(data) db_instance.EndTransaction() db_instance.close() self.db_instance = db_instance self.core_instance = roster_core.Core(USERNAME, self.config_instance) self.core_instance.RemoveZone(u'cs.university.edu') self.core_instance.RemoveZone(u'bio.university.edu') self.core_instance.RemoveZone(u'eas.university.edu') if (not os.path.exists(BINDDIR)): os.mkdir(BINDDIR) if (not os.path.exists('%s/named/' % BINDDIR.rstrip('/'))): os.mkdir('%s/named/' % BINDDIR.rstrip('/')) if (not os.path.exists(TESTDIR)): os.mkdir(TESTDIR) self.core_instance.MakeView(u'test_view') self.core_instance.MakeZone(u'forward_zone', u'master', u'sub.university.lcl.', view_name=u'test_view') self.core_instance.MakeZone(u'reverse_zone', u'master', u'0.168.192.in-addr.arpa.', view_name=u'test_view') self.core_instance.MakeZone(u'reverse_ipv6_zone', u'master', u'8.0.e.f.f.3.ip6.arpa.', view_name=u'test_view') self.assertEqual(self.core_instance.ListRecords(), []) output = os.popen('python %s -f test_data/test_zone.db ' '--view test_view -u %s --config-file %s ' '-z forward_zone' % (ZONE_IMPORTER_EXEC, USERNAME, CONFIG_FILE)) self.assertEqual( output.read(), 'Loading in test_data/test_zone.db\n' '17 records loaded from zone test_data/test_zone.db\n' '17 total records added\n') output.close() output = os.popen('python %s -f test_data/test_reverse_zone.db ' '--view test_view -u %s --config-file %s ' '-z reverse_zone' % (ZONE_IMPORTER_EXEC, USERNAME, CONFIG_FILE)) self.assertEqual( output.read(), 'Loading in test_data/test_reverse_zone.db\n' '6 records loaded from zone ' 'test_data/test_reverse_zone.db\n' '6 total records added\n') output.close() output = os.popen('python %s -f test_data/test_reverse_ipv6_zone.db ' '--view test_view -u %s --config-file %s ' '-z reverse_ipv6_zone' % (ZONE_IMPORTER_EXEC, USERNAME, CONFIG_FILE)) self.assertEqual( output.read(), 'Loading in test_data/test_reverse_ipv6_zone.db\n' '5 records loaded from zone ' 'test_data/test_reverse_ipv6_zone.db\n' '5 total records added\n') output.close() self.core_instance.MakeDnsServer(TEST_DNS_SERVER, SSH_USER, BINDDIR, TESTDIR) self.core_instance.MakeDnsServerSet(u'set1') self.core_instance.MakeDnsServerSetAssignments(TEST_DNS_SERVER, u'set1') self.core_instance.MakeDnsServerSetViewAssignments( u'test_view', 1, u'set1') self.core_instance.MakeNamedConfGlobalOption( u'set1', u'include "%s/rndc.key"; options { pid-file "test_data/named.pid";};\n'\ 'controls { inet 127.0.0.1 port %d allow{localhost;} keys {rndc-key;};};' % (BINDDIR.rstrip('/'), self.rndc_port)) # So we can test self.core_instance.MakeViewToACLAssignments(u'test_view', u'set1', u'any', 1) self.tree_exporter_instance.ExportAllBindTrees() # Copy blank named.conf to start named with shutil.copyfile('test_data/named.blank.conf', os.path.join(BINDDIR, 'named.conf')) named_file_contents = open(os.path.join(BINDDIR, 'named.conf')).read() named_file_contents = named_file_contents.replace( 'RNDC_KEY', '%s/rndc.key' % BINDDIR.rstrip('/')) named_file_contents = named_file_contents.replace( 'NAMED_DIR', '%s/named/' % BINDDIR.rstrip('/')) named_file_contents = named_file_contents.replace( 'NAMED_PID', '%s/test_data/named.pid' % os.getcwd()) named_file_contents = named_file_contents.replace( 'RNDC_PORT', str(self.rndc_port)) named_file_contents = named_file_contents.replace( 'SESSION_KEYFILE', '%s/%s' % (os.getcwd(), str(SESSION_KEYFILE))) named_file_handle = open('%s/named.conf' % BINDDIR, 'w') named_file_handle.write(named_file_contents) named_file_handle.close() # Start named out = fabric_api.local('/usr/sbin/named -p %s -u %s -c %snamed.conf' % (self.port, SSH_USER, BINDDIR), capture=True) time.sleep(2)
def setUp(self): self.config_instance = roster_core.Config(CONFIG_FILE) self.config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) self.root_config_dir = self.config_instance.config_file['exporter'][ 'root_config_dir'].rstrip('/') self.root_backup_dir = self.config_instance.config_file['exporter'][ 'backup_dir'].rstrip('/') if (os.path.exists(self.root_backup_dir)): shutil.rmtree(self.root_backup_dir) os.mkdir(self.root_backup_dir) if (os.path.exists(self.root_config_dir)): shutil.rmtree(self.root_config_dir) os.mkdir(self.root_config_dir) self.db_instance = self.config_instance.GetDb() self.db_instance.CreateRosterDatabase() self.data = open(DATA_FILE, 'r').read() self.db_instance.StartTransaction() self.db_instance.cursor.execute(self.data) self.db_instance.EndTransaction() self.db_instance.close() self.core_instance = roster_core.Core(USERNAME, self.config_instance) self.tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) self.core_instance.RemoveZone(u'cs.university.edu') self.core_instance.RemoveZone(u'bio.university.edu') self.core_instance.RemoveZone(u'eas.university.edu') self.core_instance.MakeDnsServerSet(u'set1') self.core_instance.MakeView(u'test_view') self.core_instance.MakeDnsServerSetViewAssignments( u'test_view', 1, u'set1') self.core_instance.MakeNamedConfGlobalOption( u'set1', u'include "%s/test_data/rndc.key"; options { pid-file "test_data/named.pid";};\n' 'controls { inet 127.0.0.1 port 5555 allow{localhost;} keys {rndc-key;};};' % (os.getcwd())) # So we can test self.core_instance.MakeACL(u'test_acl', u'127.0.0.1') self.core_instance.MakeViewToACLAssignments(u'test_view', u'set1', u'test_acl', 1) self.core_instance.MakeZone(u'sub.university.lcl', u'master', u'sub.university.lcl.', view_name=u'test_view') soa_args_dict = self.core_instance.GetEmptyRecordArgsDict(u'soa') soa_args_dict[u'refresh_seconds'] = 500 soa_args_dict[u'expiry_seconds'] = 500 soa_args_dict[u'name_server'] = u'ns.sub.university.lcl.' soa_args_dict[u'minimum_seconds'] = 500 soa_args_dict[u'retry_seconds'] = 500 soa_args_dict[u'serial_number'] = 1000 soa_args_dict[u'admin_email'] = u'root.sub.university.lcl.' self.core_instance.MakeRecord(u'soa', u'@', u'sub.university.lcl', soa_args_dict, view_name=u'test_view') if (not os.path.exists(TESTDIR)): os.system('mkdir %s' % TESTDIR)
def testCheckDnsServer(self): # This does not test the raise ServerCheckError: Unable to run 'named' # since we would have to run a controlled server that does not have # named installed. config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() # ExporterFileError: Can not list files in root dir if( os.path.exists(self.root_config_dir) ): shutil.rmtree(self.root_config_dir) self.assertRaises(config_lib.ExporterFileError, config_lib_instance.CheckDnsServer, 'localhost', []) # ServerCheckError: DNS server doesn't exist config_lib_instance.UnTarDnsTree() self.assertRaises(config_lib.ServerCheckError, config_lib_instance.CheckDnsServer, 'junk', []) ########################33 # Once tree exporter if complete, this will be removed ########################33 # This is temporary until DNS tree exporter is committed, at which it will be unnecessary. if( os.path.exists('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) ): os.remove('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) if( os.path.exists('%s/localhost/named/localhost_config' % self.root_config_dir) ): os.remove('%s/localhost/named/localhost_config' % self.root_config_dir) # End of needing to remove server_info1 = {'server_info':{ 'server_name': 'localhost', 'server_user': SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}} server_info2 = {'server_info':{ 'server_name': '255.254.253.252', 'server_user': SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}} config_lib_instance.WriteDnsServerInfo(server_info1) config_lib_instance.WriteDnsServerInfo(server_info2) ########################33 ########################33 ########################33 # ServerCheckError: Metadata file incorrectly written/DNE shutil.move('%s/localhost/localhost.info' % self.root_config_dir, '%s/localhost/localhost_bad.info' % self.root_config_dir) self.assertRaises(config_lib.ServerCheckError, config_lib_instance.CheckDnsServer, 'localhost', []) shutil.move('%s/localhost/localhost_bad.info' % self.root_config_dir, '%s/localhost/localhost.info' % self.root_config_dir) # ServerCheckError: Can not connect via SSH self.assertRaises(config_lib.ServerCheckError, config_lib_instance.CheckDnsServer, '255.254.253.252', []) # ServerCheckError: Remote BIND directory doesn't exist self.core_instance.UpdateDnsServer(u'localhost', u'localhost', SSH_USER, u'/junk/', u'/junk/test/') tree_exporter_instance.ExportAllBindTrees() config_lib_instance.UnTarDnsTree() self.assertRaises(config_lib.ServerCheckError, config_lib_instance.CheckDnsServer, 'localhost', []) # ServerCheckError: Remote test directory doesn't exist self.core_instance.UpdateDnsServer(u'localhost', u'localhost', SSH_USER, BINDDIR, u'/junk/test/') tree_exporter_instance.ExportAllBindTrees() config_lib_instance.UnTarDnsTree() self.assertRaises(config_lib.ServerCheckError, config_lib_instance.CheckDnsServer, 'localhost', []) # Everything Passes self.core_instance.UpdateDnsServer(u'localhost', u'localhost', SSH_USER, BINDDIR, TESTDIR) tree_exporter_instance.ExportAllBindTrees() config_lib_instance.UnTarDnsTree() ########################33 # Once tree exporter if complete, this will be removed ########################33 # This is temporary until DNS tree exporter is committed, at which it will be unnecessary. if( os.path.exists('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) ): os.remove('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) if( os.path.exists('%s/localhost/named/localhost_config' % self.root_config_dir) ): os.remove('%s/localhost/named/localhost_config' % self.root_config_dir) # End of needing to remove server_info1 = {'server_info':{ 'server_name': 'localhost', 'server_user': SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}} server_info2 = {'server_info':{ 'server_name': '255.254.253.252', 'server_user': SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}} config_lib_instance.WriteDnsServerInfo(server_info1) config_lib_instance.WriteDnsServerInfo(server_info2) ########################33 ########################33 ########################33 self.assertEqual(config_lib_instance.CheckDnsServer( 'localhost', ['named-checkzone', 'named-checkconf', 'named-compilezone', 'tar']), None) # Check that info file has additional information localhost_info = ConfigParser.SafeConfigParser() localhost_info.read('%s/localhost/localhost.info' % self.root_config_dir) self.assertTrue(localhost_info.has_section('tools')) self.assertTrue(localhost_info.has_option('tools', 'named-checkzone')) self.assertTrue(localhost_info.has_option('tools', 'named-checkconf')) self.assertTrue(localhost_info.has_option('tools', 'named-compilezone')) self.assertTrue(localhost_info.has_option('tools', 'tar')) self.assertRaises(errors.FunctionError, config_lib_instance.CheckDnsServer, 'localhost', ['bad_tool'])
def testWriteDnsServerInfo(self): server_information = {'server_info': { 'server_name': 'localhost', 'server_user': SSH_USER, 'ssh_host':'%s@localhost:22' % SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}, 'tools': {}} config_lib_instance = config_lib.ConfigLib(CONFIG_FILE) tree_exporter_instance = tree_exporter.BindTreeExport(CONFIG_FILE) tree_exporter_instance.ExportAllBindTrees() audit_id, filename = config_lib_instance.FindNewestDnsTreeFilename() config_lib_instance.UnTarDnsTree(audit_id) # This is temporary until DNS tree exporter is committed, at which it will be unnecessary. if( os.path.exists('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) ): os.remove('%s/255.254.253.252/named/255.254.253.252_config' % self.root_config_dir) if( os.path.exists('%s/localhost/named/localhost_config' % self.root_config_dir) ): os.remove('%s/localhost/named/localhost_config' % self.root_config_dir) # End of needing to remove config_lib_instance.WriteDnsServerInfo(server_information) self.assertTrue(os.path.exists('%s/localhost/localhost.info' % self.root_config_dir)) server_information = {'server_info': { 'server_name': '255.254.253.252', 'server_user': SSH_USER, 'ssh_host':'%[email protected]:22' % SSH_USER, 'bind_dir': BINDDIR, 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}, 'tools': {}} config_lib_instance.WriteDnsServerInfo(server_information) self.assertTrue(os.path.exists('%s/255.254.253.252/255.254.253.252.info'%self.root_config_dir)) server_file = ConfigParser.SafeConfigParser() server_file.read('%s/localhost/localhost.info' % self.root_config_dir) self.assertTrue(server_file.has_section('server_info')) self.assertTrue(server_file.has_section('tools')) self.assertTrue(server_file.has_option('server_info', 'ssh_host')) self.assertTrue(server_file.has_option('server_info', 'bind_dir')) self.assertTrue(server_file.has_option('server_info', 'test_dir')) self.assertEqual(len(server_file.options('tools')), 0) self.assertEqual(server_file.get('server_info', 'ssh_host'), '%s@localhost:22' % SSH_USER) self.assertEqual(server_file.get('server_info', 'bind_dir'), BINDDIR) self.assertEqual(server_file.get('server_info', 'test_dir'), TESTDIR) del(server_file) server_file = ConfigParser.SafeConfigParser() server_file.read('%s/255.254.253.252/255.254.253.252.info' % self.root_config_dir) self.assertTrue(server_file.has_section('server_info')) self.assertTrue(server_file.has_section('tools')) self.assertTrue(server_file.has_option('server_info', 'ssh_host')) self.assertTrue(server_file.has_option('server_info', 'bind_dir')) self.assertTrue(server_file.has_option('server_info', 'test_dir')) self.assertEqual(len(server_file.options('tools')), 0) self.assertEqual(server_file.get('server_info', 'ssh_host'), '%[email protected]:22' % SSH_USER) self.assertEqual(server_file.get('server_info', 'bind_dir'), BINDDIR) self.assertEqual(server_file.get('server_info', 'test_dir'), TESTDIR) # Redo this after finished refactoring dns_server_info dictionary self.assertRaises(config_lib.ServerCheckError, config_lib_instance.WriteDnsServerInfo, {}) self.assertRaises(config_lib.ServerCheckError, config_lib_instance.WriteDnsServerInfo, {'server_info': {'server_name':'bad_server','server_user':'******', 'bind_dir':'/dir','test_dir':'/dir/test', 'bind_version':'UNKNOWN'}}) config_lib_instance.TarDnsTree(audit_id) self.assertRaises(config_lib.ExporterNoFileError, config_lib_instance.WriteDnsServerInfo, server_information) config_lib_instance.UnTarDnsTree(audit_id) server_information = {'server_info': { 'server_name': 'localhost', 'server_user': SSH_USER, 'ssh_host':'%s@localhost:22' % SSH_USER, 'bind_dir': '/some/bind/', 'test_dir': TESTDIR, 'bind_version': 'UNKNOWN'}, 'tools': { 'named-compilezone': True, 'named-checkzone': True, 'named-checkconf': True, 'tar': True}} config_lib_instance.WriteDnsServerInfo(server_information) self.assertEqual(config_lib_instance.GetDnsServerInfo('localhost'), server_information)