Ejemplo n.º 1
0
    def test_receive_online_with_error_status(self):
        slave = SlaveOnlineFactory(online=False)

        error_status = Status.err({
            'method': 'online',
            'result': str(Exception('foobar'))
        })

        error_status.uuid = slave.command_uuid

        # connect webinterface on /notifications
        webinterface = WSClient()
        webinterface.join_group('notifications')

        # send online answer
        ws_client = WSClient()
        ws_client.send_and_consume('websocket.receive',
                                   path='/commands',
                                   content={'text': error_status.to_json()})

        self.assertFalse(SlaveModel.objects.get(id=slave.id).is_online)

        # test if a connected message was send on /notifications
        self.assertEqual(
            Status.err(
                'An error occurred while connecting to client {}!'.format(
                    slave.name)),
            Status.from_json(json.dumps(webinterface.receive())),
        )
Ejemplo n.º 2
0
def filesystem_entry(request, filesystem_id):
    """
    Process requests for a single `FilesystemModel`s.

    HTTP Methods
    ------------
        DELETE:
            Removes the specified entry (in the URL) from the database.
        PUT:
            Updates the specified entry (in the URL) in the database.

    Parameters
    ----------
        request: HttpRequest
            The request which should be processed.

    Returns
    -------
        HttpResponse:
            If the HTTP method is not supported, then an
            `HttpResponseForbidden` is returned.
    """

    if request.method == 'DELETE':
        try:
            filesystem = FilesystemModel.objects.get(id=filesystem_id)
            try:
                fs_delete(filesystem)
                return StatusResponse.ok("")
            except FsimError as err:
                return StatusResponse(err)
        except FilesystemModel.DoesNotExist as err:
            return StatusResponse(FilesystemNotExistError(err, filesystem_id))
    elif request.method == 'PUT':
        # create form from a new QueryDict made from the request body
        # (request.PUT is unsupported) as an update (instance) of the
        # existing slave
        try:
            model = FilesystemModel.objects.get(id=filesystem_id)

            form = FilesystemForm(QueryDict(request.body), instance=model)
            if form.is_valid():
                filesystem = form.save(commit=False)
                try:
                    filesystem.full_clean()
                    form.save()
                    return StatusResponse.ok('')
                except ValidationError as _:
                    error_dict = {
                        'name': [
                            'Filesystem with this Name already exists on this Client.'
                        ]
                    }
                    return StatusResponse(Status.err(error_dict))
            else:
                return StatusResponse(Status.err(form.errors))
        except FilesystemModel.DoesNotExist as err:
            return StatusResponse(FilesystemNotExistError(err, filesystem_id))
    else:
        return HttpResponseForbidden()
Ejemplo n.º 3
0
    def test_receive_filesystem_restore_with_error_code(self):
        filesystem = MovedFileFactory()

        error_code = 'any kind of string'

        error_status = Status.err({
            'method': 'filesystem_restore',
            'result': error_code,
        })

        error_status.uuid = filesystem.command_uuid

        #  connect webinterface
        webinterface = WSClient()
        webinterface.join_group('notifications')

        ws_client = WSClient()
        ws_client.send_and_consume(
            'websocket.receive',
            path='/commands',
            content={'text': error_status.to_json()},
        )

        query = FilesystemModel.objects.get(id=filesystem.id)
        self.assertEqual(query.error_code, error_code)

        self.assertEqual(
            Status.ok({
                'filesystem_status': 'error',
                'fid': str(filesystem.id),
                'error_code': error_code,
            }),
            Status.from_json(json.dumps(webinterface.receive())),
        )
Ejemplo n.º 4
0
    def test_receive_get_log_program_not_exists(self):
        error_status = Status.ok({
            'method': 'get_log',
            'result': {
                'log': '',
                'uuid': '0'
            },
        })

        #  connect webinterface
        webinterface = WSClient()
        webinterface.join_group('notifications')

        ws_client = WSClient()
        ws_client.send_and_consume(
            'websocket.receive',
            path='/commands',
            content={'text': error_status.to_json()},
        )

        #  test if the webinterface gets the error message
        self.assertEqual(
            Status.err('Received log from unknown program!'),
            Status.from_json(json.dumps(webinterface.receive())),
        )
Ejemplo n.º 5
0
    def test_receive_execute_with_error_status(self):
        program_status = ProgramStatusFactory(running=True)
        program = program_status.program

        error_status = Status.err({
            'method': 'execute',
            'result': str(Exception('foobar')),
        })
        error_status.uuid = program_status.command_uuid

        #  connect webinterface
        webinterface = WSClient()
        webinterface.join_group('notifications')

        ws_client = WSClient()
        ws_client.send_and_consume(
            'websocket.receive',
            path='/commands',
            content={'text': error_status.to_json()},
        )

        query = ProgramStatusModel.objects.get(program=program)
        self.assertFalse(query.running)
        self.assertEqual(query.code, 'foobar')

        #  test if the webinterface gets the error message
        self.assertEqual(
            Status.ok({
                'program_status': 'finished',
                'pid': str(program.id),
                'code': 'foobar',
            }),
            Status.from_json(json.dumps(webinterface.receive())),
        )
