-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathtest_yang_model.py
330 lines (297 loc) · 10.8 KB
/
test_yang_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# This script is used to
import yang as ly
import logging
import argparse
import sys
import ijson
import json
#import sonic_yang as sy
from glob import glob
from os import listdir
from os.path import isfile, join, splitext
#Globals vars
PASS = 0
FAIL = 1
logging.basicConfig(level=logging.DEBUG)
log = logging.getLogger("YANG-TEST")
log.setLevel(logging.INFO)
log.addHandler(logging.NullHandler())
# Global functions
def printExceptionDetails():
try:
excType, excObj, traceBack = sys.exc_info()
fileName = traceBack.tb_frame.f_code.co_filename
lineNumber = traceBack.tb_lineno
log.error(" Exception >{}< in {}:{}".format(excObj, fileName, lineNumber))
except Exception as e:
log.error(" Exception in printExceptionDetails")
return
# class for YANG Model YangModelTesting
# Run function will run all the tests
# from a user given list.
class Test_yang_models:
def initTest(self):
self.defaultYANGFailure = {
'Must': ['Must condition', 'not satisfied'],
'InvalidValue': ['Invalid value'],
'LeafRef': ['Leafref', 'non-existing'],
'When': ['When condition', 'not satisfied'],
'Pattern': ['pattern', 'does not satisfy'],
'Mandatory': ['required element', 'Missing'],
'Verify': ['verified'],
'Range': ['does not satisfy', 'range'],
'MinElements': ['Too few'],
'MaxElements': ['Too many'],
'UnknownElement': ['Unknown element'],
'None': []
}
self.ExceptionTests = { }
for test_file in glob("./tests/yang_model_tests/tests/*.json"):
try:
with open(test_file) as fp:
log.info("Loading file " + test_file)
data = json.load(fp)
for key, test in data.items():
eStr = []
if 'eStrKey' in test:
eStr = list(self.defaultYANGFailure[test['eStrKey']])
if 'eStr' in test:
eStr += list(test['eStr'])
test['eStr'] = eStr
log.debug("estr for " + key + " is " + str(eStr))
self.ExceptionTests[key] = test
except Exception as e:
log.error("Failed to load file " + test_file)
raise(e)
self.SpecialTests = {
'ALL_VLAN_TEST': {
'desc': 'VLAN TEST.',
'eStr': self.defaultYANGFailure['None']
}
}
self.tests = list(self.ExceptionTests.keys())+list(self.SpecialTests.keys())
self.yangDir = './yang-models/'
self.testFile = {}
for test_file in glob("./tests/yang_model_tests/tests_config/*.json"):
try:
with open(test_file) as fp:
log.info("Loading file " + test_file)
data = json.load(fp)
for key in data:
self.testFile[key] = test_file
except Exception as e:
log.error("Failed to load file " + test_file)
raise(e)
self.testNum = 1
# other class vars
# self.ctx
return
"""
load all YANG models before test run
"""
def loadYangModel(self, yangDir):
try:
# create context
self.ctx = ly.Context(yangDir)
# get all files
yangFiles = glob(yangDir +"/*.yang")
# load yang modules
for file in yangFiles:
log.debug(file)
m = self.ctx.parse_module_path(file, ly.LYS_IN_YANG)
if m is not None:
log.info("module: {} is loaded successfully".format(m.name()))
else:
log.info("Could not load module: {}".format(file))
except Exception as e:
printExceptionDetails()
raise e
return
"""
Get the JSON input based on func name
and return jsonInput
"""
def readJsonInput(self, test):
try:
# load test specific Dictionary, using Key = func
# this is to avoid loading very large JSON in memory
log.debug(" Read JSON Section: " + test)
jInput = ""
test_file = self.testFile[test]
with open(test_file, 'rb') as f:
jInst = ijson.items(f, test)
for it in jInst:
jInput = jInput + json.dumps(it)
log.debug("Read json JIn: {}".format(jInput))
except Exception as e:
printExceptionDetails()
return jInput
"""
Log the start of a test
"""
def logStartTest(self, desc):
log.info("\n------------------- Test "+ str(self.testNum) +\
": " + desc + "---------------------")
self.testNum = self.testNum + 1
return
"""
Load Config Data and return Exception as String
Parameters:
jInput (dict): input config to load.
verify (dict): contains xpath, key and value. This is used to verify,
that node tree at xpath contains correct key and value.
Example:
'verify': {'xpath': "/sonic-acl:sonic-acl/ACL_TABLE/ACL_TABLE_LIST\
[ACL_TABLE_NAME='NO-NSW-PACL-V4']/stage",
'key': 'sonic-acl:stage',
'value': 'INGRESS'
}
"""
def loadConfigData(self, jInput, verify=None):
s = ""
try:
node = self.ctx.parse_data_mem(jInput, ly.LYD_JSON, \
ly.LYD_OPT_CONFIG | ly.LYD_OPT_STRICT)
# verify the data tree if asked
if verify is not None:
xpath = verify['xpath']
log.info("Verify xpath: {}".format(xpath))
set = node.find_path(xpath)
for dnode in set.data():
if (xpath == dnode.path()):
log.info("Verify dnode: {}".format(dnode.path()))
data = dnode.print_mem(ly.LYD_JSON, ly.LYP_WITHSIBLINGS \
| ly.LYP_FORMAT | ly.LYP_WD_ALL)
data = json.loads(data)
log.info("Verify data: {}".format(data))
assert (data[verify['key']] == verify['value'])
s = 'verified'
except Exception as e:
s = str(e)
log.info(s)
return s
"""
Run Exception Test
"""
def runExceptionTest(self, test):
try:
desc = self.ExceptionTests[test]['desc']
self.logStartTest(desc)
jInput = self.readJsonInput(test)
# load the data, expect a exception with must condition failure
s = self.loadConfigData(jInput, self.ExceptionTests[test].get('verify'))
eStr = self.ExceptionTests[test]['eStr']
log.debug("eStr: {}".format(eStr))
log.debug("s: {}".format(s))
if len(eStr) == 0 and s != "":
raise Exception("{} in not empty".format(s))
elif (sum(1 for str in eStr if str not in s) == 0):
log.info(desc + " Passed\n")
return PASS
else:
raise Exception("Mismatch {} and {}".format(eStr, s))
except Exception as e:
printExceptionDetails()
log.info(desc + " Failed\n")
return FAIL
"""
Run Special Tests
"""
def runSpecialTest(self, test):
try:
if test == 'ALL_VLAN_TEST':
return self.runVlanSpecialTest(test);
except Exception as e:
printExceptionDetails()
log.info(desc + " Failed\n")
return FAIL
def runVlanSpecialTest(self, test):
try:
desc = self.SpecialTests[test]['desc']
self.logStartTest(desc)
jInput = json.loads(self.readJsonInput(test))
# check all Vlan from 2 to 4094
for i in range(2,4095):
vlan = 'Vlan'+str(i)
jInput["sonic-vlan:sonic-vlan"]["sonic-vlan:VLAN"]["VLAN_LIST"]\
[0]["name"] = vlan
log.debug(jInput)
s = self.loadConfigData(json.dumps(jInput))
if s!="":
raise Exception("{} in not empty".format(s))
return PASS
except Exception as e:
printExceptionDetails()
log.info(desc + " Failed\n")
return FAIL
"""
Run all tests from list self.tests
"""
def test_run_tests(self):
ret = 0
try:
self.initTest()
self.loadYangModel(self.yangDir)
assert len(self.tests) != 0
print("Tests:{}".format(self.tests))
for test in self.tests:
test = test.strip()
if test in self.ExceptionTests:
ret = ret + self.runExceptionTest(test)
elif test in self.SpecialTests:
ret = ret + self.runSpecialTest(test)
else:
raise Exception("Unexpected Test")
except Exception as e:
ret = FAIL * len(self.tests)
printExceptionDetails()
assert ret == 0
return
# End of Class
"""
Start Here
"""
def main():
parser = argparse.ArgumentParser(description='Script to run YANG model tests',
formatter_class=argparse.RawTextHelpFormatter,
epilog="""
Usage:
python yangModelTesting.py -h
""")
parser.add_argument('-t', '--tests', type=str, \
help='tests to run separated by comma')
parser.add_argument('-f', '--json-file', type=str, \
help='JSON input for tests ', required=True)
parser.add_argument('-y', '--yang-dir', type=str, \
help='Path to YANG models', required=True)
parser.add_argument('-v', '--verbose-level', \
help='Verbose mode', action='store_true')
parser.add_argument('-l', '--list-tests', \
help='list all tests', action='store_true')
args = parser.parse_args()
try:
tests = args.tests
jsonFile = args.json_file
yangDir = args.yang_dir
logLevel = args.verbose_level
listTests = args.list_tests
if logLevel:
log.setLevel(logging.DEBUG)
# Make a list
if (tests):
tests = tests.split(",")
yTest = YangModelTesting(tests, yangDir, jsonFile)
if (listTests):
for key in yTest.ExceptionTests.keys():
log.info(key)
sys.exit(0)
ret = yTest.run()
if ret == 0:
log.info("All Test Passed")
sys.exit(ret)
except Exception as e:
printExceptionDetails()
sys.exit(1)
return
if __name__ == '__main__':
main()