Beispiel #1
0
def test_scribe_tail_ctrl_c():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1", "fake_level2"]
    components = ["build", "deploy"]
    clusters = ["fake_cluster1", "fake_cluster2"]
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with mock.patch(
        "paasta_tools.cli.cmds.logs.scribereader", autospec=True
    ) as mock_scribereader:
        # There's no reason this method is the one that raises the
        # KeyboardInterrupt. This just happens to be the first convenient place
        # to simulate the user pressing Ctrl-C.
        mock_scribereader.get_env_scribe_host.side_effect = FakeKeyboardInterrupt
        with reraise_keyboardinterrupt():
            logs.scribe_tail(
                env,
                stream_name,
                service,
                levels,
                components,
                clusters,
                queue,
                filter_fn,
            )
Beispiel #2
0
def test_scribe_tail_handles_StreamTailerSetupError():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1"]
    components = ["build"]
    clusters = ["fake_cluster1"]
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with mock.patch(
        "paasta_tools.cli.cmds.logs.scribereader", autospec=True
    ) as mock_scribereader, mock.patch(
        "paasta_tools.cli.cmds.logs.log", autospec=True
    ) as mock_log:
        mock_scribereader.get_stream_tailer.side_effect = StreamTailerSetupError(
            "bla", "unused1", "unused2"
        )
        with raises(StreamTailerSetupError):
            logs.scribe_tail(
                env,
                stream_name,
                service,
                levels,
                components,
                clusters,
                queue,
                filter_fn,
            )

        mock_log.error.assert_any_call(
            "Failed to setup stream tailing for %s in fake_env" % stream_name
        )
Beispiel #3
0
def test_scribe_tail_ctrl_c():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1', 'fake_level2']
    components = ['build', 'deploy']
    clusters = ['fake_cluster1', 'fake_cluster2']
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with contextlib.nested(
        mock.patch('paasta_tools.cli.cmds.logs.scribereader', autospec=True),
    ) as (
        mock_scribereader,
    ):
        # There's no reason this method is the one that raises the
        # KeyboardInterrupt. This just happens to be the first convenient place
        # to simulate the user pressing Ctrl-C.
        mock_scribereader.get_env_scribe_host.side_effect = FakeKeyboardInterrupt
        try:
            logs.scribe_tail(
                env,
                stream_name,
                service,
                levels,
                components,
                clusters,
                queue,
                filter_fn,
            )
        # We have to catch this ourselves otherwise it will fool pytest too!
        except FakeKeyboardInterrupt:
            raise Exception('The code under test failed to catch a (fake) KeyboardInterrupt!')
Beispiel #4
0
def test_scribe_tail_handles_StreamTailerSetupError():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1']
    components = ['build']
    clusters = ['fake_cluster1']
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with contextlib.nested(
        mock.patch('paasta_tools.cli.cmds.logs.scribereader', autospec=True),
        mock.patch('paasta_tools.cli.cmds.logs.log', autospec=True),
    ) as (
        mock_scribereader,
        mock_log,
    ):
        mock_scribereader.get_stream_tailer.side_effect = StreamTailerSetupError('bla', 'unused1', 'unused2')
        with raises(StreamTailerSetupError):
            logs.scribe_tail(
                env,
                stream_name,
                service,
                levels,
                components,
                clusters,
                queue,
                filter_fn,
            )
        mock_log.error.assert_any_call('Failed to setup stream tailing for %s in fake_env' % stream_name)
Beispiel #5
0
def test_scribe_tail_ctrl_c():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1', 'fake_level2']
    components = ['build', 'deploy']
    clusters = ['fake_cluster1', 'fake_cluster2']
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with contextlib.nested(
            mock.patch('paasta_tools.cli.cmds.logs.scribereader',
                       autospec=True), ) as (mock_scribereader, ):
        # There's no reason this method is the one that raises the
        # KeyboardInterrupt. This just happens to be the first convenient place
        # to simulate the user pressing Ctrl-C.
        mock_scribereader.get_env_scribe_host.side_effect = FakeKeyboardInterrupt
        try:
            logs.scribe_tail(
                env,
                stream_name,
                service,
                levels,
                components,
                clusters,
                queue,
                filter_fn,
            )
        # We have to catch this ourselves otherwise it will fool pytest too!
        except FakeKeyboardInterrupt:
            raise Exception(
                'The code under test failed to catch a (fake) KeyboardInterrupt!'
            )
