Support OVSDB entity name format in Phosphorus
[integration/test.git] / csit / libraries / backuprestore / backuprestoretest.py
1 import unittest
2 import sys
3 import JsonDiffTool
4
5 """
6 Unit tests for the Json Diff Tool library
7 Updated: 2017-04-24
8 """
9
10 __author__ = "Diego Granados"
11 __copyright__ = "Copyright(c) 2017, Ericsson."
12 __license__ = "New-style BSD"
13 __email__ = "diego.jesus.granados.lopez@ericsson.com"
14
15
16 class PathConversionTest(unittest.TestCase):
17     """
18     Conversions from jsonpath paths to jsonpatch patches used to remove the element pointed by the path
19     """
20
21     def testArrayElementConversion(self):
22         self.assertEquals(
23             '[{"op":"remove","path":"/ietf-yang-library:modules-state/module/56"}]',
24             JsonDiffTool.from_path_to_jsonpatch(
25                 "/ietf-yang-library:modules-state/module/56"
26             ),
27             "Array element conversion failed!",
28         )
29
30     def testMapValueElementConversion(self):
31         self.assertEquals(
32             '[{"op":"remove","path":"/ietf-yang-library:modules-state/module/blablah"}]',
33             JsonDiffTool.from_path_to_jsonpatch(
34                 "/ietf-yang-library:modules-state/module/blablah"
35             ),
36             "Array element conversion failed!",
37         )
38
39
40 class JsonDiffToolTest(unittest.TestCase):
41     """
42     Tests for the tool itself, including both command-line and RIDE keyword invokation style
43     """
44
45     def testSimpleDifferenceCountingWithoutFiltering(self):
46         """
47         Identical documents
48         """
49         self.assertEquals(
50             0,
51             JsonDiffTool.Json_Diff_Check_Keyword(
52                 "testinput/arrayTwoNames.json",
53                 "testinput/arrayTwoNamesCopy.json",
54                 "",
55                 "",
56             ),
57             "failed! (expected 0 differences)",
58         )
59
60     def testEqualFilesWithScrambledArrayOrder(self):
61         """
62         This is moving an array element from one position to other. RFC 6902 describes this as "moving
63         a value", but this jsonpatch implementation constructs a patch using remove + add. Acceptable though
64         """
65         self.assertEquals(
66             2,
67             JsonDiffTool.Json_Diff_Check_Keyword(
68                 "testinput/arrayTwoNames.json",
69                 "testinput/arrayTwoNamesReversed.json",
70                 "",
71                 "",
72             ),
73             "failed! (expected 2 differences)",
74         )
75
76     def testEqualFilesWithChangedAttributeOrder(self):
77         """
78         Attributes in different order. It's not a difference
79         """
80         self.assertEquals(
81             0,
82             JsonDiffTool.Json_Diff_Check_Keyword(
83                 "testinput/setTwoNames.json",
84                 "testinput/setTwoNamesReversed.json",
85                 "",
86                 "",
87             ),
88             "failed! (expected 0 differences)",
89         )
90
91     def testSimpleDifferenceSecondFileWithExtraAttrib(self):
92         self.assertEquals(
93             1,
94             JsonDiffTool.Json_Diff_Check_Keyword(
95                 "testinput/setTwoNames.json",
96                 "testinput/setTwoNamesExtraAttrib.json",
97                 "",
98                 "",
99             ),
100             "failed! (expected 1 differences)",
101         )
102
103     def testSimpleDifferenceCountingWithoutFiltering(self):
104         """
105         Example coming from a true daexim export. No prefilters used
106         """
107         input_argv = [
108             "-i",
109             "testinput/mainTestCase/odl_backup_operational_before.json",
110             "-f",
111             "testinput/mainTestCase/odl_backup_operational_after.json",
112         ]
113         sys.argv[1:] = input_argv
114         self.assertEquals(
115             16,
116             JsonDiffTool.Json_Diff_Check(),
117             "main failed! expected 16 differences, result was: "
118             + str(JsonDiffTool.Json_Diff_Check()),
119         )
120
121     def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
122         """
123         Using a prefilter for the initial file The prefilter contains one expression only
124         """
125         input_argv = [
126             "-i",
127             "testinput/mainTestCase/odl_backup_operational_before.json",
128             "-f",
129             "testinput/mainTestCase/odl_backup_operational_after.json",
130             "-ipf",
131             "testinput/mainTestCase/json_prefilter.conf",
132             "-v",
133         ]
134         sys.argv[1:] = input_argv
135         self.assertEquals(
136             15,
137             JsonDiffTool.Json_Diff_Check(),
138             "main failed! expected 15 differences, result was: "
139             + str(JsonDiffTool.Json_Diff_Check()),
140         )
141
142     def testSimpleDifferenceCountingUsingMatchingBeforeFilterMatchingTwoEntries(self):
143         """
144         Using a prefilter for the initial file The prefilter contains two expressions
145         """
146         input_argv = [
147             "-i",
148             "testinput/mainTestCase/odl_backup_operational_before.json",
149             "-f",
150             "testinput/mainTestCase/odl_backup_operational_after.json",
151             "-ipf",
152             "testinput/mainTestCase/json_prefilter_two_matches.conf",
153             "-v",
154         ]
155         sys.argv[1:] = input_argv
156         self.assertEquals(
157             14,
158             JsonDiffTool.Json_Diff_Check(),
159             "main failed! expected 14 differences, result was: "
160             + str(JsonDiffTool.Json_Diff_Check()),
161         )
162
163     def testSimpleDifferenceCountingUsingSingleMatchingBeforeFilter(self):
164         """
165         Using a prefilter for both initial and final files
166         """
167         input_argv = [
168             "-i",
169             "testinput/mainTestCase/odl_backup_operational_before.json",
170             "-f",
171             "testinput/mainTestCase/odl_backup_operational_after.json",
172             "-ipf",
173             "testinput/mainTestCase/json_prefilter.conf",
174             "-fpf",
175             "testinput/mainTestCase/json_postfilter.conf",
176             "-v",
177         ]
178         sys.argv[1:] = input_argv
179         self.assertEquals(
180             16,
181             JsonDiffTool.Json_Diff_Check(),
182             "main failed! expected 16 differences, result was: "
183             + str(JsonDiffTool.Json_Diff_Check()),
184         )
185
186     def testUsingANonExistingFile(self):
187         """
188         The second file does not exist. Exception expected
189         """
190         self.assertRaises(
191             IOError,
192             JsonDiffTool.Json_Diff_Check_Keyword,
193             "testinput/arrayTwoNames.json",
194             "testinput/thisFileDoesNotExist.json",
195             "",
196             "",
197         )
198
199     def testNotPassingAMandatoryParameter(self):
200         """
201         Both initial and final json files are mandatory
202         """
203         input_argv = ["-f", "testinput/mainTestCase/odl_backup_operational_after.json"]
204         # parser = JsonDiffTool.parseArgs(input_argv)
205
206         with self.assertRaises(SystemExit) as cm:
207             JsonDiffTool.parse_args(input_argv)
208
209         # 2 for missing argument
210         self.assertEqual(cm.exception.code, 2)
211
212     def testUsingNotMatchingFilterExpressions(self):
213         """
214         Using prefilter files whose expressions match nothing
215         """
216         input_argv = [
217             "-i",
218             "testinput/mainTestCase/odl_backup_operational_before.json",
219             "-f",
220             "testinput/mainTestCase/odl_backup_operational_after.json",
221             "-ipf",
222             "testinput/mainTestCase/json_prefilter_zero_matches.conf",
223             "-fpf",
224             "testinput/mainTestCase/json_prefilter_zero_matches.conf",
225             "-v",
226         ]
227         sys.argv[1:] = input_argv
228         self.assertEquals(
229             16,
230             JsonDiffTool.Json_Diff_Check(),
231             "main failed! expected 16 differences, result was: "
232             + str(JsonDiffTool.Json_Diff_Check()),
233         )
234
235
236 if __name__ == "__main__":
237     unittest.main()