blob: f59bbfded35aacf1586eb053676626b823fa33cb [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
20from xosgenx.generator import XOSGenerator
21
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")
35SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto")
36VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
37TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget")
38SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget")
39
40TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/")
41
42class XOSGeneratorTest(unittest.TestCase):
43 """
44 Testing the XOS Generative Toolchain
45 """
46
47 def setUp(self):
Scott Baker7dddd512017-10-24 10:13:34 -070048 os.chdir(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), ".."))
Matteo Scandolo67654fa2017-06-09 09:33:17 -070049 filesToRemove = [f for f in os.listdir(OUTPUT_DIR)]
50 for f in filesToRemove:
51 if not f.startswith('.'):
52 os.remove(OUTPUT_DIR + '/' + f)
53
Sapan Bhatia4c835602017-07-14 01:13:17 -040054 def test_generator_custom_target_from_file(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070055 """
56 [XOS-GenX] Generate output from base.xproto
57 """
58 args = FakeArgs()
59 args.files = [TEST_XPROTO]
60 args.target = TEST_TARGET
61 output = XOSGenerator.generate(args)
62 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
63
Sapan Bhatia4c835602017-07-14 01:13:17 -040064 def test_generator_custom_target_from_inputs(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070065 """
66 [XOS-GenX] Generate output from base.xproto
67 """
68 args = FakeArgs()
69 args.inputs = open(TEST_XPROTO).read()
70 args.target = TEST_TARGET
71 output = XOSGenerator.generate(args)
72 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
73
Sapan Bhatia4c835602017-07-14 01:13:17 -040074 def test_django_with_attic(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070075 """
76 [XOS-GenX] Generate django output from test.xproto
77 """
78 args = FakeArgs()
79 args.files = [TEST_XPROTO, VROUTER_XPROTO]
80 args.target = 'django.xtarget'
81 args.attic = TEST_ATTICS
82 args.output = OUTPUT_DIR
83 args.dest_extension = 'py'
84 args.write_to_file = 'model'
85 output = XOSGenerator.generate(args)
86
87 # xosmodel has custom header attic
88 self.assertIn('from xosmodel_header import *', output['XOSModel'])
89 self.assertIn('class XOSModel(XOSBase):', output['XOSModel'])
90
91 # vrouter port use the default header
92 self.assertIn('header import *', output['VRouterPort'])
93 self.assertIn('class VRouterPort(XOSBase):', output['VRouterPort'])
94
95 #verify files
96 xosmodel = OUTPUT_DIR + '/xosmodel.py'
97 self.assertTrue(os.path.isfile(xosmodel))
98 xmf = open(xosmodel).read()
99 self.assertIn('from xosmodel_header import *', xmf)
100 self.assertIn('class XOSModel(XOSBase):', xmf)
101
102 vrouterport = OUTPUT_DIR + '/vrouterport.py'
103 self.assertTrue(os.path.isfile(vrouterport))
104 vrpf = open(vrouterport).read()
105 self.assertIn('header import *', vrpf)
106 self.assertIn('class VRouterPort(XOSBase):', vrpf)
107
Sapan Bhatia4c835602017-07-14 01:13:17 -0400108 def test_django_with_base(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700109 args = FakeArgs()
110 args.files = [TEST_XPROTO, BASE_XPROTO]
111 args.target = 'django.xtarget'
112 args.attic = TEST_ATTICS
113 args.output = OUTPUT_DIR
114 args.dest_extension = 'py'
115 args.write_to_file = 'model'
116 output = XOSGenerator.generate(args)
117
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 """
135 args = FakeArgs()
136 args.files = [TEST_XPROTO, VROUTER_XPROTO]
137 args.target = TEST_TARGET
138 args.output = OUTPUT_DIR
139 args.dest_extension = 'txt'
140 args.write_to_file = 'model'
141 XOSGenerator.generate(args)
142
143 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
144 self.assertEqual(len(generated_files), 2)
145
146 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
147 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
148
149 self.assertIn("name: XOSModel", xosmodel)
150 self.assertIn("name: VRouterPort", vrouterport)
151
152 def test_write_multiple_files_from_xtarget(self):
153 """
154 [XOS-GenX] read multiple models as input, print separate files based on +++
155 """
156 args = FakeArgs()
157 args.files = [TEST_XPROTO, VROUTER_XPROTO]
158 args.target = SPLIT_TARGET
159 args.output = OUTPUT_DIR
160 args.write_to_file = 'target'
161 XOSGenerator.generate(args)
162
163 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
164 self.assertEqual(len(generated_files), 2)
165
166 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
167 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
168
169 self.assertIn("name: XOSModel", xosmodel)
170 self.assertIn("name: VRouterPort", vrouterport)
171
Sapan Bhatia4c835602017-07-14 01:13:17 -0400172 def test_skip_django(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700173 args = FakeArgs()
174 args.files = [SKIP_DJANGO_XPROTO]
175 args.target = 'django.xtarget'
176 args.output = OUTPUT_DIR
177 args.dest_extension = 'py'
178 args.write_to_file = 'model'
179 output = XOSGenerator.generate(args)
180
181 # should not print a file if options.skip_django = True
182 file = OUTPUT_DIR + '/user.py'
183 self.assertFalse(os.path.isfile(file))
184
185 def test_service_order(self):
186 args = FakeArgs()
187 args.files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO]
188 args.target = 'service.xtarget'
189 args.output = OUTPUT_DIR
190 args.write_to_file = 'target'
191 output = XOSGenerator.generate(args)
192
193 model = OUTPUT_DIR + '/models.py'
194 self.assertTrue(os.path.isfile(model))
195 line_num = 0
196
197 for line in open(model).readlines():
198 line_num += 1
199 if line.find('class XOSBase(models.Model, PlModelMixIn):') >= 0:
200 base_line = line_num
201 if line.find('XOSModel(XOSBase):') >= 0:
202 xosmodel_line = line_num
203 if line.find('class VRouterPort(XOSBase):') >= 0:
204 vrouter_line = line_num
205 self.assertLess(base_line, xosmodel_line)
206 self.assertLess(xosmodel_line, vrouter_line)
207
208
209if __name__ == '__main__':
Sapan Bhatia4c835602017-07-14 01:13:17 -0400210 unittest.main()