Skip to content

Commit 67cd259

Browse files
authored
Merge pull request #21719 from bradh352/bradh352/libyang3-pr1
libyang: upgrade to v3 step 2 -- port sonic-yang-models test cases libyang3 porting. See master tracking ticket #22385 Port of sonic-yang-model test cases Fixes #22385
2 parents 8f18414 + 965af3b commit 67cd259

35 files changed

+162
-90
lines changed

rules/sonic-yang-models-py3.mk

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
SONIC_YANG_MODELS_PY3 = sonic_yang_models-1.0-py3-none-any.whl
22
$(SONIC_YANG_MODELS_PY3)_SRC_PATH = $(SRC_PATH)/sonic-yang-models
33
$(SONIC_YANG_MODELS_PY3)_PYTHON_VERSION = 3
4-
$(SONIC_YANG_MODELS_PY3)_DEBS_DEPENDS = $(LIBYANG) $(LIBYANG_CPP) \
5-
$(LIBYANG_PY3) \
6-
$(LIBYANG3) \
4+
$(SONIC_YANG_MODELS_PY3)_DEBS_DEPENDS = $(LIBYANG3) \
75
$(LIBYANG3_PY3)
86

97
SONIC_PYTHON_WHEELS += $(SONIC_YANG_MODELS_PY3)

src/sonic-yang-models/tests/yang_model_pytests/conftest.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
11
import os
22
import pytest
3-
import yang as ly
3+
import libyang as ly
44
from json import dumps
55
from glob import glob
66

7-
87
class YangModel:
98

109
def __init__(self) -> None:
1110
cur_dir = os.path.dirname(os.path.abspath(__file__))
1211
project_root = os.path.abspath(os.path.join(cur_dir, '..', '..'))
1312
self.model_dir = os.path.join(project_root, 'yang-models')
14-
1513
self._load_model()
1614

15+
def __del__(self) -> None:
16+
self.ctx.destroy()
17+
self.ctx = None
18+
1719
def _load_model(self) -> None:
1820
self.ctx = ly.Context(self.model_dir)
1921
yang_files = glob(self.model_dir +"/*.yang")
20-
2122
for file in yang_files:
22-
m = self.ctx.parse_module_path(file, ly.LYS_IN_YANG)
23-
if not m:
24-
raise RuntimeError("Failed to parse '{file}' model")
23+
with open(file, 'r') as f:
24+
m = self.ctx.parse_module_file(f, "yang")
25+
if not m:
26+
raise RuntimeError("Failed to parse '{file}' model")
2527

