pytest测试用例
测试步骤:
DUT:
删除portchannel1
sonic-cli –c 'configure terminal' –c ' no interface PortChannel 1'
配置手动portchannel1,切换到L3,配置portchannel1的ip
sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c ' portchannel-create mode manual' –c ' switchmode L3'
sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c 'ip address 10.0.0.56/31'
配置成员口
sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'join-portchannel 1' –c 'no shutdown'
VM:
interface ethernet 1
no channel-group 1 mode active
channel-group 1 mode on
预期结果:
删除portchannel1后show interface portchannel

配置手动portchannel1并加入成员口之后show interface portchannel

配置ip之后show ip interface

恢复配置后portchannel1显示为lacp模式

配置还原:
DUT
sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'shutdown' –c 'no join-portchannel 1'
sonic-cli –c 'configure terminal' –c 'no interface PortChannel 1'
sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c ' portchannel-create mode lacp' –c 'switchmode L3'
sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c 'ip address 10.0.0.56/31'
sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'join-portchannel 1' –c 'no shutdown'
VM
configure
interface ethernet 1
no channel-group 1 mode on
channel-group 1 mode active
import logging
import pytest
import random
import re
import time
import os
import pprint
import json
import ptf.testutils as testutils
import ptf.mask as mask
import ptf.packet as scapy
from datetime import datetime
from tests.common.helpers.assertions import pytest_assert
from tests.common import config_reload
from tests.conftest import duthost
from collections import defaultdict
from tests.common import reboot, port_toggle
from tests.common.helpers.assertions import pytest_require
from tests.common import testbed
from natsort import natsorted
import tests.common.utilities as utilities
pytestmark = [
pytest.mark.topology('t0')
]
logger = logging.getLogger(__name__)
@pytest.fixture(scope="module")
def cfg_facts(duthosts, rand_one_dut_hostname):
duthost = duthosts[rand_one_dut_hostname]
return get_cfg_facts(duthost)
def get_cfg_facts(duthost):
tmp_facts = json.loads(duthost.shell("sonic-cfggen -d --print-data")['stdout']) # return config db contents(running-config)
port_name_list_sorted = natsorted(tmp_facts['PORT'].keys())
port_index_map = {}
for idx, val in enumerate(port_name_list_sorted):
port_index_map[val] = idx
tmp_facts['config_port_indices'] = port_index_map
return tmp_facts
@pytest.fixture(scope='module', autouse=True)
def setup_dut(cfg_facts):
# dut port map ptf port
utilities.log_info("begin set params")
# set params
global portChannel1
global dutPortToVm
global newDutPortToVm
global ptfRecvPortMapVm
global ptfSendPortMapDut
global dutPortChannel1Ip
global vmPortChannel1Ip
global vmIps
global Vm1Ip
global Po1Ip
dutPortMapVm = 'Ethernet8'
dutPortChannel1Ip = '10.0.0.56/31'
vmPortChannel1Ip = '10.0.0.57/31'
vmIps = ['10.1.1.1', '10.1.2.1']
Po1Ip = '10.0.0.56/31'
portChannelList = natsorted(cfg_facts['PORTCHANNEL'].keys())
Vm1Ip = natsorted(cfg_facts['BGP_NEIGHBOR'].keys())[0].split("|")[1]
portChannel1 = portChannelList[0]
# Ethernet29
dutPortToVm = cfg_facts['PORTCHANNEL'][portChannel1]['members'][0]
pattern = "[0-9]"
# Ethernet 29
newDutPortToVm = re.sub(pattern, lambda x: " " + x.group(0), dutPortToVm, 1)
ptfRecvPortMapVm = cfg_facts['config_port_indices'][dutPortToVm]
ptfSendPortMapDut = cfg_facts['config_port_indices'][dutPortMapVm]
yield
utilities.log_info("End of the test")
@pytest.fixture(scope='function', autouse=True)
def check_config(duthost, nbrhosts):
utilities.log_info("Start checking the configuration, get the old configuration")
beforeConfigList = []
afterConfigList = []
torNeighbors = natsorted([neighbor for neighbor in nbrhosts.keys()])
tor = torNeighbors[0]
beforeConfigList = utilities.getConfigList(beforeConfigList, nbrhosts[tor]['host'], duthost)
yield
utilities.log_info("Start checking the configuration, get the new configuration")
afterConfigList = utilities.getConfigList(afterConfigList, nbrhosts[tor]['host'], duthost)
result = utilities.getConfigComp(beforeConfigList, afterConfigList)
utilities.log_info("End of configuration check, result is {}".format(str(result)))
if not result:
config_reload(duthost)
pytest_assert(result, "Configuration not cleared")
def test_portchannel_manual(duthosts, rand_one_dut_hostname, duthost, localhost, ptfadapter, nbrhosts, eos, ptfhost, tbinfo):
duthost = duthosts[rand_one_dut_hostname]
try:
utilities.log_info("Test portchannel manual start")
utilities.log_info("DUT side configuration start")
utilities.log_step("===== remove portchannel 1 on dut =====")
duthost.shell(
"sonic-cli –c 'configure terminal' –c ' no interface PortChannel 1'")
time.sleep(2)
utilities.log_step("===== create portchannel 1=====")
duthost.shell(
"sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c ' portchannel-create mode manual' –c ' switchmode L3'")
time.sleep(2)
duthost.shell(
"sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c 'ip address {}'".format(Po1Ip))
time.sleep(2)
utilities.log_step("===== add Ethernet 29 to portchannel 1 =====")
duthost.shell(
"sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'switchmode L3'")
time.sleep(2)
duthost.shell(
"sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'join-portchannel 1' –c 'no shutdown'")
time.sleep(2)
utilities.log_info("DUT side configuration finish")
utilities.log_info("VM side configuration start")
torNeighbors = natsorted([neighbor for neighbor in nbrhosts.keys()])
VM1 = torNeighbors[0]
utilities.log_info("VM1 configure")
cmds = ['no channel-group 1 mode active', 'channel-group 1 mode on']
nbrhosts[VM1]['host'].eos_config(lines=cmds, parents='interface ethernet 1')
utilities.log_info("VM side configuration finish")
time.sleep(5)
utilities.log_info("Start checking function")
utilities.log_info("L3 Ping check")
res_ping = duthost.shell('sudo ping {} -c 4 | grep received'.format(Vm1Ip))["stdout"]
utilities.log_info("res_ping : {}".format(res_ping))
pytest_assert('4 received' in res_ping, "ping {} failed".format(Vm1Ip))
utilities.log_info("Start checking switch routing")
pkt = testutils.simple_tcp_packet(
eth_dst=duthost.facts["router_mac"],
eth_src=ptfadapter.dataplane.get_mac(0, 0),
ip_src='2.1.1.1',
ip_dst='20.1.1.2',
#vlan_vid=vid,
ip_ttl=64,
tcp_sport=1234,
tcp_dport=4321)
exp_pkt = pkt.copy()
exp_pkt = mask.Mask(exp_pkt)
exp_pkt.set_do_not_care_scapy(packet.Ether, 'dst')
exp_pkt.set_do_not_care_scapy(packet.Ether, 'src')
exp_pkt.set_do_not_care_scapy(packet.IP, 'ttl')
exp_pkt.set_do_not_care_scapy(packet.IP, 'chksum')
testutils.send(ptfadapter, ptfSendPortMapDut, pkt)
testutils.verify_packet_any_port(ptfadapter, exp_pkt, ports=[ptfRecvPortMapVm])
finally:
utilities.log_info("Restore the configuration")
utilities.log_info("VM side restore start")
torNeighbors = natsorted([neighbor for neighbor in nbrhosts.keys()])
VM1 = torNeighbors[0]
cmds = ['no channel-group 1 mode on', 'channel-group 1 mode active']
nbrhosts[VM1]['host'].eos_config(lines=cmds, parents='interface ethernet 1')
utilities.log_info("VM side restore finish")
utilities.log_info("DUT side restore start")
duthost.shell("sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'shutdown' –c 'no join-portchannel 1'")
duthost.shell("sonic-cli –c 'configure terminal' –c 'no interface PortChannel 1'")
duthost.shell("sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c ' portchannel-create mode lacp' –c 'switchmode L3'")
duthost.shell("sonic-cli –c 'configure terminal' –c 'interface PortChannel 1' –c 'ip address {}'".format(Po1Ip))
duthost.shell("sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'switchmode L3'")
duthost.shell("sonic-cli –c 'configure terminal' –c 'interface Ethernet 29' –c 'join-portchannel 1' –c 'no shutdown'")
utilities.log_info("DUT side restore finish")

浙公网安备 33010602011771号