Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 17 | import unittest |
| 18 | import os |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 19 | from helpers import OUTPUT_DIR |
| 20 | from xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 21 | |
| 22 | TEST_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 | |
| 33 | BASE_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/base.xproto") |
| 34 | TEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/test.xproto") |
Scott Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 35 | FIELDTEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/fieldtest.xproto") |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 36 | FILTERTEST_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/filtertest.xproto") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 37 | SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto") |
| 38 | VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto") |
| 39 | TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget") |
Scott Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 40 | FIELDTEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/fieldtest.xtarget") |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 41 | FILTERTEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/filtertest.xtarget") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 42 | SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget") |
| 43 | |
| 44 | TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/") |
| 45 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 46 | class XOSProcessorTest(unittest.TestCase): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 47 | """ |
| 48 | Testing the XOS Generative Toolchain |
| 49 | """ |
| 50 | |
| 51 | def setUp(self): |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 52 | os.chdir(os.path.join(os.path.abspath(os.path.dirname(os.path.realpath(__file__))), "..")) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 53 | 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 58 | def test_generator_custom_target_from_file(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 59 | """ |
| 60 | [XOS-GenX] Generate output from base.xproto |
| 61 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 62 | args = XOSProcessorArgs(files = [TEST_XPROTO], |
| 63 | target = TEST_TARGET) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 64 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 65 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 66 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 67 | def test_generator_custom_target_from_inputs(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 68 | """ |
| 69 | [XOS-GenX] Generate output from base.xproto |
| 70 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 71 | args = XOSProcessorArgs(inputs = open(TEST_XPROTO).read(), |
| 72 | target = TEST_TARGET) |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 73 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 74 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 75 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 76 | def test_django_with_attic(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 77 | """ |
| 78 | [XOS-GenX] Generate django output from test.xproto |
| 79 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 80 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 86 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 87 | |
| 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 109 | def test_django_with_base(self): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 110 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 116 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 131 | def test_write_multiple_files(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 132 | """ |
| 133 | [XOS-GenX] read multiple models as input, print one file per model |
| 134 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 135 | 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 140 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 155 | args = XOSProcessorArgs(files = [TEST_XPROTO, VROUTER_XPROTO], |
| 156 | target = SPLIT_TARGET, |
| 157 | output = OUTPUT_DIR, |
| 158 | write_to_file = 'target') |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 159 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 160 | |
| 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 170 | def test_skip_django(self): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 171 | args = XOSProcessorArgs(files = [SKIP_DJANGO_XPROTO], |
| 172 | target = 'django.xtarget', |
| 173 | output = OUTPUT_DIR, |
| 174 | dest_extension = 'py', |
| 175 | write_to_file = 'model') |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 176 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 177 | |
| 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 183 | args = XOSProcessorArgs(files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO], |
| 184 | target = 'service.xtarget', |
| 185 | output = OUTPUT_DIR, |
| 186 | write_to_file = 'target') |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 187 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 188 | |
| 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 Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 204 | def test_field_numbers(self): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 205 | args = XOSProcessorArgs(files = [FIELDTEST_XPROTO], |
| 206 | target = FIELDTEST_TARGET) |
Scott Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 207 | 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 Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 225 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 253 | |
| 254 | if __name__ == '__main__': |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 255 | unittest.main() |