예제 #1
0
    def test_timeouts(self):
        """Test shutdown / sigterm timeouts"""
        setup = mongodb_setup.MongodbSetup(self.config)
        self.assertEqual(setup.shutdown_ms, 540000)
        self.assertEqual(setup.sigterm_ms, 60000)

        self.config["mongodb_setup"]["timeouts"] = {
            "shutdown_ms": "shutdown",
            "sigterm_ms": "sigterm",
        }
        setup = mongodb_setup.MongodbSetup(self.config)
        self.assertEqual(setup.shutdown_ms, "shutdown")
        self.assertEqual(setup.sigterm_ms, "sigterm")
예제 #2
0
    def test__start_with_auth3(self):
        """ Test _start with auth enabled for is_restart=True and clean_db_dir=False."""
        setup = mongodb_setup.MongodbSetup(config=self.config)

        mock_add_default_users = mock.MagicMock(name="add_default_users")
        mock_shutdown = mock.MagicMock(name="shutdown")
        setup.add_default_users = mock_add_default_users
        setup.shutdown = mock_shutdown
        with mock.patch("dsi.mongodb_setup.run_threads"
                        ) as mock_run_threads, mock.patch(
                            "dsi.mongodb_setup.partial") as mock_partial:
            mock_run_threads.return_value = [True, True]
            mock_partial.return_value = "threads"
            self.assertEqual(
                setup._start(is_restart=True,
                             restart_clean_db_dir=False,
                             restart_clean_logs=True),
                True,
            )
            setup.shutdown.assert_not_called()
            mock_partial.assert_has_calls([
                mock.call(
                    setup.start_cluster,
                    cluster=setup.clusters[0],
                    is_restart=True,
                    restart_clean_db_dir=False,
                    restart_clean_logs=True,
                    enable_auth=True,
                )
            ])
        mock_add_default_users.assert_not_called()
예제 #3
0
        def _test_start(mock_run_pre_post_commands,
                        download_status=False,
                        pre_cluster_start=False):
            test_config = copy.deepcopy(self.config)
            if pre_cluster_start:
                test_config["mongodb_setup"]["pre_cluster_start"] = [{
                    "on_all_hosts": {
                        "retrieve_files": [{
                            "source": "foo",
                            "target": "bar"
                        }]
                    }
                }]

            setup = mongodb_setup.MongodbSetup(config=test_config)
            setup.downloader = mock.MagicMock()
            setup.downloader.download_and_extract = mock.MagicMock(
                name="downloader")

            setup._start = mock.MagicMock(name="_start")
            setup.destroy = mock.MagicMock(name="destroy")
            # shutdown should never be called in this path
            setup.shutdown = mock.MagicMock(name="shutdown")
            setup.downloader.download_and_extract.return_value = download_status

            if not download_status:
                self.assertEqual(setup.start(), False)
                setup._start.assert_not_called()
            else:
                self.assertEqual(setup.start(), True)
                setup._start.assert_called_once()

            setup.destroy.assert_called_once_with(60000)
            setup.shutdown.assert_not_called()
            setup.downloader.download_and_extract.assert_called_once()
예제 #4
0
 def test_add_default_users(self):
     """
     Test MongodbSetup.add_default_users.
     """
     setup = mongodb_setup.MongodbSetup(config=self.config)
     mock_cluster1 = mock.MagicMock(name="cluster1")
     mock_cluster2 = mock.MagicMock(name="cluster2")
     setup.clusters = [mock_cluster1, mock_cluster2]
     setup.add_default_users()
     for cluster in setup.clusters:
         cluster.add_default_users.assert_called_once()
예제 #5
0
    def test_restart_auth_enabled(self):
        """ Test restart when auth is enabled. Make sure shutdown is called with auth enabled. """
        setup = mongodb_setup.MongodbSetup(config=self.config)

        setup._start = mock.MagicMock(name="_start")
        setup._start.return_value = "start clusters"

        setup.destroy = mock.MagicMock(name="destroy")
        setup.shutdown = mock.MagicMock(name="shutdown")
        setup.restart()
        setup.shutdown.assert_called_once_with(setup.shutdown_ms, True)
예제 #6
0
    def test_destroy(self):
        """Test MongodbSetup.destroy"""

        setup = mongodb_setup.MongodbSetup(config=self.config)
        mock_cluster1 = mock.MagicMock(name="cluster1")
        mock_cluster2 = mock.MagicMock(name="cluster2")
        setup.clusters = [mock_cluster1, mock_cluster2]
        with mock.patch("dsi.mongodb_setup.run_threads"
                        ) as mock_run_threads, mock.patch(
                            "dsi.mongodb_setup.partial") as mock_partial:

            mock_run_threads.return_value = [True]
            self.assertTrue(setup.destroy(1))
            mock_partial.assert_has_calls([
                mock.call(mock_cluster1.destroy, 1),
                mock.call(mock_cluster2.destroy, 1)
            ])
