forked from bugy/script-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugy#200: implemented script callback for script executions
- Loading branch information
Showing
8 changed files
with
243 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import os | ||
import sys | ||
|
||
output_file = sys.argv[1] | ||
|
||
with open(output_file, 'w') as f: | ||
for arg in sys.argv[2:]: | ||
f.write(arg + '\n') | ||
|
||
f.write('\n\n') | ||
|
||
fields = ['event_type', 'execution_id', 'pid', 'script_name', 'user', 'exit_code'] | ||
|
||
for field in fields: | ||
value = os.environ.get(field) | ||
|
||
f.write(field + ': ' + str(value) + '\n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import communications.destination_base as destination_base | ||
from model.model_helper import read_obligatory | ||
from utils import process_utils | ||
from utils.string_utils import values_to_string | ||
|
||
|
||
def _create_communicator(params_dict): | ||
return ScriptCommunicator(params_dict) | ||
|
||
|
||
class ScriptDestination(destination_base.Destination): | ||
def __init__(self, params_dict): | ||
self._communicator = _create_communicator(params_dict) | ||
|
||
def send(self, title, body, files=None): | ||
environment_variables = None | ||
|
||
if isinstance(body, dict): | ||
parameters = list(body.values()) | ||
environment_variables = values_to_string(dict(body)) | ||
elif isinstance(body, list): | ||
parameters = body | ||
else: | ||
raise Exception('Only dict or list bodies are supported') | ||
|
||
parameters = values_to_string(parameters) | ||
|
||
if files: | ||
raise Exception('Files are not supported for scripts') | ||
|
||
self._communicator.send(parameters, environment_variables=environment_variables) | ||
|
||
def __str__(self, *args, **kwargs): | ||
return type(self).__name__ + ' for ' + str(self._communicator) | ||
|
||
|
||
class ScriptCommunicator: | ||
def __init__(self, params_dict): | ||
command_config = read_obligatory(params_dict, 'command', ' for Script callback') | ||
self.command = process_utils.split_command(command_config) | ||
|
||
def send(self, parameters, environment_variables=None): | ||
full_command = self.command + parameters | ||
process_utils.invoke(full_command, environment_variables=environment_variables) | ||
|
||
def __str__(self, *args, **kwargs): | ||
return 'Script: ' + str(self.command) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import unittest | ||
from collections import OrderedDict | ||
|
||
from communications.communication_model import File | ||
from communications.destination_script import ScriptDestination | ||
from tests.communications.communication_test_utils import mock_communicators | ||
|
||
|
||
@mock_communicators | ||
class TestScriptDestination(unittest.TestCase): | ||
def test_send_when_list_body(self): | ||
body = ['param1', 'param2'] | ||
self.do_send(body) | ||
|
||
self.assert_messages([body]) | ||
|
||
def test_send_when_list_body_non_strings(self): | ||
body = [1, None, False] | ||
self.do_send(body) | ||
|
||
self.assert_messages([['1', 'None', 'False']]) | ||
|
||
def test_send_when_dict_body(self): | ||
body = OrderedDict([('param1', 'Hello'), ('param2', 'World')]) | ||
self.do_send(body) | ||
|
||
self.assert_messages([['Hello', 'World']]) | ||
|
||
def test_send_when_dict_body_non_strings(self): | ||
body = OrderedDict([('param1', 123.45), ('param2', None), ('param3', False)]) | ||
self.do_send(body) | ||
|
||
self.assert_messages([['123.45', 'None', 'False']]) | ||
|
||
def test_send_3_times(self): | ||
self.do_send(OrderedDict([('param1', 'Hello'), ('param2', 'World')])) | ||
self.do_send([]) | ||
self.do_send(['123', '456', '789']) | ||
|
||
self.assert_messages([['Hello', 'World'], [], ['123', '456', '789']]) | ||
|
||
def test_send_when_string_body(self): | ||
self.assertRaisesRegex(Exception, 'Only dict or list bodies are supported', self.do_send, 'test message') | ||
|
||
def test_send_when_files(self): | ||
self.assertRaisesRegex(Exception, 'Files are not supported for scripts', | ||
self.do_send, ['param1'], [File('test_file')]) | ||
|
||
def test_env_when_list_body(self): | ||
self.do_send(['param1', 'param2']) | ||
|
||
self.assert_env_variables([None]) | ||
|
||
def test_env_when_dict_body(self): | ||
body = OrderedDict([('param1', 'Hello'), ('param2', 'World')]) | ||
self.do_send(body) | ||
|
||
self.assert_env_variables([{'param1': 'Hello', 'param2': 'World'}]) | ||
|
||
def test_env_when_dict_body_with_non_strings(self): | ||
body = OrderedDict([('param1', 1), ('param2', None), ('param3', True)]) | ||
self.do_send(body) | ||
|
||
self.assert_env_variables([{'param1': '1', 'param2': 'None', 'param3': 'True'}]) | ||
|
||
def test_env_when_send_3_times(self): | ||
self.do_send(OrderedDict([('param1', 'Hello'), ('param2', 'World')])) | ||
self.do_send([]) | ||
self.do_send({'abc': '123'}) | ||
|
||
self.assert_env_variables([{'param1': 'Hello', 'param2': 'World'}, None, {'abc': '123'}]) | ||
|
||
def assert_env_variables(self, env_variables): | ||
communicator = self.get_communicators()[0] | ||
|
||
actual_variables = [arg.get('environment_variables') for arg in communicator.captured_arguments] | ||
self.assertEqual(env_variables, actual_variables) | ||
|
||
def assert_messages(self, expected_bodies): | ||
communicator = self.get_communicators()[0] | ||
|
||
expected_messages = [(None, body, None) for body in expected_bodies] | ||
self.assertEqual(expected_messages, communicator.messages) | ||
|
||
def do_send(self, body, files=None): | ||
self.destination.send('ignored', body, files) | ||
|
||
def setUp(self): | ||
self.destination = ScriptDestination({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters