Esempio n. 1
0
File: ci.py Progetto: chaws/squad
def watch_job(request, group_slug, project_slug, version, environment_slug):
    backend_name = request.POST.get('backend')
    if backend_name is None:
        return HttpResponseBadRequest("backend field is required")
    backend = None
    try:
        backend = Backend.objects.get(name=request.POST.get('backend'))
    except Backend.DoesNotExist:
        return HttpResponseBadRequest("requested backend does not exist")

    # project has to exist or request will result with 400
    project = request.project
    if backend is None or project is None:
        return HttpResponseBadRequest("malformed request")

    # create Build object
    build, _ = project.builds.get_or_create(version=version)

    # testjob_id points to the backend's test job
    testjob_id = request.POST.get('testjob_id', None)

    if testjob_id is None:
        return HttpResponseBadRequest("testjob_id is required")

    # create TestJob object
    test_job = TestJob.objects.create(backend=backend,
                                      target=project,
                                      target_build=build,
                                      environment=environment_slug,
                                      submitted=True,
                                      job_id=testjob_id)
    log_addition(request, test_job, "Watch Job submission")

    # return ID of test job
    return HttpResponse(test_job.id, status=201)
Esempio n. 2
0
def create_build(request, group_slug, project_slug, version):
    project = request.project

    fields = {}
    patch_source = request.POST.get('patch_source', None)
    try:
        if patch_source:
            fields['patch_source'] = PatchSource.objects.get(name=patch_source)
    except PatchSource.DoesNotExist:
        return HttpResponse('Unknown patch source: %s' % patch_source,
                            status=400)

    patch_baseline = request.POST.get('patch_baseline', None)
    try:
        if patch_baseline:
            fields['patch_baseline'] = project.builds.get(
                version=patch_baseline)
    except Build.DoesNotExist:
        return HttpResponse('Unknown patch baseline: %s' % patch_baseline,
                            status=400)

    patch_id = request.POST.get('patch_id', None)
    if patch_id:
        fields['patch_id'] = patch_id

    create_build = CreateBuild(project)
    new_build, created = create_build(version=version, **fields)
    if created:
        log_addition(request, new_build, "Build created")
    else:
        log_change(request, new_build, "Build updated")
    return HttpResponse('', status=201)
Esempio n. 3
0
File: ci.py Progetto: chaws/squad
def submit_job(request, group_slug, project_slug, version, environment_slug):
    backend_name = request.POST.get('backend')
    if backend_name is None:
        return HttpResponseBadRequest("backend field is required")

    try:
        backend = Backend.objects.get(name=backend_name)
    except Backend.DoesNotExist:
        return HttpResponseBadRequest("requested backend does not exist")

    # project has to exist or request will result with 404
    project = request.project
    if project is None:
        return HttpResponseBadRequest("malformed request")

    # `Environment.expected_test_runs == -1` means that environment will stop receiving submissions
    environment, created = project.environments.get_or_create(
        slug=environment_slug)
    if environment.expected_test_runs == -1:
        return HttpResponseBadRequest(
            "environment '%s' is disabled and squad will not accept new submissions to it"
            % environment_slug)

    # create Build object
    build, _ = project.builds.get_or_create(version=version)

    # definition can be received as a file upload or as a POST parameter
    definition = None
    if 'definition' in request.FILES:
        definition = read_file_upload(
            request.FILES['definition']).decode('utf-8')
    else:
        definition = request.POST.get('definition')

    if definition is None:
        return HttpResponseBadRequest("test job definition is required")

    # create TestJob object
    test_job = TestJob.objects.create(
        backend=backend,
        definition=definition,
        target=project,
        target_build=build,
        environment=environment_slug,
    )
    log_addition(request, test_job, "Test Job submission")
    # schedule submission
    submit.delay(test_job.id)

    # return ID of test job
    return HttpResponse(test_job.id, status=201)
Esempio n. 4
0
def add_test_run(request, group_slug, project_slug, version, environment_slug):
    project = request.project

    test_run_data = {
        'version': version,
        'environment_slug': environment_slug,
    }

    uploads = {
        'tests_file': 'tests',
        'metrics_file': 'metrics',
        'log_file': 'log',
        'metadata_file': 'metadata',
    }
    for key, field in uploads.items():
        if field in request.FILES:
            f = request.FILES[field]
            test_run_data[key] = read_file_upload(f).decode('utf-8')
        elif field in request.POST:
            test_run_data[key] = request.POST[field]

    if 'metadata_file' not in test_run_data:
        metadata = {}
        for field in ReceiveTestRun.SPECIAL_METADATA_FIELDS:
            if field in request.POST:
                metadata[field] = request.POST[field]
        if metadata:
            test_run_data['metadata_file'] = json.dumps(metadata)

    if 'attachment' in request.FILES:
        attachments = {}
        for f in request.FILES.getlist('attachment'):
            attachments[f.name] = read_file_upload(f)
        test_run_data['attachments'] = attachments

    receive = ReceiveTestRun(project)

    try:
        testrun, build = receive(**test_run_data)
        log_addition(request, testrun, "Test Run created")
        if build:
            log_addition(request, build, "Build created")
    except (exceptions.invalid_input + (exceptions.DuplicatedTestJob, )) as e:
        logger.warning(request.get_full_path() + ": " + str(e))
        return HttpResponse(str(e), status=400)

    return HttpResponse('', status=201)
Esempio n. 5
0
def resubmit_job(request, test_job_id, method='resubmit'):
    testjob = get_object_or_404(TestJob.objects, pk=test_job_id)
    user = auth_user_from_request(request, request.user)
    project = testjob.target
    if not project.can_submit_testjobs(user):
        return HttpResponse(status=401)

    call = getattr(testjob, method)
    try:
        ret_value = call()
        new_testjob = testjob.resubmitted_jobs.last()
        if new_testjob is not None:
            log_addition(request, new_testjob, "Create testjob as resubmission")
    except SubmissionIssue as e:
        return HttpResponse(str(e), status=500)

    if ret_value:
        return HttpResponse(status=201)
    # return 403 when resubmit call is unsuccessful
    return HttpResponse(status=403)