Esempio n. 1
0
    def setUp(self):
        self.cluster_state = "state"

        patcher_primitives = mock.patch(
            "pcs.lib.pacemaker.state._get_primitives_for_state_check")
        self.addCleanup(patcher_primitives.stop)
        self.get_primitives_for_state_check = patcher_primitives.start()

        patcher_roles = mock.patch(
            "pcs.lib.pacemaker.state._get_primitive_roles_with_nodes")
        self.addCleanup(patcher_roles.stop)
        self.get_primitive_roles_with_nodes = patcher_roles.start()
Esempio n. 2
0
 def test_success_read_content_from_file(self):
     mock_open = mock.mock_open()
     with mock.patch("pcs.lib.env_file.open", mock_open, create=True):
         mock_open().read.return_value = "test booth\nconfig"
         self.assertEqual(
             "test booth\nconfig",
             RealFile("some role", "/path/to.file").read()
         )
Esempio n. 3
0
    def test_sucessfully_care_about_local_file(self, mock_is_file):
        #setup, fixtures
        def next_in_line(env):
            env.booth["modified_env"] = {
                "config_file": {
                    "content": "file content",
                    "no_existing_file_expected": False,
                },
                "key_file": {
                    "content": "key file content",
                    "no_existing_file_expected": False,
                }
            }
            return "call result"
        mock_is_file.return_value = True
        mock_env = mock.MagicMock()

        mock_open = mock.mock_open()
        with mock.patch(
            "pcs.cli.booth.env.open",
            mock_open,
            create=True
        ):
            #run tested code
            booth_conf_middleware = middleware_config(
                "booth-name",
                "/local/file/path.conf",
                "/local/file/path.key",
            )

            self.assertEqual(
                "call result",
                booth_conf_middleware(next_in_line, mock_env)
            )

        #assertions
        self.assertEqual(mock_is_file.mock_calls,[
            mock.call("/local/file/path.conf"),
            mock.call("/local/file/path.key"),
        ])

        self.assertEqual(mock_env.booth["name"], "booth-name")
        self.assertEqual(mock_env.booth["config_file"], {"content": ""})
        self.assertEqual(mock_env.booth["key_file"], {"content": ""})

        self.assertEqual(mock_open.mock_calls, [
            mock.call(u'/local/file/path.conf'),
            mock.call().read(),
            mock.call(u'/local/file/path.key'),
            mock.call().read(),
            mock.call(u'/local/file/path.key', u'w'),
            mock.call().write(u'key file content'),
            mock.call().close(),
            mock.call(u'/local/file/path.conf', u'w'),
            mock.call().write(u'file content'),
            mock.call().close(),
        ])
Esempio n. 4
0
 def test_success_write_content_to_path(self):
     mock_open = mock.mock_open()
     mock_file_operation = mock.Mock()
     with mock.patch("pcs.lib.env_file.open", mock_open, create=True):
         RealFile("some role", "/etc/booth/some-name.conf").write(
             "config content", file_operation=mock_file_operation)
         mock_open.assert_called_once_with("/etc/booth/some-name.conf", "w")
         mock_open().write.assert_called_once_with("config content")
         mock_file_operation.assert_called_once_with(
             "/etc/booth/some-name.conf")
Esempio n. 5
0
    def setUp(self):
        tmpfile_patcher = mock.patch("pcs.lib.pacemaker.live.write_tmpfile")
        self.addCleanup(tmpfile_patcher.stop)
        self.mock_write_tmpfile = tmpfile_patcher.start()
        self.tmpfile_old = mock_tmpfile("old.cib")
        self.tmpfile_new = mock_tmpfile("new.cib")
        self.mock_write_tmpfile.side_effect = [
            self.tmpfile_old, self.tmpfile_new
        ]

        self.env_assist, self.config = get_env_tools(test_case=self)
Esempio n. 6
0
    def setUp(self):
        self.report_processor = MockLibraryReportProcessor(
            raise_on_errors=False
        )
        self.cluster_state = "state"

        patcher = mock.patch(
            "pcs.lib.pacemaker.state.get_resource_roles_with_nodes"
        )
        self.addCleanup(patcher.stop)
        self.get_resource_roles_with_nodes = patcher.start()
