1
1
# This script is used to
2
2
3
- import yang as ly
3
+ import libyang as ly
4
4
import logging
5
5
import argparse
6
6
import sys
7
7
import ijson
8
8
import json
9
+ import pytest
9
10
#import sonic_yang as sy
10
11
from glob import glob
11
12
from os import listdir
@@ -63,6 +64,7 @@ def initTest(self):
63
64
}
64
65
65
66
self .ExceptionTests = { }
67
+ self .FailedTests = []
66
68
67
69
for test_file in glob ("./tests/yang_model_tests/tests/*.json" ):
68
70
try :
@@ -116,17 +118,18 @@ def loadYangModel(self, yangDir):
116
118
117
119
try :
118
120
# create context
119
- self .ctx = ly .Context (yangDir )
121
+ self .ctx = ly .Context (yangDir , loose_json_datatypes = True )
120
122
# get all files
121
123
yangFiles = glob (yangDir + "/*.yang" )
122
124
# load yang modules
123
125
for file in yangFiles :
124
126
log .debug (file )
125
- m = self .ctx .parse_module_path (file , ly .LYS_IN_YANG )
126
- if m is not None :
127
- log .info ("module: {} is loaded successfully" .format (m .name ()))
128
- else :
129
- log .info ("Could not load module: {}" .format (file ))
127
+ with open (file , 'r' ) as f :
128
+ m = self .ctx .parse_module_file (f , "yang" )
129
+ if m is not None :
130
+ log .info ("module: {} is loaded successfully" .format (m .name ()))
131
+ else :
132
+ log .info ("Could not load module: {}" .format (file ))
130
133
131
134
except Exception as e :
132
135
printExceptionDetails ()
@@ -178,26 +181,35 @@ def logStartTest(self, desc):
178
181
"""
179
182
def loadConfigData (self , jInput , verify = None ):
180
183
s = ""
184
+ node = None
185
+ try :
186
+ node = self .ctx .parse_data_mem (jInput , "json" , strict = True , no_state = True )
187
+ except Exception as e :
188
+ printExceptionDetails ()
189
+ s = str (e )
190
+ log .info (s )
191
+ return s
192
+
181
193
try :
182
- node = self .ctx .parse_data_mem (jInput , ly .LYD_JSON , \
183
- ly .LYD_OPT_CONFIG | ly .LYD_OPT_STRICT )
184
194
# verify the data tree if asked
185
195
if verify is not None :
186
196
xpath = verify ['xpath' ]
187
197
log .info ("Verify xpath: {}" .format (xpath ))
188
- set = node .find_path (xpath )
189
- for dnode in set . data () :
198
+ nodes = node .find_all (xpath )
199
+ for dnode in nodes :
190
200
if (xpath == dnode .path ()):
191
201
log .info ("Verify dnode: {}" .format (dnode .path ()))
192
- data = dnode .print_mem (ly .LYD_JSON , ly .LYP_WITHSIBLINGS \
193
- | ly .LYP_FORMAT | ly .LYP_WD_ALL )
202
+ data = dnode .print_mem ("json" , with_siblings = True , pretty = True , include_implicit_defaults = True )
194
203
data = json .loads (data )
195
- log .info ("Verify data: {}" .format (data ))
204
+ log .info ("Verify path value {} is {} in {}" .format (verify ['key' ], verify ['value' ], data ))
205
+ assert (verify ['key' ] in data )
196
206
assert (data [verify ['key' ]] == verify ['value' ])
197
207
s = 'verified'
198
208
except Exception as e :
199
- s = str (e )
200
- log .info (s )
209
+ printExceptionDetails ()
210
+
211
+ node .free ()
212
+
201
213
return s
202
214
203
215
"""
@@ -221,9 +233,12 @@ def runExceptionTest(self, test):
221
233
log .info (desc + " Passed\n " )
222
234
return PASS
223
235
else :
224
- raise Exception ("Mismatch {} and {}" .format (eStr , s ))
236
+ errstr = "{}: Mismatch {} and {}" .format (test , eStr , s )
237
+ self .FailedTests .append (errstr )
238
+ raise Exception (errstr )
225
239
except Exception as e :
226
240
printExceptionDetails ()
241
+
227
242
log .info (desc + " Failed\n " )
228
243
return FAIL
229
244
@@ -253,7 +268,9 @@ def runVlanSpecialTest(self, test):
253
268
log .debug (jInput )
254
269
s = self .loadConfigData (json .dumps (jInput ))
255
270
if s != "" :
256
- raise Exception ("{} in not empty" .format (s ))
271
+ errstr = "{}[{}]: {} in not empty" .format (test ,i ,s )
272
+ self .FailedTests .append (errstr )
273
+ raise Exception (errstr )
257
274
return PASS
258
275
except Exception as e :
259
276
printExceptionDetails ()
@@ -282,6 +299,17 @@ def test_run_tests(self):
282
299
ret = FAIL * len (self .tests )
283
300
printExceptionDetails ()
284
301
302
+ if len (self .FailedTests ):
303
+ print ("{} Failures:" .format (len (self .FailedTests )))
304
+ log .error ("{} Failures:" .format (len (self .FailedTests )))
305
+ for x in self .FailedTests :
306
+ print (x )
307
+ log .error (x )
308
+
309
+ if self .ctx :
310
+ self .ctx .destroy ()
311
+ self .ctx = None
312
+
285
313
assert ret == 0
286
314
return
287
315
# End of Class
0 commit comments