def helper_test_checkout_repos(self, source, target, commands, branch=None, verbose=True): """ test_checkout_repos common test code """ local = LocalHost() # Test with non-existing target self.assertFalse(os.path.exists(target)) with patch("dsi.common.host.mkdir_p") as mock_mkdir_p, patch( "dsi.common.local_host.LocalHost.exec_command" ) as mock_exec_command: local.checkout_repos(source, target, verbose=verbose, branch=branch) mock_mkdir_p.assert_called_with(self.parent_dir) if len(commands) == 1: mock_exec_command.assert_called_once() mock_exec_command.assert_called_with(commands[0]) else: for command in commands: mock_exec_command.assert_any_call(command)
def make_host(host_info, mongodb_auth_settings=None, use_tls=False): """ Create a host object based off of host_ip_or_name. The code that receives the host is responsible for calling close on the host instance. Each RemoteHost instance can have 2*n+1 open sockets (where n is the number of exec_command calls with Pty=True) otherwise n is 1 so there is a max of 3 open sockets. :param mongodb_auth_settings: MongoDB auth settings dictionary :param namedtuple host_info: Public IP address or the string localhost, category and offset :rtype: Host """ host = None if host_info.public_ip in ["localhost", "127.0.0.1", "0.0.0.0"]: LOG.debug("Making localhost for %s", host_info.public_ip) host = LocalHost(mongodb_auth_settings, use_tls) else: LOG.debug("Making remote host for %s using ssh", host_info.public_ip) host = RemoteSSHHost( host_info.public_ip, host_info.ssh_user, host_info.ssh_key_file, mongodb_auth_settings, use_tls, ) host.alias = "{category}.{offset}".format(category=host_info.category, offset=host_info.offset) return host
def test_kill_mongo_procs(self): """ Test kill_mongo_procs """ local = LocalHost() local.kill_remote_procs = MagicMock(name="kill_remote_procs") local.kill_remote_procs.return_value = True self.assertTrue(local.kill_mongo_procs()) local.kill_remote_procs.assert_called_once_with("mongo", 9, max_time_ms=30000)
def test_checkout_repos_existing_target(self): # Test with existing target that is a git repository local = LocalHost() source = "https://github.com/mongodb/stitch-js-sdk.git" target = os.path.join(self.parent_dir, "stitch-js-sdk") command = ["cd", target, "&&", "git", "status"] with patch("dsi.common.host.os.path.isdir") as mock_isdir, patch( "dsi.common.host.mkdir_p") as mock_mkdir_p, patch( "dsi.common.local_host.LocalHost.exec_command" ) as mock_exec_command: mock_isdir.return_value = True mock_exec_command.return_value = 0 local.checkout_repos(source, target) mock_mkdir_p.assert_not_called() mock_exec_command.assert_called_once() mock_exec_command.assert_called_with(command)
def __init__(self, top_config, runner=None, retriever=None): # type: (typ.Union[config.ConfigDict, dict], Host, CedarRetriever) -> CuratorRunner """ :param runner: host.py host :param top_config: top-level config-dict :param retriever: CedarRetriever to use. Will construct one from given config if None """ self.top_config = top_config self.retriever = retriever if retriever is not None else CedarRetriever( top_config) self.runner = runner if runner is not None else LocalHost() self.auth = _auth(top_config)
def send(report, top_config, runner_host=None): # type: (Report, config.ConfigDict, Host) -> None """ :param report: CedarReport :param top_config: ConfigDict top-level config-dict :param runner_host: host.py host to run on :return: nothing """ if runner_host is None: runner_host = LocalHost() report.write_report() runner = CuratorRunner(top_config, runner_host) runner.run_curator()
def test_checkout_repos(self): """ Test Host.checkout_repos command """ # Only testing on LocalHost since `checkout_repos` is implemented in the base class and not # overidden local = LocalHost() # Test with existing target that is not a git repository source = "[email protected]:mongodb/mongo.git" target = os.path.expanduser("~") command = ["cd", target, "&&", "git", "status"] with patch("dsi.common.host.mkdir_p") as mock_mkdir_p, patch( "dsi.common.local_host.LocalHost.exec_command" ) as mock_exec_command: self.assertRaises(UserWarning, local.checkout_repos, source, target) mock_mkdir_p.assert_not_called() mock_exec_command.assert_called_once() mock_exec_command.assert_called_with(command)
def test_kill_remote_procs(self): """ Test kill_remote_procs """ local = LocalHost() local.run = MagicMock(name="run") local.run.return_value = False self.assertTrue(local.kill_remote_procs("mongo")) calls = [ call(["pkill", "-9", "mongo"], quiet=True), call(["pgrep", "mongo"], quiet=True) ] local.run.assert_has_calls(calls) with patch( "dsi.common.host_utils.create_timer") as mock_create_watchdog: local.run = MagicMock(name="run") local.run.return_value = False local.kill_remote_procs("mongo", max_time_ms=None) mock_create_watchdog.assert_called_once_with(ANY, None) with patch( "dsi.common.host_utils.create_timer") as mock_create_watchdog: local.run = MagicMock(name="run") local.run.return_value = False local.kill_remote_procs("mongo", max_time_ms=0, delay_ms=99) mock_create_watchdog.assert_called_once_with(ANY, 99) with patch( "dsi.common.host_utils.create_timer") as mock_create_watchdog: local = LocalHost() local.run = MagicMock(name="run") local.run.return_value = True mock_is_timed_out = MagicMock(name="is_timed_out") mock_create_watchdog.return_value = mock_is_timed_out mock_is_timed_out.side_effect = [False, True] self.assertFalse(local.kill_remote_procs("mongo", delay_ms=1)) local = LocalHost() local.run = MagicMock(name="run") local.run.side_effect = [False, True, False, False] self.assertTrue( local.kill_remote_procs("mongo", signal_number=15, delay_ms=1)) calls = [ call(["pkill", "-15", "mongo"], quiet=True), call(["pgrep", "mongo"], quiet=True), call(["pkill", "-15", "mongo"], quiet=True), call(["pgrep", "mongo"], quiet=True), ] local.run.assert_has_calls(calls)