Example #1
0
    def testGoodConfig1Order(self):
        myProjects = ccpyconfparser.parse("ccpy.conf.good.1")
        self.assertEqual(len(myProjects), 5)

        prjName, prjVal = next(myProjects)
        self.assertEqual(prjName, 'Product2')
        prjName, prjVal = next(myProjects)
        self.assertEqual(prjName, 'Product3')
        prjName, prjVal = next(myProjects)
        self.assertEqual(prjName, 'Product4')
        prjName, prjVal = next(myProjects)
        self.assertEqual(prjName, 'Product5')
        prjName, prjVal = next(myProjects)
        self.assertEqual(prjName, 'Product6')
        self.assertRaises(StopIteration, myProjects.__next__)
Example #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'])
Example #3
0
    def testGoodConfig1Contents(self):
        try:
            myProjects = ccpyconfparser.parse("ccpy.conf.good.1")
            self.assertEqual(len(myProjects), 5)

            # Product2 project
            myProjName = 'Product2'
            myTasks = myProjects[myProjName]['tasks']
            self.assertEqual(len(myTasks), 6)
            self.assertEqual(
                len([task for task in myTasks if isinstance(task, svntask.SvnTask)]), 1)
            self.assertEqual(
                len([task for task in myTasks if isinstance(task, gittask.GitTask)]), 1)
            self.assertEqual(
                len([task for task in myTasks if isinstance(task, maketask.MakeTask)]), 2)
            self.assertEqual(
                len([task for task in myTasks if isinstance(task, exectask.ExecTask)]), 2)
            self.assertEqual(myProjects[myProjName]['emailFrom'], '*****@*****.**')
            self.assertEqual(
                myProjects[myProjName]['emailTo'], [
                    '*****@*****.**', '*****@*****.**'])
            self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.attachment)
            self.assertEqual(myProjects[myProjName]['emailServerHost'], 'localhost')
            self.assertEqual(myProjects[myProjName]['emailServerPort'], 25)
            self.assertEqual(myProjects[myProjName]['emailServerUsername'], None)
            self.assertEqual(myProjects[myProjName]['emailServerPassword'], None)
            self.assertEqual(myProjects[myProjName]['failOnError'], True)

            myTask = myTasks[0]
            self.assertTrue(isinstance(myTask, svntask.SvnTask))
            self.assertEqual(myTask.url, "https://company.com/repos/product2/mk")
            self.assertEqual(myTask.workingDir, "/ProductBuilds/mk")
            self.assertTrue(myTask.preCleanWorkingDir)

            myTask = myTasks[1]
            self.assertTrue(isinstance(myTask, maketask.MakeTask))
            self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/Projects/common")
            self.assertEqual(myTask.args, "clean release")
            self.assertEqual(myTask.timeout, 120)

            myTask = myTasks[2]
            self.assertTrue(isinstance(myTask, maketask.MakeTask))
            self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/Projects/logging")
            self.assertEqual(myTask.args, "")
            self.assertEqual(myTask.timeout, 600)

            myTask = myTasks[3]
            self.assertTrue(isinstance(myTask, exectask.ExecTask))
            self.assertEqual(myTask.executable, "commontests")
            self.assertEqual(myTask.args, "--xmlout")
            self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/TestProjects/commontests")
            self.assertEqual(myTask.timeout, 30)
            self.assertEqual(myTask.warningExitCode, 2)

            myTask = myTasks[4]
            self.assertTrue(isinstance(myTask, exectask.ExecTask))
            self.assertEqual(myTask.executable, "loggingTests")
            self.assertEqual(myTask.args, "")
            self.assertEqual(myTask.workingDir, "")
            self.assertEqual(myTask.timeout, 600)

            myTask = myTasks[5]
            self.assertTrue(isinstance(myTask, gittask.GitTask))
            self.assertEqual(myTask.url, "https://company.com/repos/product2/Common")
            self.assertEqual(myTask.workingDir, "/ProductBuilds/Common")
            self.assertFalse(myTask.preCleanWorkingDir)

            # Product3 project
            myProjName = "Product3"
            myTasks = myProjects[myProjName]['tasks']
            self.assertEqual(len(myTasks), 1)
            self.assertEqual(
                len([task for task in myTasks if isinstance(task, svntask.SvnTask)]), 1)
            self.assertEqual(myProjects[myProjName]['emailFrom'], '*****@*****.**')
            self.assertEqual(myProjects[myProjName]['emailTo'], ['*****@*****.**'])
            self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.attachment)
            self.assertEqual(myProjects[myProjName]['emailAttachments'], [])
            self.assertEqual(myProjects[myProjName]['failOnError'], False)

            myTask = myTasks[0]
            self.assertTrue(isinstance(myTask, svntask.SvnTask))
            self.assertEqual(myTask.url, "https://company.com/repos/product3/server")
            self.assertEqual(myTask.workingDir, "/ProductBuilds/server")
            self.assertFalse(myTask.preCleanWorkingDir)

            # Product4 project
            myProjName = "Product4"
            myTasks = myProjects[myProjName]['tasks']
            self.assertEqual(len(myTasks), 1)
            self.assertEqual(
                len([task for task in myTasks if isinstance(task, maketask.MakeTask)]), 1)
            self.assertEqual(myProjects[myProjName]['failOnError'], True)
            self.assertEqual(myProjects[myProjName]['emailFrom'], '')
            self.assertEqual(myProjects[myProjName]['emailTo'], [])

            myTask = myTasks[0]
            self.assertTrue(isinstance(myTask, maketask.MakeTask))
            self.assertEqual(myTask.workingDir, "/ProductBuilds/SysInfra/Projects/common")
            self.assertEqual(myTask.args, "")
            self.assertEqual(myTask.timeout, 600)

            # Product5 project
            myProjName = "Product5"
            self.assertEqual(
                myProjects[myProjName]['emailFrom'],
                '*****@*****.**')
            self.assertEqual(
                myProjects[myProjName]['emailTo'], [
                    '*****@*****.**', '*****@*****.**'])
            self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.plain)
            self.assertEqual(myProjects[myProjName]['emailServerHost'], 'localhost')
            self.assertEqual(myProjects[myProjName]['emailServerPort'], 25)
            self.assertEqual(myProjects[myProjName]['emailServerUsername'], None)
            self.assertEqual(myProjects[myProjName]['emailServerPassword'], None)
            self.assertEqual(myProjects[myProjName]['emailAttachments'], [])

            # Product6 project
            myProjName = "Product6"
            self.assertEqual(
                myProjects[myProjName]['emailFrom'],
                '*****@*****.**')
            self.assertEqual(myProjects[myProjName]['emailTo'], ['*****@*****.**'])
            self.assertEqual(myProjects[myProjName]['emailFormat'], util.EmailFormat.html)
            self.assertEqual(myProjects[myProjName]['emailServerHost'], 'smtp.mymail.com')
            self.assertEqual(myProjects[myProjName]['emailServerPort'], 2626)
            self.assertEqual(myProjects[myProjName]['emailServerUsername'], 'jos')
            self.assertEqual(myProjects[myProjName]['emailServerPassword'], 'topsecret')
            self.assertEqual(myProjects[myProjName]['emailAttachments'], ['/var/log/messages', '/var/log/messages.1'])

        except BaseException as e:
            print(("Error. %s. %s. %s" % (type(e), str(e), util.formatTb())))
            self.assertTrue(False)