feature:新增使用logging将testcase的结果输出到文件和console上
This commit is contained in:
@@ -81,8 +81,7 @@ services:
|
||||
arp -i eth0 -s 192.0.2.130 02:42:c0:a8:fd:82
|
||||
arp -i eth0 -s 192.0.2.131 02:42:C0:A8:FD:83
|
||||
arp -i eth0 -s 192.0.2.135 02:42:C0:A8:FD:85
|
||||
mkdir -p /opt/dign_client/log/dign_client
|
||||
mkdir -p /opt/dign_client/log/conn_traffic_status
|
||||
mkdir -p /opt/dign_client/log
|
||||
cp -rf /opt/dign_client/etc/certs_import/certs/sets/current/gen/crt/ca-root.crt /usr/local/share/ca-certificates
|
||||
update-ca-certificates
|
||||
cat /opt/dign_client/etc/hosts >> /etc/hosts
|
||||
|
||||
@@ -17,6 +17,7 @@ import random
|
||||
import dns.exception
|
||||
import dns.resolver
|
||||
import sys
|
||||
import logging
|
||||
|
||||
|
||||
suite_test_config_dict = {'test_securityPolicy_bypass': {'enabled':1,'conn_timeout':1,'max_recv_speed_large':6553600},
|
||||
@@ -58,8 +59,7 @@ suite_test_config_dict = {'test_securityPolicy_bypass': {'enabled':1,'conn_timeo
|
||||
'test_ssl_firewall_allow': {'enabled':1,'conn_timeout':1,'max_recv_speed_large':6553600},
|
||||
'test_ssl_firewall_deny_drop': {'enabled':1,'conn_timeout':4,'max_recv_speed_large':6553600},
|
||||
'test_ssl_firewall_deny_rst': {'enabled':1,'conn_timeout':1,'max_recv_speed_large':6553600},
|
||||
'start_time_random_delay_range': {'enabled':1,'left_edge':1,'right_edge':30},
|
||||
'telegraf': {'host':'192.51.100.1','port':8100,'tags_key':'app_name','tags_value' :'tsg-diagnose'}}
|
||||
'start_time_random_delay_range': {'enabled':1,'left_edge':1,'right_edge':30}}
|
||||
|
||||
|
||||
ssl_bypass_info_re = "Ssl connection bypass success"
|
||||
@@ -150,6 +150,22 @@ DnsAAAARequestFirewallAllow = "Dns rdtype AAAA request data is sucess"
|
||||
DnsCNAMERequestFirewallAllow = "Dns rdtype CNAME request data is sucess"
|
||||
|
||||
|
||||
def get_logger(logPath,enableConsole=True):
|
||||
logger = logging.getLogger()
|
||||
fileHandler = logging.FileHandler(logPath)
|
||||
fmt = '%(asctime)s, %(levelname)s, %(message)s'
|
||||
formatter = logging.Formatter(fmt=fmt, datefmt='%a %b %d %H:%M:%S %Y')
|
||||
fileHandler.setFormatter(formatter)
|
||||
fileHandler.setLevel(logging.DEBUG)
|
||||
logger.addHandler(fileHandler)
|
||||
if enableConsole == True:
|
||||
consoleHandler = logging.StreamHandler()
|
||||
consoleHandler.setFormatter(formatter)
|
||||
consoleHandler.setLevel(logging.DEBUG)
|
||||
logger.addHandler(consoleHandler)
|
||||
logger.setLevel(logging.DEBUG)
|
||||
return logger
|
||||
|
||||
class DNSCheckRequestBuild:
|
||||
def dns_action_deny_subaction_drop(self):
|
||||
dns_resolver=dns.resolver.Resolver()
|
||||
@@ -613,28 +629,12 @@ class SSLFileDownloadBuild:
|
||||
self.conn.setopt(self.conn.URL,url)
|
||||
self.conn.setopt(self.conn.TIMEOUT, int(suite_test_config_dict[test_suite_name]['conn_timeout']))
|
||||
|
||||
def _write_in_nezha(self, sizeStr, connInfoDict):
|
||||
nzdict = {}
|
||||
nzname = 'conn_taffic_status_size_' + sizeStr
|
||||
dictKeyTime = "conn_traffic_" + sizeStr + "_size_total_time"
|
||||
dcitKeyStatus = "conn_traffic_" + sizeStr + "_size_status"
|
||||
nzdict[dictKeyTime] = connInfoDict['total_time']
|
||||
nzdict[dcitKeyStatus] = connInfoDict['status']
|
||||
self.client.metric(nzname, nzdict)
|
||||
|
||||
def _write_in_logfile(self, sizeStr, connInfoDict):
|
||||
logNewestPath = "/opt/dign_client/log/conn_traffic_status/conn_traffic_status_" + sizeStr
|
||||
logPath = logNewestPath + time.strftime("%Y-%m-%d_%H-%M-%S", time.localtime())
|
||||
connInfoLogPath = "/opt/dign_client/log/download_conn_info.log" + '.' + time.strftime("%Y-%m-%d", time.localtime())
|
||||
connInfoLogger = get_logger(connInfoLogPath,enableConsole=False)
|
||||
connInfoStr = json.dumps(connInfoDict)
|
||||
with open(logNewestPath,"w+") as f:
|
||||
f.write(connInfoStr)
|
||||
f.close()
|
||||
connInfoLogger.debug("Fize Size:" + sizeStr + ";connection info:" + connInfoStr)
|
||||
|
||||
with open(logPath,"w+") as f:
|
||||
fn = open(logNewestPath,'r')
|
||||
f.write(fn.read())
|
||||
fn.close()
|
||||
f.close()
|
||||
|
||||
def conn_traffic(self,test_suite_name,url,conn_taffic_re,sizeStr, size):
|
||||
self._set_conn_opt(test_suite_name, url)
|
||||
@@ -657,7 +657,6 @@ class SSLFileDownloadBuild:
|
||||
raise Exception("Error: Intercept fail: no Tango cert,cert info:%s" % issuer[1])
|
||||
|
||||
if int(conninfo["size_download"]) == size:
|
||||
self._write_in_nezha(sizeStr,conninfo)
|
||||
self._write_in_logfile(sizeStr,conninfo)
|
||||
raise Exception(conn_taffic_re)
|
||||
else:
|
||||
@@ -965,7 +964,7 @@ class SslUnitTest(unittest.TestCase):
|
||||
with self.assertRaisesRegex(Exception, ssl_firewall_deny_rst_re):
|
||||
requestHandler.action_deny_subaction_rst('test_ssl_firewall_deny_rst')
|
||||
|
||||
class TsgDiagnoseRun:
|
||||
class TsgDiagnose:
|
||||
def __init__(self):
|
||||
self.interval = 1
|
||||
self.loop = False
|
||||
@@ -973,8 +972,9 @@ class TsgDiagnoseRun:
|
||||
self.config = None
|
||||
self.client = None
|
||||
self.config_dict = {}
|
||||
self.dign_duration = 0
|
||||
|
||||
def _get_suite_option(self):
|
||||
def _get_dign_option(self):
|
||||
parser = argparse.ArgumentParser(description="Tsg Tools - tsg diagnose", epilog = "Example:help")
|
||||
parser.add_argument('-i','--interval', type = int, default = 30,help='Wait interval seconds between each tsg disagnose. The default is to wait for 30 seconds between each tsg diagnose.')
|
||||
parser.add_argument('-c','--count', type = int, default = 1, help='Specifies the count of tsg diagnoses ,range:1-65535')
|
||||
@@ -990,7 +990,7 @@ class TsgDiagnoseRun:
|
||||
parser.print_help()
|
||||
sys.exit(1)
|
||||
|
||||
def _get_suite_config(self):
|
||||
def _get_dign_config(self):
|
||||
global suite_test_config_dict
|
||||
config = ConfigParser()
|
||||
config.read(self.config, encoding='UTF-8')
|
||||
@@ -1000,13 +1000,19 @@ class TsgDiagnoseRun:
|
||||
else:
|
||||
suite_test_config_dict[section] = dict(config.items(section))
|
||||
self.config_dict = suite_test_config_dict
|
||||
for config_value in self.config_dict.values():
|
||||
if 'conn_timeout' not in config_value:
|
||||
continue
|
||||
if int(config_value['enabled']) != 1:
|
||||
continue
|
||||
self.dign_duration += int(config_value['conn_timeout'])
|
||||
|
||||
|
||||
def _add_suite(self,test_suite_name):
|
||||
if int(self.config_dict[test_suite_name]['enabled']) == 1:
|
||||
self.suite.addTest(SslUnitTest(test_suite_name))
|
||||
|
||||
def _init_suite(self):
|
||||
def _add_dign_case(self):
|
||||
self.suite = unittest.TestSuite()
|
||||
self.suite._cleanup = False
|
||||
self._add_suite('test_dnsRequest_deny_drop')
|
||||
@@ -1049,37 +1055,37 @@ class TsgDiagnoseRun:
|
||||
self._add_suite('test_ssl_firewall_deny_drop')
|
||||
self._add_suite('test_ssl_firewall_deny_rst')
|
||||
|
||||
def _stdout_suite_result(self):
|
||||
print(format(("Test start time: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())),'#^120s'))
|
||||
runner = unittest.TextTestRunner(verbosity=2)
|
||||
def _dign_running(self):
|
||||
runningLogPath = "/opt/dign_client/log/tsg-diagnose.log" + '.' + time.strftime("%Y-%m-%d", time.localtime())
|
||||
runningLogger = get_logger(runningLogPath, True)
|
||||
runningLogger.debug("Diagnose Start,the It will take up to %d seconds" %(self.dign_duration))
|
||||
result_stream = io.StringIO()
|
||||
runner = unittest.TextTestRunner(stream=result_stream,verbosity=2)
|
||||
runner.run(self.suite)
|
||||
print(format(("Test end time: " + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())),'=^120s'))
|
||||
runningLogger.debug("Diagnose end, Testing results:" + "\n" + result_stream.getvalue())
|
||||
|
||||
def _output_suite_result(self):
|
||||
self._stdout_suite_result()
|
||||
|
||||
def execute_suite_tsg_diagnose(self):
|
||||
self._get_suite_option()
|
||||
self._get_suite_config()
|
||||
self._init_suite()
|
||||
def dign_exec(self):
|
||||
self._get_dign_option()
|
||||
self._get_dign_config()
|
||||
self._add_dign_case()
|
||||
dign_counter = 0
|
||||
try:
|
||||
if int(self.config_dict['start_time_random_delay_range']['enabled']) == 1:
|
||||
time.sleep(random.randint(int(self.config_dict['start_time_random_delay_range']['left_edge']),int(self.config_dict['start_time_random_delay_range']['right_edge'])))
|
||||
counter = 0
|
||||
print("Tsg diagnose run sum: %d" % self.count)
|
||||
time.sleep(random.randint( \
|
||||
int(self.config_dict['start_time_random_delay_range']['left_edge']),\
|
||||
int(self.config_dict['start_time_random_delay_range']['right_edge'])))
|
||||
while True:
|
||||
print("\nRUN %d" %(counter + 1))
|
||||
self._output_suite_result()
|
||||
counter = counter + 1
|
||||
print("\nRUN %d" %(dign_counter + 1))
|
||||
self._dign_running()
|
||||
dign_counter = dign_counter + 1
|
||||
if not self.loop:
|
||||
if counter >= self.count:
|
||||
if dign_counter >= self.count:
|
||||
break
|
||||
time.sleep(self.interval)
|
||||
except Exception as ex:
|
||||
print("Process get an exception, will exit, Exception info:", ex)
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
tsg_diagnose_run = TsgDiagnoseRun()
|
||||
tsg_diagnose_run.execute_suite_tsg_diagnose()
|
||||
tsg_diagnose_running = TsgDiagnose()
|
||||
tsg_diagnose_running.dign_exec()
|
||||
|
||||
@@ -198,14 +198,8 @@ conn_timeout = 1
|
||||
max_recv_speed_large = 6553600
|
||||
|
||||
[start_time_random_delay_range]
|
||||
enabled = 1
|
||||
enabled = 0
|
||||
#Left_edge is the left edge of the randomly generated time in seconds
|
||||
left_edge = 0
|
||||
#Left_edge is the right edge of the randomly generated time in seconds
|
||||
right_edge = 30
|
||||
|
||||
[telegraf]
|
||||
host = 192.51.100.1
|
||||
port = 8100
|
||||
tags_key = app_name
|
||||
tags_value = tsg-diagnose
|
||||
|
||||
Reference in New Issue
Block a user