Example #1
0
async def test_stories_run_prepare(patch, app, logger, async_mock):
    patch.object(Stories, 'execute', new=async_mock())
    patch.object(Stories, 'story')
    await Stories.run(app, logger, 'story_name', context='context')
    Stories.story().prepare.assert_called_with('context')
    Stories.execute.mock \
        .assert_called_with(logger, Stories.story())
Example #2
0
async def test_stories_run_metrics_exc(patch, app, logger, async_mock, magic):
    patch.object(time, 'time')
    assert Metrics.story_run_total is not None
    assert Metrics.story_run_failure is not None
    Metrics.story_run_total = magic()
    Metrics.story_run_failure = magic()

    def exc(*args, **kwargs):
        raise Exception()

    patch.object(Stories, 'execute', new=async_mock(side_effect=exc))
    patch.object(Stories, 'story')
    with pytest.raises(Exception):
        await Stories.run(app, logger, 'story_name')
    Stories.story.assert_called_with(app, logger, 'story_name')
    Stories.story.return_value.prepare.assert_called_with(None)
    Stories.execute.mock.assert_called_with(logger, Stories.story())

    Metrics.story_run_total.labels.assert_called_with(app_id=app.app_id,
                                                      story_name='story_name')
    Metrics.story_run_total.labels.return_value.observe.assert_called_once()

    Metrics.story_run_failure.labels \
        .assert_called_with(app_id=app.app_id, story_name='story_name')
    Metrics.story_run_failure.labels.return_value.observe.assert_called_once()
Example #3
0
def test_format_command_no_args(logger, app, echo_service, echo_line):
    story = Stories.story(app, logger, 'echo.story')
    app.services = echo_service

    echo_service['alpine'][
        ServiceConstants.config]['actions']['echo']['arguments'] = None

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo'] == cmd
Example #4
0
def test_format_command_no_format(logger, app, echo_service, echo_line):
    story = Stories.story(app, logger, 'echo.story')
    app.services = echo_service

    config = app.services['alpine'][ServiceConstants.config]
    config['actions']['echo']['format'] = None

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo', '{"msg":"foo"}'] == cmd
Example #5
0
def test_format_command_with_format(patch, logger, app, echo_service,
                                    echo_line):
    story = Stories.story(app, logger, 'echo.story')
    patch.object(story, 'argument_by_name', return_value='asyncy')
    app.services = echo_service

    config = app.services['alpine'][ServiceConstants.config]
    config['actions']['echo']['format'] = 'echo {msg}'

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo', 'asyncy'] == cmd
Example #6
0
async def test_stories_run_prepare_block(patch, app, logger, async_mock):
    patch.object(Lexicon, 'execute_block', new=async_mock())
    patch.object(Stories, 'story')
    block = '1'
    await Stories.run(app,
                      logger,
                      'story_name',
                      context='context',
                      block=block)
    Stories.story().prepare.assert_called_with('context')
    Stories.story().line.assert_called_with(block)
    Stories.story().new_frame.assert_called_with(block)
    Lexicon.execute_block.mock \
        .assert_called_with(logger, Stories.story(),
                            Stories.story().line())
Example #7
0
async def test_stories_run(patch, app, logger, async_mock, magic):
    patch.object(time, 'time')
    patch.object(Stories, 'execute', new=async_mock())
    patch.object(Stories, 'story')
    assert Metrics.story_run_total is not None
    assert Metrics.story_run_success is not None
    Metrics.story_run_total = magic()
    Metrics.story_run_success = magic()

    await Stories.run(app, logger, 'story_name')
    Stories.story.assert_called_with(app, logger, 'story_name')
    Stories.story.return_value.prepare.assert_called_with(None)
    Stories.execute.mock.assert_called_with(logger, Stories.story())

    Metrics.story_run_total.labels.assert_called_with(app_id=app.app_id,
                                                      story_name='story_name')
    Metrics.story_run_total.labels.return_value.observe.assert_called_once()

    Metrics.story_run_success.labels \
        .assert_called_with(app_id=app.app_id, story_name='story_name')
    Metrics.story_run_success.labels.return_value.observe.assert_called_once()
Example #8
0
def test_format_command_no_spec(logger, app, echo_line):
    story = Stories.story(app, logger, 'echo.story')
    app.services = {}
    with pytest.raises(ContainerSpecNotRegisteredError):
        Containers.format_command(story, echo_line, 'alpine', 'echo')
Example #9
0
def test_format_command(logger, app, echo_service, echo_line):
    story = Stories.story(app, logger, 'echo.story')
    app.services = echo_service

    cmd = Containers.format_command(story, echo_line, 'alpine', 'echo')
    assert ['echo', '{"msg":"foo"}'] == cmd
Example #10
0
def test_stories_save_logger(logger, story):
    story.app_id = 'app_id'
    Stories.save(logger, story, None)
    assert logger.log.call_count == 1
Example #11
0
def test_stories_story(patch, app, logger):
    patch.init(Story)
    story = Stories.story(app, logger, 'story_name')
    Story.__init__.assert_called_with(app, 'story_name', logger)
    assert isinstance(story, Story)