Esempio n. 7
0
    def test_sucessfully_care_about_local_file(self, mock_is_file):
        #setup, fixtures
        def next_in_line(env):
            env.booth["modified_env"] = {
                "config_file": {
                    "content": "file content",
                    "no_existing_file_expected": False,
                },
                "key_file": {
                    "content": "key file content",
                    "no_existing_file_expected": False,
                }
            }
            return "call result"

        mock_is_file.return_value = True
        mock_env = mock.MagicMock()

        mock_open = mock.mock_open()
        with mock.patch("pcs.cli.booth.env.open", mock_open, create=True):
            #run tested code
            booth_conf_middleware = middleware_config(
                "booth-name",
                "/local/file/path.conf",
                "/local/file/path.key",
            )

            self.assertEqual("call result",
                             booth_conf_middleware(next_in_line, mock_env))

        #assertions
        self.assertEqual(mock_is_file.mock_calls, [
            mock.call("/local/file/path.conf"),
            mock.call("/local/file/path.key"),
        ])

        self.assertEqual(mock_env.booth["name"], "booth-name")
        self.assertEqual(mock_env.booth["config_file"], {"content": ""})
        self.assertEqual(mock_env.booth["key_file"], {"content": ""})

        self.assertEqual(mock_open.mock_calls, [
            mock.call(u'/local/file/path.conf'),
            mock.call().read(),
            mock.call(u'/local/file/path.key'),
            mock.call().read(),
            mock.call(u'/local/file/path.key', u'w'),
            mock.call().write(u'key file content'),
            mock.call().close(),
            mock.call(u'/local/file/path.conf', u'w'),
            mock.call().write(u'file content'),
            mock.call().close(),
        ])
Esempio n. 8
0
 def test_success_write_content_to_path(self):
     mock_open = mock.mock_open()
     mock_file_operation = mock.Mock()
     with mock.patch("pcs.lib.env_file.open", mock_open, create=True):
         RealFile("some role", "/etc/booth/some-name.conf").write(
             "config content",
             file_operation=mock_file_operation
         )
         mock_open.assert_called_once_with("/etc/booth/some-name.conf", "w")
         mock_open().write.assert_called_once_with("config content")
         mock_file_operation.assert_called_once_with(
             "/etc/booth/some-name.conf"
         )
Esempio n. 9
0
 def test_success_binary(self):
     mock_open = mock.mock_open()
     mock_file_operation = mock.Mock()
     with mock.patch("pcs.lib.env_file.open", mock_open, create=True):
         RealFile("some role", "/etc/booth/some-name.conf").write(
             "config content".encode("utf-8"),
             file_operation=mock_file_operation,
             is_binary=True)
         mock_open.assert_called_once_with("/etc/booth/some-name.conf",
                                           "wb")
         mock_open().write.assert_called_once_with(
             "config content".encode("utf-8"))
         mock_file_operation.assert_called_once_with(
             "/etc/booth/some-name.conf")
Esempio n. 10
0
 def test_success_binary(self):
     mock_open = mock.mock_open()
     mock_file_operation = mock.Mock()
     with mock.patch("pcs.lib.env_file.open", mock_open, create=True):
         RealFile("some role", "/etc/booth/some-name.conf").write(
             "config content".encode("utf-8"),
             file_operation=mock_file_operation,
             is_binary=True
         )
         mock_open.assert_called_once_with("/etc/booth/some-name.conf", "wb")
         mock_open().write.assert_called_once_with(
             "config content".encode("utf-8")
         )
         mock_file_operation.assert_called_once_with(
             "/etc/booth/some-name.conf"
         )