예제 #7
0
def dispatch_commands(command_key, command_list, config, current_test_id=None):
    """ Routes commands to the appropriate command runner. The command runner will run the command.

    :param str command_key: The key to use to find a command list to execute in each of the
    command_dicts. Used for error handling only.
    :param list(dict) command_list: A list of commands to run
    :param dict(ConfigDict) config: The system configuration.
    :param str current_test_id: Indicates the id for the test related to the current set of
    commands. If there is not a specific test related to the current set of commands, the value of
    current_test_id will be None.
    """

    SLOG.debug(
        "dispatch_commands",
        command_key=command_key,
        command_list=command_list,
        current_test_id=current_test_id,
    )
    # Most notably, the prefix is used for directory name under reports/.
    # It is either the test id (fio, ycsb_load...) or the command itself (post_task).
    prefix = current_test_id if current_test_id else command_key

    for item in command_list:
        # Item should be a map with one entry
        assert isinstance(item, MutableMapping), "item in list isn't a dict"
        assert len(item.keys()) == 1, "item has more than one entry"
        for target, command in item.items():
            if target == "on_atlas":
                run_atlas_command(target, command, config, prefix)
            elif target.startswith("on_"):
                run_host_command(target, command, config, prefix)
            elif target == "restart_mongodb":
                # pylint: disable=import-outside-toplevel
                from dsi import mongodb_setup

                mongo_controller = mongodb_setup.MongodbSetup(config)
                clean_db_dir = command["clean_db_dir"]
                clean_logs = command["clean_logs"]
                if not mongo_controller.restart(clean_db_dir, clean_logs):
                    raise Exception("Error restarting mongodb")
            else:
                raise KeyError("Unknown {} target {}".format(
                    command_key, target))
예제 #8
0
    def test_start1(self, host):
        """Starting ignores shutdown fails """
        setup = mongodb_setup.MongodbSetup(config=self.config)
        setup.host = host
        setup.downloader = mock.MagicMock()

        host.run = mock.MagicMock()
        mongodb_cluster.MongoNode.wait_until_up = mock.MagicMock()
        setup.destroy = mock.MagicMock(name="destroy")
        setup.shutdown = mock.MagicMock(name="shutdown")
        setup.shutdown.return_value = True
        setup.downloader = mock.MagicMock()

        with mock.patch("dsi.mongodb_setup.run_threads") as mock_run_threads:
            mock_run_threads.return_value = [True]
            self.assertTrue(setup.restart())
            setup.destroy.assert_called_once_with(60000)
            setup.shutdown.assert_called_once_with(540000, True)
            setup.downloader.download_and_extract.assert_not_called()
            mock_run_threads.assert_called_once()
예제 #9
0
    def _test__start(self, run_threads, success=True):
        setup = mongodb_setup.MongodbSetup(config=self.config)
        setup.downloader = mock.MagicMock()
        setup.downloader.download_and_extract.return_value = False
        mongodb_cluster.MongoNode.wait_until_up = mock.MagicMock()
        setup.destroy = mock.MagicMock(name="destroy")
        setup.shutdown = mock.MagicMock(name="shutdown")
        setup.shutdown.return_value = False

        with mock.patch(
                "dsi.mongodb_setup.run_threads"
        ) as mock_run_threads, mock.patch(
                "dsi.mongodb_setup.partial") as mock_partial, mock.patch(
                    "dsi.common.mongodb_cluster.MongoNode.run_mongo_shell"):
            mock_run_threads.return_value = run_threads
            mock_partial.return_value = "threads"

            self.assertEqual(setup._start(), success)
            calls = [
                mock.call(
                    setup.start_cluster,
                    cluster=setup.clusters[0],
                    is_restart=False,
                    restart_clean_db_dir=None,
                    restart_clean_logs=None,
                    enable_auth=False,
                )
            ]
            mock_partial.assert_has_calls(calls)
            setup.destroy.assert_not_called()
            if success:
                setup.shutdown.assert_has_calls([mock.call(540000)
                                                 ])  # shutdown for setup
            else:
                setup.shutdown.assert_has_calls(
                    [mock.call(540000),
                     mock.call(540000),
                     mock.call(540000)])
            setup.downloader.download_and_extract.assert_not_called()
            mock_run_threads.assert_has_calls(
                [mock.call(["threads"], daemon=True)])
예제 #10
0
        def _test_restart(shutdown=True):
            setup = mongodb_setup.MongodbSetup(config=self.config)

            setup._start = mock.MagicMock(name="_start")
            setup._start.return_value = "start clusters"

            setup.destroy = mock.MagicMock(name="destroy")
            setup.destroy.return_value = shutdown
            setup.shutdown = mock.MagicMock(name="shutdown")
            setup.shutdown.return_value = shutdown

            if not shutdown:
                self.assertEqual(setup.restart(), False)
                setup._start.assert_not_called()
            else:
                self.assertEqual(setup.restart(), "start clusters")
                setup._start.assert_called_once_with(is_restart=True,
                                                     restart_clean_db_dir=None,
                                                     restart_clean_logs=None)
            setup.destroy.assert_called_once_with(60000)
            setup.shutdown.assert_called_once_with(540000, True)