blob: 1a81f8ac28888a523d66389e9c54de1c3bb4e7e7 [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
19from helpers import FakeArgs, OUTPUT_DIR
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080020from xosgenx.generator import XOSProcessor
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")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070036SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto")
37VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
38TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget")
Scott Baker34dc67e2018-10-02 15:57:50 -070039FIELDTEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/fieldtest.xtarget")
Matteo Scandolo67654fa2017-06-09 09:33:17 -070040SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget")
41
42TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/")
43
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080044class XOSProcessorTest(unittest.TestCase):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070045 """
46 Testing the XOS Generative Toolchain
47 """
48
49 def setUp(self):
Scott Baker7dddd512017-10-24 10:13:34 -070050 os.chdir(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), ".."))
Matteo Scandolo67654fa2017-06-09 09:33:17 -070051 filesToRemove = [f for f in os.listdir(OUTPUT_DIR)]
52 for f in filesToRemove:
53 if not f.startswith('.'):
54 os.remove(OUTPUT_DIR + '/' + f)
55
Sapan Bhatia4c835602017-07-14 01:13:17 -040056 def test_generator_custom_target_from_file(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070057 """
58 [XOS-GenX] Generate output from base.xproto
59 """
60 args = FakeArgs()
61 args.files = [TEST_XPROTO]
62 args.target = TEST_TARGET
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080063 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
65
Sapan Bhatia4c835602017-07-14 01:13:17 -040066 def test_generator_custom_target_from_inputs(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070067 """
68 [XOS-GenX] Generate output from base.xproto
69 """
70 args = FakeArgs()
71 args.inputs = open(TEST_XPROTO).read()
72 args.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 """
80 args = FakeArgs()
81 args.files = [TEST_XPROTO, VROUTER_XPROTO]
82 args.target = 'django.xtarget'
83 args.attic = TEST_ATTICS
84 args.output = OUTPUT_DIR
85 args.dest_extension = 'py'
86 args.write_to_file = 'model'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -080087 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -070088
89 # xosmodel has custom header attic
90 self.assertIn('from xosmodel_header import *', output['XOSModel'])
91 self.assertIn('class XOSModel(XOSBase):', output['XOSModel'])
92
93 # vrouter port use the default header
94 self.assertIn('header import *', output['VRouterPort'])
95 self.assertIn('class VRouterPort(XOSBase):', output['VRouterPort'])
96
97 #verify files
98 xosmodel = OUTPUT_DIR + '/xosmodel.py'
99 self.assertTrue(os.path.isfile(xosmodel))
100 xmf = open(xosmodel).read()
101 self.assertIn('from xosmodel_header import *', xmf)
102 self.assertIn('class XOSModel(XOSBase):', xmf)
103
104 vrouterport = OUTPUT_DIR + '/vrouterport.py'
105 self.assertTrue(os.path.isfile(vrouterport))
106 vrpf = open(vrouterport).read()
107 self.assertIn('header import *', vrpf)
108 self.assertIn('class VRouterPort(XOSBase):', vrpf)
109
Sapan Bhatia4c835602017-07-14 01:13:17 -0400110 def test_django_with_base(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700111 args = FakeArgs()
112 args.files = [TEST_XPROTO, BASE_XPROTO]
113 args.target = 'django.xtarget'
114 args.attic = TEST_ATTICS
115 args.output = OUTPUT_DIR
116 args.dest_extension = 'py'
117 args.write_to_file = 'model'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800118 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700119
120 # verify files
121 xosmodel = OUTPUT_DIR + '/xosmodel.py'
122 self.assertTrue(os.path.isfile(xosmodel))
123 xmf = open(xosmodel).read()
124 self.assertIn('from xosmodel_header import *', xmf)
125 self.assertIn('class XOSModel(XOSBase):', xmf)
126
127 xosbase = OUTPUT_DIR + '/xosbase.py'
128 self.assertTrue(os.path.isfile(xosbase))
129 xbf = open(xosbase).read()
130 self.assertIn('header import *', xbf)
131 self.assertIn('class XOSBase(models.Model, PlModelMixIn):', xbf)
132
Sapan Bhatia4c835602017-07-14 01:13:17 -0400133 def test_write_multiple_files(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700134 """
135 [XOS-GenX] read multiple models as input, print one file per model
136 """
137 args = FakeArgs()
138 args.files = [TEST_XPROTO, VROUTER_XPROTO]
139 args.target = TEST_TARGET
140 args.output = OUTPUT_DIR
141 args.dest_extension = 'txt'
142 args.write_to_file = 'model'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800143 XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700144
145 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
146 self.assertEqual(len(generated_files), 2)
147
148 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
149 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
150
151 self.assertIn("name: XOSModel", xosmodel)
152 self.assertIn("name: VRouterPort", vrouterport)
153
154 def test_write_multiple_files_from_xtarget(self):
155 """
156 [XOS-GenX] read multiple models as input, print separate files based on +++
157 """
158 args = FakeArgs()
159 args.files = [TEST_XPROTO, VROUTER_XPROTO]
160 args.target = SPLIT_TARGET
161 args.output = OUTPUT_DIR
162 args.write_to_file = 'target'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800163 XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700164
165 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
166 self.assertEqual(len(generated_files), 2)
167
168 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
169 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
170
171 self.assertIn("name: XOSModel", xosmodel)
172 self.assertIn("name: VRouterPort", vrouterport)
173
Sapan Bhatia4c835602017-07-14 01:13:17 -0400174 def test_skip_django(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700175 args = FakeArgs()
176 args.files = [SKIP_DJANGO_XPROTO]
177 args.target = 'django.xtarget'
178 args.output = OUTPUT_DIR
179 args.dest_extension = 'py'
180 args.write_to_file = 'model'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800181 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700182
183 # should not print a file if options.skip_django = True
184 file = OUTPUT_DIR + '/user.py'
185 self.assertFalse(os.path.isfile(file))
186
187 def test_service_order(self):
188 args = FakeArgs()
189 args.files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO]
190 args.target = 'service.xtarget'
191 args.output = OUTPUT_DIR
192 args.write_to_file = 'target'
Sapan Bhatiabfb233a2018-02-09 14:53:09 -0800193 output = XOSProcessor.process(args)
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700194
195 model = OUTPUT_DIR + '/models.py'
196 self.assertTrue(os.path.isfile(model))
197 line_num = 0
198
199 for line in open(model).readlines():
200 line_num += 1
201 if line.find('class XOSBase(models.Model, PlModelMixIn):') >= 0:
202 base_line = line_num
203 if line.find('XOSModel(XOSBase):') >= 0:
204 xosmodel_line = line_num
205 if line.find('class VRouterPort(XOSBase):') >= 0:
206 vrouter_line = line_num
207 self.assertLess(base_line, xosmodel_line)
208 self.assertLess(xosmodel_line, vrouter_line)
209
Scott Baker34dc67e2018-10-02 15:57:50 -0700210 def test_field_numbers(self):
211 args = FakeArgs()
212 args.files = [FIELDTEST_XPROTO]
213 args.target = FIELDTEST_TARGET
214 output = XOSProcessor.process(args)
215
216 def _assert_field(modelname, fieldname, id):
217 self.assertIn("%s,%s,%s" % (modelname, fieldname, id), output)
218
219 _assert_field("Site", "id", 1)
220 _assert_field("Site", "base_field", 2)
221 _assert_field("Site", "base_field2", 3)
222 _assert_field("Site", "otherstuff_field", 102)
223 _assert_field("Site", "slice_field", 201)
224 _assert_field("Site", "slices_ids", 1002)
225
226 _assert_field("Slice", "id", 1)
227 _assert_field("Slice", "base_field", 2)
228 _assert_field("Slice", "base_field2", 3)
229 _assert_field("Slice", "slice_field", 101)
230 _assert_field("Slice", "site", 102)
231
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700232
233if __name__ == '__main__':
Sapan Bhatia4c835602017-07-14 01:13:17 -0400234 unittest.main()