def testExecute_nativeSsh_sshArgInConfig(self, mock_build_lab_config_pool):
        """Test execute."""
        mock_build_lab_config_pool.return_value = self.mock_lab_config_pool
        host_config = host_util.lab_config.CreateHostConfig(
            cluster_name='cluster1',
            hostname='host1',
            host_login_name='user1',
            ssh_arg='-o op1=v1 -o op2=v2')
        self.mock_lab_config_pool.GetHostConfigs.side_effect = [[host_config]]
        args_dict = self.default_args.copy()
        args_dict.update(service_account_json_key_path='path/to/key', )
        args = mock.MagicMock(**args_dict)

        host_util.Execute(args)

        self.mock_lab_config_pool.GetHostConfigs.assert_called_once_with()
        self.mock_create_context.assert_has_calls([
            mock.call('host1',
                      'user1',
                      ssh_config=ssh_util.SshConfig(
                          user='******',
                          hostname='host1',
                          ssh_args='-o op1=v1 -o op2=v2',
                          use_native_ssh=True),
                      sudo_ssh_config=None)
        ])
        self.assertSameElements(['host1'], self.mock_func_calls.keys())
    def testExecute(self, mock_build_lab_config_pool):
        """Test execute."""
        mock_build_lab_config_pool.return_value = self.mock_lab_config_pool
        self.mock_lab_config_pool.GetHostConfigs.side_effect = [[
            self.host_config1, self.host_config2
        ]]
        args_dict = self.default_args.copy()
        args_dict.update(service_account_json_key_path='path/to/key', )
        args = mock.MagicMock(**args_dict)

        host_util.Execute(args)

        self.mock_lab_config_pool.GetHostConfigs.assert_called_once_with()
        self.mock_create_context.assert_has_calls([
            mock.call('host1',
                      'user1',
                      ssh_config=self.ssh_config1,
                      sudo_ssh_config=None),
            mock.call('host2',
                      'user1',
                      ssh_config=self.ssh_config2,
                      sudo_ssh_config=None)
        ])
        self.assertSameElements(['host1', 'host2'],
                                self.mock_func_calls.keys())
        self.assertEqual(args, self.mock_func_calls['host1'][0])
        self.assertEqual(
            self.host_config1.SetServiceAccountJsonKeyPath('path/to/key'),
            self.mock_func_calls['host1'][1])
        self.assertEqual(args, self.mock_func_calls['host2'][0])
        self.assertEqual(
            self.host_config2.SetServiceAccountJsonKeyPath('path/to/key'),
            self.mock_func_calls['host2'][1])
Ejemplo n.º 3
0
def Main():
    """The entry point function for lab CLI."""
    parser = CreateParser()
    args = parser.parse_args()
    args.cli_path = os.path.realpath(sys.argv[0])
    global logger
    logger = cli_util.CreateLogger(args)
    if not args.no_check_update:
        try:
            new_path = cli_util.CheckAndUpdateTool(
                args.cli_path, cli_update_url=args.cli_update_url)
            if new_path:
                logger.debug('CLI is updated.')
                os.execv(new_path, [new_path] + sys.argv[1:])
        except Exception as e:
            logger.warning('Failed to check/update tool: %s', e)
    if not args.action:
        parser.print_usage()
        return
    if args.action == 'version':
        cli_util.PrintVersion()
        return

    lab_config_pool = host_util.BuildLabConfigPool(args.lab_config_path)
    lab_config = lab_config_pool.GetLabConfig()
    service_account_key_path = None
    # Use service account in secret first, since this is the most secure way.
    if lab_config.secret_project_id and lab_config.service_account_key_secret_id:
        service_account_key_path = _GetServiceAccountKeyFilePath(
            lab_config.secret_project_id,
            lab_config.service_account_key_secret_id)
    service_account_key_path = (service_account_key_path
                                or lab_config.service_account_json_key_path
                                or args.service_account_json_key_path)
    if service_account_key_path:
        logger = cli_util.CreateLogger(args, service_account_key_path)
    else:
        logger.warning('There is no service account key secret or path set.')
    args.service_account_json_key_path = service_account_key_path
    host_util.Execute(args, lab_config_pool)
    def testExecute_askPass(self, mock_build_lab_config_pool, mock_getpass):
        """Test execute on multiple hosts with password."""
        mock_getpass.side_effect = ['login_pswd', 'sudo_pswd']
        mock_build_lab_config_pool.return_value = self.mock_lab_config_pool
        self.mock_lab_config_pool.GetHostConfigs.side_effect = [[
            self.host_config1, self.host_config2
        ]]
        args_dict = self.default_args.copy()
        args_dict.update(
            ask_login_password=True,
            ask_sudo_password=True,
            sudo_user='******',
            service_account_json_key_path='path/to/key',
        )
        args = mock.MagicMock(**args_dict)

        host_util.Execute(args)

        self.mock_lab_config_pool.GetHostConfigs.assert_called_once_with()
        self.mock_create_context.assert_has_calls([
            mock.call('host1',
                      'user1',
                      ssh_config=ssh_util.SshConfig(user='******',
                                                    hostname='host1',
                                                    password='******'),
                      sudo_ssh_config=ssh_util.SshConfig(
                          user='******',
                          hostname='host1',
                          password='******')),
            mock.call('host2',
                      'user1',
                      ssh_config=ssh_util.SshConfig(user='******',
                                                    hostname='host2',
                                                    password='******'),
                      sudo_ssh_config=ssh_util.SshConfig(user='******',
                                                         hostname='host2',
                                                         password='******'))
        ])
        self.assertSameElements(['host1', 'host2'],
                                self.mock_func_calls.keys())