async def test_execute_inline(patch, story, command): chain = deque([Service('http'), Event('server'), Command(command)]) req = MagicMock() io_loop = MagicMock() story.context = { ContextConstants.server_request: req, ContextConstants.server_io_loop: io_loop } command_conf = { 'arguments': { 'content': { 'type': 'string', 'in': 'responseBody', 'required': True } } } patch.object(story, 'argument_by_name', return_value='hello world!') expected_body = {'command': command, 'data': {'content': 'hello world!'}} line = {} await Services.execute_inline(story, line, chain, command_conf) req.write.assert_called_with(ujson.dumps(expected_body) + '\n') if command == 'finish': io_loop.add_callback.assert_called_with(req.finish) else: io_loop.add_callback.assert_not_called()
def test_service_get_command_conf_events(story): chain = deque( [Service('service'), Command('cmd'), Event('foo'), Command('bar')]) story.app.services = { 'service': { 'configuration': { 'actions': { 'cmd': { 'events': { 'foo': { 'output': { 'actions': { 'bar': { 'a': 'b' } } } } } } } } } } assert Services.get_command_conf(story, chain) == {'a': 'b'}
async def test_execute_inline(patch, story, command, simulate_finished, bin_content): # Not a valid combination. if bin_content and command != 'write': return chain = deque([Service('http'), Event('server'), Command(command)]) req = MagicMock() req._finished = simulate_finished def is_finished(): return req._finished req.is_finished = is_finished io_loop = MagicMock() story.context = { ContextConstants.server_request: req, ContextConstants.server_io_loop: io_loop } command_conf = { 'arguments': { 'content': { 'type': 'string', 'in': 'responseBody', 'required': True } } } if bin_content: patch.object(story, 'argument_by_name', return_value=b'bin world!') else: patch.object(story, 'argument_by_name', return_value='hello world!') expected_body = { 'command': command, 'data': { 'content': 'hello world!' } } line = {} if simulate_finished: with pytest.raises(StoryscriptError): await Services.execute_inline(story, line, chain, command_conf) return else: await Services.execute_inline(story, line, chain, command_conf) if bin_content: req.write.assert_called_with(b'bin world!') else: req.write.assert_called_with(ujson.dumps(expected_body) + '\n') if command == 'finish' or bin_content: io_loop.add_callback.assert_called_with(req.finish) else: io_loop.add_callback.assert_not_called()
def test_resolve_chain(story): """ The story tested here is: alpine echo as client when client foo as echo_helper alpine echo echo_helper sonar # This isn't possible, but OK. echo_helper sonar """ story.app.services = { 'alpine': {} } story.tree = { '1': { Line.method: 'execute', Line.service: 'alpine', Line.command: 'echo', Line.enter: '2', Line.output: ['client'] }, '2': { Line.method: 'when', Line.service: 'client', Line.command: 'foo', Line.parent: '1', Line.output: ['echo_helper'] }, '3': { Line.method: 'execute', Line.service: 'alpine', Line.command: 'echo', Line.parent: '2', Line.enter: '4' }, '4': { Line.method: 'execute', Line.service: 'echo_helper', Line.command: 'sonar', Line.parent: '3' }, '5': { Line.method: 'execute', Line.service: 'echo_helper', Line.command: 'sonar', Line.parent: '2' } } assert Services.resolve_chain(story, story.tree['1']) \ == deque([Service(name='alpine'), Command(name='echo')]) assert Services.resolve_chain(story, story.tree['2']) \ == deque([Service(name='alpine'), Command(name='echo'), Event(name='foo')]) assert Services.resolve_chain(story, story.tree['3']) \ == deque([Service(name='alpine'), Command(name='echo')]) assert Services.resolve_chain(story, story.tree['4']) \ == deque([Service(name='alpine'), Command(name='echo'), Event(name='foo'), Command(name='sonar')]) assert Services.resolve_chain(story, story.tree['5']) \ == deque([Service(name='alpine'), Command(name='echo'), Event(name='foo'), Command(name='sonar')])