Beispiel #6
0
def test_scribe_tail_handles_StreamTailerSetupError():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1']
    components = ['build']
    clusters = ['fake_cluster1']
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with contextlib.nested(
            mock.patch('paasta_tools.cli.cmds.logs.scribereader',
                       autospec=True),
            mock.patch('paasta_tools.cli.cmds.logs.log', autospec=True),
    ) as (
            mock_scribereader,
            mock_log,
    ):
        mock_scribereader.get_stream_tailer.side_effect = StreamTailerSetupError(
            'bla', 'unused1', 'unused2')
        with raises(StreamTailerSetupError):
            logs.scribe_tail(
                env,
                stream_name,
                service,
                levels,
                components,
                clusters,
                queue,
                filter_fn,
            )
        mock_log.error.assert_any_call(
            'Failed to setup stream tailing for %s in fake_env' % stream_name)
Beispiel #7
0
def test_scribe_tail_log_everything():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1", "fake_level2"]
    components = ["build", "deploy"]
    clusters = ["fake_cluster1", "fake_cluster2"]
    instance = "fake_instance"
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    tailer = iter(
        [
            format_log_line(levels[0], clusters, instance, "build", "level: first. component: build."),
            format_log_line(levels[1], clusters, instance, "deploy", "level: second. component: deploy."),
        ]
    )
    with contextlib.nested(mock.patch("paasta_tools.cli.cmds.logs.scribereader", autospec=True)) as (
        mock_scribereader,
    ):
        mock_scribereader.get_env_scribe_host.return_value = {"host": "fake_host", "port": "fake_port"}
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(env, stream_name, service, levels, components, clusters, queue, filter_fn)
        assert mock_scribereader.get_env_scribe_host.call_count == 1
        mock_scribereader.get_stream_tailer.assert_called_once_with(stream_name, "fake_host", "fake_port")
        assert queue.qsize() == 2
        # Sadly, fetching with a timeout seems to be needed with
        # multiprocessing.Queue (this was not the case with Queue.Queue). It
        # failed 8/10 times with a get_nowait() vs 0/10 times with a 0.1s
        # timeout.
        first_line = queue.get(True, 0.1)
        assert "level: first. component: build." in first_line
        second_line = queue.get(True, 0.1)
        assert "level: second. component: deploy." in second_line
Beispiel #8
0
def test_scribe_tail_log_everything():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1', 'fake_level2']
    components = ['build', 'deploy']
    clusters = ['fake_cluster1', 'fake_cluster2']
    instance = 'fake_instance'
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    tailer = iter([
        format_log_line(
            levels[0],
            clusters,
            instance,
            'build',
            'level: first. component: build.',
        ),
        format_log_line(
            levels[1],
            clusters,
            instance,
            'deploy',
            'level: second. component: deploy.',
        ),
    ])
    with mock.patch('paasta_tools.cli.cmds.logs.scribereader',
                    autospec=True) as mock_scribereader:
        mock_scribereader.get_env_scribe_host.return_value = {
            'host': 'fake_host',
            'port': 'fake_port',
        }
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(
            env,
            stream_name,
            service,
            levels,
            components,
            clusters,
            queue,
            filter_fn,
        )
        assert mock_scribereader.get_env_scribe_host.call_count == 1
        mock_scribereader.get_stream_tailer.assert_called_once_with(
            stream_name,
            'fake_host',
            'fake_port',
        )
        assert queue.qsize() == 2
        # Sadly, fetching with a timeout seems to be needed with
        # multiprocessing.Queue (this was not the case with Queue.Queue). It
        # failed 8/10 times with a get_nowait() vs 0/10 times with a 0.1s
        # timeout.
        first_line = queue.get(block=True, timeout=0.1)
        assert 'level: first. component: build.' in first_line
        second_line = queue.get(block=True, queue=0.1)
        assert 'level: second. component: deploy.' in second_line
