blob: 0e7e337944ec8ab393b8e89a3aea75feb861c7cb [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):
48 filesToRemove = [f for f in os.listdir(OUTPUT_DIR)]
49 for f in filesToRemove:
50 if not f.startswith('.'):
51 os.remove(OUTPUT_DIR + '/' + f)
52
Sapan Bhatia4c835602017-07-14 01:13:17 -040053 def test_generator_custom_target_from_file(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070054 """
55 [XOS-GenX] Generate output from base.xproto
56 """
57 args = FakeArgs()
58 args.files = [TEST_XPROTO]
59 args.target = TEST_TARGET
60 output = XOSGenerator.generate(args)
61 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
62
Sapan Bhatia4c835602017-07-14 01:13:17 -040063 def test_generator_custom_target_from_inputs(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070064 """
65 [XOS-GenX] Generate output from base.xproto
66 """
67 args = FakeArgs()
68 args.inputs = open(TEST_XPROTO).read()
69 args.target = TEST_TARGET
70 output = XOSGenerator.generate(args)
71 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
72
Sapan Bhatia4c835602017-07-14 01:13:17 -040073 def test_django_with_attic(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -070074 """
75 [XOS-GenX] Generate django output from test.xproto
76 """
77 args = FakeArgs()
78 args.files = [TEST_XPROTO, VROUTER_XPROTO]
79 args.target = 'django.xtarget'
80 args.attic = TEST_ATTICS
81 args.output = OUTPUT_DIR
82 args.dest_extension = 'py'
83 args.write_to_file = 'model'
84 output = XOSGenerator.generate(args)
85
86 # xosmodel has custom header attic
87 self.assertIn('from xosmodel_header import *', output['XOSModel'])
88 self.assertIn('class XOSModel(XOSBase):', output['XOSModel'])
89
90 # vrouter port use the default header
91 self.assertIn('header import *', output['VRouterPort'])
92 self.assertIn('class VRouterPort(XOSBase):', output['VRouterPort'])
93
94 #verify files
95 xosmodel = OUTPUT_DIR + '/xosmodel.py'
96 self.assertTrue(os.path.isfile(xosmodel))
97 xmf = open(xosmodel).read()
98 self.assertIn('from xosmodel_header import *', xmf)
99 self.assertIn('class XOSModel(XOSBase):', xmf)
100
101 vrouterport = OUTPUT_DIR + '/vrouterport.py'
102 self.assertTrue(os.path.isfile(vrouterport))
103 vrpf = open(vrouterport).read()
104 self.assertIn('header import *', vrpf)
105 self.assertIn('class VRouterPort(XOSBase):', vrpf)
106
Sapan Bhatia4c835602017-07-14 01:13:17 -0400107 def test_django_with_base(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700108 args = FakeArgs()
109 args.files = [TEST_XPROTO, BASE_XPROTO]
110 args.target = 'django.xtarget'
111 args.attic = TEST_ATTICS
112 args.output = OUTPUT_DIR
113 args.dest_extension = 'py'
114 args.write_to_file = 'model'
115 output = XOSGenerator.generate(args)
116
117 # verify files
118 xosmodel = OUTPUT_DIR + '/xosmodel.py'
119 self.assertTrue(os.path.isfile(xosmodel))
120 xmf = open(xosmodel).read()
121 self.assertIn('from xosmodel_header import *', xmf)
122 self.assertIn('class XOSModel(XOSBase):', xmf)
123
124 xosbase = OUTPUT_DIR + '/xosbase.py'
125 self.assertTrue(os.path.isfile(xosbase))
126 xbf = open(xosbase).read()
127 self.assertIn('header import *', xbf)
128 self.assertIn('class XOSBase(models.Model, PlModelMixIn):', xbf)
129
Sapan Bhatia4c835602017-07-14 01:13:17 -0400130 def test_write_multiple_files(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700131 """
132 [XOS-GenX] read multiple models as input, print one file per model
133 """
134 args = FakeArgs()
135 args.files = [TEST_XPROTO, VROUTER_XPROTO]
136 args.target = TEST_TARGET
137 args.output = OUTPUT_DIR
138 args.dest_extension = 'txt'
139 args.write_to_file = 'model'
140 XOSGenerator.generate(args)
141
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 """
155 args = FakeArgs()
156 args.files = [TEST_XPROTO, VROUTER_XPROTO]
157 args.target = SPLIT_TARGET
158 args.output = OUTPUT_DIR
159 args.write_to_file = 'target'
160 XOSGenerator.generate(args)
161
162 generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')]
163 self.assertEqual(len(generated_files), 2)
164
165 xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read()
166 vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read()
167
168 self.assertIn("name: XOSModel", xosmodel)
169 self.assertIn("name: VRouterPort", vrouterport)
170
Sapan Bhatia4c835602017-07-14 01:13:17 -0400171 def test_skip_django(self):
Matteo Scandolo67654fa2017-06-09 09:33:17 -0700172 args = FakeArgs()
173 args.files = [SKIP_DJANGO_XPROTO]
174 args.target = 'django.xtarget'
175 args.output = OUTPUT_DIR
176 args.dest_extension = 'py'
177 args.write_to_file = 'model'
178 output = XOSGenerator.generate(args)
179
180 # should not print a file if options.skip_django = True
181 file = OUTPUT_DIR + '/user.py'
182 self.assertFalse(os.path.isfile(file))
183
184 def test_service_order(self):
185 args = FakeArgs()
186 args.files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO]
187 args.target = 'service.xtarget'
188 args.output = OUTPUT_DIR
189 args.write_to_file = 'target'
190 output = XOSGenerator.generate(args)
191
192 model = OUTPUT_DIR + '/models.py'
193 self.assertTrue(os.path.isfile(model))
194 line_num = 0
195
196 for line in open(model).readlines():
197 line_num += 1
198 if line.find('class XOSBase(models.Model, PlModelMixIn):') >= 0:
199 base_line = line_num
200 if line.find('XOSModel(XOSBase):') >= 0:
201 xosmodel_line = line_num
202 if line.find('class VRouterPort(XOSBase):') >= 0:
203 vrouter_line = line_num
204 self.assertLess(base_line, xosmodel_line)
205 self.assertLess(xosmodel_line, vrouter_line)
206
207
208if __name__ == '__main__':
Sapan Bhatia4c835602017-07-14 01:13:17 -0400209 unittest.main()