Example #1
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,
        )
    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 #5
0
    def test_resource_id_subscription(self):
        # Arrange
        app = Mock(ui_methods={}, ui_modules={}, async_db={"tests": None})
        resource_mock = Mock()
        resource_mock.arguments = dict()
        handler = SubscriptionHandler(app, resource_mock)
        handler._manager = Mock()

        # Act
        handler.open(resource_type="test", resource_id="1")

        # Assert
        self.assertEqual(handler._manager.createChannel.call_args[0][0], {"id": "1"})
    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 #7
0
    def test_resource_subscription(self, listen_mock):
        # Arrange
        app = Mock(ui_methods={}, ui_modules={}, async_db={"tests": None})
        resource_mock = Mock()
        resource_mock.arguments = dict()
        handler = SubscriptionHandler(app, resource_mock)
        handler._manager = Mock()

        # Act
        handler.open(resource_type="test")

        # Assert
        self.assertTrue(listen_mock.called)
        self.assertEqual(handler._manager.createChannel.call_count, 1)
        self.assertEqual(handler._manager.createChannel.call_args[0][1], "test")
Example #8
0
    def test_subscription_good_fields(self):
        # Arrange
        app = Mock(ui_methods={}, ui_modules={}, async_db={"tests": None})
        fields_request = Mock()
        fields_request.arguments = dict(fields=["name,ts"])

        manager_mock = Mock()

        handler = SubscriptionHandler(app, fields_request)
        handler._manager = manager_mock

        # Act
        handler.open(resource_type="test")

        # Assert
        self.assertEqual(manager_mock.createChannel.call_args[0][2], ["name", "ts"])
Example #9
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 #10
0
    def test_subscription_bad_query(self):
        # Arrange
        app = Mock(ui_methods={}, ui_modules={}, async_db={"tests": None})
        query_request = Mock()
        query_request.arguments = dict(query=["{ name: 'test', ts: { gte: 145 }"])

        manager_mock = Mock()

        handler = SubscriptionHandler(app, query_request)
        handler._manager = manager_mock
        handler.write_message = Mock(side_effects=ValueError)

        # Act
        handler.open(resource_type="test")

        # Assert
        self.assertFalse(handler.client)
        self.assertFalse(manager_mock.createChannel.called)
Example #11
0
    def test_subscription_good_query(self):
        # Arrange
        app = Mock(ui_methods={}, ui_modules={}, async_db={"tests": None})
        query_request = Mock()
        query_request.arguments = dict(query=['{ "name": "test", "ts": { "gte": 145 } }'])
        handler = SubscriptionHandler(app, query_request)

        manager_mock = Mock()

        handler._manager = Mock()
        handler.write_message = Mock(side_effects=ValueError)
        handler._manager = manager_mock

        # Act
        handler.open(resource_type="test")

        # Assert
        self.assertTrue(manager_mock.createChannel.called)
        self.assertEqual(manager_mock.createChannel.call_args[0][0], {"name": "test", "ts": {"gte": 145}})
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_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,
        )
Example #15
0
 def known_operator_arguments(self):
     operators = Mock()
     operators.arguments = dict(oper1=("1a", "1b"), oper2=("2a",), oper3=("3a", "3b"))
     return operators
 def test_get_version_will_fallback_to_query_params(self):
     request = Mock()
     request.headers = {"Accept": "text/html"}
     request.arguments = {"v": [1]}
     self.assertEqual(1, AcceptsVersionNegotiator().get_version(request))