Пример #1
0
    def test_cib_not_set(self, mock_get_cib, mock_push_cib):
        cib_data = "test cib data"
        new_cib_data = "new test cib data"
        mock_get_cib.return_value = cib_data
        env = LibraryEnvironment(self.mock_logger, self.mock_reporter)

        self.assertTrue(env.is_cib_live)

        self.assertEqual(cib_data, env._get_cib_xml())
        self.assertEqual(1, mock_get_cib.call_count)

        env._push_cib_xml(new_cib_data)
        self.assertEqual(1, mock_push_cib.call_count)
Пример #2
0
    def test_cib_not_set(self, mock_get_cib, mock_push_cib):
        cib_data = "test cib data"
        new_cib_data = "new test cib data"
        mock_get_cib.return_value = cib_data
        env = LibraryEnvironment(self.mock_logger, self.mock_reporter)

        self.assertTrue(env.is_cib_live)

        self.assertEqual(cib_data, env._get_cib_xml())
        self.assertEqual(1, mock_get_cib.call_count)

        env._push_cib_xml(new_cib_data)
        self.assertEqual(1, mock_push_cib.call_count)
Пример #3
0
    def test_cib_set(self, mock_get_cib, mock_push_cib):
        cib_data = "test cib data"
        new_cib_data = "new test cib data"
        env = LibraryEnvironment(self.mock_logger,
                                 self.mock_reporter,
                                 cib_data=cib_data)

        self.assertFalse(env.is_cib_live)

        self.assertEqual(cib_data, env._get_cib_xml())
        self.assertEqual(0, mock_get_cib.call_count)

        env._push_cib_xml(new_cib_data)
        self.assertEqual(0, mock_push_cib.call_count)

        self.assertEqual(new_cib_data, env._get_cib_xml())
        self.assertEqual(0, mock_get_cib.call_count)
Пример #4
0
    def test_cib_set(self, mock_get_cib, mock_push_cib):
        cib_data = "test cib data"
        new_cib_data = "new test cib data"
        env = LibraryEnvironment(
            self.mock_logger,
            self.mock_reporter,
            cib_data=cib_data
        )

        self.assertFalse(env.is_cib_live)

        self.assertEqual(cib_data, env._get_cib_xml())
        self.assertEqual(0, mock_get_cib.call_count)

        env._push_cib_xml(new_cib_data)
        self.assertEqual(0, mock_push_cib.call_count)

        self.assertEqual(new_cib_data, env._get_cib_xml())
        self.assertEqual(0, mock_get_cib.call_count)
Пример #5
0
class UpdateAlertTest(TestCase):
    def setUp(self):
        self.mock_log = mock.MagicMock(spec_set=logging.Logger)
        self.mock_run = mock.MagicMock(spec_set=CommandRunner)
        self.mock_rep = MockLibraryReportProcessor()
        self.mock_env = LibraryEnvironment(self.mock_log,
                                           self.mock_rep,
                                           cib_data="<cib/>")

    def test_update_all(self):
        self.mock_env._push_cib_xml("""
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="value"
                    />
                    <nvpair
                        id="my-alert-instance_attributes-another"
                        name="another"
                        value="val"
                    />
                </instance_attributes>
                <meta_attributes id="my-alert-meta_attributes">
                    <nvpair
                        id="my-alert-meta_attributes-meta1"
                        name="meta1"
                        value="val1"
                    />
                </meta_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """)
        cmd_alert.update_alert(self.mock_env, "my-alert", "/another/one", {
            "instance": "",
            "my-attr": "its_val"
        }, {"meta1": "val2"}, "")
        assert_xml_equal(
            """
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/another/one">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-another"
                        name="another"
                        value="val"
                    />
                    <nvpair
                        id="my-alert-instance_attributes-my-attr"
                        name="my-attr"
                        value="its_val"
                    />
                </instance_attributes>
                <meta_attributes id="my-alert-meta_attributes">
                    <nvpair
                        id="my-alert-meta_attributes-meta1"
                        name="meta1"
                        value="val2"
                    />
                </meta_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """, self.mock_env._get_cib_xml())

    def test_update_instance_attribute(self):
        self.mock_env._push_cib_xml("""
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="value"
                    />
                </instance_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """)
        cmd_alert.update_alert(self.mock_env, "my-alert", None,
                               {"instance": "new_val"}, {}, None)
        assert_xml_equal(
            """
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="new_val"
                    />
                </instance_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """, self.mock_env._get_cib_xml())

    def test_alert_doesnt_exist(self):
        self.mock_env._push_cib_xml("""
            <cib validate-with="pacemaker-2.5">
                <configuration>
                    <alerts>
                        <alert id="alert" path="path"/>
                    </alerts>
                </configuration>
            </cib>
            """)
        assert_raise_library_error(
            lambda: cmd_alert.update_alert(self.mock_env, "unknown", "test", {
            }, {}, None),
            (Severities.ERROR, report_codes.CIB_ALERT_NOT_FOUND, {
                "alert": "unknown"
            }))
