Matteo Scandolo | d2044a4 | 2017-08-07 16:08:28 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 16 | from __future__ import absolute_import |
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 xosgenx.generator import XOSProcessor, XOSProcessorArgs |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 20 | import yaml |
| 21 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 22 | PROTO_EXPECTED_OUTPUT = """ |
| 23 | message VRouterPort { |
| 24 | option bases = "XOSBase"; |
| 25 | optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = ""port friendly name"", modifier = "optional", db_index = "False" ]; |
| 26 | required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = ""port identifier in ONOS"", modifier = "required", db_index = "False" ]; |
| 27 | required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", link_type = "manytoone", db_index = "True" ]; |
| 28 | required int32 vrouter_service = 4 [ null = "False", blank = "False", model = "VRouterService", modifier = "required", type = "link", port = "device_ports", link_type = "manytoone", db_index = "True" ]; |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 29 | } |
| 30 | """ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 31 | VROUTER_XPROTO = os.path.abspath( |
| 32 | os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto" |
| 33 | ) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 34 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 35 | # Generate other formats from xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 36 | |
| 37 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 38 | class XProtoTranslatorTest(unittest.TestCase): |
| 39 | def _test_proto_generator(self): |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 40 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 41 | args.files = [VROUTER_XPROTO] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 42 | args.target = "proto.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 43 | output = XOSProcessor.process(args) |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 44 | self.assertEqual(output, PROTO_EXPECTED_OUTPUT) |
| 45 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 46 | def test_yaml_generator(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | xproto = """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 48 | option app_label = "test"; |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 49 | |
| 50 | message Port (PlCoreBase,ParameterMixin){ |
| 51 | required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False]; |
| 52 | optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True]; |
| 53 | optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 54 | optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True]; |
| 55 | optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True]; |
| 56 | required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True]; |
| 57 | } |
| 58 | |
| 59 | message Instance (PlCoreBase){ |
| 60 | optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; |
| 61 | optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; |
| 62 | required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; |
| 63 | optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; |
| 64 | optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 65 | required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; |
| 66 | optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; |
| 67 | required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; |
| 68 | required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; |
| 69 | required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; |
| 70 | required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; |
| 71 | required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False]; |
| 72 | optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True]; |
| 73 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 74 | optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; |
| 75 | optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; |
| 76 | required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True]; |
| 77 | } |
| 78 | |
| 79 | message Network (PlCoreBase,ParameterMixin) { |
| 80 | required string name = 1 [db_index = False, max_length = 32, null = False, blank = False]; |
| 81 | required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False]; |
| 82 | required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True]; |
| 83 | required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True]; |
| 84 | required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True]; |
| 85 | optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 86 | optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 87 | required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False]; |
| 88 | required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False]; |
| 89 | required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True]; |
| 90 | optional string topology_parameters = 11 [db_index = False, null = True, blank = True]; |
| 91 | optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 92 | optional string controller_parameters = 13 [db_index = False, null = True, blank = True]; |
| 93 | optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True]; |
| 94 | optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True]; |
| 95 | optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True]; |
| 96 | required bool autoconnect = 17 [help_text = "This network can be autoconnected to the slice that owns it", default = True, null = False, db_index = False, blank = True]; |
| 97 | required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True]; |
| 98 | required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True]; |
| 99 | required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True]; |
| 100 | } |
| 101 | |
| 102 | message Slice (PlCoreBase){ |
| 103 | required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False]; |
| 104 | required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True]; |
| 105 | required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True]; |
| 106 | required string description = 4 [help_text = "High level description of the slice and expected activities", max_length = 1024, null = False, db_index = False, blank = True]; |
| 107 | required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True]; |
| 108 | required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False]; |
| 109 | required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False]; |
| 110 | optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True]; |
| 111 | optional string network = 9 [blank = True, max_length = 256, null = True, db_index = False, choices = "((None, 'Default'), ('host', 'Host'), ('bridged', 'Bridged'), ('noauto', 'No Automatic Networks'))"]; |
| 112 | optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True]; |
| 113 | optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True]; |
| 114 | optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True]; |
| 115 | optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True]; |
| 116 | optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True]; |
| 117 | optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True]; |
| 118 | optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False]; |
| 119 | required string default_isolation = 17 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 120 | required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True]; |
| 121 | } |
| 122 | """ |
| 123 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 124 | args = XOSProcessorArgs() |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 125 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 126 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 127 | output = XOSProcessor.process(args) |
Zack Williams | be5ee1c | 2019-03-18 15:33:07 -0700 | [diff] [blame] | 128 | yaml_ir = yaml.safe_load(output) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 129 | self.assertEqual(len(yaml_ir["items"]), 4) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 130 | |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 131 | def test_gui_hidden_models(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 132 | xproto = """ |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 133 | option app_label = "test"; |
| 134 | |
| 135 | message Foo { |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 136 | option gui_hidden = True; |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 137 | required string name = 1 [ null = "False", blank="False"]; |
| 138 | } |
| 139 | |
| 140 | message Bar { |
| 141 | option gui_hidden = "False"; |
| 142 | required string name = 1 [ null = "False", blank="False"]; |
| 143 | } |
| 144 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 145 | args = XOSProcessorArgs() |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 146 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 147 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 148 | output = XOSProcessor.process(args) |
Zack Williams | be5ee1c | 2019-03-18 15:33:07 -0700 | [diff] [blame] | 149 | yaml_ir = yaml.safe_load(output) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 150 | self.assertEqual(len(yaml_ir["items"]), 1) |
| 151 | self.assertIn("Bar", output) |
| 152 | self.assertNotIn("Foo", output) |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 153 | |
| 154 | def test_gui_hidden_model_fields(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 155 | xproto = """ |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 156 | option app_label = "test"; |
| 157 | |
| 158 | message Foo { |
| 159 | required string name = 1 [ null = "False", blank="False"]; |
| 160 | required string secret = 1 [ null = "False", blank="False", gui_hidden = "True"]; |
| 161 | } |
| 162 | """ |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 163 | args = XOSProcessorArgs() |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 164 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 165 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 166 | output = XOSProcessor.process(args) |
Zack Williams | be5ee1c | 2019-03-18 15:33:07 -0700 | [diff] [blame] | 167 | yaml_ir = yaml.safe_load(output) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 168 | self.assertEqual(len(yaml_ir["items"]), 1) |
| 169 | self.assertIn("name", output) |
| 170 | self.assertNotIn("secret", output) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 171 | |
| 172 | def test_static_options(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 173 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 174 | option app_label = "test"; |
| 175 | |
| 176 | message Foo { |
| 177 | required string name = 1 [ null = "False", blank="False"]; |
| 178 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 179 | } |
| 180 | """ |
| 181 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 182 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 183 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 184 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 185 | output = XOSProcessor.process(args) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 186 | self.assertIn("options:", output) |
| 187 | self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output) |
| 188 | |
| 189 | def test_not_static_options(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 190 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 191 | option app_label = "test"; |
| 192 | |
| 193 | message Foo { |
| 194 | required string name = 1 [ null = "False", blank="False"]; |
| 195 | } |
| 196 | """ |
| 197 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 198 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 199 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 200 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 201 | output = XOSProcessor.process(args) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 202 | self.assertNotIn("options:", output) |
| 203 | |
| 204 | def test_default_value_in_modeldef(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 205 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 206 | option app_label = "test"; |
| 207 | |
| 208 | message Foo { |
| 209 | required string name = 1 [ null = "False", blank="False", default = "bar"]; |
| 210 | required string falsetrue = 1 [ null = "False", blank="False", default = False]; |
| 211 | required string truefalse = 1 [ null = "False", blank="False", default = True]; |
| 212 | required string some = 1 [ null = "False", blank="False", default = None]; |
| 213 | required string zero = 1 [ null = "False", blank="False", default = 0]; |
| 214 | } |
| 215 | """ |
| 216 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 217 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 218 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 219 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 220 | output = XOSProcessor.process(args) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 221 | self.assertIn('default: "bar"', output) |
| 222 | self.assertIn('default: "false"', output) |
| 223 | self.assertIn('default: "true"', output) |
| 224 | self.assertIn('default: "null"', output) |
| 225 | self.assertIn('default: "0"', output) |
| 226 | |
| 227 | def test_not_default_value_in_modeldef(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 228 | xproto = """ |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 229 | option app_label = "test"; |
| 230 | |
| 231 | message Foo { |
| 232 | required string name = 1 [ null = "False", blank="False"]; |
| 233 | } |
| 234 | """ |
| 235 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 236 | args = XOSProcessorArgs() |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 237 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 238 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 239 | output = XOSProcessor.process(args) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 240 | self.assertNotIn("default:", output) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 241 | |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 242 | def test_one_to_many_in_modeldef(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 243 | xproto = """ |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 244 | option app_label = "test"; |
| 245 | |
| 246 | message ServiceDependency { |
| 247 | required manytoone provider_service->Service:provided_dependencies = 1; |
| 248 | required manytoone subscriber_service->Service:subscribed_dependencies = 2; |
| 249 | } |
| 250 | |
| 251 | message Service { |
| 252 | required string name = 1; |
| 253 | } |
| 254 | """ |
| 255 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 256 | args = XOSProcessorArgs() |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 257 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 258 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 259 | output = XOSProcessor.process(args) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 260 | # Service deps model |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 261 | self.assertIn( |
| 262 | "{model: Service, type: manytoone, on_field: provider_service}", output |
| 263 | ) |
| 264 | self.assertIn( |
| 265 | "{model: Service, type: manytoone, on_field: provider_service}", output |
| 266 | ) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 267 | |
| 268 | # Service model |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 269 | self.assertIn( |
| 270 | "{model: ServiceDependency, type: onetomany, on_field: provider_service}", |
| 271 | output, |
| 272 | ) |
| 273 | self.assertIn( |
| 274 | "{model: ServiceDependency, type: onetomany, on_field: provider_service}", |
| 275 | output, |
| 276 | ) |
Matteo Scandolo | 1f826a4 | 2017-08-02 12:02:02 -0700 | [diff] [blame] | 277 | |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 278 | def test_model_description(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 279 | xproto = """ |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 280 | option app_label = "test"; |
| 281 | |
| 282 | message Foo { |
| 283 | option description="This is the Foo model"; |
| 284 | required string name = 1 [ null = "False", blank="False"]; |
| 285 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 286 | } |
| 287 | |
| 288 | message Bar { |
| 289 | required string name = 1; |
| 290 | } |
| 291 | """ |
| 292 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 293 | args = XOSProcessorArgs() |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 294 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 295 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 296 | output = XOSProcessor.process(args) |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 297 | self.assertIn('description: "This is the Foo model"', output) |
| 298 | |
| 299 | def test_model_verbose_name(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 300 | xproto = """ |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 301 | option app_label = "test"; |
| 302 | |
| 303 | message Foo { |
| 304 | option verbose_name="Verbose Foo Name"; |
| 305 | required string name = 1 [ null = "False", blank="False"]; |
| 306 | required string isolation = 14 [default = "vm", choices = "(('vm', 'Virtual Machine'), ('container', 'Container'), ('container_vm', 'Container In VM'))", max_length = 30, blank = False, null = False, db_index = False]; |
| 307 | } |
| 308 | |
| 309 | message Bar { |
| 310 | required string name = 1; |
| 311 | } |
| 312 | """ |
| 313 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 314 | args = XOSProcessorArgs() |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 315 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 316 | args.target = "modeldefs.xtarget" |
Sapan Bhatia | bfb233a | 2018-02-09 14:53:09 -0800 | [diff] [blame] | 317 | output = XOSProcessor.process(args) |
Matteo Scandolo | e425f9d | 2017-08-15 15:56:19 -0700 | [diff] [blame] | 318 | self.assertIn('verbose_name: "Verbose Foo Name"', output) |
| 319 | |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 320 | def test_feedback_field(self): |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 321 | xproto = """ |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 322 | option app_label = "test"; |
| 323 | |
| 324 | message ParentFoo { |
| 325 | required string parent_name = 1 [null = False, blank = False, feedback_state = True]; |
| 326 | } |
| 327 | |
| 328 | message Foo (ParentFoo) { |
| 329 | required string name = 1 [null = False, blank = False, feedback_state = True]; |
| 330 | } |
| 331 | """ |
| 332 | |
Scott Baker | 1f7791d | 2018-10-04 13:21:20 -0700 | [diff] [blame] | 333 | args = XOSProcessorArgs() |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 334 | args.inputs = xproto |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 335 | args.target = "modeldefs.xtarget" |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 336 | output = XOSProcessor.process(args) |
| 337 | |
Zack Williams | be5ee1c | 2019-03-18 15:33:07 -0700 | [diff] [blame] | 338 | read_only = [s for s in output.splitlines() if "read_only: true" in s] |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 339 | self.assertEqual(len(read_only), 3) # readonly is 1 for ParentFoo and 2 for Foo |
Matteo Scandolo | 23cf15f | 2018-03-06 18:12:36 -0800 | [diff] [blame] | 340 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 341 | |
| 342 | if __name__ == "__main__": |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 343 | unittest.main() |