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 |
| 19 | from helpers import FakeArgs, OUTPUT_DIR |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 20 | from xosgenx.generator import XOSProcessor |
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") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 36 | SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto") |
| 37 | VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto") |
| 38 | 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] | 39 | FIELDTEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/fieldtest.xtarget") |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 40 | SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget") |
| 41 | |
| 42 | TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/") |
| 43 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 44 | class XOSProcessorTest(unittest.TestCase): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 45 | """ |
| 46 | Testing the XOS Generative Toolchain |
| 47 | """ |
| 48 | |
| 49 | def setUp(self): |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 50 | 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] | 51 | 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 56 | def test_generator_custom_target_from_file(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 57 | """ |
| 58 | [XOS-GenX] Generate output from base.xproto |
| 59 | """ |
| 60 | args = FakeArgs() |
| 61 | args.files = [TEST_XPROTO] |
| 62 | args.target = TEST_TARGET |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 63 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 64 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 65 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 66 | def test_generator_custom_target_from_inputs(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 67 | """ |
| 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 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 | """ |
| 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 Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 87 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 110 | def test_django_with_base(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 118 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 133 | def test_write_multiple_files(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 143 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 163 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 174 | def test_skip_django(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 181 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 193 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | |
Scott Baker | 34dc67e | 2018-10-02 15:57:50 -0700 | [diff] [blame] | 210 | 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 Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 232 | |
| 233 | if __name__ == '__main__': |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 234 | unittest.main() |