Skip to content

Commit 920959c

Browse files
authored
[Dash] [UT] Add ZMQ test case for dash (#2967)
* Add ZMQ test case for dash
1 parent 6026b6d commit 920959c

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

tests/create_appliance.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python3
2+
3+
"""
4+
Connect to Dash orch with ZMQ and send create appliance request.
5+
usage:
6+
python3 create_appliance.py [appliance ID]
7+
Example:
8+
python3 create_appliance.py 1234
9+
"""
10+
11+
from swsscommon import swsscommon
12+
from dash_api.appliance_pb2 import *
13+
import typing
14+
import ipaddress
15+
import socket
16+
import sys
17+
18+
def to_string(value):
19+
if isinstance(value, bool):
20+
return "true" if value else "false"
21+
elif isinstance(value, bytes):
22+
return value
23+
return str(value)
24+
25+
# connect to Dash ZMQ endpoint
26+
db_connection = swsscommon.DBConnector("APPL_DB", 0)
27+
zmq_client = swsscommon.ZmqClient("tcp://127.0.0.1:8100")
28+
app_dash_appliance_table = swsscommon.ZmqProducerStateTable(
29+
db_connection,
30+
"DASH_APPLIANCE_TABLE",
31+
zmq_client,
32+
True)
33+
34+
# prepare create appliance request
35+
pairs_str = []
36+
pb = Appliance()
37+
pb.sip.ipv4 = socket.htonl(int(ipaddress.ip_address("10.0.0.1")))
38+
pb.vm_vni = int(sys.argv[1])
39+
pairs_str.append(("pb", pb.SerializeToString()))
40+
41+
# send create appliance request via ZMQ
42+
app_dash_appliance_table.set("100", pairs_str)

tests/test_zmq.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
from swsscommon import swsscommon
2+
3+
from dash_api.appliance_pb2 import *
4+
from dash_api.vnet_pb2 import *
5+
from dash_api.eni_pb2 import *
6+
from dash_api.route_pb2 import *
7+
from dash_api.route_rule_pb2 import *
8+
from dash_api.vnet_mapping_pb2 import *
9+
from dash_api.route_type_pb2 import *
10+
from dash_api.types_pb2 import *
11+
12+
import typing
13+
import time
14+
import binascii
15+
import uuid
16+
import ipaddress
17+
import sys
18+
import socket
19+
import logging
20+
import pytest
21+
22+
logging.basicConfig(level=logging.INFO)
23+
zmq_logger = logging.getLogger(__name__)
24+
25+
DVS_ENV = ["HWSKU=DPU-2P"]
26+
NUM_PORTS = 2
27+
28+
class Table(object):
29+
def __init__(self, database, table_name: str):
30+
self.table_name = table_name
31+
self.table = swsscommon.Table(database.db_connection, self.table_name)
32+
33+
def __getitem__(self, key: str):
34+
exists, result = self.table.get(str(key))
35+
if not exists:
36+
return None
37+
else:
38+
return dict(result)
39+
40+
def get_keys(self):
41+
return self.table.getKeys()
42+
43+
def get_newly_created_oid(self, old_oids):
44+
new_oids = self.asic_db.wait_for_n_keys(table, len(old_oids) + 1)
45+
oid = [ids for ids in new_oids if ids not in old_oids]
46+
return oid[0]
47+
48+
class DashZmq(object):
49+
def __init__(self, dvs):
50+
self.dvs = dvs
51+
self.asic_direction_lookup_table = Table(
52+
self.dvs.get_asic_db(), "ASIC_STATE:SAI_OBJECT_TYPE_DIRECTION_LOOKUP_ENTRY")
53+
self.asic_vip_table = Table(
54+
self.dvs.get_asic_db(), "ASIC_STATE:SAI_OBJECT_TYPE_VIP_ENTRY")
55+
56+
class TestZmqDash(object):
57+
@pytest.fixture(scope="class")
58+
def enable_orchagent_zmq(self, dvs):
59+
# change orchagent to use ZMQ
60+
dvs.runcmd("cp /usr/bin/orchagent.sh /usr/bin/orchagent.sh_zmq_ut_backup")
61+
dvs.runcmd("sed -i.bak 's/\/usr\/bin\/orchagent /\/usr\/bin\/orchagent -q tcp:\/\/127.0.0.1:8100 /g' /usr/bin/orchagent.sh")
62+
dvs.stop_swss()
63+
dvs.start_swss()
64+
65+
process_statue = dvs.runcmd("ps -ef")
66+
zmq_logger.debug("Process status: {}".format(process_statue))
67+
68+
yield
69+
70+
# revert change
71+
dvs.runcmd("cp /usr/bin/orchagent.sh_zmq_ut_backup /usr/bin/orchagent.sh")
72+
dvs.stop_swss()
73+
dvs.start_swss()
74+
75+
@pytest.mark.usefixtures("enable_orchagent_zmq")
76+
def test_appliance(self, dvs):
77+
# upload test script to test container and create applicance with it
78+
dvs.copy_file("/", "create_appliance.py")
79+
dvs.runcmd(['sh', '-c', "python3 create_appliance.py {}".format(1234)])
80+
time.sleep(3)
81+
82+
asic_direction_lookup_table = Table(
83+
dvs.get_asic_db(), "ASIC_STATE:SAI_OBJECT_TYPE_DIRECTION_LOOKUP_ENTRY")
84+
direction_entries = asic_direction_lookup_table.get_keys()
85+
zmq_logger.info("Keys from asic_direction_lookup_table: {}".format(direction_entries))
86+
87+
assert direction_entries
88+
fvs = asic_direction_lookup_table[direction_entries[0]]
89+
zmq_logger.info("Data from asic_direction_lookup_table: {}={}".format(direction_entries[0], fvs))
90+
for fv in fvs.items():
91+
if fv[0] == "SAI_DIRECTION_LOOKUP_ENTRY_ATTR_ACTION":
92+
assert fv[1] == "SAI_DIRECTION_LOOKUP_ENTRY_ACTION_SET_OUTBOUND_DIRECTION"
93+
94+
asic_vip_table = Table(
95+
dvs.get_asic_db(), "ASIC_STATE:SAI_OBJECT_TYPE_VIP_ENTRY")
96+
vip_entries = asic_vip_table.get_keys()
97+
zmq_logger.info("Keys from asic_vip_table: {}".format(direction_entries))
98+
99+
assert vip_entries
100+
fvs = asic_vip_table[vip_entries[0]]
101+
zmq_logger.info("Data from asic_vip_table: {}={}".format(vip_entries[0], fvs))
102+
for fv in fvs.items():
103+
if fv[0] == "SAI_VIP_ENTRY_ATTR_ACTION":
104+
assert fv[1] == "SAI_VIP_ENTRY_ACTION_ACCEPT"

0 commit comments

Comments
 (0)