def get_project(project_name_or_id): project_id = None project_name = project_name_or_id try: project_id = ObjectId(project_name) except: pass if project_id is None: return Project.objects(name=project_name).first() else: return Project.objects(id=project_id).first()
def find_project_by_reference(ref): """Find a project by the information provided in the reference Find a project either by id or name (found in reference) :param ref: A slickqaweb.model.projectReference.ProjectReference instance :return: An instance of Project from mongo if found, None otherwise """ assert isinstance(ref, ProjectReference) project = None if hasattr(ref, 'id') and ref.id is not None: project = Project.objects(id=ref.id).first() if project is None and hasattr(ref, 'name') and ref.name is not None and ref.name != '': project = Project.objects(name=ref.name).first() return project
def get_build_reports(project_name, release_name): """Get all summary of all the builds for a particular release.""" limit = 15 if request.args.get("limit"): try: limit = int(request.args.get("limit")) except: pass groupType = "SERIAL" if request.args.get("groupType"): groupType = request.args.get("groupType") project_id, release_id, build_id = Project.lookup_project_release_build_ids(project_name, release_name, None, get_all_builds=True, limit=limit) report = {} report['name'] = "Release Report for {} {}".format(project_name, release_name) report['builds'] = [] report['grouptype'] = groupType if build_id is None: return JsonResponse({}) for build_object in build_id: testrun_group = TestrunGroup() testrun_group.name = "Build Report for {} {} Build {}".format(project_name, release_name, build_object['name']) testrun_group.grouptype = groupType testrun_group.testruns = Testrun.objects(build__buildId=build_object['id']).order_by("-dateCreated") report['builds'].append(testrun_group) return JsonResponse(report)
def cancel_results_for_build(project_name, release_name, build_name): """Cancel all results that are scheduled for this build.""" project_id, release_id, build_id = Project.lookup_project_release_build_ids(project_name, release_name, build_name) canceled_results = [] if build_id is None: return JsonResponse(None) for testrun in Testrun.objects(build__buildId=build_id).order_by("-dateCreated"): results_to_cancel = Result.objects(testrun__testrunId=testrun.id, status='NO_RESULT', runstatus__in=['SCHEDULED', 'TO_BE_RUN']) canceled_results.extend(results_to_cancel) for result in results_to_cancel: cancel_individual_result(result.id) return JsonResponse(canceled_results)
def reschedule_results_with_status_on_build(project_name, release_name, build_name, status): """Reschedule all results with a particular status for a build.""" project_id, release_id, build_id = Project.lookup_project_release_build_ids(project_name, release_name, build_name) rescheduled_results = [] if build_id is None: return JsonResponse(None) for testrun in Testrun.objects(build__buildId=build_id).order_by("-dateCreated"): results_to_reschedule = Result.objects(testrun__testrunId=testrun.id, status=status) rescheduled_results.extend(results_to_reschedule) for result in results_to_reschedule: reschedule_individual_result(result.id) return JsonResponse(rescheduled_results)
def get_tps_report(project_name, release_name, testplan_name): """Get all summary of all the testruns run against a particular build.""" project_id, release_id, _ = Project.lookup_project_release_build_ids(project_name, release_name, None) testplan = TestPlan.objects(project__id=project_id, name=testplan_name) if len(testplan) > 0: testplan = testplan[0] report = TestrunGroup() report.name = "{} Summary for {}".format(testplan_name, release_name) report.grouptype = "SERIAL" report.testruns = [] report.testruns.extend(Testrun.objects(project__id=project_id, release__releaseId=release_id, testplanId=testplan.id).order_by('-dateCreated').limit(50)) report.testruns.reverse() return JsonResponse(report) else: return JsonResponse({})
def get_build_report(project_name, release_name, build_name): """Get all summary of all the testruns run against a particular build.""" project_id, release_id, build_id = Project.lookup_project_release_build_ids(project_name, release_name, build_name) report = TestrunGroup() report.name = "Build Report for {} {} Build {}".format(project_name, release_name, build_name) report.grouptype = "PARALLEL" report.testruns = [] testplans = [] for testrun in Testrun.objects(build__buildId=build_id).order_by("-dateCreated"): assert isinstance(testrun, Testrun) if testrun.testplanId not in testplans: report.testruns.append(testrun) testplans.append(testrun.testplanId) return JsonResponse(report)
def add_pipeline(): """Create a new pipeline.""" project_name = None release_name = None build_name = None raw = read_request() new_pipeline = deserialize_that(raw, Pipeline()) proj_id = None existing_pipeline = get_pipeline(new_pipeline.name) if existing_pipeline: new_pipeline = deserialize_that(raw, existing_pipeline) else: # resolve project, release and build, create if necessary if is_provided(new_pipeline, 'project'): project_name = new_pipeline.project.name if is_provided(new_pipeline, 'release'): release_name = new_pipeline.release.name if is_provided(new_pipeline, 'build'): build_name = new_pipeline.build.name if project_name is not None or release_name is not None or build_name is not None: # we have something to lookup / create proj_id, rel_id, bld_id = Project.lookup_project_release_build_ids(project_name, release_name, build_name, create_if_missing=True) if proj_id is not None: new_pipeline.project.id = proj_id if rel_id is not None: new_pipeline.release.releaseId = rel_id if bld_id is not None: new_pipeline.build.buildId = bld_id for ind, phase in enumerate(new_pipeline.phases): new_pipeline.phases[ind] = phase_check(new_pipeline.phases[ind]) new_pipeline = pipeline_check(new_pipeline) new_pipeline.save() # add an event events.CreateEvent(new_pipeline) return JsonResponse(new_pipeline)
def get_build_report(project_name, release_name, build_name): """Get all summary of all the testruns run against a particular build.""" project_id, release_id, build_id = Project.lookup_project_release_build_ids(project_name, release_name, build_name) report = TestrunGroup() report.name = "Build Report for {} {} Build {}".format(project_name, release_name, build_name) report.grouptype = "PARALLEL" report.testruns = [] testplans = [] if build_id is None: return JsonResponse(None) for testrun in Testrun.objects(build__buildId=build_id).order_by("-dateCreated"): assert isinstance(testrun, Testrun) if testrun.testplanId not in testplans: report.testruns.append(testrun) testplans.append(testrun.testplanId) if report.state() == "FINISHED" and not report.finished: report.finished = datetime.datetime.utcnow() # report.save() Warning, be careful not to do this, build reports don't get saved # this will create a new testrungroup every time the build report is # queried return JsonResponse(report)
def add_testrun(): """Create a new testrun.""" project_name = None release_name = None build_name = None raw = read_request() new_tr = deserialize_that(raw, Testrun()) proj_id = None # resolve project, release and build, create if necessary if is_provided(new_tr, 'project'): project_name = new_tr.project.name if is_provided(new_tr, 'release'): release_name = new_tr.release.name if is_provided(new_tr, 'build'): build_name = new_tr.build.name if project_name is not None or release_name is not None or build_name is not None: # we have something to lookup / create proj_id, rel_id, bld_id = Project.lookup_project_release_build_ids(project_name, release_name, build_name, create_if_missing=True) if proj_id is not None: new_tr.project.id = proj_id if rel_id is not None: new_tr.release.releaseId = rel_id if bld_id is not None: new_tr.build.buildId = bld_id # if testplanId not provided, but testplan object is, resolve creating if necessary if is_not_provided(new_tr, 'testplanId') and 'testplan' in raw: tplan = TestPlan() tplan = deserialize_that(raw['testplan'], tplan) """:type : TestPlan""" query = {'name': tplan.name} if proj_id is not None: query['project__id'] = proj_id existing_plan = TestPlan.objects(**query).first() if existing_plan is not None: new_tr.testplanId = existing_plan.id else: if proj_id is not None: tplan.project = ProjectReference() tplan.project.id = proj_id tplan.project.name = new_tr.project.name tplan.save() new_tr.testplanId = tplan.id if is_not_provided(new_tr, 'dateCreated'): new_tr.dateCreated = datetime.datetime.utcnow() if is_not_provided(new_tr, 'info') and is_provided(new_tr, 'build') and \ is_provided(new_tr, 'project') and is_provided(new_tr, 'release'): project = get_project(new_tr.project.name) build = None if project is None: project = get_project(new_tr.project.id) if project is not None: release = get_release(project, new_tr.release.name) if release is None: release = get_release(project, new_tr.release.releaseId) if release is not None: build = get_build(release, new_tr.build.name) if build is None: build = get_build(release, new_tr.build.buildId) if build is not None and is_provided(build, 'description'): new_tr.info = build.description new_tr.save() # add an event events.CreateEvent(new_tr) return JsonResponse(new_tr)
def get_single_scheduled_result(hostname): parameters = read_request() """:type : dict""" rawquery = {'runstatus': 'SCHEDULED', 'status': 'NO_RESULT'} update = {'set__runstatus': 'TO_BE_RUN', 'set__hostname': hostname} attr_query = dict(**parameters) if 'project' in attr_query: del attr_query['project'] if 'release' in attr_query: del attr_query['release'] if 'build' in attr_query: del attr_query['build'] if 'provides' in attr_query: del attr_query['provides'] for key, value in list(attr_query.items()): rawquery["attributes.{}".format(key)] = value project_id, release_id, build_id = Project.lookup_project_release_build_ids(parameters.get('project', None), parameters.get('release', None), parameters.get('build', None)) if project_id is not None: rawquery['project.id'] = project_id else: rawquery['project.name'] = parameters.get('project', None) if release_id is not None: rawquery['release.releaseId'] = release_id elif parameters.get('release', None) is not None: rawquery['release.name'] = parameters.get('release', None) if build_id is not None: rawquery['build.buildId'] = build_id elif parameters.get('build', None) is not None: rawquery['build.name'] = parameters.get('build', None) # if 'project' in parameters: # project = get_project(parameters["project"]) # if project is not None: # rawquery['project.id'] = project.id # else: # rawquery['project.name'] = parameters["project"] # if 'release' in parameters: # if project is not None: # release = get_release(project, parameters['release']) # if release is not None: # rawquery['release.releaseId'] = release.id # else: # rawquery['release.name'] = parameters['release'] # if 'build' in parameters: # if release is not None: # build = get_build(release, parameters['build']) # if build is not None: # rawquery['build.buildId'] = build.id # else: # rawquery['build.name'] = parameters['build'] provides = [] if 'provides' in parameters: provides = parameters['provides'] # from http://stackoverflow.com/questions/22518867/mongodb-querying-array-field-with-exclusion rawquery['requirements'] = {'$not': {'$elemMatch': {'$nin': provides}}} import mongoengine # mongoengine.QuerySet.modify() result = Result.objects(__raw__=rawquery).order_by("recorded").modify(new=True, full_response=False, **update) # query = {} # if 'project' in parameters: # query['project__name'] = parameters['project'] # if 'release' in parameters: # query['release__name'] = parameters['release'] # if 'build' in parameters: # query['build__name'] = parameters['build'] return JsonResponse(result)
def add_result(testrun_id=None): """Create a new result.""" raw = read_request() new_result = deserialize_that(raw, Result()) assert isinstance(new_result, Result) # validate -------------------------------------------------------------- # you must have a testcase reference (some info about the testcase) and a # status for the result. Otherwise it's not really a result. errors = [] if is_not_provided(new_result, 'status'): errors.append("status must be set") if is_not_provided(new_result, 'testcase') or (is_not_provided(new_result.testcase, 'name') and is_not_provided(new_result.testcase, 'testcaseId') and is_not_provided(new_result.testcase, 'automationId') and is_not_provided(new_result.testcase, 'automationKey')): errors.append("testcase must be provided with at least one identifying piece of data") if len(errors) > 0: return Response('\r\n'.join(errors), status=400, mimetype="text/plain") # fill in defaults ------------------------------------------------------- # a few fields can easily be inferred or set to a default if is_not_provided(new_result, 'runstatus'): if new_result.status == "NO_RESULT": new_result.runstatus = "TO_BE_RUN" else: new_result.runstatus = "FINISHED" if is_not_provided(new_result, 'recorded'): new_result.recorded = datetime.datetime.utcnow() # resolve references ----------------------------------------------------- testrun = None project = None testcase = None release = None build = None component = None configuration = None if testrun_id is not None: testrun = Testrun.objects(id=testrun_id).first() if testrun is not None: new_result.testrun = create_testrun_reference(testrun) # the order in this section is important. We try to find information any way we can, # so if it's not provided in the result, we look at the testrun, if it's not in the testrun, # but it is in the testcase we get it from there. # first lookup the testrun and resolve it if we can if is_provided(new_result, 'testrun') and testrun is None: testrun = find_testrun_by_reference(new_result.testrun) # don't create a new testrun if it's null, we'll do that later after we resolve the other # pieces of information # try to resolve the testcase, we won't try to create it if it's none yet. # for that we need to resolve as much of the other information we can. testcase = find_testcase_by_reference(new_result.testcase) # try to find the project from the data provided in the result. If that doesn't work, # and we do have a testrun, see if we can get it from there. If we do have a name of a # project and we still haven't found the project, create it! if is_provided(new_result, 'project'): project = find_project_by_reference(new_result.project) if project is None and testrun is not None and is_provided(testrun, 'project'): project = find_project_by_reference(testrun.project) if project is None and is_provided(new_result.project, 'name'): project = Project() project.name = new_result.project.name project.save() # if they didn't provide any project data, but did provide testrun data, try # to resolve the project from the testrun if project is None and testrun is not None and is_provided(testrun, 'project'): project = find_project_by_reference(testrun.project) # if we couldn't resolve the project previously, but we can resolve the testcase # see if we can get the project from the testcase if project is None and testcase is not None and is_provided(testcase, 'project'): project = find_project_by_reference(testcase.project) # finally, make sure that the reference we have in the result has all the info in it if project is not None: new_result.project = create_project_reference(project) # resolve the component if is_provided(new_result, 'component'): if project is not None: component = find_component_by_reference(project, new_result.component) if component is None: component = Component() component.id = ObjectId() component.name = new_result.component.name if is_provided(new_result.component, 'code'): component.code = new_result.component.code else: component.code = component.name.lower().replace(' ', '-') project.components.append(component) project.save() if component is not None: new_result.component = create_component_reference(component) # create a testcase if needed if testcase is None and is_not_provided(new_result.testcase, 'name'): return Response('Existing testcase not found, please provide a testcase name if you want one to be created.\n', status=400, mimetype="text/plain") elif testcase is None: testcase = Testcase() testcase.created = datetime.datetime.utcnow() testcase.name = new_result.testcase.name if is_provided(new_result.testcase, 'automationId'): testcase.automationId = new_result.testcase.automationId if is_provided(new_result.testcase, 'automationKey'): testcase.automationKey = new_result.testcase.automationKey if project is not None: testcase.project = create_project_reference(project) if component is not None: testcase.component = create_component_reference(component) testcase.save() testcase_changed = False if 'steps' in raw['testcase']: testcase.steps = [] for raw_step in raw['testcase']['steps']: step = deserialize_that(raw_step, Step()) testcase.steps.append(step) testcase_changed = True if 'purpose' in raw['testcase']: testcase.purpose = raw['testcase']['purpose'] testcase_changed = True if 'requirements' in raw['testcase']: testcase.requirements = raw['testcase']['requirements'] testcase_changed = True if 'author' in raw['testcase']: testcase.author = raw['testcase']['author'] testcase_changed = True # TODO: feature and automationTool if testcase_changed: testcase.save() # no matter what testcase should not be None at this point, but just in case I made a mistake if testcase is None: return Response('Somehow I was unable to find or create a testcase for this result.\n', status=400, mimetype="text/plain") new_result.testcase = create_testcase_reference(testcase) # dereference release and build if possible if is_provided(new_result, 'release') and project is not None: release = find_release_by_reference(project, new_result.release) if release is None and testrun is not None and project is not None and is_provided(testrun, 'release'): release = find_release_by_reference(project, testrun.release) if release is None and project is not None and is_provided(new_result, 'release') and is_provided(new_result.release, 'name'): release = Release() release.id = ObjectId() release.name = new_result.release.name project.releases.append(release) project.save() if release is not None: new_result.release = create_release_reference(release) if is_provided(new_result, 'build'): build = find_build_by_reference(release, new_result.build) if build is None and testrun is not None and is_provided(testrun, 'build'): build = find_build_by_reference(release, testrun.build) if build is None and project is not None and is_provided(new_result, 'build') and is_provided(new_result.build, 'name'): build = Build() build.id = ObjectId() build.name = new_result.build.name build.built = datetime.datetime.utcnow() release.builds.append(build) project.save() if build is not None: new_result.build = create_build_reference(build) # dereference configuration if is_provided(new_result, 'config'): configuration = find_configuration_by_reference(new_result.config) if configuration is None and testrun is not None and is_provided(testrun, 'config'): configuration = find_configuration_by_reference(testrun.config) if configuration is None and is_provided(new_result, 'config') and is_provided(new_result.config, 'name'): configuration = Configuration() configuration.name = new_result.config.name if is_provided(new_result.config, 'filename'): configuration.filename = new_result.config.filename configuration.save() if configuration is not None: new_result.config = create_configuration_reference(configuration) # if there is no testrun, create one with the information provided if testrun is None: testrun = Testrun() if is_provided(new_result, 'testrun') and is_provided(new_result.testrun, 'name'): testrun.name = new_result.testrun.name else: testrun.name = 'Testrun starting %s' % str(datetime.datetime.utcnow()) if project is not None: testrun.project = create_project_reference(project) if configuration is not None: testrun.config = create_configuration_reference(configuration) if release is not None: testrun.release = create_release_reference(release) if build is not None: testrun.build = create_build_reference(build) testrun.dateCreated = datetime.datetime.utcnow() testrun.runStarted = datetime.datetime.utcnow() testrun.state = 'RUNNING' testrun.save() if testrun is not None: new_result.testrun = create_testrun_reference(testrun) status_name = "inc__summary__resultsByStatus__" + new_result.status Testrun.objects(id=testrun.id).update_one(**{status_name: 1}) apply_triage_notes(new_result, testcase) new_result.history, estimatedRuntime = find_history(new_result) if new_result.attributes is None: new_result.attributes = {} new_result.attributes['estimatedRuntime'] = str(estimatedRuntime) new_result.save() events.CreateEvent(new_result) return JsonResponse(new_result)