给你一段测试脚本代码,请讲解每一部分的作用:#!/usr/bin/env python
# -*- coding: UTF-8 -*-
###############################################################################
# Copyright (C), 2018, TP-Link Technologies Co., Ltd.
#
# File Name: FG999998__cdent_class_test.py
# Author : zhaohongmei
# History:
# 1. 2024-08-10, zhaohongmei, create
###############################################################################
from pysat.sat_test import TestCase, CaseInfo
from pysat.sat_result import ResultInfo
from pysat import sat_conf
from pysat import rpc3
import logging
from PyNetConfig import NetConfig as net
from PyPing import my_ping as ping
import PyScapy as capture
import time
logger = logging.getLogger(__name__)
_gcfg = sat_conf.TestbedConfig()
def get_config(name):
'''get the testbed config info, return the value'''
return _gcfg.get(name.lower())
class Test(TestCase):
def __init__(self):
TestCase.__init__(self)
self.logger = logging.getLogger(self.__class__.__name__)
self.case_info = CaseInfo()
self.case_info.name = 'cdent_class_test'
self.case_info.id = 'FG999998'
self.case_info.version = '202306271536'
self.case_info.author = 'zhaohongmei@tp-link.com.hk'
self.case_info.runtime = '8min'
self.case_info.testbeds = self.get_testbed_list()
self.case_info.description = ''' '''
def add_task(self):
self.register_case(self.FG999998__cdent_class_test, self.case_info)
def FG999998__cdent_class_test(self):
try:
result = ResultInfo()
self.logger.info('前置操作:从数据库中读取预期的参数信息')
nic_name = get_config('pc_nic_name')
nic_ip = get_config('pc_nic_ip')
nic_mask = get_config('pc_nic_mask')
nic_gateway = get_config('pc_nic_gateway')
nic_dns1 = get_config('pc_nic_dns1')
nic_dns2 = get_config('pc_nic_dns2')
self.logger.info(f'nic_ip {nic_ip}')
# 实例化网卡
pc_nic = net(nic_name)
self.logger.step('步骤1: 读取当前pc网卡的IP/MASK/gateway/dns信息')
ip_list = pc_nic.get_ip_list()
ip = ip_list[0]['ip']
mask = ip_list[0]['mask']
flag = True
if ip != nic_ip:
flag = False
result.add_result(passfail=ResultInfo.FAIL, actual_result=ip,
expected_result=nic_ip,
test_comment='读取当前pc的网卡IP地址', item_id='步骤1.1')
if mask != nic_mask:
flag = False
result.add_result(passfail=ResultInfo.FAIL, actual_result=mask,
expected_result=nic_mask,
test_comment='读取当前pc的网卡mask', item_id='步骤1.2')
gateway = pc_nic.get_gateway_list()[0]
if gateway != nic_gateway:
flag = False
result.add_result(passfail=ResultInfo.FAIL, actual_result=gateway,
expected_result=nic_gateway,
test_comment='读取当前pc的网卡gateway', item_id='步骤1.3')
dns = pc_nic.get_dns_list()
dns1 = dns[0]
dns2 = dns[1]
if dns1 != nic_dns1:
flag = False
result.add_result(passfail=ResultInfo.FAIL, actual_result=dns1,
expected_result=nic_dns1,
test_comment='读取当前pc的网卡dns1', item_id='步骤1.4')
if dns2 != nic_dns2:
flag = False
result.add_result(passfail=ResultInfo.FAIL, actual_result=dns2,
expected_result=nic_dns2,
test_comment='读取当前pc的网卡dns2', item_id='步骤1.5')
if flag:
result.add_result(passfail=ResultInfo.PASS, actual_result=f'ip:{ip}, mask:{mask}, gateway:{gateway}, dns1:{dns1}, dns2:{dns2}',
expected_result=f'ip:{nic_ip}, mask:{nic_mask}, gateway:{nic_gateway}, dns1:{nic_dns1}, dns2:{nic_dns2}',
test_comment='读取当前pc网卡的IP/MASK/gateway/dns信息', item_id='步骤1')
self.logger.step('步骤2: 检查网卡能否ping通内网主页(portal.tp-link.com)')
capture.tsniff(iface=nic_name)
ping_result = ping.check_ping('portal.tp-link.com')
if ping_result is True:
result.add_result(passfail=ResultInfo.PASS, actual_result=ping_result,
expected_result='True',
test_comment='网卡ping通内网主页(portal.tp-link.com)', item_id='步骤2.1')
else:
result.add_result(passfail=ResultInfo.FAIL, actual_result=ping_result,
expected_result='True',
test_comment='网卡ping通内网主页(portal.tp-link.com)', item_id='步骤2.1')
time.sleep(10)
capture.stop_sniff()
pkts = capture.join_tsniff(timeout=10)
lambda_icmp = "lambda x:x.haslayer('ICMP')"
pkt_icmp = pkts.filter(capture.eval_lfilter(lambda_icmp))
dest_ip = pkt_icmp[0]['IP'].dst
result.add_result(passfail=ResultInfo.REF, actual_result=dest_ip,
expected_result='',
test_comment='网卡所ping的目的IP', item_id='步骤2.2')
return result
except Exception as e:
result = ResultInfo()
result.add_result(passfail=ResultInfo.FAIL, test_comment='Test fail: %s' % e)
self.break_test('Test fail: %s' % e)
def clean_test(self):
try:
self.logger.info('[clean_test] start to clean test...')
except Exception as e:
self.logger.error('[clean_test] clean test failed: %s' % e)
raise e