Beispiel #9
0
def test_scribe_tail_log_everything():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1", "fake_level2"]
    components = ["build", "deploy"]
    clusters = ["fake_cluster1", "fake_cluster2"]
    instance = "fake_instance"
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    tailer = iter(
        [
            format_log_line(
                levels[0],
                clusters,
                instance,
                "build",
                "level: first. component: build.",
            ),
            format_log_line(
                levels[1],
                clusters,
                instance,
                "deploy",
                "level: second. component: deploy.",
            ),
        ]
    )
    with mock.patch(
        "paasta_tools.cli.cmds.logs.scribereader", autospec=True
    ) as mock_scribereader:
        mock_scribereader.get_env_scribe_host.return_value = {
            "host": "fake_host",
            "port": "fake_port",
        }
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(
            env, stream_name, service, levels, components, clusters, queue, filter_fn
        )
        assert mock_scribereader.get_env_scribe_host.call_count == 1
        mock_scribereader.get_stream_tailer.assert_called_once_with(
            stream_name, "fake_host", "fake_port"
        )
        assert queue.qsize() == 2
        # Sadly, fetching with a timeout seems to be needed with
        # multiprocessing.Queue (this was not the case with Queue.Queue). It
        # failed 8/10 times with a get_nowait() vs 0/10 times with a 0.1s
        # timeout.
        first_line = queue.get(block=True, timeout=0.1)
        assert "level: first. component: build." in first_line
        second_line = queue.get(block=True, queue=0.1)
        assert "level: second. component: deploy." in second_line
Beispiel #10
0
def test_scribe_tail_log_nothing():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1', 'fake_level2']
    components = ['build', 'deploy']
    clusters = ['fake_cluster1', 'fake_cluster2']
    instance = 'fake_instance'
    queue = Queue()
    filter_fn = mock.Mock(return_value=False)

    tailer = iter([
        format_log_line(
            levels[0],
            clusters,
            instance,
            'build',
            'level: first. component: build.',
        ),
        format_log_line(
            levels[1],
            clusters,
            instance,
            'deploy',
            'level: second. component: deploy.',
        ),
    ])
    with contextlib.nested(
        mock.patch('paasta_tools.cli.cmds.logs.scribereader', autospec=True),
    ) as (
        mock_scribereader,
    ):
        mock_scribereader.get_env_scribe_host.return_value = {
            'host': 'fake_host',
            'port': 'fake_port',
        }
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(
            env,
            stream_name,
            service,
            levels,
            components,
            clusters,
            queue,
            filter_fn,
        )
        assert queue.qsize() == 0
Beispiel #11
0
def test_scribe_tail_log_nothing():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1', 'fake_level2']
    components = ['build', 'deploy']
    clusters = ['fake_cluster1', 'fake_cluster2']
    instance = 'fake_instance'
    queue = Queue()
    filter_fn = mock.Mock(return_value=False)

    tailer = iter([
        format_log_line(
            levels[0],
            clusters,
            instance,
            'build',
            'level: first. component: build.',
        ),
        format_log_line(
            levels[1],
            clusters,
            instance,
            'deploy',
            'level: second. component: deploy.',
        ),
    ])
    with contextlib.nested(
        mock.patch('paasta_tools.cli.cmds.logs.scribereader', autospec=True),
    ) as (
        mock_scribereader,
    ):
        mock_scribereader.get_env_scribe_host.return_value = {
            'host': 'fake_host',
            'port': 'fake_port',
        }
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(
            env,
            stream_name,
            service,
            levels,
            components,
            clusters,
            queue,
            filter_fn,
        )
        assert queue.qsize() == 0
Beispiel #12
0
def test_scribe_tail_handles_StreamTailerSetupError():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1"]
    components = ["build"]
    clusters = ["fake_cluster1"]
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    with contextlib.nested(
        mock.patch("paasta_tools.cli.cmds.logs.scribereader", autospec=True),
        mock.patch("paasta_tools.cli.cmds.logs.log", autospec=True),
    ) as (mock_scribereader, mock_log):
        mock_scribereader.get_stream_tailer.side_effect = StreamTailerSetupError("bla", "unused1", "unused2")
        with raises(StreamTailerSetupError):
            logs.scribe_tail(env, stream_name, service, levels, components, clusters, queue, filter_fn)
        mock_log.error.assert_any_call("Failed to setup stream tailing for %s in fake_env" % stream_name)
