Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

use tracking id from yadtcontroller (if available) for publish_failed…

… and publish_finished events
  • Loading branch information...
commit 5b1ac7f3fe8ccc46c9ad2cefc933e643eea1ddd3 1 parent 446f34b
@mriehl mriehl authored
View
12 src/main/python/yadtreceiver/__init__.py
@@ -41,7 +41,6 @@
from yadtreceiver.events import Event
-
class ReceiverException(Exception):
"""
To be raised when an exception occurs within the receiver.
@@ -91,7 +90,9 @@ def handle_request(self, event):
command_and_arguments_list = [python_command, script_to_execute] + event.arguments
command_with_arguments = ' '.join(command_and_arguments_list)
- process_protocol = ProcessProtocol(hostname, self.broadcaster, event.target, command_with_arguments)
+ tracking_id = _determine_tracking_id(command_and_arguments_list)
+
+ process_protocol = ProcessProtocol(hostname, self.broadcaster, event.target, command_with_arguments, tracking_id=tracking_id)
target_dir = self.get_target_directory(event.target)
reactor.spawnProcess(process_protocol, python_command, command_and_arguments_list, env={}, path=target_dir)
@@ -252,3 +253,10 @@ def _connect_broadcaster(self):
self.broadcaster = WampBroadcaster(host, port, 'yadtreceiver')
self.broadcaster.addOnSessionOpenHandler(self.onConnect)
self.broadcaster.connect()
+
+def _determine_tracking_id(command_and_arguments_list):
+ tracking_id = None
+ for command_or_argument in command_and_arguments_list:
+ if command_or_argument.startswith('--tracking-id='):
+ tracking_id = command_or_argument.split('=')[1]
+ return tracking_id
View
9 src/main/python/yadtreceiver/protocols.py
@@ -27,7 +27,7 @@
class ProcessProtocol(protocol.ProcessProtocol):
- def __init__(self, hostname, broadcaster, target, readable_command):
+ def __init__(self, hostname, broadcaster, target, readable_command, tracking_id=None):
"""
Initializes the process protocol with the given properties.
"""
@@ -35,6 +35,7 @@ def __init__(self, hostname, broadcaster, target, readable_command):
self.hostname = hostname
self.readable_command = readable_command
self.target = target
+ self.tracking_id = tracking_id
log.msg('(%s) target[%s] executing "%s"' % (self.hostname, target, readable_command))
@@ -60,7 +61,8 @@ def publish_finished(self):
message = '(%s) target[%s] request finished: "%s" succeeded.' \
% (self.hostname, self.target, self.readable_command)
log.msg(message)
- self.broadcaster.publish_cmd_for_target(self.target, self.readable_command, events.FINISHED, message)
+ self.broadcaster.publish_cmd_for_target(self.target, self.readable_command, events.FINISHED,
+ message, tracking_id=self.tracking_id)
def publish_failed(self, return_code):
@@ -71,4 +73,5 @@ def publish_failed(self, return_code):
error_message = '(%s) target[%s] request "%s" failed: return code was %s.' \
% (self.hostname, self.target, self.readable_command, return_code)
log.err(error_message)
- self.broadcaster.publish_cmd_for_target(self.target, self.readable_command, events.FAILED, error_message)
+ self.broadcaster.publish_cmd_for_target(self.target, self.readable_command, events.FAILED,
+ error_message, tracking_id=self.tracking_id)
View
9 src/unittest/python/protocols_tests.py
@@ -26,12 +26,13 @@
class ProcessProtocolTests (unittest.TestCase):
def test_should_initialize_with_given_properties (self):
mock_broadcaster = Mock()
- protocol = ProcessProtocol('hostname', mock_broadcaster, 'devabc123', '/usr/bin/python abc 123')
+ protocol = ProcessProtocol('hostname', mock_broadcaster, 'devabc123', '/usr/bin/python abc 123', tracking_id='tracking-id')
self.assertEquals('hostname', protocol.hostname)
self.assertEquals(mock_broadcaster, protocol.broadcaster)
self.assertEquals('devabc123', protocol.target)
self.assertEquals('/usr/bin/python abc 123', protocol.readable_command)
+ self.assertEqual('tracking-id', protocol.tracking_id)
def test_should_publish_as_failed_event_when_return_code_not_zero (self):
@@ -61,10 +62,11 @@ def test_should_publish_finished_event (self):
mock_protocol.hostname = 'hostname'
mock_protocol.target = 'dev123'
mock_protocol.readable_command = '/usr/bin/python abc'
+ mock_protocol.tracking_id = 'tracking-id'
ProcessProtocol.publish_finished(mock_protocol)
- self.assertEquals(call('dev123', '/usr/bin/python abc', 'finished', '(hostname) target[dev123] request finished: "/usr/bin/python abc" succeeded.'), mock_broadcaster.publish_cmd_for_target.call_args)
+ self.assertEquals(call('dev123', '/usr/bin/python abc', 'finished', '(hostname) target[dev123] request finished: "/usr/bin/python abc" succeeded.', tracking_id='tracking-id'), mock_broadcaster.publish_cmd_for_target.call_args)
@patch('yadtreceiver.protocols.log')
def test_should_publish_failed_event (self, mock_log):
@@ -74,8 +76,9 @@ def test_should_publish_failed_event (self, mock_log):
mock_protocol.hostname = 'hostname'
mock_protocol.target = 'dev123'
mock_protocol.readable_command = '/usr/bin/python abc'
+ mock_protocol.tracking_id = 'tracking_id'
ProcessProtocol.publish_failed(mock_protocol, 123)
- self.assertEquals(call('dev123', '/usr/bin/python abc', 'failed', '(hostname) target[dev123] request "/usr/bin/python abc" failed: return code was 123.'), mock_broadcaster.publish_cmd_for_target.call_args)
+ self.assertEquals(call('dev123', '/usr/bin/python abc', 'failed', '(hostname) target[dev123] request "/usr/bin/python abc" failed: return code was 123.', tracking_id='tracking_id'), mock_broadcaster.publish_cmd_for_target.call_args)
View
62 src/unittest/python/yadtreceiver_tests.py
@@ -13,6 +13,7 @@
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
+import yadtreceiver
__author__ = 'Michael Gruber'
@@ -288,9 +289,60 @@ def test_should_spawn_new_process_on_reactor(self, mock_protocol, mock_reactor,
Receiver.handle_request(mock_receiver, mock_event)
- self.assertEquals(call('hostname', mock_broadcaster, 'devabc123', '/usr/bin/python /usr/bin/yadtshell update'), mock_protocol.call_args)
+ self.assertEquals(call('hostname', mock_broadcaster, 'devabc123', '/usr/bin/python /usr/bin/yadtshell update', tracking_id=None), mock_protocol.call_args)
self.assertEquals(call('mock-protocol', '/usr/bin/python', ['/usr/bin/python', '/usr/bin/yadtshell', 'update'], path='/etc/yadtshell/targets/devabc123', env={}), mock_reactor.spawnProcess.call_args)
+ @patch('yadtreceiver.reactor')
+ @patch('yadtreceiver.ProcessProtocol')
+ def test_should_create_process_protocol_with_tracking_id_if_given(self, mock_protocol, mock_reactor):
+ mock_protocol.return_value = 'mock-protocol'
+ mock_receiver = Mock(Receiver)
+ mock_broadcaster = Mock()
+ mock_receiver.broadcaster = mock_broadcaster
+ mock_receiver.get_target_directory.return_value = '/etc/yadtshell/targets/devabc123'
+
+ mock_receiver.configuration = {'hostname': 'hostname',
+ 'graphite_active': False,
+ 'python_command': '/usr/bin/python',
+ 'script_to_execute': '/usr/bin/yadtshell'}
+
+ mock_event = Mock(Event)
+ mock_event.target = 'devabc123'
+ mock_event.command = 'yadtshell'
+ mock_event.arguments = ['update', '--tracking-id=foo']
+
+ Receiver.handle_request(mock_receiver, mock_event)
+
+ expected_command_with_arguments = '/usr/bin/python /usr/bin/yadtshell update --tracking-id=foo'
+
+ self.assertEqual(call('hostname', mock_broadcaster, 'devabc123', expected_command_with_arguments,
+ tracking_id='foo'), mock_protocol.call_args)
+
+ @patch('yadtreceiver.reactor')
+ @patch('yadtreceiver.ProcessProtocol')
+ def test_should_create_process_protocol_with_no_tracking_id_if_not_given(self, mock_protocol, mock_reactor):
+ mock_protocol.return_value = 'mock-protocol'
+ mock_receiver = Mock(Receiver)
+ mock_broadcaster = Mock()
+ mock_receiver.broadcaster = mock_broadcaster
+ mock_receiver.get_target_directory.return_value = '/etc/yadtshell/targets/devabc123'
+
+ mock_receiver.configuration = {'hostname': 'hostname',
+ 'graphite_active': False,
+ 'python_command': '/usr/bin/python',
+ 'script_to_execute': '/usr/bin/yadtshell'}
+
+ mock_event = Mock(Event)
+ mock_event.target = 'devabc123'
+ mock_event.command = 'yadtshell'
+ mock_event.arguments = ['update']
+
+ Receiver.handle_request(mock_receiver, mock_event)
+
+ expected_command_with_arguments = '/usr/bin/python /usr/bin/yadtshell update'
+
+ self.assertEqual(call('hostname', mock_broadcaster, 'devabc123', expected_command_with_arguments, tracking_id=None), mock_protocol.call_args)
+
@patch('yadtreceiver.send_update_notification_to_graphite')
def test_should_not_notify_graphite_on_update_if_command_is_status(self, mock_send):
mock_receiver = Mock(Receiver)
@@ -484,3 +536,11 @@ def test_should_log_shutting_down_of_service (self, mock_log):
# Since all the method does is logging we are asserting it here.
self.assertEquals(call('shutting down service'), mock_log.msg.call_args)
+
+ def test_determine_tracking_id_should_return_tracking_id_if_present(self):
+ list_with_tracking_id = ['foo', 'bar', 'baz=fubar', '--tracking-id=test', 'foofoo']
+ self.assertEqual(yadtreceiver._determine_tracking_id(list_with_tracking_id), 'test')
+
+ def test_determine_tracking_id_should_return_none_if_no_tracking_id_present(self):
+ list_with_tracking_id = ['foo', 'bar', 'baz=fubar', '--no-tracking-id=test', 'foofoo']
+ self.assertEqual(yadtreceiver._determine_tracking_id(list_with_tracking_id), None)
Please sign in to comment.
Something went wrong with that request. Please try again.