Пример #6
0
class CreateAlertTest(TestCase):
    def setUp(self):
        self.mock_log = mock.MagicMock(spec_set=logging.Logger)
        self.mock_run = mock.MagicMock(spec_set=CommandRunner)
        self.mock_rep = MockLibraryReportProcessor()
        self.mock_env = LibraryEnvironment(self.mock_log,
                                           self.mock_rep,
                                           cib_data="<cib/>")

    def test_no_path(self, mock_upgrade_cib):
        assert_raise_library_error(
            lambda: cmd_alert.create_alert(self.mock_env, None, None, None,
                                           None),
            (Severities.ERROR, report_codes.REQUIRED_OPTION_IS_MISSING, {
                "option_name": "path"
            }))
        self.assertEqual(0, mock_upgrade_cib.call_count)

    def test_upgrade_needed(self, mock_upgrade_cib):
        self.mock_env._push_cib_xml("""
            <cib validate-with="pacemaker-2.4.1">
                <configuration>
                </configuration>
            </cib>
            """)
        mock_upgrade_cib.return_value = etree.XML("""
            <cib validate-with="pacemaker-2.5.0">
                <configuration>
                </configuration>
            </cib>
            """)
        cmd_alert.create_alert(self.mock_env, "my-alert", "/my/path", {
            "instance": "value",
            "another": "val"
        }, {"meta1": "val1"}, "my description")
        assert_xml_equal(
            """
<cib validate-with="pacemaker-2.5.0">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <meta_attributes id="my-alert-meta_attributes">
                    <nvpair
                        id="my-alert-meta_attributes-meta1"
                        name="meta1"
                        value="val1"
                    />
                </meta_attributes>
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-another"
                        name="another"
                        value="val"
                    />
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="value"
                    />
                </instance_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """, self.mock_env._get_cib_xml())
        self.assertEqual(1, mock_upgrade_cib.call_count)
Пример #7
0
class CreateAlertTest(TestCase):
    def setUp(self):
        self.mock_log = mock.MagicMock(spec_set=logging.Logger)
        self.mock_run = mock.MagicMock(spec_set=CommandRunner)
        self.mock_rep = MockLibraryReportProcessor()
        self.mock_env = LibraryEnvironment(
            self.mock_log, self.mock_rep, cib_data="<cib/>"
        )

    def test_no_path(self, mock_upgrade_cib):
        assert_raise_library_error(
            lambda: cmd_alert.create_alert(
                self.mock_env, None, None, None, None
            ),
            (
                Severities.ERROR,
                report_codes.REQUIRED_OPTION_IS_MISSING,
                {"option_name": "path"}
            )
        )
        self.assertEqual(0, mock_upgrade_cib.call_count)

    def test_upgrade_needed(self, mock_upgrade_cib):
        self.mock_env._push_cib_xml(
            """
            <cib validate-with="pacemaker-2.4.1">
                <configuration>
                </configuration>
            </cib>
            """
        )
        mock_upgrade_cib.return_value = etree.XML(
            """
            <cib validate-with="pacemaker-2.5.0">
                <configuration>
                </configuration>
            </cib>
            """
        )
        cmd_alert.create_alert(
            self.mock_env,
            "my-alert",
            "/my/path",
            {
                "instance": "value",
                "another": "val"
            },
            {"meta1": "val1"},
            "my description"
        )
        assert_xml_equal(
            """
<cib validate-with="pacemaker-2.5.0">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <meta_attributes id="my-alert-meta_attributes">
                    <nvpair
                        id="my-alert-meta_attributes-meta1"
                        name="meta1"
                        value="val1"
                    />
                </meta_attributes>
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-another"
                        name="another"
                        value="val"
                    />
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="value"
                    />
                </instance_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """,
            self.mock_env._get_cib_xml()
        )
        self.assertEqual(1, mock_upgrade_cib.call_count)
Пример #8
0
class UpdateAlertTest(TestCase):
    def setUp(self):
        self.mock_log = mock.MagicMock(spec_set=logging.Logger)
        self.mock_run = mock.MagicMock(spec_set=CommandRunner)
        self.mock_rep = MockLibraryReportProcessor()
        self.mock_env = LibraryEnvironment(
            self.mock_log, self.mock_rep, cib_data="<cib/>"
        )

    def test_update_all(self):
        self.mock_env._push_cib_xml(
            """
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="value"
                    />
                    <nvpair
                        id="my-alert-instance_attributes-another"
                        name="another"
                        value="val"
                    />
                </instance_attributes>
                <meta_attributes id="my-alert-meta_attributes">
                    <nvpair
                        id="my-alert-meta_attributes-meta1"
                        name="meta1"
                        value="val1"
                    />
                </meta_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """
        )
        cmd_alert.update_alert(
            self.mock_env,
            "my-alert",
            "/another/one",
            {
                "instance": "",
                "my-attr": "its_val"
            },
            {"meta1": "val2"},
            ""
        )
        assert_xml_equal(
            """
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/another/one">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-another"
                        name="another"
                        value="val"
                    />
                    <nvpair
                        id="my-alert-instance_attributes-my-attr"
                        name="my-attr"
                        value="its_val"
                    />
                </instance_attributes>
                <meta_attributes id="my-alert-meta_attributes">
                    <nvpair
                        id="my-alert-meta_attributes-meta1"
                        name="meta1"
                        value="val2"
                    />
                </meta_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """,
            self.mock_env._get_cib_xml()
        )

    def test_update_instance_attribute(self):
        self.mock_env._push_cib_xml(
            """
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="value"
                    />
                </instance_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """
        )
        cmd_alert.update_alert(
            self.mock_env,
            "my-alert",
            None,
            {"instance": "new_val"},
            {},
            None
        )
        assert_xml_equal(
            """
<cib validate-with="pacemaker-2.5">
    <configuration>
        <alerts>
            <alert id="my-alert" path="/my/path" description="my description">
                <instance_attributes id="my-alert-instance_attributes">
                    <nvpair
                        id="my-alert-instance_attributes-instance"
                        name="instance"
                        value="new_val"
                    />
                </instance_attributes>
            </alert>
        </alerts>
    </configuration>
</cib>
            """,
            self.mock_env._get_cib_xml()
        )

    def test_alert_doesnt_exist(self):
        self.mock_env._push_cib_xml(
            """
            <cib validate-with="pacemaker-2.5">
                <configuration>
                    <alerts>
                        <alert id="alert" path="path"/>
                    </alerts>
                </configuration>
            </cib>
            """
        )
        assert_raise_library_error(
            lambda: cmd_alert.update_alert(
                self.mock_env, "unknown", "test", {}, {}, None
            ),
            (
                Severities.ERROR,
                report_codes.CIB_ALERT_NOT_FOUND,
                {"alert": "unknown"}
            )
        )