Esempio n. 11
0
def patch_env(call_queue, config, init_env):
    #It is mandatory to patch some env objects/methods. It is ok when command
    #does not use this objects/methods and specify no call for it. But it would
    #be a problem when the test succeded because the live call respond correctly
    #by accident. Such test would fails on different machine (with another live
    #environment)

    patcher_list = [
        patch_lib_env(
            "cmd_runner", lambda env: spy.Runner(init_env.cmd_runner())
            if config.spy else Runner(call_queue,
                                      env_vars={}
                                      if not config.env.cib_tempfile else {
                                          "CIB_file": config.env.cib_tempfile,
                                      })),
        mock.patch(
            "pcs.lib.env.get_local_corosync_conf",
            get_get_local_corosync_conf(call_queue)
            if not config.spy else spy.get_local_corosync_conf),
        patch_lib_env(
            "get_node_communicator", lambda env: NodeCommunicator(call_queue)
            if not config.spy else spy.NodeCommunicator(
                init_env.get_node_communicator()))
    ]

    #It is not always desirable to patch the method push_cib. Some tests can
    #patch only the internals (runner...). So push_cib is patched only when it
    #is explicitly configured
    if is_push_cib_call_in(call_queue):
        patcher_list.append(patch_lib_env("push_cib",
                                          get_push_cib(call_queue)))

    for patcher in patcher_list:
        patcher.start()

    def unpatch():
        for patcher in patcher_list:
            patcher.stop()

    return unpatch
Esempio n. 12
0
 def patch(target, *args, **kwargs):
     return mock.patch("{0}.{1}".format(prefix, target), *args, **kwargs)
Esempio n. 13
0
def patch_env(call_queue, config, init_env):
    #It is mandatory to patch some env objects/methods. It is ok when command
    #does not use this objects/methods and specify no call for it. But it would
    #be a problem when the test succeded because the live call respond correctly
    #by accident. Such test would fails on different machine (with another live
    #environment)

    get_cmd_runner = init_env.cmd_runner
    get_node_communicator = init_env.get_node_communicator

    patcher_list = [
        patch_lib_env(
            "cmd_runner", lambda env: spy.Runner(get_cmd_runner())
            if config.spy else Runner(call_queue,
                                      env_vars={}
                                      if not config.env.cib_tempfile else {
                                          "CIB_file": config.env.cib_tempfile,
                                      })),
        mock.patch(
            "pcs.lib.env.get_local_corosync_conf",
            get_get_local_corosync_conf(call_queue)
            if not config.spy else spy.get_local_corosync_conf),
        patch_lib_env(
            "get_node_communicator", lambda env: NodeCommunicator(call_queue)
            if not config.spy else spy.NodeCommunicator(get_node_communicator(
            )))
    ]

    if is_fs_call_in(call_queue):
        fs_mock = get_fs_mock(call_queue)
        builtin = (
            ("__builtin__" if sys.version_info[0] == 2 else "builtins") +
            ".{0}").format

        patcher_list.extend([
            mock.patch(builtin("open"), fs_mock("open", open)),
            mock.patch("os.path.exists",
                       fs_mock("os.path.exists", os.path.exists)),
            mock.patch("os.chmod", fs_mock("os.chmod", os.chmod)),
            mock.patch("os.chown", fs_mock("os.chown", os.chown)),
        ])

    # It is not always desirable to patch these methods. Some tests may patch
    # only the internals (runner etc.). So these methods are only patched when
    # it is explicitly configured.
    if is_push_cib_call_in(call_queue):
        patcher_list.append(patch_lib_env("push_cib",
                                          get_push_cib(call_queue)))
    if is_push_corosync_conf_call_in(call_queue):
        patcher_list.append(
            patch_lib_env("push_corosync_conf",
                          get_push_corosync_conf(call_queue)))

    for patcher in patcher_list:
        patcher.start()

    def unpatch():
        for patcher in patcher_list:
            patcher.stop()

    return unpatch
Esempio n. 14
0
File: misc.py Progetto: idevat/pcs
 def patch(target, *args, **kwargs):
     return mock.patch("{0}.{1}".format(target_prefix, target), *args, **kwargs)
Esempio n. 15
0
 def test_success_read_content_from_file(self):
     mock_open = mock.mock_open()
     with mock.patch("pcs.lib.env_file.open", mock_open, create=True):
         mock_open().read.return_value = "test booth\nconfig"
         self.assertEqual("test booth\nconfig",
                          RealFile("some role", "/path/to.file").read())