blob: a13fb384306eac7ed5865e0984b4f08a6618d261 [file] [log] [blame]
Matteo Scandolo67654fa2017-06-09 09:33:17 -07001import unittest
2import os
3from helpers import FakeArgs, OUTPUT_DIR
4from xosgenx.generator import XOSGenerator
5
6TEST_EXPECTED_OUTPUT = """
7 name: XOSModel
8 fields:
9 name:
10 type: string
11 description: "Help Name"
12 files:
13 type: string
14 description: "Help Files"
15"""
16
17BASE_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/base.xproto")
18TEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/test.xproto")
19SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto")
20VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto")
21TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget")
22SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget")
23
24TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/")
25
26class XOSGeneratorTest(unittest.TestCase):
27 """
28 Testing the XOS Generative Toolchain
29 """
30
31 def setUp(self):
32 filesToRemove = [f for f in os.listdir(OUTPUT_DIR)]
33 for f in filesToRemove:
34 if not f.startswith('.'):
35 os.remove(OUTPUT_DIR + '/' + f)
36
37 def _test_generator_custom_target_from_file(self):
38 """
39 [XOS-GenX] Generate output from base.xproto
40 """
41 args = FakeArgs()
42 args.files = [TEST_XPROTO]
43 args.target = TEST_TARGET
44 output = XOSGenerator.generate(args)
45 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
46
47 def _test_generator_custom_target_from_inputs(self):
48 """
49 [XOS-GenX] Generate output from base.xproto
50 """
51 args = FakeArgs()
52 args.inputs = open(TEST_XPROTO).read()
53 args.target = TEST_TARGET
54 output = XOSGenerator.generate(args)
55 self.assertEqual(output, TEST_EXPECTED_OUTPUT)
56
57 def _test_generator_tosca(self):
58 """
59 [XOS-GenX] Generate TOSCA from base.xproto, and write to file
60 """
61 args = FakeArgs()
62 args.files = [TEST_XPROTO, VROUTER_XPROTO]
63 args.target = 'tosca.xtarget'
64 args.output = OUTPUT_DIR
65 args.write_to_file = 'single'
66 args.dest_file = 'base.yaml'
67 XOSGenerator.generate(args)
68
69 dest_file = OUTPUT_DIR + '/' + args.dest_file
70 self.assertTrue(os.path.isfile(dest_file))
71
72 output = open(dest_file, "r").read()
73 self.assertIn("tosca.nodes.XOSModel", output)
74 self.assertIn("tosca.nodes.VRouterPort", output)
75
76 def _test_django_with_attic(self):
77 """
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'
87 output = XOSGenerator.generate(args)
88
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
110 def _test_django_with_base(self):
111 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'
118 output = XOSGenerator.generate(args)
119
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
133 def _test_write_multiple_files(self):
134 """
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'
143 XOSGenerator.generate(args)
144
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'
163 XOSGenerator.generate(args)
164
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
174 def _test_skip_django(self):
175 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'
181 output = XOSGenerator.generate(args)
182
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'
193 output = XOSGenerator.generate(args)
194
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
210
211if __name__ == '__main__':
212 unittest.main()