Beispiel #1
0
class SoapUIRunner:
    def __init__(self, configs):
        self.timeout    = 30 * 60        # length of timeout (in seconds)
        self.kill_delay = 2  
        self.returncode = 1  
        self.result     = 1              # 1 = there were problems, 0 = everything was fine
        self.summary    = ''
        self.configs    = configs
        self.configure()
        self.logger     = SetupLogger().logger("SoapUIRunner")
    
    def configure(self):
        self.runner_location = self.configs.get('soapui_runner_dir')
        self.execution_root   = self.configs.get('execution_root')
    
    def command(self, service_id, test_id, project_file):
        self.service_id   = service_id
        self.test_id      = test_id
        self.project_file = project_file
        self.test_dir     = os.path.join(self.execution_root, str(service_id), str(test_id))
        self.work_dir     = os.path.join(self.test_dir, 'package')
        self.log          = os.path.join(self.test_dir, os.path.join("logs", ("%s.tmp.log" % datetime.datetime.now()))) 
        
        options = ' -a -r -f %s '%self.work_dir
        cmd  = os.path.join(self.runner_location, 'testrunner.sh' )
        cmd +=' %s ' %options
        cmd +=' %s ' %os.path.join(self.work_dir, self.project_file)
        self.cmd = cmd
        self.logger.debug( cmd)
        return cmd
    
    def parse_log(self, log=''):
        if log=='' : log = self.log
        try:
            lines = open(log, 'r').readlines()
            lines = [line for line in lines if (line.startswith('SoapUI') 
                                                or line.startswith('Time')
                                                or line.startswith('Total') )]
            result_line = [line for line in lines if line.startswith('Total TestCases:')]
            for line in lines:
                self.summary += line
            
            if len(result_line) == 1:
                parts  = result_line[0].split()
                total  = parts[2]
                failed = parts[3][1:]
                if int(failed) == 0 :
                    self.result  = 0
                else:
                    self.result  = 1
                
            print "Overall result %s "% self.result
            print "Summary Report"
            print self.summary
            
        except Exception, e:
            print "There were problems parsing the log"
            print str(e)
class TestScriptRunner:
    def __init__(self, configs):
        self.configs = configs
        self.timeout = 30 * 60  # length of timeout (in seconds)
        self.kill_delay = 2
        self.returncode = 1
        self.result = 1  # 1 = there were problems, 0 = everything was fine
        self.summary = ''
        self.configure()
        self.logger = SetupLogger().logger("TestScriptRunner")

    def configure(self):
        self.runner_location = self.configs.get('script_runner_dir')
        self.execution_root = self.configs.get('execution_root')

    def command(self, service_id, test_id, script_name, conf_file):
        self.service_id = service_id
        self.test_id = test_id
        self.script_name = script_name
        self.test_dir = os.path.join(self.execution_root, str(service_id),
                                     str(test_id))
        self.work_dir = os.path.join(self.test_dir, 'package')
        self.log = os.path.join(
            self.test_dir,
            os.path.join("logs", ("%s.tmp.log" % datetime.datetime.now())))

        self.cmd = os.path.join(self.work_dir, self.script_name)
        self.logger.debug(self.cmd + ' %s' % conf_file)
        return self.cmd + ' %s' % conf_file

    def post_result(self, url, user, psswd):
        self.logger.debug("Posting result to  %s" % url)
        conf = {
            'result': int(self.result),
            'message': self.summary,
            'action': self.script_name,
            'test_id': int(self.test_id)
        }
        tr = TestResult(conf)
        return tr.post(url, user, psswd)

    def signal_handler(self, signum, child):
        os.kill(child.pid, signal.SIGKILL)

    def status_message(self, exit_code):
        if exit_code == 0:
            return "Success: Script ran successfully"
        elif (exit_code in range(255)):
            return "Failure: Script failed with exit code : %s" % str(
                exit_code)
        else:
            return "Warning: Script exited with code : %s " % str(exit_code)
class ConfigReader:
    def __init__(self, config_file):
        self.config = config_file
        self.logger = SetupLogger().logger("ConfigReader")
        
    def read(self):
        self.logger.debug("Reading the config file")
        params = {}
        lines = open(self.config,'r').readlines()
        lines = [line.strip() for line in lines]
        for line in lines:
            #skip empty lines and comment lines
            if not line.startswith('#') and line.strip():
                k,v = line.split('=') 
                params[k.strip()] = v.strip()
        #print "Done"
        return params
