Beispiel #1
0
    def testCcPyStateGetSet(self):
        try:
            myCcPyState = CcPyState('ccpy.state.tmp')
            self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.OK)
            self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.UNKNOWN)
            myCcPyState.setPrjState('Product V3', PrjStates.FAILED)
            self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.OK)
            self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.FAILED)
            myCcPyState.setPrjState('Product V2', PrjStates.FAILED)
            myCcPyState.setPrjState('Product V3', PrjStates.OK)
            self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.FAILED)
            self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.OK)

            os.remove('ccpy.state.tmp')
            myCcPyState = CcPyState('ccpy.state.tmp')
            self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.UNKNOWN)
            self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.UNKNOWN)
            myCcPyState.setPrjState('Product V2', PrjStates.OK)
            self.assertEqual(myCcPyState.getPrjState('Product V2'), PrjStates.OK)
            self.assertEqual(myCcPyState.getPrjState('Product V3'), PrjStates.UNKNOWN)
        except BaseException as e:
            print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb())))
            self.assertTrue(False)
Beispiel #2
0
def execTasks(aCcPyConf):
    """ Execute tasks from the ccpy project configuration file and send email report for each project """
    Logger = logging.getLogger(common.LoggerName)
    Logger.debug("Loading configuration from %s" % aCcPyConf)
    myProjects = ccpyconfparser.parse(aCcPyConf)
    Logger.debug("%u project(s) queued for integration", len(myProjects))

    # Iterate thru projects
    for prjName, prjVal in myProjects:
        myPrjStart = datetime.datetime.today()
        myNumSucceededTasks = 0
        myNumSucceededTasksWithWarning = 0
        myNumFailedTasks = 0
        myFailOnErrorSetting = prjVal['failOnError']
        myFailedBecauseOfTaskError = False
        myTasksStatus = []

        # Iterate thru tasks
        for task in prjVal['tasks']:
            myTaskStart = datetime.datetime.today()

            # execute the task
            myTaskExecStatus = task.execute()
            myTaskStatus = {'name': task.__class__.__name__,
                            'description': myTaskExecStatus['statusDescr'],
                            'startTime': myTaskStart,
                            'endTime': datetime.datetime.today(),
                            }
            if myTaskExecStatus['statusFlag']:
                myNumSucceededTasks += 1
                if "warning" in myTaskExecStatus and myTaskExecStatus["warning"]:
                    myTaskStatus['status'] = "WARNING"
                    myNumSucceededTasksWithWarning += 1
                else:
                    myTaskStatus['status'] = "OK"
                if isinstance(task, maketask.MakeTask) or isinstance(task, exectask.ExecTask):
                    myTaskStatus['allocatedTime'] = task.timeout
            else:
                myTaskStatus['status'] = 'FAILED'
                myNumFailedTasks += 1
                if myFailOnErrorSetting:
                    myFailedBecauseOfTaskError = True
            if 'stdout' in myTaskExecStatus:
                myTaskStatus['stdout'] = myTaskExecStatus['stdout']
            if 'stderr' in myTaskExecStatus:
                myTaskStatus['stderr'] = myTaskExecStatus['stderr']

            myTasksStatus.append(myTaskStatus)
            Logger.debug('  Task finished. Status: %s', myTaskStatus)
            if myFailedBecauseOfTaskError:
                break
        # End: Iterate thru tasks

        myPrjEnd = datetime.datetime.today()

        myCcPyState = CcPyState()
        myOldPrjState = myCcPyState.getPrjState(prjName)

        if myNumFailedTasks > 0:
            # fail
            myCcPyState.setPrjState(prjName, PrjStates.FAILED)
            myPrjStatusStr = str(PrjStates.FAILED)
        elif myNumSucceededTasks > 0:
            # success
            myCcPyState.setPrjState(prjName, PrjStates.OK)
            if myNumSucceededTasksWithWarning > 0:
                myPrjStatusStr = "WARNING %s" % (
                    ' (FIXED)' if myOldPrjState == PrjStates.FAILED else '', )
            else:
                myPrjStatusStr = "%s%s" % (
                    PrjStates.OK, ' (FIXED)' if myOldPrjState == PrjStates.FAILED else '')
        else:
            # nothing is ran, use yesterday weather
            myCcPyState.setPrjState(prjName, myOldPrjState)
            myPrjStatusStr = str(myOldPrjState)

        Logger.debug(
            "Finished with project %s. Status: %s. %u task(s) SUCCEEDED of which %d have WARNINGs, %u task(s) FAILED.  Elapsed time: %s (started %s, ended %s)" %
            (prjName,
             myPrjStatusStr,
             myNumSucceededTasks,
             myNumSucceededTasksWithWarning,
             myNumFailedTasks,
             util.formatTimeDelta(myPrjEnd - myPrjStart),
             myPrjStart,
             myPrjEnd))
        if len(prjVal['emailTo']):
            Logger.debug(
                "Sending email notification as %s to %s using %s:%d" %
                (prjVal['emailFormat'],
                 ", ".join(
                    prjVal['emailTo']),
                    prjVal['emailServerHost'],
                    prjVal['emailServerPort']))
            mySubj = "Integration status for %s: %s" % (prjName, myPrjStatusStr)
            myBody = report.makeEmailBody(prjVal['emailFormat'],
                                          {'prjName': prjName,
                                           'prjStatus': myPrjStatusStr,
                                           'numSucceededTasks': myNumSucceededTasks,
                                           'numFailedTasks': myNumFailedTasks,
                                           'numSucceededTasksWithWarning': myNumSucceededTasksWithWarning,
                                           'startTime': myPrjStart,
                                           'endTime': myPrjEnd,
                                           },
                                          myTasksStatus,
                                          myFailedBecauseOfTaskError)
            myAttachmentText = report.makeAttachmentText(
                prjVal['emailFormat'],
                myTasksStatus,
                myFailedBecauseOfTaskError)
            util.sendEmailNotification(prjVal['emailFrom'],
                                       prjVal['emailTo'],
                                       mySubj,
                                       myBody,
                                       myAttachmentText,
                                       prjVal['emailAttachments'],
                                       prjVal['emailFormat'],
                                       prjVal['emailServerHost'],
                                       prjVal['emailServerPort'],
                                       prjVal['emailServerUsername'],
                                       prjVal['emailServerPassword'])