Beispiel #13
0
def test_scribe_tail_log_nothing():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1", "fake_level2"]
    components = ["build", "deploy"]
    clusters = ["fake_cluster1", "fake_cluster2"]
    instance = "fake_instance"
    queue = Queue()
    filter_fn = mock.Mock(return_value=False)

    tailer = iter(
        [
            format_log_line(
                levels[0],
                clusters,
                instance,
                "build",
                "level: first. component: build.",
            ),
            format_log_line(
                levels[1],
                clusters,
                instance,
                "deploy",
                "level: second. component: deploy.",
            ),
        ]
    )
    with mock.patch(
        "paasta_tools.cli.cmds.logs.scribereader", autospec=True
    ) as mock_scribereader:
        mock_scribereader.get_env_scribe_host.return_value = {
            "host": "fake_host",
            "port": "fake_port",
        }
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(
            env, stream_name, service, levels, components, clusters, queue, filter_fn
        )
        assert queue.qsize() == 0
Beispiel #14
0
def test_scribe_tail_log_nothing():
    env = "fake_env"
    stream_name = "fake_stream"
    service = "fake_service"
    levels = ["fake_level1", "fake_level2"]
    components = ["build", "deploy"]
    clusters = ["fake_cluster1", "fake_cluster2"]
    instance = "fake_instance"
    queue = Queue()
    filter_fn = mock.Mock(return_value=False)

    tailer = iter(
        [
            format_log_line(levels[0], clusters, instance, "build", "level: first. component: build."),
            format_log_line(levels[1], clusters, instance, "deploy", "level: second. component: deploy."),
        ]
    )
    with contextlib.nested(mock.patch("paasta_tools.cli.cmds.logs.scribereader", autospec=True)) as (
        mock_scribereader,
    ):
        mock_scribereader.get_env_scribe_host.return_value = {"host": "fake_host", "port": "fake_port"}
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(env, stream_name, service, levels, components, clusters, queue, filter_fn)
        assert queue.qsize() == 0
Beispiel #15
0
def test_scribe_tail_log_everything():
    env = 'fake_env'
    stream_name = 'fake_stream'
    service = 'fake_service'
    levels = ['fake_level1', 'fake_level2']
    components = ['build', 'deploy']
    clusters = ['fake_cluster1', 'fake_cluster2']
    instance = 'fake_instance'
    queue = Queue()
    filter_fn = mock.Mock(return_value=True)

    tailer = iter([
        format_log_line(
            levels[0],
            clusters,
            instance,
            'build',
            'level: first. component: build.',
        ),
        format_log_line(
            levels[1],
            clusters,
            instance,
            'deploy',
            'level: second. component: deploy.',
        ),
    ])
    with contextlib.nested(
        mock.patch('paasta_tools.cli.cmds.logs.scribereader', autospec=True),
    ) as (
        mock_scribereader,
    ):
        mock_scribereader.get_env_scribe_host.return_value = {
            'host': 'fake_host',
            'port': 'fake_port',
        }
        mock_scribereader.get_stream_tailer.return_value = tailer
        logs.scribe_tail(
            env,
            stream_name,
            service,
            levels,
            components,
            clusters,
            queue,
            filter_fn
        )
        assert mock_scribereader.get_env_scribe_host.call_count == 1
        mock_scribereader.get_stream_tailer.assert_called_once_with(
            stream_name,
            'fake_host',
            'fake_port',
        )
        assert queue.qsize() == 2
        # Sadly, fetching with a timeout seems to be needed with
        # multiprocessing.Queue (this was not the case with Queue.Queue). It
        # failed 8/10 times with a get_nowait() vs 0/10 times with a 0.1s
        # timeout.
        first_line = queue.get(True, 0.1)
        assert 'level: first. component: build.' in first_line
        second_line = queue.get(True, 0.1)
        assert 'level: second. component: deploy.' in second_line