def test_configure(self): self.__mgr._setup_ccsession() # Pretend specified directories exist and writable os.path.isdir = lambda x: True os.access = lambda x, y: True # At the initial configuration, if mapped_file_dir isn't specified, # the default value will be set. self.assertEqual((0, None), parse_answer(self.__mgr._config_handler({}))) self.assertEqual('mapped_files', self.__mgr._config_params['mapped_file_dir']. split('/')[-1]) # Update the configuration. user_cfg = {'mapped_file_dir': '/some/path/dir'} self.assertEqual((0, None), parse_answer(self.__mgr._config_handler(user_cfg))) self.assertEqual('/some/path/dir', self.__mgr._config_params['mapped_file_dir']) # Bad update: diretory doesn't exist (we assume it really doesn't # exist in the tested environment). Update won't be made. os.path.isdir = self.__orig_isdir # use real library user_cfg = {'mapped_file_dir': '/another/path/dir'} answer = parse_answer(self.__mgr._config_handler(user_cfg)) self.assertEqual(1, answer[0]) self.assertIsNotNone(re.search('not a directory', answer[1]))
def __check_load_or_update_zone(self, cmd, handler): "Common checks for load or update zone callbacks." commands = [] self.__mgr._cmd_to_builder = lambda cmd: commands.append(cmd) sgmt_info = MockSegmentInfo() dsrc_info = MockDataSrcInfo(sgmt_info) self.__mgr._datasrc_info_list.append(dsrc_info) # Expected builder event for the loadzone parameters expected_event = ('load', bundy.dns.Name('zone'), dsrc_info, bundy.dns.RRClass('IN'), 'name') # If start_update() returns an event, it's passed to the builder. ans = handler(cmd, {'datasource': 'name', 'class': 'IN', 'origin': 'zone'}) if cmd == 'loadzone': self.assertEqual(0, parse_answer(ans)[0]) self.assertEqual([expected_event], sgmt_info.events) self.assertEqual([expected_event], commands) # If start_update() returns None, the event is only stored in the # segment info. sgmt_info.start_update = lambda: None ans = handler(cmd, {'datasource': 'name', 'class': 'IN', 'origin': 'zone'}) if cmd == 'loadzone': self.assertEqual(0, parse_answer(ans)[0]) self.assertEqual([expected_event, expected_event], sgmt_info.events) self.assertEqual([expected_event], commands)
def __check_load_or_update_zone(self, cmd, handler): "Common checks for load or update zone callbacks." commands = [] self.__mgr._cmd_to_builder = lambda cmd: commands.append(cmd) sgmt_info = MockSegmentInfo() dsrc_info = MockDataSrcInfo(sgmt_info) self.__mgr._datasrc_info = dsrc_info # Expected builder event for the loadzone parameters expected_event = ('load', bundy.dns.Name('zone'), dsrc_info, bundy.dns.RRClass('IN'), 'name') # If start_update() returns an event, it's passed to the builder. cmd_args = {'datasource': 'name', 'class': 'IN', 'origin': 'zone'} if cmd == 'zone_updated': cmd_args['generation-id'] = TEST_GENERATION_ID ans = handler(cmd, cmd_args) if cmd == 'loadzone': self.assertEqual(0, parse_answer(ans)[0]) self.assertEqual([expected_event], sgmt_info.events) self.assertEqual([expected_event], commands) # If start_update() returns None, the event is only stored in the # segment info. sgmt_info.start_update = lambda: None ans = handler(cmd, cmd_args) if cmd == 'loadzone': self.assertEqual(0, parse_answer(ans)[0]) self.assertEqual([expected_event, expected_event], sgmt_info.events) self.assertEqual([expected_event], commands)
def test_configure(self): self.__mgr._setup_ccsession() # Pretend specified directories exist and writable os.path.isdir = lambda x: True os.access = lambda x, y: True # At the initial configuration, if mapped_file_dir isn't specified, # the default value will be set. self.assertEqual((0, None), parse_answer(self.__mgr._config_handler({}))) self.assertEqual( 'mapped_files', self.__mgr._config_params['mapped_file_dir'].split('/')[-1]) # Update the configuration. user_cfg = {'mapped_file_dir': '/some/path/dir'} self.assertEqual((0, None), parse_answer(self.__mgr._config_handler(user_cfg))) self.assertEqual('/some/path/dir', self.__mgr._config_params['mapped_file_dir']) # Bad update: diretory doesn't exist (we assume it really doesn't # exist in the tested environment). Update won't be made. os.path.isdir = self.__orig_isdir # use real library user_cfg = {'mapped_file_dir': '/another/path/dir'} answer = parse_answer(self.__mgr._config_handler(user_cfg)) self.assertEqual(1, answer[0]) self.assertIsNotNone(re.search('not a directory', answer[1]))
def test_configure_bad_permissions(self): self.__mgr._setup_ccsession() # Pretend specified directories exist and writable os.path.isdir = lambda x: True os.access = lambda x, y: True # Initial configuration. self.assertEqual((0, None), parse_answer(self.__mgr._config_handler({}))) os.path.isdir = self.__orig_isdir os.access = self.__orig_os_access # Bad update: directory exists but is not writable. os.mkdir(self.__test_mapped_file_dir, 0o500) # drop writable bit user_cfg = {'mapped_file_dir': self.__test_mapped_file_dir} answer = parse_answer(self.__mgr._config_handler(user_cfg)) self.assertEqual(1, answer[0]) self.assertIsNotNone(re.search('not writable', answer[1]))
def test_bad_loadzone(self): "Check various invalid cases of loadzone command" # there's no datasrc info self.assertEqual( 1, parse_answer(self.__mgr._mod_command_handler('loadzone', {}))[0]) sgmt_info = MockSegmentInfo() dsrc_info = MockDataSrcInfo(sgmt_info) self.__mgr._datasrc_info = dsrc_info # missing necesary keys or invalid values self.assertEqual( 1, parse_answer(self.__mgr._mod_command_handler('loadzone', {}))[0]) self.assertEqual( 1, parse_answer( self.__mgr._mod_command_handler('loadzone', { 'class': 'badclass', 'datasource': 'name', 'origin': 'zone' }))[0]) self.assertEqual( 1, parse_answer( self.__mgr._mod_command_handler('loadzone', { 'class': 'IN', 'datasource': 'name', 'origin': 'bad..name' }))[0]) self.assertEqual( 1, parse_answer( self.__mgr._mod_command_handler('loadzone', { 'class': 'IN', 'datasource': 'noname', 'origin': 'zone' }))[0])
def test_bad_loadzone(self): "Check various invalid cases of loadzone command" # there's no datasrc info self.assertEqual(1, parse_answer(self.__mgr._mod_command_handler( 'loadzone', {}))[0]) sgmt_info = MockSegmentInfo() dsrc_info = MockDataSrcInfo(sgmt_info) self.__mgr._datasrc_info_list.append(dsrc_info) # missing necesary keys or invalid values self.assertEqual(1, parse_answer(self.__mgr._mod_command_handler( 'loadzone', {}))[0]) self.assertEqual(1, parse_answer(self.__mgr._mod_command_handler( 'loadzone', {'class': 'badclass', 'datasource': 'name', 'origin': 'zone'}))[0]) self.assertEqual(1, parse_answer(self.__mgr._mod_command_handler( 'loadzone', {'class': 'IN', 'datasource': 'name', 'origin': 'bad..name'}))[0]) self.assertEqual(1, parse_answer(self.__mgr._mod_command_handler( 'loadzone', {'class': 'IN', 'datasource': 'noname', 'origin': 'zone'}))[0])
def test_mod_command_handler(self): # unknown name of command will be rejected. known cases are tested # separately. ans = self.__mgr._mod_command_handler('unknown', {}) self.assertEqual(1, parse_answer(ans)[0])