Ejemplo n.º 6
0
    def test_rpc_method_raise_exception(self):  # pylint: disable=R0201
        """
        Tests what happens if a rpc method raises an exception
        """
        @Rpc.method
        @asyncio.coroutine
        def raises_async():  # pylint: disable=R0201,W0612
            """
            Simple async rpc function, that raises an Exception.
            """
            raise Exception('foo')

        cmd = Command('raises_async')
        status = Status.err({
            'method': 'raises_async',
            'result': str(Exception('foo'))
        })
        status.uuid = cmd.uuid

        Server(
            [
                cmd.to_json(),
            ],
            [
                status.to_json(),
            ],
        ).run()
Ejemplo n.º 7
0
    def test_state_next_offline_filesystem(self):
        webinterface = WSClient()
        webinterface.join_group('notifications')

        self.prog1.delete()

        self.sched._Scheduler__index = -1
        self.sched._Scheduler__event = asyncio.Event(loop=self.sched.loop)
        self.sched._Scheduler__script = self.script.id
        self.sched._Scheduler__state = SchedulerStatus.NEXT_STEP
        self.sched._Scheduler__state_next()

        self.assertEqual(
            self.sched._Scheduler__state,
            SchedulerStatus.ERROR,
        )

        self.assertStatusRegex(
            Status.err(SlaveOfflineError),
            Status.err(self.sched._Scheduler__error_code),
        )
Ejemplo n.º 8
0
def notify_err(message):
    """
    Sending the given `message` to the notification channel. Indicating that
    the message is an error message.

    Parameters
    ----------
        message: JSON object
            This message is send to the web interface with an Status.err()
            wrapped around.
    """
    Group('notifications').send({'text': Status.err(message).to_json()})
Ejemplo n.º 9
0
    def test_receive_get_log_with_error_status(self):
        error_status = Status.err({
            'method': 'get_log',
            'result': str(Exception('foobar')),
        })

        #  connect webinterface
        webinterface = WSClient()
        webinterface.join_group('notifications')

        ws_client = WSClient()
        ws_client.send_and_consume(
            'websocket.receive',
            path='/commands',
            content={'text': error_status.to_json()},
        )

        #  test if the webinterface gets the error message
        self.assertEqual(
            Status.err('An error occured while reading a log file!'),
            Status.from_json(json.dumps(webinterface.receive())),
        )
Ejemplo n.º 10
0
    def test_state_next_offline_program(self):
        webinterface = WSClient()
        webinterface.join_group('notifications')

        self.slave1.online = True
        self.slave1.save()

        self.sched._Scheduler__index = -1
        self.sched._Scheduler__event = asyncio.Event(loop=self.sched.loop)
        self.sched._Scheduler__script = self.script.id
        self.sched._Scheduler__state = SchedulerStatus.NEXT_STEP
        self.sched._Scheduler__state_next()

        self.assertEqual(
            Status.ok({
                'program_status': 'started',
                'pid': self.prog1.id,
            }),
            Status.from_json(json.dumps(webinterface.receive())),
        )

        self.assertEqual(
            self.sched._Scheduler__state,
            SchedulerStatus.WAITING_FOR_PROGRAMS_FILESYSTEMS,
        )

        self.sched._Scheduler__state = SchedulerStatus.NEXT_STEP
        self.sched._Scheduler__state_next()

        self.assertEqual(
            self.sched._Scheduler__state,
            SchedulerStatus.ERROR,
        )

        self.assertStatusRegex(
            Status.err(SlaveOfflineError),
            Status.err(self.sched._Scheduler__error_code),
        )
Ejemplo n.º 11
0
    def err(cls, payload, **kwargs):
        """
        Shorthand for StatusResponse(Status.err(payload))

        Arguments
        ---------
            payload: Payload for Status object
            **kwargs: arguments for HttpResponse

        Returns
        -------
            StatusResponse object with 'err' in Status
        """
        return cls(Status.err(payload), **kwargs)
Ejemplo n.º 12
0
    def test_receive_chain_commands_with_error_status(self):
        error_chain = Status.err({
            'method': 'chain_execution',
            'result': None,
        })

        #  connect webinterface
        webinterface = WSClient()
        webinterface.join_group('notifications')

        ws_client = WSClient()
        ws_client.send_and_consume(
            'websocket.receive',
            path='/commands',
            content={'text': error_chain.to_json()},
        )

        self.assertIsNone(webinterface.receive())
Ejemplo n.º 13
0
 def to_status(self):
     """
     Returns a Status object wich has the error as the payload.
     """
     return Status.err(str(self))