Skip to content

Commit 6edc00e

Browse files
jktjktclaude
andcommitted
tests: pass ly_yang_module_dir() explicitly where needed
Upstream libyang now unconditionally needs certain core YANG modules, and they are not available as builtins in the library. Load them via the usual lookups from disk instead. Since context creation without access to on-disk data is now broken, the tests which excercie this path have to first change to loading "a minimal context" with file lookup enabled, and then disable this feature via a newly wrapped function. Signed-off-by: Jan Kundrát <jan.kundrat@cesnet.cz> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 49a1e9a commit 6edc00e

9 files changed

Lines changed: 94 additions & 65 deletions

File tree

‎cffi/cdefs.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ typedef enum {
4040
LY_ERR ly_ctx_new(const char *, uint16_t, struct ly_ctx **);
4141
void ly_ctx_destroy(struct ly_ctx *);
4242
int ly_ctx_set_searchdir(struct ly_ctx *, const char *);
43+
const char *ly_yang_module_dir(void);
44+
LY_ERR ly_ctx_set_options(struct ly_ctx *, uint32_t);
45+
LY_ERR ly_ctx_unset_options(struct ly_ctx *, uint32_t);
4346

4447
typedef enum
4548
{

‎libyang/context.py‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,14 @@ def compile_schema(self):
281281
if ret != lib.LY_SUCCESS:
282282
raise self.error("could not compile schema")
283283

284+
def set_disable_searchdirs(self, disable_searchdirs: bool) -> None:
285+
if disable_searchdirs:
286+
ret = lib.ly_ctx_set_options(self.cdata, lib.LY_CTX_DISABLE_SEARCHDIRS)
287+
else:
288+
ret = lib.ly_ctx_unset_options(self.cdata, lib.LY_CTX_DISABLE_SEARCHDIRS)
289+
if ret != lib.LY_SUCCESS:
290+
raise self.error("cannot change disable_searchdirs")
291+
284292
def get_yanglib_data(self, content_id_format=""):
285293
dnode = ffi.new("struct lyd_node **")
286294
ret = lib.ly_ctx_get_yanglib_data(self.cdata, dnode, str2c(content_id_format))

‎tests/_common.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright (c) 2018-2019 Robin Jarry
2+
# SPDX-License-Identifier: MIT
3+
4+
import os
5+
6+
from _libyang import lib
7+
from libyang.util import c2str
8+
9+
10+
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
11+
CORE_MODULE_DIR = c2str(lib.ly_yang_module_dir())
12+
SEARCH_DIRS = ":".join([YANG_DIR, CORE_MODULE_DIR])

‎tests/test_context.py‎

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@
44
import os
55
import unittest
66

7+
from _common import CORE_MODULE_DIR, SEARCH_DIRS, YANG_DIR
78
from libyang import Context, LibyangError, Module, SContainer, SLeaf, SLeafList
89
from libyang.util import c2str
910

1011

11-
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
12-
13-
1412
# -------------------------------------------------------------------------------------
1513

1614

1715
class ContextTest(unittest.TestCase):
1816
def test_ctx_no_dir(self):
19-
with Context() as ctx:
17+
with Context(CORE_MODULE_DIR) as ctx:
2018
self.assertIsNot(ctx, None)
2119

2220
def test_ctx_yanglib(self):
2321
ctx = Context(
24-
YANG_DIR,
22+
":".join([YANG_DIR, CORE_MODULE_DIR]),
2523
yanglib_path=YANG_DIR + "/yang-library.json",
2624
compile_obsolete=True,
2725
)
@@ -31,22 +29,24 @@ def test_ctx_yanglib(self):
3129
self.assertIsInstance(j, str)
3230

3331
def test_ctx_dir(self):
34-
with Context(YANG_DIR) as ctx:
32+
with Context(SEARCH_DIRS) as ctx:
3533
self.assertIsNot(ctx, None)
3634

3735
def test_ctx_duplicate_searchpath(self):
38-
duplicate_search_path = ":".join([YANG_DIR, YANG_DIR])
36+
duplicate_search_path = ":".join([YANG_DIR, YANG_DIR, CORE_MODULE_DIR])
3937
try:
4038
Context(duplicate_search_path)
4139
except LibyangError:
4240
self.fail("Context.__init__ should not raise LibyangError")
4341

4442
def test_ctx_invalid_dir(self):
45-
with Context("/does/not/exist") as ctx:
43+
with Context(":".join(["/does/not/exist", CORE_MODULE_DIR])) as ctx:
4644
self.assertIsNot(ctx, None)
4745

4846
def test_ctx_missing_dir(self):
49-
with Context(os.path.join(YANG_DIR, "yolo")) as ctx:
47+
with Context(
48+
":".join([os.path.join(YANG_DIR, "yolo"), CORE_MODULE_DIR])
49+
) as ctx:
5050
self.assertIsNot(ctx, None)
5151
with self.assertRaises(LibyangError):
5252
ctx.load_module("yolo-system")
@@ -56,51 +56,53 @@ def test_ctx_env_search_dir(self):
5656
os.environ["YANGPATH"] = ":".join(
5757
[os.path.join(YANG_DIR, "omg"), os.path.join(YANG_DIR, "wtf")]
5858
)
59-
with Context(os.path.join(YANG_DIR, "yolo")) as ctx:
59+
with Context(
60+
":".join([os.path.join(YANG_DIR, "yolo"), CORE_MODULE_DIR])
61+
) as ctx:
6062
mod = ctx.load_module("yolo-system")
6163
self.assertIsInstance(mod, Module)
6264
finally:
6365
del os.environ["YANGPATH"]
6466

6567
def test_ctx_load_module(self):
66-
with Context(YANG_DIR) as ctx:
68+
with Context(SEARCH_DIRS) as ctx:
6769
mod = ctx.load_module("yolo-system")
6870
self.assertIsInstance(mod, Module)
6971

7072
def test_ctx_load_module_with_features(self):
71-
with Context(YANG_DIR) as ctx:
73+
with Context(SEARCH_DIRS) as ctx:
7274
mod = ctx.load_module("yolo-system", None, ["*"])
7375
self.assertIsInstance(mod, Module)
7476
for f in list(mod.features()):
7577
self.assertTrue(f.state())
7678

7779
def test_ctx_get_module(self):
78-
with Context(YANG_DIR) as ctx:
80+
with Context(SEARCH_DIRS) as ctx:
7981
ctx.load_module("yolo-system")
8082
mod = ctx.get_module("wtf-types")
8183
self.assertIsInstance(mod, Module)
8284

8385
def test_ctx_get_invalid_module(self):
84-
with Context(YANG_DIR) as ctx:
86+
with Context(SEARCH_DIRS) as ctx:
8587
ctx.load_module("wtf-types")
8688
with self.assertRaises(LibyangError):
8789
ctx.get_module("yolo-system")
8890

8991
def test_ctx_load_invalid_module(self):
90-
with Context(YANG_DIR) as ctx:
92+
with Context(SEARCH_DIRS) as ctx:
9193
with self.assertRaises(LibyangError):
9294
ctx.load_module("invalid-module")
9395

9496
def test_ctx_find_path(self):
95-
with Context(YANG_DIR) as ctx:
97+
with Context(SEARCH_DIRS) as ctx:
9698
ctx.load_module("yolo-system")
9799
node = next(ctx.find_path("/yolo-system:conf/offline"))
98100
self.assertIsInstance(node, SLeaf)
99101
node2 = next(ctx.find_path("../number", root_node=node))
100102
self.assertIsInstance(node2, SLeafList)
101103

102104
def test_ctx_find_xpath_atoms(self):
103-
with Context(YANG_DIR) as ctx:
105+
with Context(SEARCH_DIRS) as ctx:
104106
ctx.load_module("yolo-system")
105107
node_iter = ctx.find_xpath_atoms("/yolo-system:conf/offline")
106108
node = next(node_iter)
@@ -116,7 +118,7 @@ def test_ctx_find_xpath_atoms(self):
116118
self.assertIsInstance(node, SLeafList)
117119

118120
def test_ctx_iter_modules(self):
119-
with Context(YANG_DIR) as ctx:
121+
with Context(SEARCH_DIRS) as ctx:
120122
ctx.load_module("yolo-system")
121123
modules = list(iter(ctx))
122124
self.assertGreater(len(modules), 0)
@@ -126,29 +128,31 @@ def test_ctx_iter_modules(self):
126128
def test_ctx_parse_module(self):
127129
with open(self.YOLO_MOD_PATH, encoding="utf-8") as f:
128130
mod_str = f.read()
129-
with Context(YANG_DIR) as ctx:
131+
with Context(SEARCH_DIRS) as ctx:
130132
mod = ctx.parse_module_str(mod_str, features=["turbo-boost", "networking"])
131133
self.assertIsInstance(mod, Module)
132134

133135
with open(self.YOLO_MOD_PATH, encoding="utf-8") as f:
134-
with Context(YANG_DIR) as ctx:
136+
with Context(SEARCH_DIRS) as ctx:
135137
mod = ctx.parse_module_file(f, features=["turbo-boost", "networking"])
136138
self.assertIsInstance(mod, Module)
137139

138140
def test_ctx_leafref_extended(self):
139-
with Context(YANG_DIR, leafref_extended=True) as ctx:
141+
with Context(SEARCH_DIRS, leafref_extended=True) as ctx:
140142
mod = ctx.load_module("yolo-leafref-extended")
141143
self.assertIsInstance(mod, Module)
142144

143145
def test_context_dict(self):
144-
with Context(YANG_DIR) as ctx:
146+
with Context(SEARCH_DIRS) as ctx:
145147
orig_str = "teststring"
146148
handle = ctx.add_to_dict(orig_str)
147149
self.assertEqual(orig_str, c2str(handle))
148150
ctx.remove_from_dict(orig_str)
149151

150152
def test_ctx_disable_searchdirs(self):
151-
with Context(YANG_DIR, disable_searchdirs=True) as ctx:
153+
# LY_CTX_DISABLE_SEARCHDIRS no longer works at init time
154+
with Context(SEARCH_DIRS) as ctx:
155+
ctx.set_disable_searchdirs(True)
152156
with self.assertRaises(LibyangError):
153157
ctx.load_module("yolo-nodetypes")
154158

@@ -168,7 +172,8 @@ def get_module_valid_clb(mod_name, *_):
168172
def get_module_invalid_clb(mod_name, *_):
169173
return None
170174

171-
with Context(YANG_DIR, disable_searchdirs=True) as ctx:
175+
with Context(SEARCH_DIRS) as ctx:
176+
ctx.set_disable_searchdirs(True)
172177
with self.assertRaises(LibyangError):
173178
ctx.load_module("yolo-nodetypes")
174179

‎tests/test_data.py‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from unittest import mock
99
from unittest.mock import patch
1010

11+
from _common import SEARCH_DIRS
1112
from _libyang import lib
1213
from libyang import (
1314
Context,
@@ -27,13 +28,10 @@
2728
from libyang.data import dict_to_dnode
2829

2930

30-
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
31-
32-
3331
# -------------------------------------------------------------------------------------
3432
class DataTest(unittest.TestCase):
3533
def setUp(self):
36-
self.ctx = Context(YANG_DIR, compile_obsolete=True)
34+
self.ctx = Context(SEARCH_DIRS, compile_obsolete=True)
3735
modules = [
3836
self.ctx.load_module("ietf-netconf"),
3937
self.ctx.load_module("yolo-system"),
@@ -1094,7 +1092,10 @@ def test_dnode_leafref_linking(self):
10941092
}"""
10951093
self.ctx.destroy()
10961094
self.ctx = Context(
1097-
YANG_DIR, leafref_extended=True, leafref_linking=True, compile_obsolete=True
1095+
SEARCH_DIRS,
1096+
leafref_extended=True,
1097+
leafref_linking=True,
1098+
compile_obsolete=True,
10981099
)
10991100
mod = self.ctx.load_module("yolo-leafref-extended")
11001101
self.assertIsInstance(mod, Module)
@@ -1126,7 +1127,9 @@ def test_dnode_builtin_plugins_only(self):
11261127
MAIN = {"yolo-nodetypes:ip-address": "test"}
11271128
self.tearDown()
11281129
gc.collect()
1129-
self.ctx = Context(YANG_DIR, builtin_plugins_only=True, compile_obsolete=True)
1130+
self.ctx = Context(
1131+
SEARCH_DIRS, builtin_plugins_only=True, compile_obsolete=True
1132+
)
11301133
module = self.ctx.load_module("yolo-nodetypes")
11311134
dnode = dict_to_dnode(MAIN, module, None, validate=False, store_only=True)
11321135
self.assertIsInstance(dnode, DLeaf)
@@ -1145,7 +1148,9 @@ def test_merge_builtin_plugins_only(self):
11451148
MAIN = {"yolo-nodetypes:ip-address": "test"}
11461149
self.tearDown()
11471150
gc.collect()
1148-
self.ctx = Context(YANG_DIR, builtin_plugins_only=True, compile_obsolete=True)
1151+
self.ctx = Context(
1152+
SEARCH_DIRS, builtin_plugins_only=True, compile_obsolete=True
1153+
)
11491154
module = self.ctx.load_module("yolo-nodetypes")
11501155
dnode = module.parse_data_dict(MAIN, validate=False, store_only=True)
11511156
self.assertIsInstance(dnode, DLeaf)

‎tests/test_diff.py‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import os
55
import unittest
66

7+
from _common import CORE_MODULE_DIR, SEARCH_DIRS as NEW_SEARCH_DIRS
78
from libyang import (
89
BaseTypeAdded,
910
BaseTypeRemoved,
@@ -24,7 +25,10 @@
2425

2526

2627
OLD_YANG_DIR = os.path.join(os.path.dirname(__file__), "yang-old")
27-
NEW_YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
28+
29+
# yang-old/ doesn't carry its own copy of the core modules that
30+
# ly_ctx_new() now loads unconditionally (see _common.CORE_MODULE_DIR)
31+
# - add libyang's install-tree copy explicitly.
2832

2933

3034
# -------------------------------------------------------------------------------------
@@ -97,9 +101,9 @@ class DiffTest(unittest.TestCase):
97101
)
98102

99103
def test_diff(self):
100-
with Context(OLD_YANG_DIR, compile_obsolete=True) as ctx_old, Context(
101-
NEW_YANG_DIR, compile_obsolete=True
102-
) as ctx_new:
104+
with Context(
105+
":".join([OLD_YANG_DIR, CORE_MODULE_DIR]), compile_obsolete=True
106+
) as ctx_old, Context(NEW_SEARCH_DIRS, compile_obsolete=True) as ctx_new:
103107
mod = ctx_old.load_module("yolo-system")
104108
mod.feature_enable_all()
105109
mod = ctx_new.load_module("yolo-system")

‎tests/test_extension.py‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
# SPDX-License-Identifier: MIT
33

44
import logging
5-
import os
65
from typing import Any, Optional
76
import unittest
87

8+
from _common import SEARCH_DIRS
99
from libyang import (
1010
Context,
1111
ExtensionCompiled,
@@ -19,9 +19,6 @@
1919
)
2020

2121

22-
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
23-
24-
2522
# -------------------------------------------------------------------------------------
2623
class TestExtensionPlugin(ExtensionPlugin):
2724
def __init__(self, context: Context) -> None:
@@ -76,7 +73,7 @@ def _compile_free_clb(self, ext: ExtensionCompiled) -> None:
7673
# -------------------------------------------------------------------------------------
7774
class ExtensionTest(unittest.TestCase):
7875
def setUp(self):
79-
self.ctx = Context(YANG_DIR)
76+
self.ctx = Context(SEARCH_DIRS)
8077
self.plugin = TestExtensionPlugin(self.ctx)
8178

8279
def tearDown(self):
@@ -174,7 +171,7 @@ def _compile_clb(self, pext: ExtensionParsed, cext: ExtensionCompiled) -> None:
174171

175172
class ExtensionExampleTest(unittest.TestCase):
176173
def setUp(self):
177-
self.ctx = Context(YANG_DIR)
174+
self.ctx = Context(SEARCH_DIRS)
178175
self.plugins = []
179176

180177
def tearDown(self):

‎tests/test_log.py‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,16 @@
22
# SPDX-License-Identifier: MIT
33

44
import logging
5-
import os
65
import sys
76
import unittest
87

8+
from _common import SEARCH_DIRS
99
from libyang import Context, LibyangError, configure_logging, temp_log_options
1010

1111

12-
YANG_DIR = os.path.join(os.path.dirname(__file__), "yang")
13-
14-
1512
class LogTest(unittest.TestCase):
1613
def setUp(self):
17-
self.ctx = Context(YANG_DIR)
14+
self.ctx = Context(SEARCH_DIRS)
1815
configure_logging(False, logging.INFO)
1916

2017
def tearDown(self):

0 commit comments

Comments
 (0)