blob: 06da7ee5cf767906d2157972cfa65fc47fd152df [file] [log] [blame]
Matteo Scandolod2044a42017-08-07 16:08:28 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Matteo Scandolo67654fa2017-06-09 09:33:17 -070017import unittest
18import os
Scott Baker1f7791d2018-10-04 13:21:20 -070019from helpers import OUTPUT_DIR
20from xosgenx.generator import XOSProcessor, XOSProcessorArgs
Matteo Scandolo67654fa2017-06-09 09:33:17 -070021
22TEST_EXPECTED_OUTPUT = """
23 name: XOSModel
24 fields:
25 name:
26 type: string
27 description: "Help Name"
28 files:
29 type: string
30 description: "Help Files"
31"""
32
33BASE_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/base.xproto")
34TEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/test.xproto")
Scott Baker34dc67e2018-10-02 15:57:50 -070035FIELDTEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/fieldtest.xproto")
Scott Baker1f7791d2018-10-04 13:21:20 -070036FILTERTEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/filtertest.xproto")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070037SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto")
38VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
39TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget")
Scott Baker34dc67e2018-10-02 15:57:50 -070040FIELDTEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/fieldtest.xtarget")
Scott Baker1f7791d2018-10-04 13:21:20 -070041FILTERTEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/filtertest.xtarget")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070042SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget")
43
44TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/")
45
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080046class XOSProcessorTest(unittest.TestCase):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070047 """
48 Testing the XOS Generative Toolchain
49 """
50
51 def setUp(self):
Scott Baker7dddd512017-10-24 10:13:34 -070052 os.chdir(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), ".."))
Matteo Scandolo67654fa2017-06-09 09:33:17 -070053 filesToRemove = [f for f in os.listdir(OUTPUT_DIR)]
54 for f in filesToRemove:
55 if not f.startswith('.'):
56 os.remove(OUTPUT_DIR + '/' + f)
57
Sapan Bhatia4c835602017-07-14 01:13:17 -040058 def test_generator_custom_target_from_file(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070059 """
60 [XOS-GenX] Generate output from base.xproto
61 """
Scott Baker1f7791d2018-10-04 13:21:20 -070062 args = XOSProcessorArgs(files = [TEST_XPROTO],
63 target = TEST_TARGET)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080064 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070065 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
66
Sapan Bhatia4c835602017-07-14 01:13:17 -040067 def test_generator_custom_target_from_inputs(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070068 """
69 [XOS-GenX] Generate output from base.xproto
70 """
Scott Baker1f7791d2018-10-04 13:21:20 -070071 args = XOSProcessorArgs(inputs = open(TEST_XPROTO).read(),
72 target = TEST_TARGET)
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080073 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070074 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
75
Sapan Bhatia4c835602017-07-14 01:13:17 -040076 def test_django_with_attic(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070077 """
78 [XOS-GenX] Generate django output from test.xproto
79 """
Scott Baker1f7791d2018-10-04 13:21:20 -070080 args = XOSProcessorArgs(files = [TEST_XPROTO, VROUTER_XPROTO],
81 target = 'django.xtarget',
82 attic = TEST_ATTICS,
83 output = OUTPUT_DIR,
84 dest_extension = 'py',
85 write_to_file = 'model')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080086 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070087
88 # xosmodel has custom header attic
89 self.assertIn('from xosmodel_header import *', output['XOSModel'])
90 self.assertIn('class XOSModel(XOSBase):', output['XOSModel'])
91
92 # vrouter port use the default header
93 self.assertIn('header import *', output['VRouterPort'])
94 self.assertIn('class VRouterPort(XOSBase):', output['VRouterPort'])
95
96 #verify files
97 xosmodel = OUTPUT_DIR + '/xosmodel.py'
98 self.assertTrue(os.path.isfile(xosmodel))
99 xmf = open(xosmodel).read()
100 self.assertIn('from xosmodel_header import *', xmf)
101 self.assertIn('class XOSModel(XOSBase):', xmf)
102
103 vrouterport = OUTPUT_DIR + '/vrouterport.py'
104 self.assertTrue(os.path.isfile(vrouterport))
105 vrpf = open(vrouterport).read()
106 self.assertIn('header import *', vrpf)
107 self.assertIn('class VRouterPort(XOSBase):', vrpf)
108
Sapan Bhatia4c835602017-07-14 01:13:17 -0400109 def test_django_with_base(self):
Scott Baker1f7791d2018-10-04 13:21:20 -0700110 args = XOSProcessorArgs(files = [TEST_XPROTO, BASE_XPROTO],
111 target = 'django.xtarget',
112 attic = TEST_ATTICS,
113 output = OUTPUT_DIR,
114 dest_extension = 'py',
115 write_to_file = 'model')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800116 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700117
118 # verify files
119 xosmodel = OUTPUT_DIR + '/xosmodel.py'
120 self.assertTrue(os.path.isfile(xosmodel))
121 xmf = open(xosmodel).read()
122 self.assertIn('from xosmodel_header import *', xmf)
123 self.assertIn('class XOSModel(XOSBase):', xmf)
124
125 xosbase = OUTPUT_DIR + '/xosbase.py'
126 self.assertTrue(os.path.isfile(xosbase))
127 xbf = open(xosbase).read()
128 self.assertIn('header import *', xbf)
129 self.assertIn('class XOSBase(models.Model, PlModelMixIn):', xbf)
130
Sapan Bhatia4c835602017-07-14 01:13:17 -0400131 def test_write_multiple_files(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700132 """
133 [XOS-GenX] read multiple models as input, print one file per model
134 """
Scott Baker1f7791d2018-10-04 13:21:20 -0700135 args = XOSProcessorArgs(files = [TEST_XPROTO, VROUTER_XPROTO],
136 target = TEST_TARGET,
137 output = OUTPUT_DIR,
138 dest_extension = 'txt',
139 write_to_file = 'model')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800140 XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700141
142 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
143 self.assertEqual(len(generated_files), 2)
144
145 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
146 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
147
148 self.assertIn("name: XOSModel", xosmodel)
149 self.assertIn("name: VRouterPort", vrouterport)
150
151 def test_write_multiple_files_from_xtarget(self):
152 """
153 [XOS-GenX] read multiple models as input, print separate files based on +++
154 """
Scott Baker1f7791d2018-10-04 13:21:20 -0700155 args = XOSProcessorArgs(files = [TEST_XPROTO, VROUTER_XPROTO],
156 target = SPLIT_TARGET,
157 output = OUTPUT_DIR,
158 write_to_file = 'target')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800159 XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700160
161 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
162 self.assertEqual(len(generated_files), 2)
163
164 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
165 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
166
167 self.assertIn("name: XOSModel", xosmodel)
168 self.assertIn("name: VRouterPort", vrouterport)
169
Sapan Bhatia4c835602017-07-14 01:13:17 -0400170 def test_skip_django(self):
Scott Baker1f7791d2018-10-04 13:21:20 -0700171 args = XOSProcessorArgs(files = [SKIP_DJANGO_XPROTO],
172 target = 'django.xtarget',
173 output = OUTPUT_DIR,
174 dest_extension = 'py',
175 write_to_file = 'model')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800176 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700177
178 # should not print a file if options.skip_django = True
179 file = OUTPUT_DIR + '/user.py'
180 self.assertFalse(os.path.isfile(file))
181
182 def test_service_order(self):
Scott Baker1f7791d2018-10-04 13:21:20 -0700183 args = XOSProcessorArgs(files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO],
184 target = 'service.xtarget',
185 output = OUTPUT_DIR,
186 write_to_file = 'target')
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800187 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700188
189 model = OUTPUT_DIR + '/models.py'
190 self.assertTrue(os.path.isfile(model))
191 line_num = 0
192
193 for line in open(model).readlines():
194 line_num += 1
195 if line.find('class XOSBase(models.Model, PlModelMixIn):') >= 0:
196 base_line = line_num
197 if line.find('XOSModel(XOSBase):') >= 0:
198 xosmodel_line = line_num
199 if line.find('class VRouterPort(XOSBase):') >= 0:
200 vrouter_line = line_num
201 self.assertLess(base_line, xosmodel_line)
202 self.assertLess(xosmodel_line, vrouter_line)
203
Scott Baker34dc67e2018-10-02 15:57:50 -0700204 def test_field_numbers(self):
Scott Baker1f7791d2018-10-04 13:21:20 -0700205 args = XOSProcessorArgs(files = [FIELDTEST_XPROTO],
206 target = FIELDTEST_TARGET)
Scott Baker34dc67e2018-10-02 15:57:50 -0700207 output = XOSProcessor.process(args)
208
209 def _assert_field(modelname, fieldname, id):
210 self.assertIn("%s,%s,%s" % (modelname, fieldname, id), output)
211
212 _assert_field("Site", "id", 1)
213 _assert_field("Site", "base_field", 2)
214 _assert_field("Site", "base_field2", 3)
215 _assert_field("Site", "otherstuff_field", 102)
216 _assert_field("Site", "slice_field", 201)
217 _assert_field("Site", "slices_ids", 1002)
218
219 _assert_field("Slice", "id", 1)
220 _assert_field("Slice", "base_field", 2)
221 _assert_field("Slice", "base_field2", 3)
222 _assert_field("Slice", "slice_field", 101)
223 _assert_field("Slice", "site", 102)
224
Scott Baker1f7791d2018-10-04 13:21:20 -0700225 def test_unfiltered(self):
226 """ With no include_* args, should get all models """
227 args = XOSProcessorArgs(files = [FILTERTEST_XPROTO],
228 target = FILTERTEST_TARGET)
229 output = XOSProcessor.process(args)
230
231 self.assertEqual(output, "Model1,Model2,Model3,")
232
233 def test_filter_models(self):
234 """ Should only get models specified by include_models """
235 args = XOSProcessorArgs(files = [FILTERTEST_XPROTO],
236 target = FILTERTEST_TARGET,
237 include_models = ["Model1", "Model3"])
238 output = XOSProcessor.process(args)
239
240 self.assertEqual(output, "Model1,Model3,")
241
242 def test_filter_apps(self):
243 """ Should only get models whose apps are specified by include_apps """
244 args = XOSProcessorArgs(files = [FILTERTEST_XPROTO],
245 target = FILTERTEST_TARGET,
246 include_apps = ["core"])
247 output = XOSProcessor.process(args)
248
249 self.assertEqual(output, "Model1,Model2,")
250
251
252
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700253
254if __name__ == '__main__':
Sapan Bhatia4c835602017-07-14 01:13:17 -0400255 unittest.main()