def setUp(self): self.failure = False python_logger.write('Entering stage: ' + self.test_case_id + ' at ' + time.asctime(time.localtime(time.time())) + '\n') logger.info("########## STARTING METHOD: " + self.test_method_id + " ##########") Test.test_case_id = self.test_case_id
def tearDownClass(cls): cls.uuid = Test.uuid Descriptions.testcase.append( {cls.test_case_id: Descriptions.description}) # if not cls.uuid or cls.uuid != 'NonTC': # try: # Descriptions.testcase.append({Test.test_case_id: Test.description}) # except Exception as e: # logger.warning('description not defined when this is a testcase.') # append the result to csv file cls.end_time = str(datetime.datetime.now().replace(microsecond=0)) output_file = open(SUITE_RES_FILE_WITH_PATH, 'a', newline='') a = csv.writer(output_file, delimiter=',') a.writerow([ cls.test_case_id, Test.test_case_result, cls.start_time, cls.end_time, cls.uuid ]) output_file.close() logger.info("########## Testcase: " + cls.test_case_id + ' ' * 3 + Test.test_case_result + " ##########" + '\n' * 2) logger.removeHandler(cls.handler) python_logger.write('Exting Testcase: ' + cls.test_case_id + ' at ' + time.asctime(time.localtime(time.time())) + ' ... Result: ' + Test.test_case_result + '\n') if Test.test_case_result == 'FAILED': FAILED_TESTCASE.append(cls.test_case_id)
def setUpClass(cls): Test.test_case_result = 'PASSED' cls.start_time = str(datetime.datetime.now().replace(microsecond=0)) cls.test_case_id = cls.__name__ Params.dts_jira_link[cls.test_case_id] = '' Descriptions.description = cls.test_case_id python_logger.write('Entering Testcase: ' + cls.test_case_id + ' at ' + time.asctime(time.localtime(time.time())) + '\n') case_log_dir = LOG_DIR + '/' + cls.test_case_id # case_log = case_log_dir + '/' + cls.test_case_id + '.log' relative_case_log = cls.test_case_id + '/' + cls.test_case_id + '.log' os.path.exists(case_log_dir) or os.makedirs(case_log_dir) Descriptions.teststage.append({cls.test_case_id: {}}) TestcaseLog.log[cls.test_case_id] = relative_case_log cls.handler = LOGGING.FileHandler(case_log_dir + '/' + cls.test_case_id + '.log', mode='w') formatter1 = LOGGING.Formatter( '[%(asctime)s] [%(levelname)s] [%(module)s] - %(message)s') if Params.log_level == 'DEBUG2': formatter1 = LOGGING.Formatter( '[%(asctime)s] [%(levelname)s] [%(module)s] [%(funcName)s] - %(message)s' ) cls.handler.setFormatter(formatter1) cls.handler.setLevel(Params.log_level) logger.addHandler(cls.handler) logger.info("########## STARTING CASE: " + cls.test_case_id + " ##########")
def upload_result_to_sonicauto(self): for i in range(0,5): result = self.sa.save_result(results=self.results) if result == 'Result got saved successfully': break time.sleep(5) logger.info('Try again to upload result to sonicauto...') self.sa = SonicAuto()
def _initiate_log(self): self._initiate_python_log() output_file = open(SUITE_RES_FILE_WITH_PATH, 'w', newline='') output_writer = csv.writer(output_file) output_writer.writerow(['ID', 'RESULT', 'STARTTIME', 'ENDTIME', 'UUID']) output_file.close() command_output_file = open(SUITE_COMMAND_LINE_FILE_WITH_PATH, 'w', newline='') command_output_file.write(self.cmd_line) command_output_file.close() logger.info("Loaded suite")
def subTest(self, *args, **kwargs): python_logger.write('Entering Sub-Testcase: ' + kwargs['name'] + ' at ' + time.asctime(time.localtime(time.time())) + '\n') try: logger.info("########## STARTING SUBTEST CASE: " + kwargs['name'] + " ##########") super().subTest(msg=None, **kwargs) except AttributeError: super().subTest = contextlib.contextmanager(lambda *a, **kw: (yield)) return super().subTest(*args, **kwargs)
def run(self): self.send_start_email() self.run_and_parse_result() self.parse_result_file() self.generate_failed_log() Params.finishtime = datetime.datetime.now().replace(microsecond=0) self.upload_result_to_sonicauto() if self.has_uuid and Params.qbsjobid != 'None': logger.info("Will upload result to ApTest") Params.total_aptest = self.upload_result_to_aptest() self.postrun() self.send_email()
def assert_equal(cls, actual_output, expected_output, msg): logger.info("actual output is:") logger.info(actual_output) logger.info("expected output is:") logger.info(expected_output) try: assert actual_output == expected_output, msg except AssertionError as e: logger.error(traceback.format_exc(), "error") raise e
def assert_not_regular(cls, actual_output, expected_output, msg): logger.info("actual output is:") logger.info(actual_output) logger.info("expected output is:") logger.info(expected_output) try: assert not re.search(r'' + expected_output + '', actual_output, re.I), msg except AssertionError as e: logger.error(traceback.format_exc(), "error") raise e
def populate_console(self): testbed = self.testbed product = self.product product = re.sub(r'pro|nsa|octeon', '', product.lower()) tbdefcsl = '' if self.openstack != '1': tbdefcsl = testbed.lower() + '-' + product tqtest_resource = self.resource tqtest_resources = tqtest_resource.split(',') for element in tqtest_resources: element = re.sub(r':\d+$', '', element) logger.info("RESOURCE ELEMENT: " + element) if re.match(r'^(SHA|CLUSTER)$', element): console = tbdefcsl tbdefcsl = console + '-pri' self.consoles.append(tbdefcsl) tbdefcsl = console + '-sec' elif not self.testbed in self.openstack_setup: try: xml = self.testbed + '.xml' xml_path = 'http://osservices-sj.eng.sonicwall.com/topologies/' + xml content = self.log_path + '/' + xml cmd = ['/bin/rm', '-Rf', content] retval = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()[0] params = ['-P', self.log_path, xml_path] retval = subprocess.Popen(['wget'] + params, stdout=subprocess.PIPE).communicate()[0] doc = minidom.parse(content) rec = doc.getElementsByTagNameNS('*', 'topology')[0] console = rec.getElementsByTagName('node')[0] tbdefcsl = console.getElementsByTagName('topology-resource-name')[0].firstChild.data except: logger.error("Unable to retrieve xml file from osservices") return 1 self.consoles.append(tbdefcsl) return self
def subTestTearDown(self, outcome, test_case_id, uuid): if outcome is not None: logger.error(test_case_id + ": FAILED") self.test_case_result = "FAILED" else: logger.info(test_case_id + ": PASSED") self.test_case_result = "PASSED" self.end_time = str(datetime.datetime.now().replace(microsecond=0)) output_file = open(SUITE_RES_FILE_WITH_PATH, 'a', newline='') a = csv.writer(output_file, delimiter=',') a.writerow([ test_case_id, self.test_case_result, self.start_time, self.end_time, uuid ]) output_file.close() logger.info("########## SubTest Case: " + test_case_id + ' ' * 3 + self.test_case_result + " ##########" + '\n' * 3) python_logger.write('Exting Sub-Testcase: ' + test_case_id + ' at ' + time.asctime(time.localtime(time.time())) + ' ... Result: ' + self.test_case_result + '\n')
def runTest(self): types_list_igmp = [ "None", "Msg 17 : Member Query", "Msg 18 : V1 Member Report", "Msg 22 : V2 Member Report", "Msg 23 : Leave Group", "Msg 34 : V3 Member Report" ] Test.subTestUUID = [ 'uuid1', 'uuid2', 'uuid3', 'uuid4', 'uuid5', 'uuid6' ] Test.subTestName = types_list_igmp for n in types_list_igmp: with self.subTest(name=n): try: logger.info('-----test_traffic----' + str(n)) value1 = 'foo' value2 = 'FOO' Assertion.assert_equal(value1.upper(), value2, "ERR: values equal") except Exception as err: print(err)
def inner_func(): if newcls.__name__ in dts: flag = True newcls.start_time = str( datetime.datetime.now().replace(microsecond=0)) output_file = open(SUITE_RES_FILE_WITH_PATH, 'a', newline='') a = csv.writer(output_file, delimiter=',') a.writerow([ newcls.__name__, 'SKIPPED', newcls.start_time, newcls.start_time, '----' ]) output_file.close() logger.info("########## Testcase: " + newcls.__name__ + ' ' * 3 + 'SKIPPED' + " ##########" + '\n' * 2) else: flag = False test = unittest.skipIf( flag, "########## Testcase: {} SKIPPED due to dts ##########\n\n". format(newcls.__name__))(newcls) return test()
def start_mail(self, start_summary, dut_log, user): msg = MIMEMultipart('alternative') try: suite_name = start_summary['Testsuite Display Name'] test_bed = start_summary['Test Bed'] product = start_summary['Product'] scmlabel = start_summary['Software Version'] rgname = start_summary['rgname'] subject = 'SonicTest: ' + rgname[ rgname.find('['):rgname.find(']') + 1] + suite_name + ': ' + test_bed + '- Test Started on Test Bed ' + test_bed start_summary.pop('rgname') msg['Subject'] = subject msg['From'] = self.from_user msg['To'] = self.to_users msg['Cc'] = self.cc_users full_html = START_TEST_COMPLETED_TEMPLATE.render( suite_name=suite_name, test_bed=test_bed, user=user, start_summary=start_summary, dut_log=dut_log, ) html_display = MIMEText(full_html.encode('iso-8859-1'), 'html', 'iso-8859-1') msg.attach(html_display) send = smtplib.SMTP(self.mail_srv) # send.set_debuglevel(1) receiver = [self.to_users] if self.cc_users is not None: for i in self.cc_users.split(','): receiver.append(i) send.sendmail(self.from_user, receiver, msg.as_string()) except Exception as e: logger.info(e.args) logger.error('Fail to get ' + e.args)
def assert_equal_multiple_value(cls, actual_output, expected_output, msg): logger.info("actual output is : " + str(actual_output)) logger.info("expected output is any from : ") logger.info(expected_output) try: assert any(expected_input == actual_output for expected_input in expected_output), msg except AssertionError as e: logger.error(traceback.format_exc()) raise e
def assert_raises(cls, methodToRun, expected_exception, msg): try: with Assertion.assertRaises( cls, expected_exception=expected_exception) as cm: methodToRun() except Exception as e: logger.error("actual exception is:") logger.error(e) logger.error("expected exception is:") logger.error(expected_exception) logger.error(msg) logger.error(traceback.format_exc(), "error") raise e logger.info("actual exception is:") logger.info(cm.exception) logger.info("expected exception is:") logger.info(expected_exception)
def wrapper(self): for i in range(repeat): logger.info(f'Run for {i} time') try: re = testfunc(self) logger.info('-------------------') logger.info(re) return re except Exception as e: self.failure = True Test.tearDown(self) Test.setUp(self) raise Exception
def inner_cls(): for i in range(repeat): logger.info(f'Run for {i} time') try: re = test_cls() logger.info('-------------------') logger.info(Test.test_case_result) return re except Exception as e: # self.failure = True print('####################') Test.tearDownClass() Test.setUpClass() raise Exception
def tearDown(self): Test.uuid = self.uuid Test.subTestCount = 0 try: Descriptions.description = self.test_case_id + ' - ' + self.description except: Descriptions.description = self.test_case_id try: if re.search(r'^\d+$', self.dts): Params.dts_jira_link[ self. test_case_id] = 'https://sonicdts.eng.sonicwall.com/update_bug.asp?jobid=' + str( self.dts) except: try: if re.search(r'^\w*-\d*$', self.jira): Params.dts_jira_link[ self. test_case_id] = 'https://track.eng.sonicwall.com/browse/' + str( self.jira).upper() except: pass # fetch the test result according to # http://stackoverflow.com/questions/4414234/getting-pythons-unittest-results-in-a-teardown-method if hasattr(self, '_outcome'): result = self.defaultTestResult() self._feedErrorsToResult(result, self._outcome.errors) else: result = getattr(self, '_outcomeForDoCleanups', self._resultForDoCleanups) self.error = self.list2reason(result.errors) self.failure = self.list2reason( result.failures) if not self.failure else True self.skipped = self.list2reason(result.skipped) ok = not self.error and not self.failure test_result = None if not ok: typ, text = ('ERROR', self.error) if self.error else ('FAIL', self.failure) if str(typ) == "FAIL" or str(typ) == "ERROR": logger.info("########## Method: " + self.test_method_id + ' Failed' + " ##########" + '\n') test_result = "FAILED" elif self.skipped: logger.info("########## Method: " + self.test_method_id + ' Skipped' + " ##########" + '\n') else: logger.info("########## Method: " + self.test_method_id + ' Passed' + " ##########" + '\n') test_result = "PASSED" if test_result == 'FAILED': Test.test_case_result = 'FAILED' try: Descriptions.teststage[-1][self.test_case_id][ self.test_method_id] = 'failed' except Exception as e: logger.warning('Warn: {}'.format(e)) Test.test_case_id = self.test_case_id python_logger.write('Exiting stage: ' + self.test_case_id + ' at ' + time.asctime(time.localtime(time.time())) + '... Result: ' + test_result + '\n' * 2)