当前位置: 首页>>代码示例>>Python>>正文


Python Shell.run方法代码示例

本文整理汇总了Python中st2client.shell.Shell.run方法的典型用法代码示例。如果您正苦于以下问题:Python Shell.run方法的具体用法?Python Shell.run怎么用?Python Shell.run使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在st2client.shell.Shell的用法示例。


在下文中一共展示了Shell.run方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_failed_to_get_locale_encoding_warning_is_printed

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
    def test_failed_to_get_locale_encoding_warning_is_printed(self, mock_logger):
        shell = Shell()
        shell.run(argv=['trigger', 'list'])

        call_args = mock_logger.warn.call_args[0][0]
        self.assertTrue('Locale unknown with encoding unknown which is not UTF-8 is used.' in
                        call_args)
开发者ID:nzlosh,项目名称:st2,代码行数:9,代码来源:test_shell.py

示例2: test_get_version_package_metadata_file_exists_stable_version

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
    def test_get_version_package_metadata_file_exists_stable_version(self):
        # stable version, package metadata file exists on disk - no git revision should be included
        package_metadata_path = self._write_mock_package_metadata_file()
        st2client.shell.PACKAGE_METADATA_FILE_PATH = package_metadata_path

        shell = Shell()
        shell.run(argv=['--version'])

        self.version_output.seek(0)
        stderr = self.version_output.read()
        self.assertTrue('v2.8.0, on Python' in stderr)
开发者ID:nzlosh,项目名称:st2,代码行数:13,代码来源:test_shell.py

示例3: test_help_command_line_arg_works_for_supported_commands

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
    def test_help_command_line_arg_works_for_supported_commands(self):
        shell = Shell()

        for command in self.COMMANDS:
            # First test longhang notation
            argv = command + ['--help']

            try:
                result = shell.run(argv)
            except SystemExit as e:
                self.assertEqual(e.code, 0)
            else:
                self.assertEqual(result, 0)

            stdout = self.stdout.getvalue()

            self.assertTrue('usage:' in stdout)
            self.assertTrue(' '.join(command) in stdout)
            # self.assertTrue('positional arguments:' in stdout)
            self.assertTrue('optional arguments:' in stdout)

            # Reset stdout and stderr after each iteration
            self._reset_output_streams()

            # Then shorthand notation
            argv = command + ['-h']

            try:
                result = shell.run(argv)
            except SystemExit as e:
                self.assertEqual(e.code, 0)
            else:
                self.assertEqual(result, 0)

            stdout = self.stdout.getvalue()

            self.assertTrue('usage:' in stdout)
            self.assertTrue(' '.join(command) in stdout)
            # self.assertTrue('positional arguments:' in stdout)
            self.assertTrue('optional arguments:' in stdout)

            # Verify that the actual help usage string was triggered and not the invalid
            # "too few arguments" which would indicate command doesn't actually correctly handle
            # --help flag
            self.assertTrue('too few arguments' not in stdout)

            self._reset_output_streams()
开发者ID:StackStorm,项目名称:st2,代码行数:49,代码来源:test_commands.py

示例4: test_dont_warn_multiple_times

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
    def test_dont_warn_multiple_times(self):
        mock_temp_dir_path = tempfile.mkdtemp()
        mock_config_dir_path = os.path.join(mock_temp_dir_path, 'testconfig')
        mock_config_path = os.path.join(mock_config_dir_path, 'config')

        # Make the temporary config directory
        os.makedirs(mock_config_dir_path)

        old_perms = os.stat(mock_config_dir_path).st_mode
        new_perms = old_perms | 0o7
        os.chmod(mock_config_dir_path, new_perms)

        # Make the temporary config file
        shutil.copyfile(CONFIG_FILE_PATH_FULL, mock_config_path)
        os.chmod(mock_config_path, 0o777)  # nosec

        shell = Shell()
        shell.LOG = mock.Mock()

        # Test without token.
        shell.run(['--config-file', mock_config_path, 'action', 'list'])

        self.assertEqual(shell.LOG.warn.call_count, 2)
        self.assertEqual(
            shell.LOG.warn.call_args_list[0][0][0][:63],
            'The StackStorm configuration directory permissions are insecure')
        self.assertEqual(
            shell.LOG.warn.call_args_list[1][0][0][:58],
            'The StackStorm configuration file permissions are insecure')

        self.assertEqual(shell.LOG.info.call_count, 2)
        self.assertEqual(
            shell.LOG.info.call_args_list[0][0][0], "The SGID bit is not "
            "set on the StackStorm configuration directory.")

        self.assertEqual(
            shell.LOG.info.call_args_list[1][0][0], 'Skipping parsing CLI config')
开发者ID:nzlosh,项目名称:st2,代码行数:39,代码来源:test_shell.py

示例5: TestShell

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
class TestShell(base.BaseCLITestCase):
    capture_output = True

    def __init__(self, *args, **kwargs):
        super(TestShell, self).__init__(*args, **kwargs)
        self.shell = Shell()

    def test_endpoints_default(self):
        base_url = 'http://127.0.0.1'
        auth_url = 'http://127.0.0.1:9100'
        api_url = 'http://127.0.0.1:9101/v1'
        args = ['trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)

    def test_endpoints_base_url_from_cli(self):
        base_url = 'http://www.st2.com'
        auth_url = 'http://www.st2.com:9100'
        api_url = 'http://www.st2.com:9101/v1'
        args = ['--url', base_url, 'trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)

    def test_endpoints_base_url_from_env(self):
        base_url = 'http://www.st2.com'
        auth_url = 'http://www.st2.com:9100'
        api_url = 'http://www.st2.com:9101/v1'
        os.environ['ST2_BASE_URL'] = base_url
        args = ['trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)

    def test_endpoints_override_from_cli(self):
        base_url = 'http://www.st2.com'
        auth_url = 'http://www.st2.com:8888'
        api_url = 'http://www.stackstorm1.com:9101/v1'
        args = ['--url', base_url,
                '--auth-url', auth_url,
                '--api-url', api_url,
                'trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)

    def test_endpoints_override_from_env(self):
        base_url = 'http://www.st2.com'
        auth_url = 'http://www.st2.com:8888'
        api_url = 'http://www.stackstorm1.com:9101/v1'
        os.environ['ST2_BASE_URL'] = base_url
        os.environ['ST2_AUTH_URL'] = auth_url
        os.environ['ST2_API_URL'] = api_url
        args = ['trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)

    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(return_value=base.FakeResponse(json.dumps(base.RESOURCES), 200, 'OK')))
    def test_exit_code_on_success(self):
        argv = ['trigger', 'list']
        self.assertEqual(self.shell.run(argv), 0)

    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(return_value=base.FakeResponse(None, 500, 'INTERNAL SERVER ERROR')))
    def test_exit_code_on_error(self):
        argv = ['trigger', 'list']
        self.assertEqual(self.shell.run(argv), 1)

    def _validate_parser(self, args_list, is_subcommand=True):
        for args in args_list:
            ns = self.shell.parser.parse_args(args)
            func = (self.shell.commands[args[0]].run_and_print
                    if not is_subcommand
                    else self.shell.commands[args[0]].commands[args[1]].run_and_print)
            self.assertEqual(ns.func, func)

    def test_action(self):
        args_list = [
            ['action', 'list'],
            ['action', 'get', 'abc'],
            ['action', 'create', '/tmp/action.json'],
            ['action', 'update', '123', '/tmp/action.json'],
            ['action', 'delete', 'abc'],
            ['action', 'execute', '-h'],
            ['action', 'execute', 'remote', '-h'],
#.........这里部分代码省略.........
开发者ID:azamsheriff,项目名称:st2,代码行数:103,代码来源:test_shell.py

示例6: ActionExecutionTailCommandTestCase

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
class ActionExecutionTailCommandTestCase(BaseCLITestCase):
    capture_output = True

    def __init__(self, *args, **kwargs):
        super(ActionExecutionTailCommandTestCase, self).__init__(*args, **kwargs)
        self.shell = Shell()

    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(return_value=base.FakeResponse(json.dumps(MOCK_LIVEACTION_1_SUCCEEDED),
                                                      200, 'OK')))
    def test_tail_simple_execution_already_finished_succeeded(self):
        argv = ['execution', 'tail', 'idfoo1']

        self.assertEqual(self.shell.run(argv), 0)
        stdout = self.stdout.getvalue()
        stderr = self.stderr.getvalue()
        self.assertTrue('Execution idfoo1 has completed (status=succeeded)' in stdout)
        self.assertEqual(stderr, '')

    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(return_value=base.FakeResponse(json.dumps(MOCK_LIVEACTION_2_FAILED),
                                                      200, 'OK')))
    def test_tail_simple_execution_already_finished_failed(self):
        argv = ['execution', 'tail', 'idfoo2']

        self.assertEqual(self.shell.run(argv), 0)
        stdout = self.stdout.getvalue()
        stderr = self.stderr.getvalue()
        self.assertTrue('Execution idfoo2 has completed (status=failed)' in stdout)
        self.assertEqual(stderr, '')

    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(return_value=base.FakeResponse(json.dumps(MOCK_LIVEACTION_1_RUNNING),
                                                      200, 'OK')))
    @mock.patch('st2client.client.StreamManager', autospec=True)
    def test_tail_simple_execution_running_no_data_produced(self, mock_stream_manager):
        argv = ['execution', 'tail', 'idfoo1']

        MOCK_EVENTS = [
            MOCK_LIVEACTION_1_SUCCEEDED
        ]

        mock_cls = mock.Mock()
        mock_cls.listen = mock.Mock()
        mock_listen_generator = mock.Mock()
        mock_listen_generator.return_value = MOCK_EVENTS
        mock_cls.listen.side_effect = mock_listen_generator
        mock_stream_manager.return_value = mock_cls

        self.assertEqual(self.shell.run(argv), 0)
        self.assertEqual(mock_listen_generator.call_count, 1)

        stdout = self.stdout.getvalue()
        stderr = self.stderr.getvalue()

        expected_result = """
Execution idfoo1 has completed (status=succeeded).
"""
        self.assertEqual(stdout, expected_result)
        self.assertEqual(stderr, '')

    @mock.patch.object(
        httpclient.HTTPClient, 'get',
        mock.MagicMock(return_value=base.FakeResponse(json.dumps(MOCK_LIVEACTION_3_RUNNING),
                                                      200, 'OK')))
    @mock.patch('st2client.client.StreamManager', autospec=True)
    def test_tail_simple_execution_running_with_data(self, mock_stream_manager):
        argv = ['execution', 'tail', 'idfoo3']

        MOCK_EVENTS = [
            MOCK_LIVEACTION_3_RUNNING,
            MOCK_OUTPUT_1,
            MOCK_OUTPUT_2,
            MOCK_LIVEACTION_3_SUCCEDED
        ]

        mock_cls = mock.Mock()
        mock_cls.listen = mock.Mock()
        mock_listen_generator = mock.Mock()
        mock_listen_generator.return_value = MOCK_EVENTS
        mock_cls.listen.side_effect = mock_listen_generator
        mock_stream_manager.return_value = mock_cls

        self.assertEqual(self.shell.run(argv), 0)
        self.assertEqual(mock_listen_generator.call_count, 1)

        stdout = self.stdout.getvalue()
        stderr = self.stderr.getvalue()

        expected_result = """
Execution idfoo3 has started.

line 1
line 2

Execution idfoo3 has completed (status=succeeded).
""".lstrip()
#.........这里部分代码省略.........
开发者ID:lyandut,项目名称:st2,代码行数:103,代码来源:test_execution_tail_command.py

示例7: ShellTestCase

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
class ShellTestCase(base.BaseCLITestCase):
    capture_output = True

    def __init__(self, *args, **kwargs):
        super(ShellTestCase, self).__init__(*args, **kwargs)
        self.shell = Shell()

    def setUp(self):
        super(ShellTestCase, self).setUp()

        if six.PY3:
            # In python --version outputs to stdout and in 2.x to stderr
            self.version_output = self.stdout
        else:
            self.version_output = self.stderr

    def test_commands_usage_and_help_strings(self):
        # No command, should print out user friendly usage / help string
        self.assertEqual(self.shell.run([]), 2)

        self.stderr.seek(0)
        stderr = self.stderr.read()
        self.assertTrue('Usage: ' in stderr)
        self.assertTrue('For example:' in stderr)
        self.assertTrue('CLI for StackStorm' in stderr)
        self.assertTrue('positional arguments:' in stderr)

        self.stdout.truncate()
        self.stderr.truncate()

        # --help should result in the same output
        try:
            self.assertEqual(self.shell.run(['--help']), 0)
        except SystemExit as e:
            self.assertEqual(e.code, 0)

        self.stdout.seek(0)
        stdout = self.stdout.read()
        self.assertTrue('Usage: ' in stdout)
        self.assertTrue('For example:' in stdout)
        self.assertTrue('CLI for StackStorm' in stdout)
        self.assertTrue('positional arguments:' in stdout)

        self.stdout.truncate()
        self.stderr.truncate()

        # Sub command with no args
        try:
            self.assertEqual(self.shell.run(['action']), 2)
        except SystemExit as e:
            self.assertEqual(e.code, 2)

        self.stderr.seek(0)
        stderr = self.stderr.read()

        self.assertTrue('usage' in stderr)

        if six.PY2:
            self.assertTrue('{list,get,create,update' in stderr)
            self.assertTrue('error: too few arguments' in stderr)

    def test_endpoints_default(self):
        base_url = 'http://127.0.0.1'
        auth_url = 'http://127.0.0.1:9100'
        api_url = 'http://127.0.0.1:9101/v1'
        stream_url = 'http://127.0.0.1:9102/v1'
        args = ['trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)
        self.assertEqual(client.endpoints['stream'], stream_url)

    def test_endpoints_base_url_from_cli(self):
        base_url = 'http://www.st2.com'
        auth_url = 'http://www.st2.com:9100'
        api_url = 'http://www.st2.com:9101/v1'
        stream_url = 'http://www.st2.com:9102/v1'
        args = ['--url', base_url, 'trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)
        self.assertEqual(client.endpoints['stream'], stream_url)

    def test_endpoints_base_url_from_env(self):
        base_url = 'http://www.st2.com'
        auth_url = 'http://www.st2.com:9100'
        api_url = 'http://www.st2.com:9101/v1'
        stream_url = 'http://www.st2.com:9102/v1'
        os.environ['ST2_BASE_URL'] = base_url
        args = ['trigger', 'list']
        parsed_args = self.shell.parser.parse_args(args)
        client = self.shell.get_client(parsed_args)
        self.assertEqual(client.endpoints['base'], base_url)
        self.assertEqual(client.endpoints['auth'], auth_url)
        self.assertEqual(client.endpoints['api'], api_url)
        self.assertEqual(client.endpoints['stream'], stream_url)
#.........这里部分代码省略.........
开发者ID:nzlosh,项目名称:st2,代码行数:103,代码来源:test_shell.py

示例8: test_non_unicode_encoding_locale_warning_is_printed

# 需要导入模块: from st2client.shell import Shell [as 别名]
# 或者: from st2client.shell.Shell import run [as 别名]
    def test_non_unicode_encoding_locale_warning_is_printed(self, mock_logger):
        shell = Shell()
        shell.run(argv=['trigger', 'list'])

        call_args = mock_logger.warn.call_args[0][0]
        self.assertTrue('Locale en_US with encoding iso which is not UTF-8 is used.' in call_args)
开发者ID:nzlosh,项目名称:st2,代码行数:8,代码来源:test_shell.py


注:本文中的st2client.shell.Shell.run方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。