class TestScriptRunner:
    def __init__(self, configs):
        self.configs = configs
        self.timeout    = 30 * 60        # length of timeout (in seconds)
        self.kill_delay = 2  
        self.returncode = 1  
        self.result     = 1              # 1 = there were problems, 0 = everything was fine
        self.summary    = ''
        self.configure()
        self.logger     = SetupLogger().logger("TestScriptRunner")
    
    def configure(self):
        self.runner_location = self.configs.get('script_runner_dir')
        self.execution_root   = self.configs.get('execution_root')
    
    def command(self, service_id, test_id, script_name, conf_file):
        self.service_id   = service_id
        self.test_id      = test_id
        self.script_name  = script_name
        self.test_dir     = os.path.join(self.execution_root, str(service_id), str(test_id))
        self.work_dir     = os.path.join(self.test_dir, 'package')
        self.log          = os.path.join(self.test_dir, os.path.join("logs", ("%s.tmp.log" % datetime.datetime.now()))) 
        
        self.cmd = os.path.join(self.work_dir, self.script_name)
        self.logger.debug( self.cmd +' %s'%conf_file )
        return self.cmd +' %s'%conf_file
    
    def post_result(self, url, user, psswd):
        self.logger.debug( "Posting result to  %s"%url)
        conf = {'result' : int(self.result),
                'message' : self.summary,
                'action'  : self.script_name,
                'test_id' : int(self.test_id) }
        tr = TestResult(conf)
        return tr.post(url, user, psswd)
    
    def signal_handler(self, signum, child):
        os.kill(child.pid, signal.SIGKILL)
        
    def status_message(self, exit_code):
        if exit_code == 0:
            return "Success: Script ran successfully"
        elif (exit_code in range(255)):
            return "Failure: Script failed with exit code : %s"%str(exit_code)
        else:
            return "Warning: Script exited with code : %s "%str(exit_code)
class ConfigReader:
    def __init__(self, config_file):
        self.config = config_file
        self.logger = SetupLogger().logger("ConfigReader")

    def read(self):
        self.logger.debug("Reading the config file")
        params = {}
        lines = open(self.config, 'r').readlines()
        lines = [line.strip() for line in lines]
        for line in lines:
            #skip empty lines and comment lines
            if not line.startswith('#') and line.strip():
                k, v = line.split('=')
                params[k.strip()] = v.strip()
        #print "Done"
        return params
Beispiel #6
0
class DirectoryMaker:
    def __init__(self, fn):
        self.configs = ConfigReader(fn).read()
        self.logger  = SetupLogger().logger("DirectoryMaker")
        
    def get_script_references(self, db):
        
        #stmt  = "SELECT service_tests.service_id, service_tests.test_id, test_scripts.filename, content_blobs.data "
        stmt  = "SELECT service_tests.service_id, service_tests.id, test_scripts.filename, content_blobs.data "
        stmt += "FROM service_tests,test_scripts,content_blobs  "
        stmt += "WHERE service_tests.test_id = test_scripts.id AND service_tests.test_type ='TestScript' "
        stmt += "AND test_scripts.content_blob_id = content_blobs.id AND service_tests.activated_at IS NOT NULL; " 
        self.logger.debug(stmt)
        results    = db.execute(stmt)
        references = db.fetchall()
        return references 
    
    #make dirs of pattern <base_dir>/<service id>/<test_id>/package/ 
    def setup_run_dirs(self, base_dir, results =[]):
        if not os.path.exists(base_dir): return "ERROR! test base dir %s does not exist"%base_dir
        for result in results:
            service_id = result[0]
            test_id    = result[1]
            run_dir    = os.path.join(base_dir, str(service_id), str(test_id), 'package')
            
            if not os.path.exists(run_dir):
                self.create_run_dir(run_dir, result[2], result[3])
                self.logger.debug("Created test script run directory %s"%run_dir)
            else:
                self.logger.debug("Test script run directory %s already exists "%run_dir)
            
    # create the directory to run the jobs
    def create_run_dir(self, dir, fname, data):
        cwd = os.getcwd()
        base, package    = os.path.split(dir)
        base, test_id    = os.path.split(base)
        base, service_id = os.path.split(base)
        try:
            if not os.path.exists(os.path.join(base, service_id)): 
                os.mkdir(os.path.join(base, service_id))
            if not os.path.exists(os.path.join(base, service_id, test_id)):
                os.mkdir(os.path.join(base, service_id, test_id))
            os.mkdir(dir)
            os.mkdir(os.path.join(os.path.split(dir)[0], "logs"))
        except Exception, e:
            e.stacktrace()
        if not os.path.exists(os.path.join(dir, fname)):
            if data != None:
                try:
                    fh = open(os.path.join(dir, fname), 'wb')   
                    fh.write(data)
                    fh.close()
                    
                    if self.is_zip(fname):
                        os.chdir(dir)
                        os.system("unzip %s"%fname )
                        os.chdir(cwd)
                    cmd = "chmod -R u=rxw  %s"%dir
                    self.logger.debug(cmd)
                    os.system(cmd)
                except Exception, e:
                    self.logger.debug(str(e))