Esempio n. 1
0
def post_task():
    data = request.json

    if data is None:
        raise HTTPError(400)

    try:
        jsonschema.validate(data, post_task_schema)
    except jsonschema.ValidationError:
        raise HTTPError(400)

    session = create_session()

    task_uuid = str(data['task_id'])
    task_type = str(data['task_type'])
    task_test_id = (data['test_id'])
    task_data = ""

    task = Task(
        uuid=task_uuid,
        type=task_type,
        version=int(data['task_version']),
        test_id=task_test_id
    )

    if 'task_data' in data:
        task_data = json.dumps(data['task_data'])
        task.data = task_data

    try:
        session.add(task)
    except IntegrityError:
        session.rollback()
        raise HTTPError(400)

    try:
        session.commit()
    except (IntegrityError, ProgrammingError):
        session.rollback()
        logger.error("Failed to commit database changes for BPMS task POST")
        raise HTTPError(400)
    finally:
        session.close()

    logger.info("Task posted by BPMS - Task's type: {}, test process id: {}, uuid: {}, parameters: {}"
                .format(task_type, task_test_id, task_uuid, task_data))
def dev_post_tasks():
    data = request.json
    if data is None:
        raise HTTPError(400)

    try:
        jsonschema.validate(data, dev_post_tasks_schema)
    except jsonschema.ValidationError:
        raise HTTPError(400)

    try:
        session = create_session()
    except:
        raise HTTPError(500)

    task = Task(
        uuid=str(data['task_id']),
        type=data['task_type'],
        version=data['task_version'],
        test_id=data['test_id'])

    if 'task_data' in data:
        task.data = json.dumps(data['task_data'])

    try:
        session.add(task)
    except IntegrityError:
        session.rollback()
        session.close()
        raise HTTPError(400)

    try:
        session.commit()
    except (IntegrityError, ProgrammingError):
        session.rollback()
        raise HTTPError(400)
    finally:
        session.close()
def dev_post_tasks():
    data = request.json
    if data is None:
        raise HTTPError(400)

    try:
        jsonschema.validate(data, dev_post_tasks_schema)
    except jsonschema.ValidationError:
        raise HTTPError(400)

    try:
        session = create_session()
    except:
        raise HTTPError(500)

    task = Task(uuid=str(data['task_id']),
                type=data['task_type'],
                version=data['task_version'],
                test_id=data['test_id'])

    if 'task_data' in data:
        task.data = json.dumps(data['task_data'])

    try:
        session.add(task)
    except IntegrityError:
        session.rollback()
        session.close()
        raise HTTPError(400)

    try:
        session.commit()
    except (IntegrityError, ProgrammingError):
        session.rollback()
        raise HTTPError(400)
    finally:
        session.close()
    def test_push_response():
        test_app = TestApp(app)
        session = create_session()
        task = Task()
        task.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546013'
        task.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546013'
        task.claimed = datetime.utcnow()
        session.add(task)
        session.commit()
        test_app.post_json(
            '/tasks/response', {
                'protocol': 1,
                'task_id': 'de305d54-75b4-431b-adb2-eb6b9e546013',
                'task_data': {
                    'key': 'value',
                    'another_key': 5
                }
            })

        session = create_session()
        task = session.query(Task).filter(
            Task.uuid == 'de305d54-75b4-431b-adb2-eb6b9e546013').one()

        assert task.completed is not None
        assert task.result_data is not None
        assert task.failed is None
        assert task.error is None

        task.completed = None
        task.result_data = None
        session.commit()
        test_app.post_json(
            '/tasks/response', {
                'protocol': 1,
                'task_id': 'de305d54-75b4-431b-adb2-eb6b9e546013',
                'task_error': 'Something went terribly wrong'
            })

        session = create_session()
        task = session.query(Task).filter(
            Task.uuid == 'de305d54-75b4-431b-adb2-eb6b9e546013').one()

        assert task.completed is None
        assert task.result_data is None
        assert task.failed is not None
        assert task.error is not None

        session.close()
    def test_push_response():
        test_app = TestApp(app)
        session = create_session()
        task = Task()
        task.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546013'
        task.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546013'
        task.claimed = datetime.utcnow()
        session.add(task)
        session.commit()
        test_app.post_json('/tasks/response', {
            'protocol': 1,
            'task_id': 'de305d54-75b4-431b-adb2-eb6b9e546013',
            'task_data': {
                'key': 'value',
                'another_key': 5
            }
        })

        session = create_session()
        task = session.query(Task).filter(Task.uuid == 'de305d54-75b4-431b-adb2-eb6b9e546013').one()

        assert task.completed is not None
        assert task.result_data is not None
        assert task.failed is None
        assert task.error is None

        task.completed = None
        task.result_data = None
        session.commit()
        test_app.post_json('/tasks/response', {
            'protocol': 1,
            'task_id': 'de305d54-75b4-431b-adb2-eb6b9e546013',
            'task_error': 'Something went terribly wrong'
        })

        session = create_session()
        task = session.query(Task).filter(Task.uuid == 'de305d54-75b4-431b-adb2-eb6b9e546013').one()

        assert task.completed is None
        assert task.result_data is None
        assert task.failed is not None
        assert task.error is not None

        session.close()
    def test_post_task_duplicate():
        test_app = TestApp(app)

        session = create_session()
        task1 = Task()
        task1.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546018'
        task1.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546018'
        task1.claimed = datetime.utcnow()
        task1.data = json.dumps({'wait_time': 123})
        session.add(task1)
        session.commit()
        session.close()

        assert test_app.post_json('/task', {
            'task_id': 'de305d54-75b4-431b-adb2-eb6b9e546018',
            'test_id': 'de305d54-75b4-431b-adb2-eb6b9e546013',
            'task_type': 'wait',
            'task_version': 1
        }, expect_errors=True).status_int == 400
    def test_pull_task():
        test_app = TestApp(app)

        session = create_session()
        task1 = Task()
        task1.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546018'
        task1.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546018'
        task1.claimed = datetime.utcnow()
        task1.data = json.dumps({'wait_time': 123})
        session.add(task1)

        task2 = Task()
        task2.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546019'
        task2.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546019'
        task2.claimed = datetime.utcnow()
        task2.completed = datetime.utcnow()
        task2.result_data = json.dumps({'result': 'epic success'})
        session.add(task2)

        task3 = Task()
        task3.uuid = 'de305d54-75b4-431b-adb2-eb6b9e546020'
        task3.test_id = 'de305d54-75b4-431b-adb2-eb6b9e546020'
        task3.claimed = datetime.utcnow()
        task3.failed = datetime.utcnow()
        task3.error = 'unknown error'
        session.add(task3)

        session.commit()

        test_app.get('/task/de305d54-75b4-431b-adb2-eb6b9e546018')
        test_app.get('/task/de305d54-75b4-431b-adb2-eb6b9e546019')
        test_app.get('/task/de305d54-75b4-431b-adb2-eb6b9e546020')