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 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 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 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 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 copy_file_to_log_server(self, file_name): uploaddir = self.uploaddir dst = uploaddir src = file_name params = ['-avz'] params.extend((src, dst)) python_logger.write('Entering utils::log::copy_file_to_log_server...'+ '\n' ) python_logger.write(''*4 + 'Rsyncing logs rsync: '+ ' '.join(['rsync'] + params)+ '\n') try: if not os.path.exists(dst): os.makedirs(dst) rsync_result_string = subprocess.Popen(['rsync'] + params, stdout=subprocess.PIPE).communicate()[0] #mtab_params = ['/etc/mtab'] python_logger.flush() except: logger.error("Unable to rsync files or /logs nfs mount cannot be determined from /etc/mtab") return 1 return uploaddir
def update_results(self, scmlabel, platform, aptest_suite_name, session_number, **tc): uuid = tc['uuid'] result = '' if 'result' in tc: result = tc['result'] note = '' if 'note' in tc: note = tc['note'] custom_key = '' if 'custom_key' in tc: custom_key = tc['custom_key'] custom_val = '' if 'custom_val' in tc: custom_val = tc['custom_val'] note_default = '' if scmlabel != '1.1.1.1' and platform != 'TestProd': note_default = 'BuildVersion:' + scmlabel + '__Product' + platform if note: note_default = note_default + '__Note:' + note transform_result = { 'PASSED': 'pass', 'FAILED': 'fail', 'SKIPPED': 'untested' } if result in transform_result: result = transform_result[result] execdata = 'EXECDATA' + '_' + custom_key rpc_query_string = '?rpctoken=' + self.token + '&username='******'&suite=' + aptest_suite_name + '&command=result&sess=' + session_number + '&uuid=' + uuid + '&result=' + result + '&' + execdata + '=' + custom_val + '¬e=' + note_default rpc_query_string = rpc_query_string.replace('[', '\[') rpc_query_string = rpc_query_string.replace(']', '\]') rpc_url = 'https://' + self.host + '/run/rpc.mpl' + rpc_query_string python_logger.write("DEBUG: RPC URL is: " + rpc_url + '\n') params = ['--connect-timeout', '20', '--silent', '-k'] params.append(rpc_url) try: rpc_result_string = subprocess.Popen( ['curl'] + params, stdout=subprocess.PIPE).communicate()[0] return_string = rpc_result_string.decode('ASCII') rpc_result_list = return_string.split('\n') rpc_return_value = rpc_result_list[0] rpc_return_message = rpc_result_list[1] python_logger.write("DEBUG: RPC result value is: " + rpc_return_value + '\n') python_logger.write("DEBUG: RPC result message is: " + rpc_return_message + '\n') return rpc_return_value except: python_logger.error("ERROR: Unable to run rpc query") return
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)
def _initiate_python_log(self): python_logger.write('Dumping ENV:'+ '\n') for env in os.environ: python_logger.write(' '*8 +env + '= ' + os.environ[env] + '\n') python_logger.write('Start time: ' + TIMESTAMP + '\n') python_logger.write('Command line: ' + self.cmd_line+ '\n') python_logger.write('Test log dir: ' + LOG_DIR+ '\n') if re.search(r'Linux', platform.system(), re.I): python_logger.write('Route table:' + str(subprocess.Popen(['route'] + ['-n'], stdout=subprocess.PIPE).communicate()[0],encoding="utf8")+ '\n') python_logger.write('DNS setting:' + str(subprocess.Popen(['cat'] + ['/etc/resolv.conf'], stdout=subprocess.PIPE).communicate()[0],encoding="utf8")+ '\n') elif re.search(r'cygwin|mswin32', platform.system(), re.I): python_logger.write('Route table:' + str(subprocess.Popen(['/cygdrive/c/WINDOWS/system32/route'] + ['print'], stdout=subprocess.PIPE).communicate()[0],encoding="utf8")+ '\n') python_logger.write('DNS setting:' + str(subprocess.Popen(['/cygdrive/c/WINDOWS/system32/ipconfig'] + ['/all'], stdout=subprocess.PIPE).communicate()[0],encoding="utf8")+ '\n') if Params.sonicos_ver: sys.path.append(os.environ["PYTHON_COMMON_HOME"]) from util.openstack import Openstack try: if Params.testbed and Params.openstack and int(Params.openstack) == 1: ostack = Openstack(Params.testbed) nodes = ostack.get_nodes() python_logger.write('OpenStack Topology Definition: \n') for node in nodes: python_logger.write(' '*4 + '{\n') for node_key in node: python_logger.write(' '*8 + node_key + '=> ' + str(node[node_key]) + '\n') python_logger.write(' '*4 + '}\n\n') except Exception as e: logger.error(e.args)
def postrun(self): python_logger.write('\nExiting Testsuite...\n\n') python_logger.flush()
def get_session_numbers_from_group_id(self, aptest_suite_name, session_group_id): rpc_query_string = '?suite=' + aptest_suite_name + '&sessiongroupid=' + session_group_id rpc_url = 'https://' + self.host + '/sw-getSessionsWithVar.pl' + rpc_query_string python_logger.write("DEBUG: RPC URL is: " + rpc_url + '\n') params = ['--connect-timeout', '20', '--silent', '-k'] params.append(rpc_url) try: rpc_result_string = subprocess.Popen( ['curl'] + params, stdout=subprocess.PIPE).communicate()[0] return_string = rpc_result_string.decode('ASCII') rpc_result_list = return_string.split('\n') rpc_return_value = rpc_result_list.pop(0) python_logger.write("DEBUG: RPC result value is: " + rpc_return_value + '\n') for session_setname in rpc_result_list: if session_setname == '': continue session_number, set_name = session_setname.split(',') rpc_query_string = '?suite=' + aptest_suite_name + '&set=' + set_name + '&session=' + session_number rpc_url = 'https://' + self.host + '/sw-autotclist.pl' + rpc_query_string python_logger.write("DEBUG: RPC URL is: " + rpc_url + '\n') params = ['--connect-timeout', '20', '--silent', '-k'] params.append(rpc_url) rpc_result_string = subprocess.Popen( ['curl'] + params, stdout=subprocess.PIPE).communicate()[0] return_string = rpc_result_string.decode('ASCII') rpc_result_list_uuid = return_string.split('\n') rpc_return_code = rpc_result_list_uuid.pop(0) python_logger.write("DEBUG: TC list result code is: " + rpc_return_code + '\n') if rpc_return_code == 1: python_logger.write( "Failed return status from getUUIDsFromSessionNumber" + '\n') elif len(rpc_result_list_uuid) == 1: python_logger.write( "No corresponding testcases for the given session number" + '\n') else: for testcase_data in rpc_result_list_uuid: uuid = testcase_data.split(',') self.uuid_map[uuid[0]] = session_number except: python_logger.error("Unable to run rpc query") return 1
def upload_aptest(self, results, platform, scmlabel): num_of_case_uploaded = 0 try: # get session_group_id sa = SonicAuto() product_id = str(sa.get_product_id_from_platform(platform)) if product_id == 0: raise UploadAptestException("product id is not found") session_group_id = sa.get_session_group_name(scmlabel, product_id) if session_group_id == '': raise UploadAptestException("session group id is not found") # get ApTest suite name from product_name application_platform_aptest_mapping = { 'GMS': 'GMSVP', 'LICENSEMANAGER': 'License_Manager', 'MYSONICWALL': 'Latte', 'SANDBOX': 'Latte', 'SNOWSHOE': 'Latte', 'CFC': 'Desktop_Clients' } productid_aptest_mapping = { '2': 'SMA_1000_AO_Phase-6', '3': 'EmailSecurity', '5': 'SMA_100_AO_Phase6', '6': 'firmware_sonicos', '7': 'wxa', '8': 'Web_Application_Firewall', '9': 'SMA_1000_AO_Phase-6', '10': 'CloudWAF', '11': 'Capture_Client', '12': 'Latte' } aptest_suite_name = '' if product_id == '1': aptest_suite_name = application_platform_aptest_mapping[ platform] else: aptest_suite_name = productid_aptest_mapping[product_id] retry = self.retry while (retry): if self.get_session_numbers_from_group_id( aptest_suite_name, session_group_id): retry -= 1 time.sleep(20) else: break for tc in results: if not 'uuid' in tc: python_logger.write( "INFO: Testcase uuid not defined. Results not uploaded to ApTest" + '\n') continue uuid = tc['uuid'] if uuid in self.uuid_map: session_number = self.uuid_map[uuid] if self.update_results(scmlabel, platform, aptest_suite_name, session_number, **tc) == '0': num_of_case_uploaded += 1 except UploadAptestException as e: python_logger.write(e.args[0] + '\n') except Exception as e: python_logger.write(e.args[0] + '\n') return num_of_case_uploaded