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_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 #3
0
    def test_set_char_status_mutually_exclusive(self):
        mock_char = Mock(name="char")
        mock_char.states = []

        with patch.object(self.character_controller, "create_character", Mock(return_value=mock_char)):
            self.character_controller.set_char_status("character", "online")

        self.assertTrue("online" in mock_char.states)
Example #4
0
    def test_set_char_status_with_new_state(self):
        mock_char = Mock(name="char")
        mock_char.states = []

        with patch.object(self.character_controller, "create_character", Mock(return_value=mock_char)):
            self.character_controller.set_char_status("character", "alive")

        self.assertTrue("alive" in mock_char.states)
Example #5
0
    def test_set_char_status(self):
        mock_char = Mock(name="char")
        mock_char.states = ["alive"]

        with patch.object(self.character_controller, "create_character", Mock(return_value=mock_char)):
            result = self.character_controller.set_char_status("character", "alive")

        self.assertEqual(result, mock_char)
Example #6
0
 def setup_mock_options(cls, force=False, shards=None, states="RUNNING", listformat=None):
     mock_options = Mock(spec=["force", "shards", "states", "listformat", "verbosity"])
     mock_options.force = force
     mock_options.shards = shards
     mock_options.states = states
     mock_options.listformat = listformat or "%role%/%jobName%/%instanceId% %status%"
     mock_options.verbosity = False
     return mock_options
Example #7
0
    def test_create_character_with_new_charobj(self):
        mock_char = Mock(name="char", spec=[])
        mock_char.states = []
        MockCharacter = Mock(name="Character", return_value=mock_char)

        with patch.object(zoneserver, "Character", MockCharacter):
            with patch.object(self.character_controller, "get_character", Mock(return_value=None)):
                result = self.character_controller.create_character("character")

        self.assertEqual(result, mock_char)
    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_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)
Example #10
0
    def test_get_character_non_existent_empty_states(self):
        mock_char = Mock()
        mock_char.states = []
        MockCharacter = Mock()
        MockCharacter.objects().first = Mock(return_value=mock_char)

        with patch.object(zoneserver, "Character", MockCharacter):
            result = self.character_controller.get_character("character")

        self.assertFalse(result)
    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 #12
0
    def test_should_handle_request_with_wamp_v2(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, {"id": "request", "cmd": "command", "args": "args"}  # wamp v2: no topic in onEvent
        )

        self.assertEqual(call(None, {"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_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()
Example #15
0
    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)
Example #16
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 #17
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,
        )