示例#1
0
 def task_events(self, task_id):
     events = [
         x for x in TaskEvent.select().join(Task).where(
             Task.task_id == task_id).order_by(TaskEvent.timestamp)
     ]
     if len(events):
         return events
     else:
         raise ZeroResults(f'No results returned for task_id: {task_id}')
示例#2
0
def test_get_task_events_no_events(api, client, testtask):
    task_id = testtask().json['task']['task_id']

    task = Task.get(Task.task_id == task_id)

    TaskEvent.delete() \
             .where(TaskEvent.task == task) \
             .execute()

    events = TaskEvent.select() \
                      .join(Task) \
                      .where(Task.task_id == task_id)

    assert len([x for x in events]) == 0, 'events where not cleared out'

    resp = client.get(api.url_for(GMan, task_id=task_id, events='events'))

    assert resp.status_code == 404
示例#3
0
    def task_completed_event(self, task_id):
        events = [
            x for x in (TaskEvent.select().join(Task).where(
                (Task.task_id == task_id)
                & (TaskEvent.status.in_(['failed', 'completed']))))
        ]

        if len(events):
            return events[0]
        else:
            raise ZeroResults(f'No results returned for task_id: {task_id}')
示例#4
0
    def post(self, *args, **kwargs):

        try:
            if len(args) or len(kwargs):
                raise BadRequest('Invalid args specified for POST')

            raw = request.get_json(force=True)

            marshaller = ArtManMarshaller(raw)
            marshaller.enforce('create_artifact')
            task = Task.get(Task.task_id == marshaller.task.data.task_id)
            try:
                # exact duplicate artifact post detection
                artifact = Artifact.get(
                    (Artifact.sri == marshaller.artifact.data.sri)
                    & (Artifact.task_id == marshaller.task.data.task_id))
                marshaller.errors.add('sri', 'Artifact Exists')
                marshaller.errors.add('event_id', 'Artifact Exists')
                return marshaller.errors.emit(), 409
            except DoesNotExist:
                # create an event for this artifact and store
                event = GMan.put(self,
                                 task.task_id,
                                 json={
                                     'status': 'info',
                                     'message': 'Adding artifact'
                                 })

                event = TaskEvent.get(
                    TaskEvent.event_id == event.data['event_id'])

                art_status = marshaller.artifact.data.status

                artifact = Artifact.create(task=task,
                                           event_id=event,
                                           type=marshaller.artifact.data.type,
                                           status=art_status,
                                           sri=marshaller.artifact.data.sri,
                                           uri=marshaller.artifact.data.uri)

                if not artifact:
                    raise QueryFailed(
                        f'Failed to create artifact for task {task.task_id}')

            return ArtifactSchema().dump(artifact)
        except MarshalError as e:
            return e.errors.emit(), 422
        except DoesNotExist:
            marshaller.errors.add('task_id', 'does not exist')
            return marshaller.errors.emit(), 404
        except BadRequest as e:
            return self.BadRequest(str(e))
        except (QueryFailed, Exception) as e:
            return self.InternalError(str(e))
示例#5
0
    def post(self, *args, **kwargs):

        try:
            if 'task_id' in kwargs or 'events' in kwargs:
                raise BadRequest('Invalid args for this method')

            if 'json' in kwargs:
                raw = kwargs['json']
            else:
                raw = request.get_json(force=True)

            marshaller = GManMarshaller(raw)
            marshaller.enforce('create_task')

            if marshaller.task.data.thread_id != marshaller.task.data.task_id:
                Task.get(Task.task_id == marshaller.task.data.thread_id)

            if marshaller.task.data.parent_id:
                Task.get(Task.task_id == marshaller.task.data.parent_id)

            task = Task.create(task_id=marshaller.task.data.task_id,
                               run_id=marshaller.task.data.run_id,
                               project=marshaller.task.data.project,
                               thread_id=marshaller.task.data.thread_id,
                               parent_id=marshaller.task.data.parent_id,
                               caller=marshaller.task.data.caller)

            event = TaskEvent.create(task=task,
                                     message=marshaller.event.data.message,
                                     status=marshaller.event.data.status,
                                     timestamp=marshaller.event.data.timestamp)

            return TaskEventSchema(exclude=['id']).dump(event)

        except MarshalError as e:
            return e.errors.emit(), 422
        except DoesNotExist:
            marshaller.errors.add(
                'missing_task', 'An existing task_id must exist'
                ' for thread_id and parent_id')
            return marshaller.errors.emit(), 422
        except AttributeError as e:
            reg = re.compile(r" '(.+)'")
            matches = reg.search(str(e))
            marshaller.errors.add(matches.groups()[0],
                                  'Is required but not valid')
            return marshaller.errors.emit(), 422
        except BadRequest as e:
            return self.BadRequest(str(e))
        except Exception as e:
            return self.InternalError(str(e))
示例#6
0
def test_raw_artifact_bad_hash(testtask):
    task_resp = testtask()
    task = Task().get(Task.task_id == task_resp.json['task']['task_id'])

    event = TaskEvent.create(task=task,
                             message='testing creating an artifact',
                             status='info',
                             timestamp=datetime.datetime.now())
    with pytest.raises(ValueError):
        Artifact().create(task=task,
                          event_id=event,
                          type='log',
                          status='unknown',
                          sri='some non sri value',
                          uri='https://www.example.com')
示例#7
0
    def put(self, task_id, events=None, json=None):

        try:
            if events:
                raise BadRequest('Events may not be specified on a PUT')

            if json:
                raw = json
            else:
                raw = request.get_json(force=True)

            marshaller = GManMarshaller(raw)
            marshaller.enforce('add_event')
            task = Task.get(Task.task_id == task_id)
            try:
                completed = self.task_completed_event(task_id)
                marshaller.errors.add(
                    'status', f'A closing event for {completed.task_id} of '
                    f'{completed.status} already exists.')

                return marshaller.errors.emit(), 422
            except ZeroResults:
                pass

            event = TaskEvent.create(task=task,
                                     message=marshaller.event.data.message,
                                     status=marshaller.event.data.status,
                                     timestamp=marshaller.event.data.timestamp)
            if event:
                return TaskEventSchema(exclude=['id']).dump(event)
            else:
                raise QueryFailed(
                    f'Event Creation Error for task_id {task_id}')

        except MarshalError as e:
            return e.errors.emit(), 422
        except DoesNotExist:
            return self.NotFound()
        except BadRequest as e:
            return self.BadRequest(str(e))
        except Exception as e:
            return self.InternalError(str(e))