Example #1
0
def uploadify(request):
    """
    Given the name of a file to parse from the upload client-side, save the file
    to the filesystem, parse the file and attempt to match it in the database,
    and output the results of the parsing.
    """
    # Single file, retrieve the InMemoryUploadedFile and read its contents
    uploadedFile = request.FILES['Filedata']   #'Filedata' is set by client lib
    raw = uploadedFile.read()
    uploadedFile.close()

    # Get team and release objects
    team = Team.objects.get(team_name = request.POST.get('team'))
    release = Release.objects.get(release_name = request.POST.get('release'))
       
    # Create the log directory if necessary
    fullLogPath = os.path.join(tmpPath, team.team_name, release.release_name)
    if not os.path.exists(fullLogPath):
        os.makedirs(fullLogPath)
    
    # Write the file content to the temp location
    fpath = os.path.join(fullLogPath, uploadedFile.name)
    with open(fpath,'w') as f:
        f.write(raw)
    
    # parse logs
    parser = LogParser(fpath)
    parser.parseInput()
    db = DBInterfaceWrapper(parser.results, team, release)
    db.writeLogsToDB(request.POST.get('userId'))

    results = render_to_string('logs/parse_summary.html', {'results': parser.results._dict})
    failForm = render_to_string('logs/report_failures.html', {'results': parser.results._dict})
    
    return JSONHttpResponse({'results': results, 'fail_form': failForm})
Example #2
0
def processDolphinWorkorderComplete(request):
    """
    When a workorder is completed, Dolphin will send the filename, workorder id,
    and contents of the test log file as a JSON POST. This method grabs that 
    data and handles the updates to the workorder and saves the log file content.
    """

    # Dolphin sends just json in the raw post, so decode it here
    post = loads(request.raw_post_data)
    woid = post.get('workorder')
    log_file = urllib.unquote(post.get('logname'))
    raw_log = urllib.unquote_plus(post.get('log'))
    station = urllib.unquote(post.get('station', None))
    cluster = post.get('cluster')
    logger.info('Received status completion message from the regression system for workorder: %s, cluster: %s' % (woid, cluster))

    # First, find the workorder, if fortyTwo knows about it, the calling 
    # function already checked for existence of this workorder
    controller = ControllerInfo.objects.get(cluster_name=cluster)
    wo = WorkOrder.objects.get(workorder_id=woid, testplan__team=controller.team, testplan__release=controller.release)
    
    logger.info('Retrieved workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    

    # Save the log file contents to a file on the system
    # Create the log directory if necessary
    basePath = os.path.join(LOG_PARSE_BASE_PATH, wo.testplan.team.team_name, wo.testplan.release.release_name)
    if not os.path.exists(basePath):
        os.makedirs(basePath)

    # Write the file content to the workorder log location and update the workorder
    fpath = os.path.join(basePath, log_file)
    try:
        f = open(fpath,'w')
        f.write(raw_log)

    except Exception as ee:
        logger.error('fortyTwo was not able to save the log content for workorder %s. Error as follows: %s' % (wo.workorder_id, ee.message))
        raise

    finally:
        f.close()
    logger.info('Saved the log content for workorder id = %s (fortytwo id: %s) to %s' % (woid, wo.id, fpath))    

    wo.complete = True
    wo.result_log_location = os.sep.join([wo.testplan.team.team_name, wo.testplan.release.release_name, log_file])
    wo.result_test_station = station
    if wo.state != WorkOrder.STATE_CANCELLED:
        wo.state = WorkOrder.STATE_COMPLETE
    wo.save()

    # Parse the log to get some results and update the workorder model
    try:
        logger.info('Attempting to parse the log content for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    
        parser = LogParser(fpath)
        parser.parseInput()
        logger.info('Successfully parsed the log content for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    

        # Incomplete log parsing -> parse error, parse error is denoted by a complete
        # workorder, with false results, and null runtime
        if len(parser.results._dict['incomplete']) > 0:
            logger.info('Log content is determined to be an incomplete log for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    
            wo.result_passed = False
            wo.result_test_runtime = None

        # Store the results. By fiat, workorders only have one case, so take the first one
        else:
            logger.info('Log content was successfully parsed for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    
            wo.result_passed = parser.results.cases[0].passed
            wo.result_test_runtime = convertTimeToString(parser.results.cases[0].elapsedTime.rpartition('.')[0])
            wo.result_test_starttime = parser.results.cases[0].startTime
            wo.passfailerror = ",".join([str(parser.results.cases[0].verificationsPassed),
                                         str(parser.results.cases[0].verificationFailures),
                                         str(parser.results.cases[0].systemErrors)])
            wo.exception_json = json.dumps(parser.results.cases[0].exceptionInfo)
            wo.traceback = parser.results.cases[0].traceback

            # Put the log into the database if the batch is setup to do so
            # Only parse passing logs
            if wo.state != WorkOrder.STATE_CANCELLED and ((wo.batch.auto_log_parse == Batch.AUTOPARSE_PASS_ONLY and wo.result_passed is True) or wo.batch.auto_log_parse == Batch.AUTOPARSE_ALL):

                logger.info('Attempting to write the log content to the database for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))                    
                db = DBInterfaceWrapper(parser.results, wo.batch.team, wo.batch.release)
                db.writeLogsToDB()

                if parser.results._dict['matched']:
                    logger.info('Log content autoparsing has matched the log content to the database for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    
                    wo.testlog = wo.testplan.latesttestlog_set.get().testlog
                    wo.save()
                    logger.info('Log content saved to the database for workorder id = %s (fortytwo id: %s)' % (woid, wo.id))    
                else:
                    logger.warn('The log for workorder %s did not match a planned execution in the database' % woid)
                    raise Exception('The log for workorder %s did not match a planned execution in the database' % woid)
            else:
                logger.info('Log not inserted in database due to batch autoparse options for workorder id = %s (fortytwo id: %s) or because the workorder was already marked as cancelled' % (woid, wo.id))    

    except Exception as error:
        logger.info('Log parsing encountered an error for workorder id = %s (fortytwo id: %s). Error: %s' % (woid, wo.id, error))
        wo.result_passed = False
        wo.result_test_runtime = None

    wo.save()

    # Update the batch
    if wo.batch.run_state != Batch.STATE_COMPLETE_CANCEL: 
        updateBatchOnWorkorderComplete(wo)
    return JSONHttpResponse({'status': True, 'msg': 'Successfully received workorder %s complete notice. So long and thanks for all the fish.' % woid })