Skip to content

Commit d049d65

Browse files
authored
[vstest]: get subscribed objects from appdb and asicdb (sonic-net#664)
Signed-off-by: Guohan Lu <[email protected]>
1 parent 057a329 commit d049d65

File tree

2 files changed

+70
-13
lines changed

2 files changed

+70
-13
lines changed

tests/conftest.py

+64
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,12 @@ def add_log_marker(self):
327327
self.ctn.exec_run("logger {}".format(marker))
328328
return marker
329329

330+
def SubscribeAppDbObject(self, objpfx):
331+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.APP_DB)
332+
pubsub = r.pubsub()
333+
pubsub.psubscribe("__keyspace@0__:%s*" % objpfx)
334+
return pubsub
335+
330336
def SubscribeAsicDbObject(self, objpfx):
331337
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB)
332338
pubsub = r.pubsub()
@@ -356,6 +362,64 @@ def CountSubscribedObjects(self, pubsub, ignore=None, timeout=10):
356362

357363
return (nadd, ndel)
358364

365+
def GetSubscribedAppDbObjects(self, pubsub, ignore=None, timeout=10):
366+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.APP_DB)
367+
368+
addobjs = []
369+
delobjs = []
370+
idle = 0
371+
372+
while True and idle < timeout:
373+
message = pubsub.get_message()
374+
if message:
375+
print message
376+
key = message['channel'].split(':', 1)[1]
377+
if ignore:
378+
fds = message['channel'].split(':')
379+
if fds[2] in ignore:
380+
continue
381+
if message['data'] == 'hset':
382+
value=r.hgetall(key)
383+
addobjs.append({'key':k, 'vals':value})
384+
elif message['data'] == 'del':
385+
delobjs.append(key)
386+
idle = 0
387+
else:
388+
time.sleep(1)
389+
idle += 1
390+
391+
return (addobjs, delobjs)
392+
393+
394+
def GetSubscribedAsicDbObjects(self, pubsub, ignore=None, timeout=10):
395+
r = redis.Redis(unix_socket_path=self.redis_sock, db=swsscommon.ASIC_DB)
396+
397+
addobjs = []
398+
delobjs = []
399+
idle = 0
400+
401+
while True and idle < timeout:
402+
message = pubsub.get_message()
403+
if message:
404+
print message
405+
key = message['channel'].split(':', 1)[1]
406+
if ignore:
407+
fds = message['channel'].split(':')
408+
if fds[2] in ignore:
409+
continue
410+
if message['data'] == 'hset':
411+
value=r.hgetall(key)
412+
(_, t, k) = key.split(':', 2)
413+
addobjs.append({'type':t, 'key':k, 'vals':value})
414+
elif message['data'] == 'del':
415+
delobjs.append(key)
416+
idle = 0
417+
else:
418+
time.sleep(1)
419+
idle += 1
420+
421+
return (addobjs, delobjs)
422+
359423
def get_map_iface_bridge_port_id(self, asic_db):
360424
port_id_2_iface = self.asicdb.portoidmap
361425
tbl = swsscommon.Table(asic_db, "ASIC_STATE:SAI_OBJECT_TYPE_BRIDGE_PORT")

tests/test_route.py

+6-13
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,16 @@ def test_RouteAdd(dvs, testlog):
2222
ps = swsscommon.ProducerStateTable(db, "ROUTE_TABLE")
2323
fvs = swsscommon.FieldValuePairs([("nexthop","10.0.0.1"), ("ifname", "Ethernet0")])
2424

25-
ps.set("2.2.2.0/24", fvs)
25+
pubsub = dvs.SubscribeAsicDbObject("SAI_OBJECT_TYPE_ROUTE_ENTRY")
2626

27-
time.sleep(1)
27+
ps.set("2.2.2.0/24", fvs)
2828

2929
# check if route was propagated to ASIC DB
3030

31-
db = swsscommon.DBConnector(1, dvs.redis_sock, 0)
32-
33-
tbl = swsscommon.Table(db, "ASIC_STATE:SAI_OBJECT_TYPE_ROUTE_ENTRY")
34-
35-
keys = tbl.getKeys()
31+
(addobjs, delobjs) = dvs.GetSubscribedAsicDbObjects(pubsub)
3632

37-
found_route = False
38-
for k in keys:
39-
rt_key = json.loads(k)
33+
assert len(addobjs) == 1
4034

41-
if rt_key['dest'] == "2.2.2.0/24":
42-
found_route = True
35+
rt_key = json.loads(addobjs[0]['key'])
4336

44-
assert found_route
37+
assert rt_key['dest'] == "2.2.2.0/24"

0 commit comments

Comments
 (0)