Example #1
0
def test_app_will_call_on_startup(config_obj):
    config_obj['on_connection_add'] = 'echo "on_startup"'
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)
    mock_display = MagicMock()
    MockDisplay = MagicMock(return_value=mock_display)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    with patch('peek.peekapp.get_config', get_config), patch('peek.peekapp.SqLiteHistory', MockHistory), \
         patch('peek.peekapp.Display', MockDisplay):
        app = PeekApp(extra_config_options=('log_level=None',
                                            'use_keyring=False'))
        app.reset()
        mock_display.info.assert_has_calls(
            [call('"on_startup"'), call('"on_startup"')])
Example #2
0
def test_app_will_respect_on_connection_add_callback(config_obj):
    config_obj['on_connection_add'] = 'echo "on_connection_add"'
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)
    mock_display = MagicMock()
    MockDisplay = MagicMock(return_value=mock_display)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    with patch('peek.peekapp.get_config', get_config), patch('peek.peekapp.SqLiteHistory', MockHistory), \
         patch('peek.peekapp.Display', MockDisplay):
        app = PeekApp(extra_config_options=('log_level=None',
                                            'use_keyring=False'))
        mock_display.info.assert_called_with('"on_connection_add"')
        app.config['on_connection_add'] = 'echo "second_time"'
        app.process_input('connect')
        mock_display.info.assert_any_call('"second_time"')
Example #3
0
def test_app_can_start_with_no_connection(config_obj):
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    class MockCliNs:
        def __init__(self):
            self.zero_connection = True

    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        app = PeekApp(extra_config_options=('log_level=None',
                                            'use_keyring=False'),
                      cli_ns=MockCliNs())

        assert len(app.es_client_manager.clients()) == 0
        app._get_message()  # as long as it does not error
Example #4
0
def test_multiple_stmts(config_obj):
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    text = """get abc

post abc/_doc
{ "foo":
         "bar"
}

conn foo=bar
get abc
"""
    nodes = []
    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        peek = PeekApp(batch_mode=True,
                       extra_config_options=('log_level=None',
                                             'use_keyring=False'))
        peek.execute_node = lambda stmt: nodes.append(stmt)
        peek.process_input(text)

    for n in nodes:
        print(n)

    assert [str(n) for n in nodes] == [
        r'''get abc {}
''',
        r'''post abc/_doc {}
{"foo":"bar"}
''',
        r'''conn [] [] {foo:bar}
''',
        r'''get abc {}
''',
    ]
Example #5
0
def test_app_will_not_auto_load_session_by_default(config_obj):
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        PeekApp(extra_config_options=('log_level=None', 'use_keyring=False'))

    mock_history.load_session.assert_not_called()
Example #6
0
def test_app_will_not_auto_load_session_if_connection_is_specified_in_rcfile(
        config_obj):
    config_obj['auto_load_session'] = True
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(
            ConfigObj(
                {'connection': {
                    'username': '******',
                    'password': '******',
                }}))
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        PeekApp(extra_config_options=('log_level=None', 'use_keyring=False'))

    mock_history.load_session.assert_not_called()
Example #7
0
def test_app_will_auto_save_session(config_obj):
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        app = PeekApp(extra_config_options=('log_level=None',
                                            'use_keyring=False'))
        app.signal_exit()
        app.run()

    mock_history.save_session.assert_called_once()
Example #8
0
def test_app_will_not_auto_load_session_if_connection_is_specified_on_cli(
        config_obj):
    config_obj['auto_load_session'] = True
    mock_history = MagicMock()
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    class MockCliNs:
        def __init__(self):
            self.username = '******'
            self.password = '******'
            self.zero_connection = False

    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        PeekApp(extra_config_options=('log_level=None', 'use_keyring=False'),
                cli_ns=MockCliNs())

    mock_history.load_session.assert_not_called()
