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") |
| 35 | SKIP_DJANGO_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/skip_django.xproto") |
| 36 | VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto") |
| 37 | TEST_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/test.xtarget") |
| 38 | SPLIT_TARGET = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xtarget/split.xtarget") |
| 39 | |
| 40 | TEST_ATTICS = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/attics/") |
| 41 | |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 42 | class XOSProcessorTest(unittest.TestCase): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 43 | """ |
| 44 | Testing the XOS Generative Toolchain |
| 45 | """ |
| 46 | |
| 47 | def setUp(self): |
Scott Baker | 7dddd51 | 2017-10-24 10:13:34 -0700 | [diff] [blame] | 48 | 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] | 49 | 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 54 | def test_generator_custom_target_from_file(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 55 | """ |
| 56 | [XOS-GenX] Generate output from base.xproto |
| 57 | """ |
| 58 | args = FakeArgs() |
| 59 | args.files = [TEST_XPROTO] |
| 60 | args.target = TEST_TARGET |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 61 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 62 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 63 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 64 | def test_generator_custom_target_from_inputs(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 65 | """ |
| 66 | [XOS-GenX] Generate output from base.xproto |
| 67 | """ |
| 68 | args = FakeArgs() |
| 69 | args.inputs = open(TEST_XPROTO).read() |
| 70 | args.target = TEST_TARGET |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 71 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 72 | self.assertEqual(output, TEST_EXPECTED_OUTPUT) |
| 73 | |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 74 | def test_django_with_attic(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 75 | """ |
| 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 85 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 108 | def test_django_with_base(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 109 | 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' |
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 | """ |
| 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 141 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 161 | XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 172 | def test_skip_django(self): |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 173 | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 179 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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' |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 191 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 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 | |
| 209 | if __name__ == '__main__': |
Sapan Bhatia | 4c83560 | 2017-07-14 01:13:17 -0400 | [diff] [blame] | 210 | unittest.main() |