def test_should_initialize_logging_when_service_starts(self): mock_receiver = Mock(Receiver) Receiver.startService(mock_receiver) self.assertEquals( call(), mock_receiver.initialize_twisted_logging.call_args)
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.states = {'foo': Mock()} mock_receiver.broadcaster = mock_broadcaster mock_receiver.get_target_directory.return_value = '/etc/yadtshell/targets/devabc123' mock_receiver.configuration = {'hostname': 'hostname', '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.perform_request(mock_receiver, mock_event, Mock()) 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)
def test_should_set_configuration(self): configuration = 'configuration' receiver = Receiver() receiver.set_configuration(configuration) self.assertEquals(configuration, receiver.configuration)
def test_should_queue_call_to_refresh_connection(self, mock_reactor): mock_receiver = Mock(Receiver) mock_receiver.broadcaster = Mock() Receiver._refresh_connection(mock_receiver, 123) self.assertEquals( call(123, mock_receiver._refresh_connection), mock_reactor.callLater.call_args)
def test_should_not_close_connection_to_broadcaster_when_first_call(self, mock_reactor): mock_receiver = Mock(Receiver) mock_broadcaster = Mock() mock_receiver.broadcaster = mock_broadcaster Receiver._refresh_connection(mock_receiver, 123, first_call=True) self.assertEquals(None, mock_broadcaster.client.sendClose.call_args)
def test_should_log_shutting_down_of_service(self, mock_log): mock_receiver = Mock(Receiver) Receiver.stopService(mock_receiver) # 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_should_set_the_client_to_none(self, mock_log): mock_receiver = Mock(Receiver) mock_broadcaster = Mock() mock_broadcaster.client = 'Test client' mock_receiver.broadcaster = mock_broadcaster Receiver.onConnectionLost(mock_receiver, 'Spam eggs.') self.assertEquals(None, mock_broadcaster.client)
def test_should_raise_exception_when_target_directory_does_not_exist(self, mock_exists): mock_exists.return_value = False receiver = Receiver() configuration = {'hostname': 'hostname', 'targets_directory': '/etc/yadtshell/targets/'} receiver.set_configuration(configuration) self.assertRaises( ReceiverException, receiver.get_target_directory, 'spargel')
def test_should_announce_showdown(self, call_later): receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.states = {'foo': None} event = Mock() event.arguments = ['--tracking-id=foo'] Receiver.handle_request(receiver, event) call_later.assert_called_with(10, receiver.states['foo'].showdown)
def test_should_initialize_fsm_when_handling_request(self, uuid_fun): receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.states = {'foo': None} event = Mock() event.arguments = ['--tracking-id=foo'] Receiver.handle_request(receiver, event) request_fsm = receiver.states['foo'] self.assertEqual(request_fsm.current, 'negotiating')
def test_should_call_when_lower_vote_received(self): receiver = Mock(Receiver) fsm = Mock() fsm.vote = 42 receiver.states = {'id123': fsm} lower_vote_event = {'id': 'vote', 'tracking_id': 'id123', 'payload': 5} Receiver.onEvent(receiver, 'target', lower_vote_event) fsm.call.assert_called_with()
def test_should_join_target_name_with_targets_directory(self, mock_exists): mock_exists.return_value = True receiver = Receiver() configuration = {'hostname': 'hostname', 'targets_directory': '/etc/yadtshell/targets/'} receiver.set_configuration(configuration) actual_target_directory = receiver.get_target_directory('spargel') self.assertEquals( '/etc/yadtshell/targets/spargel', actual_target_directory)
def test_should_vote_when_handling_request(self, uuid_fun): uuid_fun.return_value = "1234-5678" receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.states = {'foo': None} event = Mock() event.arguments = ['--tracking-id=foo'] event.target = 'target' Receiver.handle_request(receiver, event) receiver.broadcaster._sendEvent.assert_called_with( 'vote', data='1234-5678', tracking_id='foo', target='target')
def test_should_fold_when_higher_vote_received(self): receiver = Mock(Receiver) fsm = Mock() fsm.vote = 5 receiver.states = {'id123': fsm} higher_vote_event = {'id': 'vote', 'tracking_id': 'id123', 'payload': 42 } Receiver.onEvent(receiver, 'target', higher_vote_event) fsm.fold.assert_called_with()
def test_should_call_when_lower_vote_received(self): receiver = Mock(Receiver) fsm = Mock() fsm.vote = 42 receiver.states = {'id123': fsm} lower_vote_event = {'id': 'vote', 'tracking_id': 'id123', 'payload': 5 } Receiver.onEvent(receiver, 'target', lower_vote_event) fsm.call.assert_called_with()
def test_write_metrics_to_file(self, path_, open_): # initialize a receiver with given configuration configuration = {'metrics_directory': '/tmp/metrics', 'metrics_file': '/tmp/metrics/yrc.metrics' } yrc = Receiver() yrc.set_configuration(configuration) open_.return_value = MagicMock(spec=file) path_.return_value = True yrc.write_metrics_to_file() open_.assert_called_once_with('/tmp/metrics/yrc.metrics', 'w') file_handle = open_.return_value.__enter__.return_value file_handle.write.assert_called_once_with('foo=42\n')
def test_should_vote_when_handling_request(self, uuid_fun): uuid_fun.return_value = "1234-5678" receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.states = {'foo': None} event = Mock() event.arguments = ['--tracking-id=foo'] event.target = 'target' Receiver.handle_request(receiver, event) receiver.broadcaster._sendEvent.assert_called_with('vote', data='1234-5678', tracking_id='foo', target='target')
def test_should_handle_request(self, mock_event_class): mock_receiver = Mock(Receiver) mock_receiver.states = {None: Mock()} mock_event = Mock(Event) mock_event.is_a_vote = False mock_event_class.return_value = mock_event Receiver.onEvent(mock_receiver, 'target', { 'id': 'request', 'cmd': 'command', 'args': 'args'}) self.assertEqual( call('target', {'id': 'request', 'cmd': 'command', 'args': 'args'}), mock_event_class.call_args) self.assertEqual( call(mock_event), mock_receiver.handle_request.call_args)
def test_should_fold_when_higher_vote_received(self): receiver = Mock(Receiver) fsm = Mock() fsm.vote = 5 receiver.states = {'id123': fsm} higher_vote_event = { 'id': 'vote', 'tracking_id': 'id123', 'payload': 42 } Receiver.onEvent(receiver, 'target', higher_vote_event) fsm.fold.assert_called_with()
def test_should_cleanup_fsm_after_finishing(self): receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.states = {'foo': None} receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.states = {'foo': None} event = Mock() event.arguments = ['--tracking-id=foo'] Receiver.handle_request(receiver, event) receiver.states['foo'].showdown() receiver.states['foo'].spawned() self.assertEqual(receiver.states, {})
def test_should_call_start_logging_when_initializing_twisted_logging(self, mock_log, mock_log_file_class): receiver = Receiver() receiver.set_configuration({'log_filename': 'log/file.log', 'targets': set(['devabc123']), 'broadcaster_host': 'broadcaster_host', 'broadcaster_port': 1234}) mock_log_file = Mock(LogFile) mock_log_file_class.fromFullPath.return_value = mock_log_file receiver.initialize_twisted_logging() self.assertEqual( call('log/file.log', rotateLength=20000000, maxRotatedFiles=10), mock_log_file_class.fromFullPath.call_args) self.assertEquals(call(mock_log_file), mock_log.startLogging.call_args)
def test_subscribe_target_is_allowed(self): mock_receiver = Mock(Receiver) mock_config = Mock(ReceiverConfig) mock_config.configuration = {'allowed_targets': ['foo']} mock_config.__getitem__ = lambda _self, key: mock_config.configuration[ key] mock_receiver.broadcaster = Mock() mock_receiver.broadcaster.client = Mock() mock_receiver.configuration = mock_config Receiver.subscribeTarget(mock_receiver, 'foo') mock_receiver.configuration.reload_targets.assert_called_with() mock_receiver.broadcaster.client.subscribe.assert_called_with( mock_receiver.onEvent, 'foo')
def test_should_not_refresh_when_no_broadcaster_but_hour_is_2_am(self, datetime): time = Mock() datetime.now.return_value = time time.hour = 2 receiver = Mock(Receiver) self.assertFalse(Receiver._should_refresh_connection(receiver))
def test_should_publish_event_about_failed_request_when_handle_request_fails(self, mock_log, mock_event_class): mock_receiver = Mock(Receiver) mock_receiver.handle_request.side_effect = ReceiverException( 'It failed!') mock_receiver.states = {'some-id': Mock()} mock_event = Mock(Event) mock_event.is_a_vote = False mock_event.tracking_id = 'some-id' mock_event_class.return_value = mock_event Receiver.onEvent(mock_receiver, 'target', { 'id': 'request', 'cmd': 'command', 'args': 'args'}) self.assertEqual( call('target', {'id': 'request', 'cmd': 'command', 'args': 'args'}), mock_event_class.call_args) self.assertEqual( call(mock_event, 'It failed!'), mock_receiver.publish_failed.call_args)
def test_should_refresh_when_connected_and_hour_is_2_am(self, datetime): time = Mock() datetime.now.return_value = time time.hour = 2 receiver = Mock(Receiver) receiver.broadcaster = Mock() receiver.broadcaster.client = Mock() self.assertTrue(Receiver._should_refresh_connection(receiver))
def test_should_publish_event_about_failed_command_on_target(self, mock_log): mock_receiver = Mock(Receiver) mock_broadcaster = Mock() mock_receiver.broadcaster = mock_broadcaster mock_event = Mock(Event) mock_event.target = 'devabc123' mock_event.tracking_id = 'any-tracking-id' mock_event.command = 'yadtshell' mock_event.arguments = ['update'] Receiver.publish_failed(mock_receiver, mock_event, 'It failed!') mock_broadcaster.publish_cmd_for_target.assert_called_with( 'devabc123', 'yadtshell', 'failed', 'It failed!', tracking_id='any-tracking-id') self.assertEqual(yadtreceiver.METRICS['commands_failed.devabc123'], 1)
def test_should_broadcast_error_when_spawning_fails(self, mock_protocol, mock_reactor): mock_protocol.side_effect = RuntimeError('Booom!') 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.states = {None: Mock()} mock_receiver.configuration = {'hostname': 'hostname', '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.perform_request(mock_receiver, mock_event, Mock()) mock_receiver.publish_failed.assert_called_with(mock_event, "<type 'exceptions.RuntimeError'> : Booom!")
def test_should_publish_event_about_started_command_on_target(self, mock_log): mock_receiver = Mock(Receiver) mock_receiver.configuration = {'hostname': 'hostname'} mock_broadcaster = Mock() mock_receiver.broadcaster = mock_broadcaster mock_event = Mock(Event) mock_event.tracking_id = 'any-tracking-id' mock_event.target = 'devabc123' mock_event.command = 'yadtshell' mock_event.arguments = ['update'] Receiver.publish_start(mock_receiver, mock_event) mock_broadcaster.publish_cmd_for_target.assert_called_with( 'devabc123', 'yadtshell', 'started', '(hostname) target[devabc123] request: command="yadtshell", arguments=[\'update\']', tracking_id='any-tracking-id') self.assertEqual(yadtreceiver.METRICS['commands_started.devabc123'], 1)
def test_should_exit_when_no_target_configured(self, mock_exit, mock_log): receiver = Receiver() configuration = ConfigurationDict(allowed_targets=set(), broadcaster_host='broadcaster_host', broadcaster_port=1234) receiver.set_configuration(configuration) mock_broadcaster_client = Mock() receiver.broadcaster = mock_broadcaster_client receiver.onConnect() self.assertEquals(call(1), mock_exit.call_args)
def test_should_spawn_new_process_on_reactor_even_when_not_registered(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.states = {} mock_receiver.configuration = {'hostname': 'hostname', '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.perform_request(mock_receiver, mock_event, Mock()) 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)
def test_should_subscribe_to_target_from_configuration_when_connected(self): receiver = Receiver() mock_broadcaster_client = Mock() receiver.broadcaster = mock_broadcaster_client configuration = ConfigurationDict(allowed_targets=set(['devabc123']), broadcaster_host='broadcaster_host', broadcaster_port=1234) receiver.set_configuration(configuration) receiver.onConnect() self.assertEquals(call(receiver.onEvent, 'devabc123'), mock_broadcaster_client.client.subscribe.call_args)
def test_should_initialize_broadcaster_when_connecting_broadcaster(self, mock_wamb): configuration = {'broadcaster_host': 'broadcaster-host', 'broadcaster_port': 1234} receiver = Receiver() receiver.set_configuration(configuration) receiver._connect_broadcaster() self.assertEquals( call('broadcaster-host', 1234, 'yadtreceiver'), mock_wamb.call_args)
def test_write_metrics_to_file_is_noop_with_no_metrics_directory(self, path_, open_): configuration = {'metrics_directory': None, 'metrics_file': None, } yrc = Receiver() yrc.set_configuration(configuration) yrc.write_metrics_to_file() self.assertFalse(open_.called) self.assertFalse(path_.called)
def test_should_add_session_handler_to_broadcaster_when_connecting_broadcaster(self, mock_wamb): receiver = Receiver() configuration = {'broadcaster_host': 'broadcasterhost', 'broadcaster_port': 1234} receiver.set_configuration(configuration) mock_broadcaster_client = Mock() mock_wamb.return_value = mock_broadcaster_client receiver._connect_broadcaster() self.assertEquals( call(receiver.onConnect), mock_broadcaster_client.addOnSessionOpenHandler.call_args)
def test_should_subscribe_to_targets_from_configuration_in_alphabetical_order_when_connected(self): receiver = Receiver() mock_broadcaster_client = Mock() receiver.broadcaster = mock_broadcaster_client configuration = ConfigurationDict(allowed_targets=set(['dev01', 'dev02', 'dev03']), broadcaster_host='broadcaster_host', broadcaster_port=1234) receiver.set_configuration(configuration) receiver.onConnect() self.assertEquals( [call(receiver.onEvent, 'dev01'), call(receiver.onEvent, 'dev02'), call(receiver.onEvent, 'dev03')], mock_broadcaster_client.client.subscribe.call_args_list)