Example #9
0
def peek_app():
    from peek import __file__ as package_root
    package_root = os.path.dirname(package_root)
    package_config_file = os.path.join(package_root, 'peekrc')
    config_obj = ConfigObj(package_config_file)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    class MockCliNs:
        def __init__(self):
            self.username = '******'
            self.password = '******'
            self.zero_connection = False

    with patch('peek.peekapp.PromptSession', MagicMock()), \
         patch('peek.peekapp.get_config', get_config), \
         patch('peek.peekapp.SqLiteHistory', MockHistory):
        return PeekApp(extra_config_options=('log_level=None',
                                             'use_keyring=False'),
                       cli_ns=MockCliNs())
Example #10
0
def test_app_will_auto_load_session_if_nothing_is_overriding_it(config_obj):
    config_obj['auto_load_session'] = True
    mock_history = MagicMock()
    mock_history.load_session = MagicMock(return_value=json.dumps({
        '_index_current':
        0,
        '_clients': [{
            'username': '******',
            'password': '******',
            'hosts': 'localhost:9200'
        }]
    }))
    MockHistory = MagicMock(return_value=mock_history)

    def get_config(_, extra_config):
        config_obj.merge(ConfigObj(extra_config))
        return config_obj

    with patch('peek.peekapp.get_config',
               get_config), patch('peek.peekapp.SqLiteHistory', MockHistory):
        PeekApp(extra_config_options=('log_level=None', 'use_keyring=False'))

    mock_history.load_session.assert_called_with(AUTO_SAVE_NAME)
Example #11
0
File: cli.py Project: ywangd/peek
def main():
    """Console script for peek."""
    parser = argparse.ArgumentParser()

    parser.add_argument('input', nargs='*', help='script files')

    parser.add_argument('--config',
                        default=config_location() + 'peekrc',
                        help='Configuration file to load')

    parser.add_argument('-e',
                        '--extra-config-option',
                        action='append',
                        help='Extra configuration option to override')

    parser.add_argument('--name', help='A friendly name for the connection')
    parser.add_argument('--hosts',
                        default=argparse.SUPPRESS,
                        help='ES hosts to connect to (default localhost:9200)')
    parser.add_argument('--cloud_id', help='Elastic Cloud ID')
    parser.add_argument('--username', help='Username')
    parser.add_argument('--password', help='Password')
    parser.add_argument('--api_key', help='API key of format id:key')
    parser.add_argument('--token', help='Token for authentication')
    parser.add_argument('--use_ssl',
                        action='store_true',
                        default=argparse.SUPPRESS,
                        help='Enable TLS for connecting to ES')
    parser.add_argument('--verify_certs',
                        action='store_true',
                        default=argparse.SUPPRESS,
                        help='Verify server certificate')
    parser.add_argument('--assert_hostname',
                        action='store_true',
                        default=argparse.SUPPRESS,
                        help='Verify hostname')
    parser.add_argument('--ca_certs', help='Location of CA certificates')
    parser.add_argument('--client_cert', help='Location of client certificate')
    parser.add_argument('--client_key', help='Location of client private key')
    parser.add_argument('--force_prompt',
                        action='store_true',
                        default=argparse.SUPPRESS,
                        help='Force prompting for password')
    parser.add_argument('--no_prompt',
                        action='store_true',
                        default=argparse.SUPPRESS,
                        help='Do not prompt for password')
    parser.add_argument('-z',
                        '--zero_connection',
                        action='store_true',
                        help='Start the session with no connection')

    parser.add_argument('-V',
                        '--version',
                        action='version',
                        version=__version__)

    ns = parser.parse_args()

    isatty = sys.stdin.isatty()
    batch_mode = (not isatty) or bool(ns.input)

    peek = PeekApp(
        batch_mode=batch_mode,
        config_file=ns.config,
        extra_config_options=ns.extra_config_option,
        cli_ns=ns,
    )
    if not batch_mode:
        peek.run()
    else:
        if ns.input:
            for f in ns.input:
                with open(f) as ins:
                    peek.process_input(ins.read())
        else:
            stdin_read = sys.stdin.read()
            peek.process_input(stdin_read)

    return 0