def test_invalid(self): reporter = MockLibraryReportProcessor() lib._validate_target_typewise(reporter, "bad_target") report = [(severity.ERROR, report_codes.INVALID_OPTION_TYPE, { "option_name": "target", "allowed_types": ["node", "regular expression", "attribute_name=value"], }, None)] assert_report_item_list_equal(reporter.report_item_list, report)
def fixture_to_cib(cib_file, xml): environ = dict(os.environ) environ["CIB_file"] = cib_file runner = CommandRunner(mock.MagicMock(logging.Logger), MockLibraryReportProcessor(), environ) stdout, stderr, retval = runner.run([ os.path.join(settings.pacemaker_binaries, "cibadmin"), "--create", "--scope", "resources", "--xml-text", xml ]) assert retval == 0, ("Error running fixture_to_cib:\n" + stderr + "\n" + stdout)
def setUp(self): self.reporter = MockLibraryReportProcessor() self.topology_el = "a topology element" self.resources_el = "a resources element" self.level = "a level" self.target_type = "a target type" self.target_value = "a target value" self.devices = ["device1", "device2"] self.cluster_status_nodes = "a status" self.force_device = "a force for a device" self.force_node = "a force for a node"
def test_success_when_no_duplication_found(self, export_with_set): export_with_set.return_value = "exported_duplicate_element" element = mock.MagicMock() element.tag = "constraint_type" #no exception raised report_processor = MockLibraryReportProcessor() constraint.check_is_without_duplication( report_processor, fixture_constraint_section([]), element, are_duplicate=lambda e1, e2: True, export_element=constraint.export_with_set, )
def test_node_invalid_force(self): reporter = MockLibraryReportProcessor() lib._validate_target_valuewise(reporter, self.state, TARGET_TYPE_NODE, "rh7-x", force_node=True) report = [(severity.WARNING, report_codes.NODE_NOT_FOUND, { "node": "rh7-x", }, None)] assert_report_item_list_equal(reporter.report_item_list, report)
def test_node_invalid_not_forceable(self): reporter = MockLibraryReportProcessor() lib._validate_target_valuewise(reporter, self.state, TARGET_TYPE_NODE, "rh7-x", allow_force=False) report = [(severity.ERROR, report_codes.NODE_NOT_FOUND, { "node": "rh7-x", }, None)] assert_report_item_list_equal(reporter.report_item_list, report)
def test_success(self, mock_find): mock_find.return_value = [] reporter = MockLibraryReportProcessor() lib._validate_level_target_devices_does_not_exist( reporter, "tree", "level", "target_type", "target_value", ["devices"]) mock_find.assert_called_once_with("tree", "level", "target_type", "target_value", ["devices"]) assert_report_item_list_equal(reporter.report_item_list, [])
def test_raises_on_invalid_options(self): report_processor = MockLibraryReportProcessor() expected_errors = [ ( severities.ERROR, report_codes.INVALID_OPTIONS, { "option_names": ["site"], "option_type": "booth ticket", "allowed": list(config_structure.TICKET_KEYS), "allowed_patterns": [], }, ), ( severities.ERROR, report_codes.INVALID_OPTIONS, { "option_names": ["port"], "option_type": "booth ticket", "allowed": list(config_structure.TICKET_KEYS), "allowed_patterns": [], }, ), ( severities.ERROR, report_codes.INVALID_OPTION_VALUE, { "option_name": "timeout", "option_value": " ", "allowed_values": "no-empty", "cannot_be_empty": False, "forbidden_characters": None, }, ), (severities.ERROR, report_codes.INVALID_OPTIONS, { "option_names": ["unknown"], "option_type": "booth ticket", "allowed": list(config_structure.TICKET_KEYS), "allowed_patterns": [], }, report_codes.FORCE_OPTIONS), ] assert_raise_library_error( lambda: config_structure.validate_ticket_options( report_processor, { "site": "a", "port": "b", "timeout": " ", "unknown": "c", }, allow_unknown_options=False, ), *expected_errors) assert_report_item_list_equal(report_processor.report_item_list, expected_errors)
def test_warn_when_config_exists_and_overwrite_allowed(self, mock_exists): # pylint: disable=unused-argument report_processor = MockLibraryReportProcessor() self.check(report_processor, can_overwrite_existing=True) assert_report_item_list_equal(report_processor.report_item_list, [( severities.WARNING, report_codes.FILE_ALREADY_EXISTS, { "file_path": CONF_PATH }, )])
def test_remove_config_even_if_cannot_get_its_content_when_forced(self): env = _env_fixture("somename") env.report_processor = MockLibraryReportProcessor() commands.config_destroy(env, ignore_config_load_problems=True) env.booth.remove_config.assert_called_once_with() assert_report_item_list_equal(env.report_processor.report_item_list, [ ( Severities.WARNING, report_codes.BOOTH_CANNOT_IDENTIFY_KEYFILE, {} ) ])
def setUp(self): self.mock_reporter = MockLibraryReportProcessor() self.tree = etree.XML(""" <cib> <configuration> <alerts> <alert id="alert" path="/path"> <recipient id="alert-recipient" value="test_val"/> </alert> </alerts> </configuration> </cib> """)
def test_raises_when_config_exists_and_overwrite_not_allowed( self, mock_ex): # pylint: disable=unused-argument assert_raise_library_error( lambda: self.check(MockLibraryReportProcessor()), ( severities.ERROR, report_codes.FILE_ALREADY_EXISTS, { "file_path": CONF_PATH }, report_codes.FORCE_FILE_OVERWRITE, ), )
def test_warn_when_multiple_booth_resources_removed(self): report_processor = MockLibraryReportProcessor() commands._find_resource_elements_for_operation( mock.MagicMock(report_processor=report_processor), "somename", allow_multiple=True, ) assert_report_item_list_equal(report_processor.report_item_list, [( Severities.WARNING, report_codes.BOOTH_MULTIPLE_TIMES_IN_CIB, { 'name': 'somename', }, )])
def setUp(self): self.known_hosts = { "host{}".format(i): PcsKnownHost( "host{}".format(i), "token{}".format(i), [ Destination("addr{}{}".format(i, j), "port{}{}".format( i, j)) for j in range(2) ], ) for i in range(2) } self.report_processor = MockLibraryReportProcessor() self.factory = lib.NodeTargetLibFactory(self.known_hosts, self.report_processor)
def test_invalid(self): reporter = MockLibraryReportProcessor() lib._validate_level(reporter, "") lib._validate_level(reporter, 0) lib._validate_level(reporter, "0") lib._validate_level(reporter, -1) lib._validate_level(reporter, "-1") lib._validate_level(reporter, "1abc") report_list = [] for value in ["", 0, "0", -1, "-1", "1abc"]: report_list.append( (severity.ERROR, report_codes.INVALID_OPTION_VALUE, { "option_value": value, "option_name": "level", "allowed_values": "a positive integer", }, None)) assert_report_item_list_equal(reporter.report_item_list, report_list)
def test_error(self, mock_find): mock_find.return_value = ["element"] reporter = MockLibraryReportProcessor() lib._validate_level_target_devices_does_not_exist( reporter, "tree", "level", "target_type", "target_value", ["devices"]) mock_find.assert_called_once_with("tree", "level", "target_type", "target_value", ["devices"]) report = [(severity.ERROR, report_codes.CIB_FENCING_LEVEL_ALREADY_EXISTS, { "devices": ["devices"], "target_type": "target_type", "target_value": "target_value", "level": "level", }, None)] assert_report_item_list_equal(reporter.report_item_list, report)
def test_failures(self): resources = etree.fromstring("<resources />") reporter = MockLibraryReportProcessor() lib.verify(reporter, self.tree, resources, []) report = [ (severity.ERROR, report_codes.STONITH_RESOURCES_DO_NOT_EXIST, { "stonith_ids": ["d1", "d2", "d3", "d4", "d5", "dR", "dR-special"], }, None), (severity.ERROR, report_codes.NODE_NOT_FOUND, { "node": "nodeA", }, None), (severity.ERROR, report_codes.NODE_NOT_FOUND, { "node": "nodeB", }, None), ] assert_report_item_list_equal(reporter.report_item_list, report)
def test_raises_when_duplicate_element_found(self): element = mock.MagicMock() element.tag = "constraint_type" report_processor = MockLibraryReportProcessor() assert_raise_library_error( lambda: constraint.check_is_without_duplication( report_processor, fixture_constraint_section( [etree.Element("tag", {"id": "duplicate_element"})]), element, are_duplicate=lambda e1, e2: True, export_element=constraint.export_with_set, )) assert_report_item_list_equal( report_processor.report_item_list, [ ( severities.INFO, report_codes.DUPLICATE_CONSTRAINTS_LIST, { "constraint_info_list": [{ "resource_sets": [], "options": { "id": "duplicate_element" }, }], "constraint_type": "constraint_type", }, ), ( severities.ERROR, report_codes.DUPLICATE_CONSTRAINTS_EXIST, { "constraint_ids": ["duplicate_element"], }, report_codes.FORCE, ), ], )
def get_env(self): self.__call_queue = CallQueue(self.__config.calls) #pylint: disable=attribute-defined-outside-init self._env = LibraryEnvironment( mock.MagicMock(logging.Logger), MockLibraryReportProcessor(), cib_data=self.__config.env.cib_data, corosync_conf_data=self.__config.env.corosync_conf_data, known_hosts_getter=((lambda: self.__config.spy.known_hosts) if self.__config.spy else self.__config.env.known_hosts_getter), booth=self.__config.env.booth, ) self.__unpatch = patch_env(self.__call_queue, self.__config, self._env) # If pushing corosync.conf has not been patched in the # LibraryEnvironment, store any corosync.conf passed to the # LibraryEnvironment for check for changes in cleanup. if not is_push_corosync_conf_call_in(self.__call_queue): self.__original_mocked_corosync_conf = ( self.__config.env.corosync_conf_data) return self._env
def setUp(self): self.mock_logger = mock.MagicMock(logging.Logger) self.mock_reporter = MockLibraryReportProcessor() self.lib_env = LibraryEnvironment(self.mock_logger, self.mock_reporter) self.metadata = """ <resource-agent> <shortdesc>short desc</shortdesc> <longdesc>long desc</longdesc> <parameters> </parameters> <actions> </actions> </resource-agent> """ self.description = { "name": "ocf:test:Dummy", "shortdesc": "short desc", "longdesc": "long desc", "parameters": [], "actions": [], "default_actions": [{"interval": "60s", "name": "monitor"}], }
def test_raises_when_duplicate_element_found(self, export_with_set): export_with_set.return_value = "exported_duplicate_element" element = mock.MagicMock() element.tag = "constraint_type" report_processor = MockLibraryReportProcessor() assert_raise_library_error( lambda: constraint.check_is_without_duplication( report_processor, fixture_constraint_section(["duplicate_element"]), element, are_duplicate=lambda e1, e2: True, export_element=constraint.export_with_set, ), ( severities.ERROR, report_codes.DUPLICATE_CONSTRAINTS_EXIST, { 'constraint_info_list': ['exported_duplicate_element'], 'constraint_type': 'constraint_type' }, report_codes.FORCE_CONSTRAINT_DUPLICATE ), )
def test_unknown_options_are_forceable(self): report_processor = MockLibraryReportProcessor() expected_errors = [ ( severities.ERROR, report_codes.INVALID_OPTIONS, { "option_names": ["site"], "option_type": "booth ticket", "allowed": list(config_structure.TICKET_KEYS), "allowed_patterns": [], }, ), ] assert_raise_library_error( lambda: config_structure.validate_ticket_options( report_processor, { "site": "a", "unknown": "c", }, allow_unknown_options=True, ), *expected_errors) assert_report_item_list_equal( report_processor.report_item_list, expected_errors + [ ( severities.WARNING, report_codes.INVALID_OPTIONS, { "option_names": ["unknown"], "option_type": "booth ticket", "allowed": list(config_structure.TICKET_KEYS), "allowed_patterns": [], }, ), ])
def setUp(self): self.mock_logger = mock.MagicMock(logging.Logger) self.mock_reporter = MockLibraryReportProcessor() self.lib_env = LibraryEnvironment(self.mock_logger, self.mock_reporter)
def setUp(self): self.mock_logger = mock.MagicMock(logging.Logger) self.mock_reporter = MockLibraryReportProcessor()
def setUp(self): self.create_env = partial( LibraryEnvironment, mock.MagicMock(logging.Logger), MockLibraryReportProcessor(), )
def setUp(self): self.env = LibraryEnvironment(mock.MagicMock(logging.Logger), MockLibraryReportProcessor())
from pcs.common.fencing_topology import ( TARGET_TYPE_ATTRIBUTE, TARGET_TYPE_NODE, TARGET_TYPE_REGEXP, ) from pcs.common.tools import Version from pcs.lib.env import LibraryEnvironment from pcs.lib.commands import fencing_topology as lib # pylint: disable=no-self-use create_lib_env = partial( LibraryEnvironment, mock.MagicMock(logging.Logger), MockLibraryReportProcessor() ) patch_env = partial(mock.patch.object, LibraryEnvironment) patch_command = create_patcher("pcs.lib.commands.fencing_topology") @patch_command("cib_fencing_topology.add_level") @patch_command("get_resources") @patch_command("get_fencing_topology") @patch_env("push_cib") @patch_command("ClusterState") @patch_command("get_cluster_status_xml") @patch_env("get_cib") @patch_env("cmd_runner", lambda self: "mocked cmd_runner") class AddLevel(TestCase): def prepare_mocks(
def setUp(self): self.mock_runner = mock.MagicMock() self.mock_rep = MockLibraryReportProcessor()
def test_success(self): reporter = MockLibraryReportProcessor() lib._validate_devices(reporter, self.resources_el, ["stonith1"]) lib._validate_devices(reporter, self.resources_el, ["stonith1", "stonith2"]) assert_report_item_list_equal(reporter.report_item_list, [])
def test_node_valid(self): reporter = MockLibraryReportProcessor() lib._validate_target_valuewise(reporter, self.state, TARGET_TYPE_NODE, "nodeA") assert_report_item_list_equal(reporter.report_item_list, [])