Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 1 | import unittest |
| 2 | import os |
| 3 | from xosgenx.generator import XOSGenerator |
| 4 | from helpers import FakeArgs |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 5 | import yaml |
| 6 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 7 | PROTO_EXPECTED_OUTPUT = """ |
| 8 | message VRouterPort { |
| 9 | option bases = "XOSBase"; |
| 10 | optional string name = 1 [ null = "True", max_length = "20", blank = "True", help_text = ""port friendly name"", modifier = "optional", db_index = "False" ]; |
| 11 | required string openflow_id = 2 [ null = "False", max_length = "21", blank = "False", help_text = ""port identifier in ONOS"", modifier = "required", db_index = "False" ]; |
| 12 | required int32 vrouter_device = 3 [ null = "False", blank = "False", model = "VRouterDevice", modifier = "required", type = "link", port = "ports", link_type = "manytoone", db_index = "True" ]; |
| 13 | 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] | 14 | } |
| 15 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 16 | VROUTER_XPROTO = os.path.abspath(os.path.dirname(os.path.realpath(__file__)) + "/xproto/vrouterport.xproto") |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 17 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 18 | # Generate other formats from xproto |
| 19 | class XProtoTranslatorTest(unittest.TestCase): |
| 20 | def _test_proto_generator(self): |
| 21 | args = FakeArgs() |
| 22 | args.files = [VROUTER_XPROTO] |
| 23 | args.target = 'proto.xtarget' |
| 24 | output = XOSGenerator.generate(args) |
| 25 | self.assertEqual(output, PROTO_EXPECTED_OUTPUT) |
| 26 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 27 | def test_yaml_generator(self): |
| 28 | xproto = \ |
| 29 | """ |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 30 | option app_label = "test"; |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 31 | |
| 32 | message Port (PlCoreBase,ParameterMixin){ |
| 33 | required manytoone network->Network:links = 1 [db_index = True, null = False, blank = False]; |
| 34 | optional manytoone instance->Instance:ports = 2 [db_index = True, null = True, blank = True]; |
| 35 | optional string ip = 3 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 36 | optional string port_id = 4 [help_text = "Neutron port id", max_length = 256, null = True, db_index = False, blank = True]; |
| 37 | optional string mac = 5 [help_text = "MAC address associated with this port", max_length = 256, null = True, db_index = False, blank = True]; |
| 38 | required bool xos_created = 6 [default = False, null = False, db_index = False, blank = True]; |
| 39 | } |
| 40 | |
| 41 | message Instance (PlCoreBase){ |
| 42 | optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; |
| 43 | optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; |
| 44 | required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; |
| 45 | optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; |
| 46 | optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False]; |
| 47 | required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; |
| 48 | optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; |
| 49 | required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; |
| 50 | required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; |
| 51 | required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; |
| 52 | required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; |
| 53 | required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", default = "get_default_flavor()", null = False, db_index = True, blank = False]; |
| 54 | optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True]; |
| 55 | 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]; |
| 56 | optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; |
| 57 | optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; |
| 58 | required manytomany tags->Tag = 17 [db_index = False, null = False, blank = True]; |
| 59 | } |
| 60 | |
| 61 | message Network (PlCoreBase,ParameterMixin) { |
| 62 | required string name = 1 [db_index = False, max_length = 32, null = False, blank = False]; |
| 63 | required manytoone template->NetworkTemplate:network = 2 [db_index = True, null = False, blank = False]; |
| 64 | required string subnet = 3 [db_index = False, max_length = 32, null = False, blank = True]; |
| 65 | required string start_ip = 4 [db_index = False, max_length = 32, null = False, blank = True]; |
| 66 | required string end_ip = 5 [db_index = False, max_length = 32, null = False, blank = True]; |
| 67 | optional string ports = 6 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 68 | optional string labels = 7 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 69 | required manytoone owner->Slice:ownedNetworks = 8 [help_text = "Slice that owns control of this Network", null = False, db_index = True, blank = False]; |
| 70 | required int32 guaranteed_bandwidth = 9 [default = 0, null = False, db_index = False, blank = False]; |
| 71 | required bool permit_all_slices = 10 [default = False, null = False, db_index = False, blank = True]; |
| 72 | optional string topology_parameters = 11 [db_index = False, null = True, blank = True]; |
| 73 | optional string controller_url = 12 [db_index = False, max_length = 1024, null = True, blank = True]; |
| 74 | optional string controller_parameters = 13 [db_index = False, null = True, blank = True]; |
| 75 | optional string network_id = 14 [help_text = "Quantum network", max_length = 256, null = True, db_index = False, blank = True]; |
| 76 | optional string router_id = 15 [help_text = "Quantum router id", max_length = 256, null = True, db_index = False, blank = True]; |
| 77 | optional string subnet_id = 16 [help_text = "Quantum subnet id", max_length = 256, null = True, db_index = False, blank = True]; |
| 78 | 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]; |
| 79 | required manytomany permitted_slices->Slice/Network_permitted_slices:availableNetworks = 18 [db_index = False, null = False, blank = True]; |
| 80 | required manytomany slices->Slice/NetworkSlice:networks = 19 [db_index = False, null = False, blank = True]; |
| 81 | required manytomany instances->Instance/Port:networks = 20 [db_index = False, null = False, blank = True]; |
| 82 | } |
| 83 | |
| 84 | message Slice (PlCoreBase){ |
| 85 | required string name = 1 [max_length = 80, content_type = "stripped", blank = False, help_text = "The Name of the Slice", null = False, db_index = False]; |
| 86 | required bool enabled = 2 [help_text = "Status for this Slice", default = True, null = False, db_index = False, blank = True]; |
| 87 | required bool omf_friendly = 3 [default = False, null = False, db_index = False, blank = True]; |
| 88 | 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]; |
| 89 | required string slice_url = 5 [db_index = False, max_length = 512, null = False, content_type = "url", blank = True]; |
| 90 | required manytoone site->Site:slices = 6 [help_text = "The Site this Slice belongs to", null = False, db_index = True, blank = False]; |
| 91 | required int32 max_instances = 7 [default = 10, null = False, db_index = False, blank = False]; |
| 92 | optional manytoone service->Service:slices = 8 [db_index = True, null = True, blank = True]; |
| 93 | 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'))"]; |
| 94 | optional string exposed_ports = 10 [db_index = False, max_length = 256, null = True, blank = True]; |
| 95 | optional manytoone serviceClass->ServiceClass:slices = 11 [db_index = True, null = True, blank = True]; |
| 96 | optional manytoone creator->User:slices = 12 [db_index = True, null = True, blank = True]; |
| 97 | optional manytoone default_flavor->Flavor:slices = 13 [db_index = True, null = True, blank = True]; |
| 98 | optional manytoone default_image->Image:slices = 14 [db_index = True, null = True, blank = True]; |
| 99 | optional manytoone default_node->Node:slices = 15 [db_index = True, null = True, blank = True]; |
| 100 | optional string mount_data_sets = 16 [default = "GenBank", max_length = 256, content_type = "stripped", blank = True, null = True, db_index = False]; |
| 101 | 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]; |
| 102 | required manytomany tags->Tag = 18 [db_index = False, null = False, blank = True]; |
| 103 | } |
| 104 | """ |
| 105 | |
Matteo Scandolo | 67654fa | 2017-06-09 09:33:17 -0700 | [diff] [blame] | 106 | args = FakeArgs() |
| 107 | args.inputs = xproto |
| 108 | args.target = 'modeldefs.xtarget' |
| 109 | output = XOSGenerator.generate(args) |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 110 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 111 | yaml_ir = yaml.load(output) |
| 112 | self.assertEqual(len(yaml_ir['items']), 4) |
| 113 | |
Matteo Scandolo | 5341859 | 2017-07-26 15:51:29 -0700 | [diff] [blame] | 114 | def test_gui_hidden_models(self): |
| 115 | xproto = \ |
| 116 | """ |
| 117 | option app_label = "test"; |
| 118 | |
| 119 | message Foo { |
| 120 | option gui_hidden = "True"; |
| 121 | required string name = 1 [ null = "False", blank="False"]; |
| 122 | } |
| 123 | |
| 124 | message Bar { |
| 125 | option gui_hidden = "False"; |
| 126 | required string name = 1 [ null = "False", blank="False"]; |
| 127 | } |
| 128 | """ |
| 129 | args = FakeArgs() |
| 130 | args.inputs = xproto |
| 131 | args.target = 'modeldefs.xtarget' |
| 132 | output = XOSGenerator.generate(args) |
| 133 | yaml_ir = yaml.load(output) |
| 134 | self.assertEqual(len(yaml_ir['items']), 1) |
| 135 | self.assertIn('Bar', output) |
| 136 | self.assertNotIn('Foo', output) |
| 137 | |
| 138 | def test_gui_hidden_model_fields(self): |
| 139 | xproto = \ |
| 140 | """ |
| 141 | option app_label = "test"; |
| 142 | |
| 143 | message Foo { |
| 144 | required string name = 1 [ null = "False", blank="False"]; |
| 145 | required string secret = 1 [ null = "False", blank="False", gui_hidden = "True"]; |
| 146 | } |
| 147 | """ |
| 148 | args = FakeArgs() |
| 149 | args.inputs = xproto |
| 150 | args.target = 'modeldefs.xtarget' |
| 151 | output = XOSGenerator.generate(args) |
| 152 | yaml_ir = yaml.load(output) |
| 153 | self.assertEqual(len(yaml_ir['items']), 1) |
| 154 | self.assertIn('name', output) |
| 155 | self.assertNotIn('secret', output) |
Matteo Scandolo | 292cc2a | 2017-07-31 19:02:12 -0700 | [diff] [blame] | 156 | |
| 157 | def test_static_options(self): |
| 158 | xproto = \ |
| 159 | """ |
| 160 | option app_label = "test"; |
| 161 | |
| 162 | message Foo { |
| 163 | required string name = 1 [ null = "False", blank="False"]; |
| 164 | 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]; |
| 165 | } |
| 166 | """ |
| 167 | |
| 168 | args = FakeArgs() |
| 169 | args.inputs = xproto |
| 170 | args.target = 'modeldefs.xtarget' |
| 171 | output = XOSGenerator.generate(args) |
| 172 | self.assertIn("options:", output) |
| 173 | self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output) |
| 174 | |
| 175 | def test_not_static_options(self): |
| 176 | xproto = \ |
| 177 | """ |
| 178 | option app_label = "test"; |
| 179 | |
| 180 | message Foo { |
| 181 | required string name = 1 [ null = "False", blank="False"]; |
| 182 | } |
| 183 | """ |
| 184 | |
| 185 | args = FakeArgs() |
| 186 | args.inputs = xproto |
| 187 | args.target = 'modeldefs.xtarget' |
| 188 | output = XOSGenerator.generate(args) |
| 189 | self.assertNotIn("options:", output) |
| 190 | |
| 191 | def test_default_value_in_modeldef(self): |
| 192 | xproto = \ |
| 193 | """ |
| 194 | option app_label = "test"; |
| 195 | |
| 196 | message Foo { |
| 197 | required string name = 1 [ null = "False", blank="False", default = "bar"]; |
| 198 | required string falsetrue = 1 [ null = "False", blank="False", default = False]; |
| 199 | required string truefalse = 1 [ null = "False", blank="False", default = True]; |
| 200 | required string some = 1 [ null = "False", blank="False", default = None]; |
| 201 | required string zero = 1 [ null = "False", blank="False", default = 0]; |
| 202 | } |
| 203 | """ |
| 204 | |
| 205 | args = FakeArgs() |
| 206 | args.inputs = xproto |
| 207 | args.target = 'modeldefs.xtarget' |
| 208 | output = XOSGenerator.generate(args) |
| 209 | self.assertIn('default: "bar"', output) |
| 210 | self.assertIn('default: "false"', output) |
| 211 | self.assertIn('default: "true"', output) |
| 212 | self.assertIn('default: "null"', output) |
| 213 | self.assertIn('default: "0"', output) |
| 214 | |
| 215 | def test_not_default_value_in_modeldef(self): |
| 216 | xproto = \ |
| 217 | """ |
| 218 | option app_label = "test"; |
| 219 | |
| 220 | message Foo { |
| 221 | required string name = 1 [ null = "False", blank="False"]; |
| 222 | } |
| 223 | """ |
| 224 | |
| 225 | args = FakeArgs() |
| 226 | args.inputs = xproto |
| 227 | args.target = 'modeldefs.xtarget' |
| 228 | output = XOSGenerator.generate(args) |
| 229 | self.assertNotIn('default:', output) |
| 230 | |
Sapan Bhatia | d022aeb | 2017-06-07 15:49:55 +0200 | [diff] [blame] | 231 | if __name__ == '__main__': |
| 232 | unittest.main() |
| 233 | |
| 234 | |