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)
Esempio n. 2
0
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)
Esempio n. 3
0
 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"
Esempio n. 4
0
 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,
     )
Esempio n. 5
0
 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)
Esempio n. 6
0
 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)
Esempio n. 7
0
    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, [])
Esempio n. 8
0
 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)
Esempio n. 9
0
 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
         },
     )])
Esempio n. 10
0
 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,
             {}
         )
     ])
Esempio n. 11
0
 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>
         """)
Esempio n. 12
0
 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,
         ),
     )
Esempio n. 13
0
 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',
         },
     )])
Esempio n. 14
0
 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)
Esempio n. 15
0
 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)
Esempio n. 16
0
    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)
Esempio n. 17
0
    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)
Esempio n. 18
0
    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,
                ),
            ],
        )
Esempio n. 19
0
 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
Esempio n. 20
0
 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"}],
     }
Esempio n. 21
0
    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
            ),
        )
Esempio n. 22
0
 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": [],
                 },
             ),
         ])
Esempio n. 23
0
 def setUp(self):
     self.mock_logger = mock.MagicMock(logging.Logger)
     self.mock_reporter = MockLibraryReportProcessor()
     self.lib_env = LibraryEnvironment(self.mock_logger, self.mock_reporter)
Esempio n. 24
0
 def setUp(self):
     self.mock_logger = mock.MagicMock(logging.Logger)
     self.mock_reporter = MockLibraryReportProcessor()
Esempio n. 25
0
 def setUp(self):
     self.create_env = partial(
         LibraryEnvironment,
         mock.MagicMock(logging.Logger),
         MockLibraryReportProcessor(),
     )
Esempio n. 26
0
 def setUp(self):
     self.env = LibraryEnvironment(mock.MagicMock(logging.Logger),
                                   MockLibraryReportProcessor())
Esempio n. 27
0
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(
Esempio n. 28
0
 def setUp(self):
     self.mock_runner = mock.MagicMock()
     self.mock_rep = MockLibraryReportProcessor()
Esempio n. 29
0
 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, [])
Esempio n. 30
0
 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, [])