From c2b32603c79762bf1121d6c5859755d496c84c78 Mon Sep 17 00:00:00 2001 From: Shuotian Cheng Date: Thu, 25 Jul 2019 09:46:20 -0700 Subject: [PATCH] [test]: Update test_vlan to use dvs functions (#986) Signed-off-by: Shu0t1an Cheng --- tests/test_vlan.py | 185 +++++++++++++++++++++------------------------ 1 file changed, 86 insertions(+), 99 deletions(-) diff --git a/tests/test_vlan.py b/tests/test_vlan.py index 0bc12af5490..aa0df6c1223 100644 --- a/tests/test_vlan.py +++ b/tests/test_vlan.py @@ -8,45 +8,18 @@ class TestVlan(object): - def setup_db(self, dvs): - self.pdb = swsscommon.DBConnector(0, dvs.redis_sock, 0) - self.adb = swsscommon.DBConnector(1, dvs.redis_sock, 0) - self.cdb = swsscommon.DBConnector(4, dvs.redis_sock, 0) - - def create_vlan(self, vlan): - tbl = swsscommon.Table(self.cdb, "VLAN") - fvs = swsscommon.FieldValuePairs([("vlanid", vlan)]) - tbl.set("Vlan" + vlan, fvs) - time.sleep(1) - - def remove_vlan(self, vlan): - tbl = swsscommon.Table(self.cdb, "VLAN") - tbl._del("Vlan" + vlan) - time.sleep(1) - - def create_vlan_member(self, vlan, interface): - tbl = swsscommon.Table(self.cdb, "VLAN_MEMBER") - fvs = swsscommon.FieldValuePairs([("tagging_mode", "untagged")]) - tbl.set("Vlan" + vlan + "|" + interface, fvs) - time.sleep(1) - - def remove_vlan_member(self, vlan, interface): - tbl = swsscommon.Table(self.cdb, "VLAN_MEMBER") - tbl._del("Vlan" + vlan + "|" + interface) - time.sleep(1) - def check_syslog(self, dvs, marker, process, err_log, vlan_str, expected_cnt): (exitcode, num) = dvs.runcmd(['sh', '-c', "awk \'/%s/,ENDFILE {print;}\' /var/log/syslog | grep %s | grep \"%s\" | grep -i %s | wc -l" % (marker, process, err_log, vlan_str)]) assert num.strip() == str(expected_cnt) def test_VlanAddRemove(self, dvs, testlog): - self.setup_db(dvs) + dvs.setup_db() # create vlan - self.create_vlan("2") + dvs.create_vlan("2") # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 1 vlan_oid = vlan_entries[0] @@ -58,11 +31,11 @@ def test_VlanAddRemove(self, dvs, testlog): assert fv[1] == "2" # create vlan member - self.create_vlan_member("2", "Ethernet0") + dvs.create_vlan_member("2", "Ethernet0") # check asic database bridge_port_map = {} - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT") bridge_port_entries = tbl.getKeys() for key in bridge_port_entries: (status, fvs) = tbl.get(key) @@ -71,7 +44,7 @@ def test_VlanAddRemove(self, dvs, testlog): if fv[0] == "SAI_BRIDGE_PORT_ATTR_PORT_ID": bridge_port_map[key] = fv[1] - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 1 @@ -89,7 +62,7 @@ def test_VlanAddRemove(self, dvs, testlog): assert False # check port pvid - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_PORT") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_PORT") (status, fvs) = tbl.get(dvs.asicdb.portnamemap["Ethernet0"]) assert status == True assert "SAI_PORT_ATTR_PORT_VLAN_ID" in [fv[0] for fv in fvs] @@ -98,7 +71,7 @@ def test_VlanAddRemove(self, dvs, testlog): assert fv[1] == "2" # check host interface vlan tag - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_HOSTIF") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_HOSTIF") (status, fvs) = tbl.get(dvs.asicdb.hostifnamemap["Ethernet0"]) assert status == True assert "SAI_HOSTIF_ATTR_VLAN_TAG" in [fv[0] for fv in fvs] @@ -107,87 +80,87 @@ def test_VlanAddRemove(self, dvs, testlog): assert fv[1] == "SAI_HOSTIF_VLAN_TAG_KEEP" # remove vlan member - self.remove_vlan_member("2", "Ethernet0") + dvs.remove_vlan_member("2", "Ethernet0") - # remvoe vlan - self.remove_vlan("2") + # remove vlan + dvs.remove_vlan("2") def test_MultipleVlan(self, dvs, testlog): return - self.setup_db(dvs) + dvs.setup_db() # create vlan and vlan members - self.create_vlan("18") - self.create_vlan_member("18", "Ethernet0") - self.create_vlan_member("18", "Ethernet4") - self.create_vlan_member("18", "Ethernet8") + dvs.create_vlan("18") + dvs.create_vlan_member("18", "Ethernet0") + dvs.create_vlan_member("18", "Ethernet4") + dvs.create_vlan_member("18", "Ethernet8") # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 1 - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 3 # remove vlan members - self.remove_vlan_member("18", "Ethernet0") - self.remove_vlan_member("18", "Ethernet4") - self.remove_vlan_member("18", "Ethernet8") + dvs.remove_vlan_member("18", "Ethernet0") + dvs.remove_vlan_member("18", "Ethernet4") + dvs.remove_vlan_member("18", "Ethernet8") - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 0 # create vlan and vlan members - self.create_vlan("188") - self.create_vlan_member("188", "Ethernet20") - self.create_vlan_member("188", "Ethernet24") - self.create_vlan_member("188", "Ethernet28") + dvs.create_vlan("188") + dvs.create_vlan_member("188", "Ethernet20") + dvs.create_vlan_member("188", "Ethernet24") + dvs.create_vlan_member("188", "Ethernet28") # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 2 - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 3 # create vlan members - self.create_vlan_member("18", "Ethernet40") - self.create_vlan_member("18", "Ethernet44") - self.create_vlan_member("18", "Ethernet48") + dvs.create_vlan_member("18", "Ethernet40") + dvs.create_vlan_member("18", "Ethernet44") + dvs.create_vlan_member("18", "Ethernet48") - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 6 # remove vlan members - self.remove_vlan_member("18", "Ethernet40") - self.remove_vlan_member("18", "Ethernet44") - self.remove_vlan_member("18", "Ethernet48") + dvs.remove_vlan_member("18", "Ethernet40") + dvs.remove_vlan_member("18", "Ethernet44") + dvs.remove_vlan_member("18", "Ethernet48") - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 3 # remove vlan members - self.remove_vlan_member("188", "Ethernet20") - self.remove_vlan_member("188", "Ethernet24") - self.remove_vlan_member("188", "Ethernet28") + dvs.remove_vlan_member("188", "Ethernet20") + dvs.remove_vlan_member("188", "Ethernet24") + dvs.remove_vlan_member("188", "Ethernet28") - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 0 # remove vlans - self.remove_vlan("18") - self.remove_vlan("188") + dvs.remove_vlan("18") + dvs.remove_vlan("188") # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 0 @@ -298,7 +271,7 @@ def test_VlanIncrementalConfig(self, dvs, testlog): # remove vlan member dvs.remove_vlan_member("2", "Ethernet0") - # remvoe vlan + # remove vlan dvs.remove_vlan("2") @@ -310,19 +283,19 @@ def test_VlanIncrementalConfig(self, dvs, testlog): (["Vlan", "5"], 1), ]) def test_AddVlanWithIncorrectKeyPrefix(self, dvs, testlog, test_input, expected): - self.setup_db(dvs) + dvs.setup_db() marker = dvs.add_log_marker() vlan_prefix = test_input[0] vlan = test_input[1] # create vlan - tbl = swsscommon.Table(self.cdb, "VLAN") + tbl = swsscommon.Table(dvs.cdb, "VLAN") fvs = swsscommon.FieldValuePairs([("vlanid", vlan)]) tbl.set(vlan_prefix + vlan, fvs) time.sleep(1) # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == expected @@ -331,7 +304,7 @@ def test_AddVlanWithIncorrectKeyPrefix(self, dvs, testlog, test_input, expected) self.check_syslog(dvs, marker, "vlanmgrd", "Invalid key format. No 'Vlan' prefix:", vlan_prefix+vlan, 1) else: #remove vlan - self.remove_vlan(vlan) + dvs.remove_vlan(vlan) @pytest.mark.skipif(StrictVersion(platform.linux_distribution()[1]) <= StrictVersion('8.9'), reason="Debian 8.9 or before has no support") @pytest.mark.parametrize("test_input, expected", [ @@ -341,19 +314,19 @@ def test_AddVlanWithIncorrectKeyPrefix(self, dvs, testlog, test_input, expected) (["Vlan", "5"], 1), ]) def test_AddVlanWithIncorrectValueType(self, dvs, testlog, test_input, expected): - self.setup_db(dvs) + dvs.setup_db() marker = dvs.add_log_marker() vlan_prefix = test_input[0] vlan = test_input[1] # create vlan - tbl = swsscommon.Table(self.cdb, "VLAN") + tbl = swsscommon.Table(dvs.cdb, "VLAN") fvs = swsscommon.FieldValuePairs([("vlanid", vlan)]) tbl.set(vlan_prefix + vlan, fvs) time.sleep(1) # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == expected @@ -362,55 +335,55 @@ def test_AddVlanWithIncorrectValueType(self, dvs, testlog, test_input, expected) self.check_syslog(dvs, marker, "vlanmgrd", "Invalid key format. Not a number after \'Vlan\' prefix:", vlan_prefix+vlan, 1) else: #remove vlan - self.remove_vlan(vlan) + dvs.remove_vlan(vlan) def test_AddVlanMemberWithNonExistVlan(self, dvs, testlog): - self.setup_db(dvs) + dvs.setup_db() marker = dvs.add_log_marker() vlan = "2" # create vlan member - self.create_vlan_member(vlan, "Ethernet0") + dvs.create_vlan_member(vlan, "Ethernet0") # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 0 - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN_MEMBER") vlan_member_entries = tbl.getKeys() assert len(vlan_member_entries) == 0 # remove vlan member from cfgdb - self.remove_vlan_member(vlan, "Ethernet0") + dvs.remove_vlan_member(vlan, "Ethernet0") def test_RemoveNonexistentVlan(self, dvs, testlog): - self.setup_db(dvs) + dvs.setup_db() marker = dvs.add_log_marker() vlan = "2" # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 0 # remove nonexistent vlan - self.remove_vlan(vlan) + dvs.remove_vlan(vlan) # create vlan - self.create_vlan(vlan) + dvs.create_vlan(vlan) # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 1 # remove vlan - self.remove_vlan(vlan) + dvs.remove_vlan(vlan) @pytest.mark.skip(reason="AddMaxVlan take too long to execute") def test_AddMaxVlan(self, dvs, testlog): - self.setup_db(dvs) + dvs.setup_db() min_vid = 2 max_vid = 4094 @@ -418,22 +391,22 @@ def test_AddMaxVlan(self, dvs, testlog): # create max vlan vlan = min_vid while vlan <= max_vid: - self.create_vlan(str(vlan)) + dvs.create_vlan(str(vlan)) vlan += 1 # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == (4094-1) # remove all vlan vlan = min_vid while vlan <= max_vid: - self.remove_vlan(str(vlan)) + dvs.remove_vlan(str(vlan)) vlan += 1 # check asic database - tbl = swsscommon.Table(self.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] assert len(vlan_entries) == 0 @@ -465,14 +438,28 @@ def test_RemoveVlanWithRouterInterface(self, dvs, testlog): # one loopback router interface one vlan based router interface assert len(intf_entries) == 2 - # remvoe vlan + # remove vlan dvs.remove_vlan("100") - # check error log - self.check_syslog(dvs, marker, "orchagent", "Failed to remove ref count", "Vlan100", 1) + # check asic database still contains the vlan + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] + assert len(vlan_entries) == 1 + vlan_oid = vlan_entries[0] + + (status, fvs) = tbl.get(vlan_oid) + assert status == True + for fv in fvs: + if fv[0] == "SAI_VLAN_ATTR_VLAN_ID": + assert fv[1] == "100" # remove IP from interface dvs.remove_ip_address("Vlan100", "20.0.0.8/29") - # remvoe vlan + # remove vlan dvs.remove_vlan("100") + + # check asic database does not contain the vlan anymore + tbl = swsscommon.Table(dvs.adb, "ASIC_STATE:SAI_OBJECT_TYPE_VLAN") + vlan_entries = [k for k in tbl.getKeys() if k != dvs.asicdb.default_vlan_id] + assert len(vlan_entries) == 0