|
| 1 | +from swsscommon import swsscommon |
| 2 | +import redis |
| 3 | +import time |
| 4 | +import os |
| 5 | +import pytest |
| 6 | + |
| 7 | +@pytest.yield_fixture |
| 8 | +def port_config(request, dvs): |
| 9 | + file_name = "/usr/share/sonic/hwsku/port_config.ini" |
| 10 | + dvs.runcmd("cp %s %s.bak" % (file_name, file_name)) |
| 11 | + yield file_name |
| 12 | + dvs.runcmd("mv %s.bak %s" % (file_name, file_name)) |
| 13 | + |
| 14 | +class TestPortConfig(object): |
| 15 | + |
| 16 | + def getPortName(self, dvs, port_vid): |
| 17 | + cnt_db = swsscommon.DBConnector(swsscommon.COUNTERS_DB, dvs.redis_sock, 0) |
| 18 | + port_map = swsscommon.Table(cnt_db, 'COUNTERS_PORT_NAME_MAP') |
| 19 | + |
| 20 | + for k in port_map.get('')[1]: |
| 21 | + if k[1] == port_vid: |
| 22 | + return k[0] |
| 23 | + |
| 24 | + return '' |
| 25 | + |
| 26 | + |
| 27 | + def getPortOid(self, dvs, port_name): |
| 28 | + cnt_r = redis.Redis(unix_socket_path=dvs.redis_sock, db=swsscommon.COUNTERS_DB) |
| 29 | + return cnt_r.hget("COUNTERS_PORT_NAME_MAP", port_name); |
| 30 | + |
| 31 | + |
| 32 | + def getVIDfromRID(self, dvs, port_rid): |
| 33 | + asic_r = redis.Redis(unix_socket_path=dvs.redis_sock, db=swsscommon.ASIC_DB) |
| 34 | + return asic_r.hget("RIDTOVID", port_rid); |
| 35 | + |
| 36 | + |
| 37 | + def test_port_hw_lane(self, dvs): |
| 38 | + |
| 39 | + app_db = swsscommon.DBConnector(swsscommon.APPL_DB, dvs.redis_sock, 0) |
| 40 | + port_tbl = swsscommon.Table(app_db, "PORT_TABLE") |
| 41 | + |
| 42 | + asic_r = redis.Redis(unix_socket_path=dvs.redis_sock, db=swsscommon.ASIC_DB) |
| 43 | + num_lanes = asic_r.hlen("LANES") |
| 44 | + for i in range(1, num_lanes+1): |
| 45 | + port_rid = asic_r.hget("LANES", i) |
| 46 | + port_vid = self.getVIDfromRID(dvs, port_rid) |
| 47 | + port_name = self.getPortName(dvs, port_vid) |
| 48 | + |
| 49 | + (status, fvs) = port_tbl.get(port_name) |
| 50 | + assert status == True |
| 51 | + for fv in fvs: |
| 52 | + if fv[0] == "lanes": |
| 53 | + assert str(i) in list(fv[1].split(",")) |
| 54 | + |
| 55 | + |
| 56 | + def test_port_breakout(self, dvs, port_config): |
| 57 | + |
| 58 | + # check port_config.ini |
| 59 | + (exitcode, output) = dvs.runcmd(['sh', '-c', "cat %s | tail -n 1" % (port_config)]) |
| 60 | + try: |
| 61 | + name_str, lanes_str, alias_str, index_str, speed_str = list(output.split()) |
| 62 | + except: |
| 63 | + print "parse port_config.ini fail" |
| 64 | + |
| 65 | + LANES_L = list(lanes_str.split(",")) |
| 66 | + assert len(LANES_L) == 4 |
| 67 | + assert int(speed_str) == 40000 |
| 68 | + |
| 69 | + # modify port_config.ini |
| 70 | + eth = int(name_str.replace("Ethernet", "")) |
| 71 | + index = int(index_str) |
| 72 | + speed_str = "tenGigE0" |
| 73 | + speed = 10000 |
| 74 | + dvs.runcmd("sed -i '$d' %s" % (port_config)) == 0 |
| 75 | + for i in range(0,4): |
| 76 | + dvs.runcmd("sed -i '$a Ethernet%-7d %-17d %s/%-8d %-11d %d' %s" % |
| 77 | + (eth+i, int(LANES_L[i]), speed_str, eth+i, index+i, speed, port_config)) == 0 |
| 78 | + |
| 79 | + # delete port config |
| 80 | + conf_db = swsscommon.DBConnector(swsscommon.CONFIG_DB, dvs.redis_sock, 0) |
| 81 | + portTbl = swsscommon.Table(conf_db, swsscommon.CFG_PORT_TABLE_NAME) |
| 82 | + keys = portTbl.getKeys() |
| 83 | + assert len(keys) > 0 |
| 84 | + for key in keys: |
| 85 | + portTbl._del(key) |
| 86 | + |
| 87 | + # restart to apply new port_config.ini |
| 88 | + dvs.stop_swss() |
| 89 | + dvs.start_swss() |
| 90 | + time.sleep(5) |
| 91 | + |
| 92 | + asic_db = swsscommon.DBConnector(swsscommon.ASIC_DB, dvs.redis_sock, 0) |
| 93 | + |
| 94 | + for i in range(0,4): |
| 95 | + port_name = 'Ethernet{0}'.format(eth+i) |
| 96 | + port_oid = self.getPortOid(dvs, port_name) |
| 97 | + port_tbl = swsscommon.Table(asic_db, 'ASIC_STATE:SAI_OBJECT_TYPE_PORT:{0}'.format(port_oid)) |
| 98 | + hw_lane_value = None |
| 99 | + |
| 100 | + for k in port_tbl.get('')[1]: |
| 101 | + if k[0] == "SAI_PORT_ATTR_HW_LANE_LIST": |
| 102 | + hw_lane_value = k[1] |
| 103 | + |
| 104 | + assert hw_lane_value, "Can't get hw_lane list" |
| 105 | + assert hw_lane_value == "1:%s" % (LANES_L[i]) |
| 106 | + |
| 107 | + |
0 commit comments