Пример #1
0
    def test_delete_task_with_synced_status_incorrect_region(self, m):
        """
        Test that we can delete a task with valid status (synced).

        :param m: The request mock.
        :return: Nothing.
        """
        m.get(KEYSTONE_URL + '/v2.0/tokens/token', json=self.validate_info_v2)
        m.post(KEYSTONE_URL + '/v2.0/tokens', json=self.validate_info_v2)

        # We have to secure that we have a task in the db with status synced.
        user = User(region='Spain',
                    name='*****@*****.**',
                    task_id='5678',
                    role='fake role',
                    status=Task.SYNCED)
        db.session.add(user)
        db.session.commit()

        result = self.app.delete('/regions/Trento/tasks/5678',
                                 headers={'X-Auth-Token': 'token'})

        self.assertEqual(
            result._status, '400 BAD REQUEST',
            'The result status of the operation is not the expected one')

        self.assertEqual(
            result._status_code, httplib.BAD_REQUEST,
            'The result status of the operation is not the expected one')
Пример #2
0
    def setUp(self):
        """
        Configure the test environment with the creation of the DB and storing some data to test it.

        :return: Nothing.
        """
        db.create_all()

        # create users:
        user1 = User(region='Spain',
                     name='*****@*****.**',
                     task_id='1234',
                     role='fake role',
                     status=Task.SYNCED)
        user2 = User(region='Spain2',
                     name='*****@*****.**',
                     task_id='5678',
                     role='fake role',
                     status=Task.SYNCING)
        db.session.add(user1)
        db.session.add(user2)
        db.session.commit()
Пример #3
0
    def test_run_in_thread(self, glancesync):
        """
        Check run_in_thread method works properly

        :param glancesync: Request mock decorator.
        :return: Nothing
        """
        user1 = User(region='Spain',
                     name='*****@*****.**',
                     task_id='1234',
                     role='fake role',
                     status=Task.SYNCING)
        db.session.add(user1)
        db.session.commit()

        controller.run_in_thread("Spain", user1)

        us = User.query.filter(User.task_id == user1.task_id).one()
        self.assertEquals(us.status, Task.SYNCED)
Пример #4
0
    def test_run_in_threadFail(self, glancesync, sync_region):
        """
        Check run_in_thread method when there is a section synchronizing the region.

        :param m: Request mock decorator.
        :return: Nothing
        """
        sync_region.side_effect = Exception('Boom!')

        user1 = User(region='Spain',
                     name='*****@*****.**',
                     task_id='1234',
                     role='fake role',
                     status=Task.SYNCING)
        db.session.add(user1)
        db.session.commit()

        controller.run_in_thread("Spain", user1)

        us = User.query.filter(User.task_id == user1.task_id).one()
        self.assertEquals(us.status, Task.FAILED)
Пример #5
0
    def test_delete_task_with_syncing_status(self, m):
        """
        Test that we receive a BAD REQUEST when we try to delete a task with incorrect status (status is syncing).

        :param m: The request mock.
        :return: Nothing.
        """
        m.get(KEYSTONE_URL + '/v2.0/tokens/token', json=self.validate_info_v2)
        m.post(KEYSTONE_URL + '/v2.0/tokens', json=self.validate_info_v2)

        # We have to secure that we have a task in the db with status synced.
        user = User(region='Spain',
                    name='*****@*****.**',
                    task_id='1234',
                    role='fake role',
                    status=Task.SYNCING)
        db.session.add(user)
        db.session.commit()

        result = self.app.delete('/regions/Trento/tasks/1234',
                                 headers={'X-Auth-Token': 'token'})

        self.assertEqual(
            result._status, '400 BAD REQUEST',
            'The result status of the operation is not the expected one')

        self.assertEqual(
            result._status_code, httplib.BAD_REQUEST,
            'The result status of the operation is not the expected one')

        data = json.loads(result.data)

        self.assertEqual(
            data['error']['message'],
            'Task status is syncing. Unable to delete it.',
            'The error message returned is not the expected one.')

        self.assertEqual(
            data['error']['code'], httplib.BAD_REQUEST,
            'The status od the task returned is not the expected one.')
Пример #6
0
    def test_get_task_status(self, m):
        """
        Test that we can recover the status of a task.

        :param m: The request mock.
        :return: Nothing.
        """
        m.get(KEYSTONE_URL + '/v2.0/tokens/token', json=self.validate_info_v2)
        m.post(KEYSTONE_URL + '/v2.0/tokens', json=self.validate_info_v2)

        # We have to secure that we have a task in the db with status synced.
        user = User(region='Spain',
                    name='*****@*****.**',
                    task_id='1234',
                    role='fake role',
                    status=Task.SYNCED)
        db.session.add(user)
        db.session.commit()

        result = self.app.get('/regions/Trento/tasks/1234',
                              headers={'X-Auth-Token': 'token'})

        self.assertEqual(
            result._status, "200 OK",
            'The result status of the operation is not the expected one')
        self.assertEqual(
            result._status_code, httplib.OK,
            'The result status of the operation is not the expected one')

        data = json.loads(result.data)

        self.assertEqual(data['taskId'], '1234',
                         'The task id returned is not the expected one.')
        self.assertEqual(
            data['status'], Task.SYNCED,
            'The status od the task returned is not the expected one.')
Пример #7
0
def synchronize(regionid, token=None):
    """
    Synchronize the images of the corresponding region defined by its regionId.
    The operation is asynchronous a response a taskId in order that you can follow
    the execution of the process.

    :param regionid: Region name how you can obtain from the Keystone
                     service. Example: Spain2.
    :param token: The token of the request to be authorized.
    :return: JSON message with the identification of the created task.
    """
    # WARNING
    # It is for testing only, the functionality of this method is create a new Task,
    # store the content of the new task with <taskid> in DB with status 'syncing'
    # and launch a fork to start the execution of the synchronization process. At the
    # end of the synchronization process, we update the DB registry with the status
    # 'synced'
    message = "POST, create a new synchronization task in the region: {}".format(regionid)

    logger_api.info(message)

    # Previously to each operation, we have to check if there is a task in the DB
    # with the status syncing associated to this region.
    users = User.query.filter(User.region == regionid).all()
    newtask = None

    if not users:
        newtask = Task(status=Task.SYNCING)

        # name and role should be returned from authorized operation, to be extended in Sprint 16.02
        newuser = User(region=regionid, name=token.username, task_id=str(newtask.taskid),
                       role='admin', status=newtask.status)

        db.session.add(newuser)

        try:
            db.session.commit()

            # Do the stuff to sync the images here...
            logger_api.debug("new thread")
            t = threading.Thread(target=run_in_thread, args=(regionid, newuser))
            t.start()

        except Exception as e:
            message = '''
            {
                "error": {
                    "message": "Please check that you have initialized the DB. See the documentation about it.",
                    "code": %s
                }
            }
            ''' % httplib.BAD_REQUEST

            abort(httplib.BAD_REQUEST, message)
    elif len(users) == 1 and users[0].region == regionid and users[0].status == Task.SYNCING:
        newtask = Task(taskid=users[0].task_id, status=users[0].status)

    if newtask is None:
        message = '''
            {
                "error": {
                    "message": "Already exist some task for this region",
                    "code": %s
                }
            }
            ''' % httplib.BAD_REQUEST

        abort(httplib.BAD_REQUEST, message)

    resp = make_response(newtask.dump(), httplib.OK)
    resp.headers[SERVER_HEADER] = SERVER
    resp.headers[CONTENT_TYPE] = JSON_TYPE

    logger_api.info('Return result: %s', newtask.dump())

    return resp