2628
def _load_data(self, data) -> None:
27-
self.ctx.parse_data_mem(dumps(data), ly.LYD_JSON,
28-
ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
29+
dnode = self.ctx.parse_data_mem(dumps(data), "json", strict=True, no_state=True, json_string_datatypes=True)
30+
dnode.free()
2931

3032
def load_data(self, data, expected_error=None) -> None:
3133
if expected_error:
32-
with pytest.raises(RuntimeError) as exc_info:
34+
with pytest.raises(Exception) as exc_info:
3335
self._load_data(data)
34-
3536
assert expected_error in str(exc_info)
3637
else:
3738
self._load_data(data)

src/sonic-yang-models/tests/yang_model_pytests/test_acl.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def test_valid_data_trimming(self, yang_model):
5454

5555
@pytest.mark.parametrize(
5656
"action,error_message", [
57-
pytest.param('INVALID_VALUE', 'Invalid value', id="invalid-value")
57+
pytest.param('INVALID_VALUE', 'Invalid enumeration value', id="invalid-value")
5858
]
5959
)
6060
def test_neg_rule_packet_action(self, yang_model, action, error_message):

src/sonic-yang-models/tests/yang_model_pytests/test_buffer_profile.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def test_valid_data_lossy(self, yang_model, action):
3535

3636
@pytest.mark.parametrize(
3737
"action,error_message", [
38-
pytest.param('INVALID_VALUE', 'Invalid value', id="invalid-value")
38+
pytest.param('INVALID_VALUE', 'Invalid enumeration value', id="invalid-value")
3939
]
4040
)
4141
def test_neg_packet_discard_action(self, yang_model, action, error_message):

src/sonic-yang-models/tests/yang_model_pytests/test_crm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ def test_crm_valid_data(self, yang_model, resource, threshold):
6161

6262
@pytest.mark.parametrize(
6363
"high, low, error_message", [
64-
(-1, 70, 'Invalid value "-1"'),
65-
(100, -70, 'Invalid value "-70"'),
64+
(-1, 70, 'Value "-1" is out of type uint16 min/max bounds'),
65+
(100, -70, 'Value "-70" is out of type uint16 min/max bounds'),
6666
(10, 70, 'high_threshold should be more than low_threshold')]
6767
)
6868
def test_crm_thresholds(self, yang_model, resource, threshold, high, low, error_message):
@@ -82,7 +82,7 @@ def test_crm_thresholds(self, yang_model, resource, threshold, high, low, error_
8282

8383
@pytest.mark.parametrize(
8484
"high, low, th_type, error_message", [
85-
(100, 70, 'wrong', 'Value "wrong" does not satisfy the constraint'),
85+
(100, 70, 'wrong', 'Unsatisfied pattern'),
8686
(110, 20, 'percentage', 'Must condition')]
8787
)
8888
def test_crm_threshold_type(self, yang_model, resource, high, low, th_type, error_message):

src/sonic-yang-models/tests/yang_model_pytests/test_dash_crm.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def test_dash_crm_valid_data(self, yang_model, data, resource, threshold):
6868

6969
@pytest.mark.parametrize(
7070
"high, low, error_message", [
71-
(-1, 70, 'Invalid value "-1"'),
72-
(100, -70, 'Invalid value "-70"'),
71+
(-1, 70, 'Value "-1" is out of type uint16 min/max bounds'),
72+
(100, -70, 'Value "-70" is out of type uint16 min/max bounds'),
7373
(10, 70, 'high_threshold should be more than low_threshold')]
7474
)
7575
def test_dash_crm_thresholds(self, yang_model, data, resource, threshold, high, low, error_message):
@@ -87,7 +87,7 @@ def test_dash_crm_thresholds(self, yang_model, data, resource, threshold, high,
8787

8888
@pytest.mark.parametrize(
8989
"high, low, th_type, error_message", [
90-
(100, 70, 'wrong', 'Value "wrong" does not satisfy the constraint'),
90+
(100, 70, 'wrong', 'Unsatisfied pattern'),
9191
(110, 20, 'percentage', 'Must condition')]
9292
)
9393
def test_dash_crm_threshold_type(self, yang_model, data, resource, high, low, th_type, error_message):

src/sonic-yang-models/tests/yang_model_pytests/test_smart_switch.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def test_valid_data(self, yang_model):
3232
@pytest.mark.parametrize(
3333
"bridge_name, error_message", [
3434
("bridge-midplane", None),
35-
("wrong_name", 'Value "wrong_name" does not satisfy the constraint "bridge-midplane"')]
35+
("wrong_name", 'Unsatisfied pattern')]
3636
)
3737
def test_bridge_name(self, yang_model, bridge_name, error_message):
3838
data = {
@@ -51,7 +51,7 @@ def test_bridge_name(self, yang_model, bridge_name, error_message):
5151
@pytest.mark.parametrize(
5252
"ip_prefix, error_message", [
5353
("169.254.200.254/24", None),
54-
("169.254.xyz.254/24", 'Value "169.254.xyz.254/24" does not satisfy the constraint')]
54+
("169.254.xyz.254/24", 'Unsatisfied pattern')]
5555
)
5656
def test_bridge_ip_prefix(self, yang_model, ip_prefix, error_message):
5757
data = {
@@ -70,7 +70,7 @@ def test_bridge_ip_prefix(self, yang_model, ip_prefix, error_message):
7070
@pytest.mark.parametrize(
7171
"dpu_name, error_message", [
7272
("dpu0", None),
73-
("xyz", 'Value "xyz" does not satisfy the constraint "dpu[0-9]+')]
73+
("xyz", 'Unsatisfied pattern')]
7474
)
7575
def test_dpu_name(self, yang_model, dpu_name, error_message):
7676
data = {
@@ -91,7 +91,7 @@ def test_dpu_name(self, yang_model, dpu_name, error_message):
9191
@pytest.mark.parametrize(
9292
"midplane_interface, error_message", [
9393
("dpu0", None),
94-
("xyz", 'Value "xyz" does not satisfy the constraint "dpu[0-9]+')]
94+
("xyz", 'Unsatisfied pattern')]
9595
)
9696
def test_dpu_midplane_interface(self, yang_model, midplane_interface, error_message):
9797
data = {
@@ -112,7 +112,7 @@ def test_dpu_midplane_interface(self, yang_model, midplane_interface, error_mess
112112
@pytest.mark.parametrize(
113113
"dpu_name, error_message", [
114114
("str-8102-t1-dpu0", None),
115-
("str-8102-t1-dpu0a", 'Value "str-8102-t1-dpu0a" does not satisfy the constraint "[a-zA-Z0-9-]+[0-9]"')]
115+
("str-8102-t1-dpu0a", 'Unsatisfied pattern')]
116116
)
117117
def test_dpu_name(self, yang_model, dpu_name, error_message):
118118
data = {
@@ -172,7 +172,7 @@ def test_dpu_local_port(self, yang_model, local_port, error_message):
172172
@pytest.mark.parametrize(
173173
"vip_ipv4, error_message", [
174174
("192.168.1.1", None),
175-
("192.168.1.xyz", 'Value "192.168.1.xyz" does not satisfy the constraint')]
175+
("192.168.1.xyz", 'Unsatisfied pattern')]
176176
)
177177
def test_dpu_vip_ipv4(self, yang_model, vip_ipv4, error_message):
178178
data = {
@@ -202,7 +202,7 @@ def test_dpu_vip_ipv4(self, yang_model, vip_ipv4, error_message):
202202
@pytest.mark.parametrize(
203203
"vip_ipv6, error_message", [
204204
("2001:db8::1", None),
205-
("2001:db8::xyz", 'Value "2001:db8::xyz" does not satisfy the constraint')]
205+
("2001:db8::xyz", 'Unsatisfied pattern')]
206206
)
207207
def test_dpu_vip_ipv6(self, yang_model, vip_ipv6, error_message):
208208
data = {
@@ -232,7 +232,7 @@ def test_dpu_vip_ipv6(self, yang_model, vip_ipv6, error_message):
232232
@pytest.mark.parametrize(
233233
"pa_ipv4, error_message", [
234234
("192.168.1.2", None),
235-
("192.168.1.xyz", 'Value "192.168.1.xyz" does not satisfy the constraint')]
235+
("192.168.1.xyz", 'Unsatisfied pattern')]
236236
)
237237
def test_dpu_pa_ipv4(self, yang_model, pa_ipv4, error_message):
238238
data = {
@@ -262,7 +262,7 @@ def test_dpu_pa_ipv4(self, yang_model, pa_ipv4, error_message):
262262
@pytest.mark.parametrize(
263263
"pa_ipv6, error_message", [
264264
("2001:db8::2", None),
265-
("2001:db8::xyz", 'Value "2001:db8::xyz" does not satisfy the constraint')]
265+
("2001:db8::xyz", 'Unsatisfied pattern')]
266266
)
267267
def test_dpu_pa_ipv6(self, yang_model, pa_ipv6, error_message):
268268
data = {
@@ -292,7 +292,7 @@ def test_dpu_pa_ipv6(self, yang_model, pa_ipv6, error_message):
292292
@pytest.mark.parametrize(
293293
"dpu_id, error_message", [
294294
("0", None),
295-
("xyz", 'Value "xyz" does not satisfy the constraint')]
295+
("xyz", 'Unsatisfied pattern - "xyz"')]
296296
)
297297
def test_dpu_id(self, yang_model, dpu_id, error_message):
298298
data = {
@@ -322,7 +322,7 @@ def test_dpu_id(self, yang_model, dpu_id, error_message):
322322
@pytest.mark.parametrize(
323323
"gnmi_port, error_message", [
324324
(8080, None),
325-
(99999, 'Invalid value "99999" in "gnmi_port" element.')]
325+
(99999, 'Value "99999" is out of type uint16 min/max bounds')]
326326
)
327327
def test_dpu_gnmi_port(self, yang_model, gnmi_port, error_message):
328328
data = {
@@ -352,7 +352,7 @@ def test_dpu_gnmi_port(self, yang_model, gnmi_port, error_message):
352352
@pytest.mark.parametrize(
353353
"orchagent_zmq_port, error_message", [
354354
(50, None),
355-
(99999, 'Invalid value "99999" in "orchagent_zmq_port" element.')]
355+
(99999, 'Value "99999" is out of type uint16 min/max bounds')]
356356
)
357357
def test_dpu_orchagent_zmq_port(self, yang_model, orchagent_zmq_port, error_message):
358358
data = {
@@ -381,9 +381,9 @@ def test_dpu_orchagent_zmq_port(self, yang_model, orchagent_zmq_port, error_mess
381381

382382
@pytest.mark.parametrize(
383383
"dpu_id, swbus_port, error_message", [
384-
(0, 23606, None),
385-
(1, 23607, None),
386-
(7, 23613, None)]
384+
("0", 23606, None),
385+
("1", 23607, None),
386+
("7", 23613, None)]
387387
)
388388
def test_remote_dpu_swbus_port(self, yang_model, dpu_id, swbus_port, error_message):
389389
data = {

src/sonic-yang-models/tests/yang_model_pytests/test_trimming.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ def test_valid_data(self, yang_model, dscp, queue):
3232

3333
@pytest.mark.parametrize(
3434
"size,error_message", [
35-
pytest.param('-1', 'Invalid value', id="min-1"),
36-
pytest.param('4294967296', 'Invalid value', id="max+1")
35+
pytest.param('-1', 'out of type uint32 min/max bounds', id="min-1"),
36+
pytest.param('4294967296', 'out of type uint32 min/max bounds', id="max+1")
3737
]
3838
)
3939
def test_neg_size(self, yang_model, size, error_message):
@@ -51,8 +51,8 @@ def test_neg_size(self, yang_model, size, error_message):
5151

5252
@pytest.mark.parametrize(
5353
"dscp,error_message", [
54-
pytest.param('-1', 'Invalid value', id="min-1"),
55-
pytest.param('64', 'Invalid value', id="max+1")
54+
pytest.param('-1', 'Invalid union value', id="min-1"),
55+
pytest.param('64', 'Invalid union value', id="max+1")
5656
]
5757
)
5858
def test_neg_dscp_value(self, yang_model, dscp, error_message):
@@ -70,8 +70,8 @@ def test_neg_dscp_value(self, yang_model, dscp, error_message):
7070

7171
@pytest.mark.parametrize(
7272
"tc,error_message", [
73-
pytest.param('-1', 'Invalid value', id="min-1"),
74-
pytest.param('256', 'Invalid value', id="max+1")
73+
pytest.param('-1', 'uint8 min/max bounds', id="min-1"),
74+
pytest.param('256', 'uint8 min/max bounds', id="max+1")
7575
]
7676
)
7777
def test_neg_tc_value(self, yang_model, tc, error_message):
@@ -89,8 +89,8 @@ def test_neg_tc_value(self, yang_model, tc, error_message):
8989

9090
@pytest.mark.parametrize(
9191
"queue,error_message", [
92-
pytest.param('-1', 'Invalid value', id="min-1"),
93-
pytest.param('256', 'Invalid value', id="max+1")
92+
pytest.param('-1', 'Invalid union value', id="min-1"),
93+
pytest.param('256', 'Invalid union value', id="max+1")
9494
]
9595
)
9696
def test_neg_queue_index(self, yang_model, queue, error_message):

0 commit comments

Comments
 (0)