def test_recent_jobs(self, now): now.return_value = NOW db = DB('sqlite://') db.create_schema() job1 = Job(change_num="change_num1", project_name="project") job2 = Job(change_num="change_num2", project_name="project") with db.get_session() as session: session.add(job1) job1.created=PAST job1.db = db job1.state=constants.RUNNING job1.updated=PAST session.add(job2) recent_jobs = Job.getRecent(db) self.assertEqual(len(recent_jobs), 1) recent_jobs = Job.getRecent(db, 200000) self.assertEqual(len(recent_jobs), 2)
def func_list(options, queue): table = PrettyTable([ "ID", "Project", "Change", "State", "IP", "Result", "Age (hours)", "Duration" ]) table.align = 'l' now = time.time() all_jobs = Job.getRecent(queue.db, int(options.recent)) state_dict = {} result_dict = {} if options.states and len(options.states) > 0: states = options.states.split(',') else: # Default should be everything except obsolete jobs states = constants.STATES.values() states.remove(constants.STATES[constants.OBSOLETE]) for job in all_jobs: updated = time.mktime(job.updated.timetuple()) age_hours = (now - updated) / 3600 state_count = state_dict.get(constants.STATES[job.state], 0) + 1 state_dict[constants.STATES[job.state]] = state_count result_count = result_dict.get(job.result, 0) + 1 result_dict[job.result] = result_count if states and constants.STATES[job.state] not in states: continue if job.node_id: node_ip = job.node_ip else: node_ip = '(%s)' % job.node_ip age = '%.02f' % (age_hours) duration = '-' if job.test_started and job.test_stopped: started = time.mktime(job.test_started.timetuple()) stopped = time.mktime(job.test_stopped.timetuple()) if started < stopped: duration = "%.02f" % ((stopped - started) / 3600) table.add_row([ job.id, job.project_name, job.change_ref, constants.STATES[job.state], node_ip, job.result, age, duration ]) output_str = str(state_dict) + "\n" output_str = output_str + str(result_dict) + "\n" output_str = output_str + str(table) return output_str
def func_list(options, queue): table = PrettyTable(["ID", "Project", "Change", "State", "IP", "Result", "Age (hours)", "Duration"]) table.align = 'l' now = time.time() all_jobs = Job.getRecent(queue.db, int(options.recent)) state_dict = {} result_dict = {} if options.states and len(options.states) > 0: states = options.states.split(',') else: # Default should be everything except obsolete jobs states = constants.STATES.values() states.remove(constants.STATES[constants.OBSOLETE]) for job in all_jobs: updated = time.mktime(job.updated.timetuple()) age_hours = (now - updated) / 3600 state_count = state_dict.get(constants.STATES[job.state], 0) + 1 state_dict[constants.STATES[job.state]] = state_count result_count = result_dict.get(job.result, 0)+1 result_dict[job.result] = result_count if states and constants.STATES[job.state] not in states: continue if job.node_id: node_ip = job.node_ip else: node_ip = '(%s)'%job.node_ip age = '%.02f' % (age_hours) duration = '-' if job.test_started and job.test_stopped: started = time.mktime(job.test_started.timetuple()) stopped = time.mktime(job.test_stopped.timetuple()) if started < stopped: duration = "%.02f"%((stopped - started)/3600) table.add_row([job.id, job.project_name, job.change_ref, constants.STATES[job.state], node_ip, job.result, age, duration]) output_str = str(state_dict)+"\n" output_str = output_str + str(result_dict)+"\n" output_str = output_str + str(table) return output_str
def func_failures(options, queue): output_str = '' table = PrettyTable([ "ID", "Project", "Change", "State", "Result", "Age", "Duration", "URL" ]) table.align = 'l' now = time.time() all_jobs = Job.getRecent(queue.db, int(options.recent)) all_failed_tests = {} for job in all_jobs: if not job.result or (job.result != 'Failed' and job.result.find('Aborted') != 0): continue updated = time.mktime(job.updated.timetuple()) age_hours = (now - updated) / 3600 age = '%.02f' % (age_hours) duration = '-' if job.test_started and job.test_stopped: started = time.mktime(job.test_started.timetuple()) stopped = time.mktime(job.test_stopped.timetuple()) duration = "%.02f" % ((stopped - started) / 3600) job_failed = job.failed if job.failed is not None else '' failed_tests = [ m.group(0) for m in re.finditer('tempest.[^ ()]+', job_failed) ] if options.withfail is not None: if len(options.withfail) == 0: if len(failed_tests) != 0: continue else: if options.withfail not in job.failed: continue table.add_row([ job.id, job.project_name, job.change_num, constants.STATES[job.state], job.result, age, duration, job.logs_url ]) if len(failed_tests) == 0: failed_tests = ['No tempest failures detected'] elif int(options.max_fails) > 0 and len(failed_tests) > int( options.max_fails): failed_tests = ['More than %s failures' % options.max_fails] for failed_test in failed_tests: # Treat JSON and XML as the same since we're only interested in driver failures failed_test = failed_test.replace('JSON', '') failed_test = failed_test.replace('XML', '') cur_count = all_failed_tests.get(failed_test, 0) all_failed_tests[failed_test] = cur_count + 1 if options.min_dup: msg = 'Fewer than %s duplicates' % options.min_dup for failed_test in list(all_failed_tests.keys()): if all_failed_tests[failed_test] < int(options.min_dup): cur_count = all_failed_tests.get(msg, 0) all_failed_tests[msg] = cur_count + 1 del all_failed_tests[failed_test] output_str += str(table) + '\n' output_str += '\n' output_str += 'Failures\n' output_str += '-------------------\n' single_count = 0 sorted_tests = sorted(all_failed_tests, key=all_failed_tests.get, reverse=True) for failed_test in sorted_tests: output_str += "%3d %s\n" % (all_failed_tests[failed_test], failed_test) return output_str
def func_failures(options, queue): output_str = '' table = PrettyTable(["ID", "Project", "Change", "State", "Result", "Age", "Duration", "URL"]) table.align = 'l' now = time.time() all_jobs = Job.getRecent(queue.db, int(options.recent)) all_failed_tests = {} for job in all_jobs: if not job.result or (job.result != 'Failed' and job.result.find('Aborted') != 0): continue updated = time.mktime(job.updated.timetuple()) age_hours = (now - updated) / 3600 age = '%.02f' % (age_hours) duration = '-' if job.test_started and job.test_stopped: started = time.mktime(job.test_started.timetuple()) stopped = time.mktime(job.test_stopped.timetuple()) duration = "%.02f"%((stopped - started)/3600) job_failed = job.failed if job.failed is not None else '' failed_tests = [m.group(0) for m in re.finditer('tempest.[^ ()]+', job_failed)] if options.withfail is not None: if len(options.withfail) == 0: if len(failed_tests) != 0: continue else: if options.withfail not in job.failed: continue table.add_row([job.id, job.project_name, job.change_num, constants.STATES[job.state], job.result, age, duration, job.logs_url]) if len(failed_tests) == 0: failed_tests = ['No tempest failures detected'] elif int(options.max_fails) > 0 and len(failed_tests) > int(options.max_fails): failed_tests = ['More than %s failures'%options.max_fails] for failed_test in failed_tests: # Treat JSON and XML as the same since we're only interested in driver failures failed_test = failed_test.replace('JSON', '') failed_test = failed_test.replace('XML', '') cur_count = all_failed_tests.get(failed_test, 0) all_failed_tests[failed_test] = cur_count + 1 if options.min_dup: msg='Fewer than %s duplicates'%options.min_dup for failed_test in list(all_failed_tests.keys()): if all_failed_tests[failed_test] < int(options.min_dup): cur_count = all_failed_tests.get(msg, 0) all_failed_tests[msg] = cur_count + 1 del all_failed_tests[failed_test] output_str += str(table) + '\n' output_str += '\n' output_str += 'Failures\n' output_str += '-------------------\n' single_count =0 sorted_tests = sorted(all_failed_tests, key=all_failed_tests.get, reverse=True) for failed_test in sorted_tests: output_str += "%3d %s\n"%(all_failed_tests[failed_test], failed_test) return output_str