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, {})
Example #2
0
    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)
Example #3
0
    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_receiver.states = {None: Mock()}
        mock_broadcaster = 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"]

        Receiver.perform_request(mock_receiver, mock_event, Mock())

        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,
        )
Example #4
0
    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)
Example #5
0
    def test_should_not_refresh_when_connected_but_hour_is_not_2_am(self, datetime):
        time = Mock()
        datetime.now.return_value = time
        time.hour = 8
        receiver = Mock(Receiver)
        receiver.broadcaster = Mock()
        receiver.broadcaster.client = Mock()

        self.assertFalse(Receiver._should_refresh_connection(receiver))
Example #6
0
    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_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_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')
Example #10
0
    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")
Example #11
0
    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)
Example #12
0
    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)
Example #13
0
    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!")
Example #14
0
    def test_should_publish_failed_event_with_stderr_from_process(self, mock_log):
        mock_protocol = Mock(ProcessProtocol)
        mock_broadcaster = Mock()
        mock_protocol.broadcaster = mock_broadcaster
        mock_protocol.hostname = "hostname"
        mock_protocol.target = "dev123"
        mock_protocol.readable_command = "/usr/bin/python abc"
        mock_protocol.tracking_id = "tracking_id"
        mock_protocol.error_buffer = StringIO("Someone has shut down the internet.")

        ProcessProtocol.publish_failed(mock_protocol, 123)

        self.assertEquals(
            call(
                "dev123",
                "/usr/bin/python abc",
                "failed",
                message="Someone has shut down the internet.",
                tracking_id="tracking_id",
            ),
            mock_broadcaster.publish_cmd_for_target.call_args,
        )
        self.assertEqual(METRICS["commands_failed.dev123"], 1)
Example #15
0
    def test_should_publish_finished_event(self):
        mock_protocol = Mock(ProcessProtocol)
        mock_broadcaster = Mock()
        mock_protocol.broadcaster = mock_broadcaster
        mock_protocol.hostname = "hostname"
        mock_protocol.target = "dev123"
        mock_protocol.readable_command = "/usr/bin/python abc"
        mock_protocol.tracking_id = "tracking-id"
        mock_protocol.error_buffer = Mock()

        ProcessProtocol.publish_finished(mock_protocol)

        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,
        )
        self.assertEqual(METRICS["commands_succeeded.dev123"], 1)
Example #16
0
    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,
        )