Skip to content

Commit 362d051

Browse files
donaldhkuba-moo
authored andcommitted
tools: ynl: add schema checking
Add a --validate flag to pyynl for explicit schema check with error reporting and add a schema_check make target to check all YNL specs. make -C tools/net/ynl schema_check make: Entering directory '/home/donaldh/net-next/tools/net/ynl' ok 1 binder.yaml schema validation not ok 2 conntrack.yaml schema validation 'labels mask' does not match '^[0-9a-z-]+$' Failed validating 'pattern' in schema['properties']['attribute-sets']['items']['properties']['attributes']['items']['properties']['name']: {'type': 'string', 'pattern': '^[0-9a-z-]+$'} On instance['attribute-sets'][14]['attributes'][22]['name']: 'labels mask' ok 3 devlink.yaml schema validation [...] Signed-off-by: Donald Hunter <donald.hunter@gmail.com> Link: https://patch.msgid.link/20251127123502.89142-2-donald.hunter@gmail.com Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent 2c80116 commit 362d051

2 files changed

Lines changed: 35 additions & 6 deletions

File tree

tools/net/ynl/Makefile

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ endif
1212
libdir ?= $(prefix)/$(libdir_relative)
1313
includedir ?= $(prefix)/include
1414

15+
SPECDIR=../../../Documentation/netlink/specs
16+
1517
SUBDIRS = lib generated samples ynltool tests
1618

1719
all: $(SUBDIRS) libynl.a
@@ -54,4 +56,20 @@ install: libynl.a lib/*.h
5456
run_tests:
5557
@$(MAKE) -C tests run_tests
5658

57-
.PHONY: all clean distclean install run_tests $(SUBDIRS)
59+
60+
schema_check:
61+
@N=1; \
62+
for spec in $(SPECDIR)/*.yaml ; do \
63+
NAME=$$(basename $$spec) ; \
64+
OUTPUT=$$(./pyynl/cli.py --spec $$spec --validate) ; \
65+
if [ $$? -eq 0 ] ; then \
66+
echo "ok $$N $$NAME schema validation" ; \
67+
else \
68+
echo "not ok $$N $$NAME schema validation" ; \
69+
echo "$$OUTPUT" ; \
70+
echo ; \
71+
fi ; \
72+
N=$$((N+1)) ; \
73+
done
74+
75+
.PHONY: all clean distclean install run_tests schema_check $(SUBDIRS)

tools/net/ynl/pyynl/cli.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import textwrap
1111

1212
sys.path.append(pathlib.Path(__file__).resolve().parent.as_posix())
13-
from lib import YnlFamily, Netlink, NlError
13+
from lib import YnlFamily, Netlink, NlError, SpecFamily
1414

1515
sys_schema_dir='/usr/share/ynl'
1616
relative_schema_dir='../../../../Documentation/netlink'
@@ -127,6 +127,7 @@ def main():
127127
group.add_argument('--list-msgs', action='store_true')
128128
group.add_argument('--list-attrs', dest='list_attrs', metavar='OPERATION', type=str,
129129
help='List attributes for an operation')
130+
group.add_argument('--validate', action='store_true')
130131

131132
parser.add_argument('--duration', dest='duration', type=int,
132133
help='when subscribed, watch for DURATION seconds')
@@ -168,15 +169,25 @@ def output(msg):
168169

169170
if args.family:
170171
spec = f"{spec_dir()}/{args.family}.yaml"
171-
if args.schema is None and spec.startswith(sys_schema_dir):
172-
args.schema = '' # disable schema validation when installed
173-
if args.process_unknown is None:
174-
args.process_unknown = True
175172
else:
176173
spec = args.spec
177174
if not os.path.isfile(spec):
178175
raise Exception(f"Spec file {spec} does not exist")
179176

177+
if args.validate:
178+
try:
179+
SpecFamily(spec, args.schema)
180+
except Exception as error:
181+
print(error)
182+
exit(1)
183+
return
184+
185+
if args.family: # set behaviour when using installed specs
186+
if args.schema is None and spec.startswith(sys_schema_dir):
187+
args.schema = '' # disable schema validation when installed
188+
if args.process_unknown is None:
189+
args.process_unknown = True
190+
180191
ynl = YnlFamily(spec, args.schema, args.process_unknown,
181192
recv_size=args.dbg_small_recv)
182193
if args.dbg_small_recv:

0 commit comments

Comments
 (0)