def test_django_with_attic(self): """ [XOS-GenX] Generate django output from test.xproto """ args = FakeArgs() args.files = [TEST_XPROTO, VROUTER_XPROTO] args.target = 'django.xtarget' args.attic = TEST_ATTICS args.output = OUTPUT_DIR args.dest_extension = 'py' args.write_to_file = 'model' output = XOSGenerator.generate(args) # xosmodel has custom header attic self.assertIn('from xosmodel_header import *', output['XOSModel']) self.assertIn('class XOSModel(XOSBase):', output['XOSModel']) # vrouter port use the default header self.assertIn('header import *', output['VRouterPort']) self.assertIn('class VRouterPort(XOSBase):', output['VRouterPort']) #verify files xosmodel = OUTPUT_DIR + '/xosmodel.py' self.assertTrue(os.path.isfile(xosmodel)) xmf = open(xosmodel).read() self.assertIn('from xosmodel_header import *', xmf) self.assertIn('class XOSModel(XOSBase):', xmf) vrouterport = OUTPUT_DIR + '/vrouterport.py' self.assertTrue(os.path.isfile(vrouterport)) vrpf = open(vrouterport).read() self.assertIn('header import *', vrpf) self.assertIn('class VRouterPort(XOSBase):', vrpf)
def test_skip_django(self): args = FakeArgs() args.files = [SKIP_DJANGO_XPROTO] args.target = 'django.xtarget' args.output = OUTPUT_DIR args.dest_extension = 'py' args.write_to_file = 'model' output = XOSGenerator.generate(args) # should not print a file if options.skip_django = True file = OUTPUT_DIR + '/user.py' self.assertFalse(os.path.isfile(file))
def test_write_multiple_files_from_xtarget(self): """ [XOS-GenX] read multiple models as input, print separate files based on +++ """ args = FakeArgs() args.files = [TEST_XPROTO, VROUTER_XPROTO] args.target = SPLIT_TARGET args.output = OUTPUT_DIR args.write_to_file = 'target' XOSProcessor.process(args) generated_files = [f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.')] self.assertEqual(len(generated_files), 2) xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read() vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read() self.assertIn("name: XOSModel", xosmodel) self.assertIn("name: VRouterPort", vrouterport)
def test_swagger_target(self): """ [XOS-GenX] The swagger xtarget should generate the appropriate json """ # xosgenx --output . --target xosgenx/targets/swagger.xtarget --write-to-file single --dest-file swagger.yaml ../../xos/core/models/core.xproto # http-server --cors Users/teone/Sites/opencord/orchestration/xos/lib/xos-genx/ xproto = \ """ option app_label = "core"; message Instance::instance_policy (XOSBase) { option validators = "instance_creator:Instance has no creator, instance_isolation: Container instance {obj.name} must use container image, instance_isolation_container_vm_parent:Container-vm instance {obj.name} must have a parent, instance_parent_isolation_container_vm:Parent field can only be set on Container-vm instances ({obj.name}), instance_isolation_vm: VM Instance {obj.name} must use VM image, instance_creator_privilege: instance creator has no privileges on slice"; optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False, gui_hidden = True]; required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", null = False, db_index = True, blank = False]; optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True, varchar = True]; 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]; optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; } """ args = FakeArgs() args.inputs = xproto args.target = 'swagger.xtarget' args.output = OUTPUT_DIR args.write_to_file = "single" args.dest_file = "swagger.yaml" args.quiet = False output = XOSProcessor.process(args) self.assertIn("/xosapi/v1/core/instances/:", output) self.assertIn("/xosapi/v1/core/instances/{id}:", output) self.assertIn("Instance:", output)
def test_service_order(self): args = FakeArgs() args.files = [BASE_XPROTO, TEST_XPROTO, VROUTER_XPROTO] args.target = 'service.xtarget' args.output = OUTPUT_DIR args.write_to_file = 'target' output = XOSGenerator.generate(args) model = OUTPUT_DIR + '/models.py' self.assertTrue(os.path.isfile(model)) line_num = 0 for line in open(model).readlines(): line_num += 1 if line.find('class XOSBase(models.Model, PlModelMixIn):') >= 0: base_line = line_num if line.find('XOSModel(XOSBase):') >= 0: xosmodel_line = line_num if line.find('class VRouterPort(XOSBase):') >= 0: vrouter_line = line_num self.assertLess(base_line, xosmodel_line) self.assertLess(xosmodel_line, vrouter_line)
def test_django_with_base(self): args = FakeArgs() args.files = [TEST_XPROTO, BASE_XPROTO] args.target = 'django.xtarget' args.attic = TEST_ATTICS args.output = OUTPUT_DIR args.dest_extension = 'py' args.write_to_file = 'model' output = XOSGenerator.generate(args) # verify files xosmodel = OUTPUT_DIR + '/xosmodel.py' self.assertTrue(os.path.isfile(xosmodel)) xmf = open(xosmodel).read() self.assertIn('from xosmodel_header import *', xmf) self.assertIn('class XOSModel(XOSBase):', xmf) xosbase = OUTPUT_DIR + '/xosbase.py' self.assertTrue(os.path.isfile(xosbase)) xbf = open(xosbase).read() self.assertIn('header import *', xbf) self.assertIn('class XOSBase(models.Model, PlModelMixIn):', xbf)
def test_swagger_target(self): """ [XOS-GenX] The swagger xtarget should generate the appropriate json """ # xosgenx --output . --target xosgenx/targets/swagger.xtarget --write-to-file single --dest-file swagger.yaml ../../xos/core/models/core.xproto # http-server --cors Users/teone/Sites/opencord/orchestration/xos/lib/xos-genx/ xproto = \ """ option app_label = "core"; message Instance::instance_policy (XOSBase) { option validators = "instance_creator:Instance has no creator, instance_isolation: Container instance {obj.name} must use container image, instance_isolation_container_vm_parent:Container-vm instance {obj.name} must have a parent, instance_parent_isolation_container_vm:Parent field can only be set on Container-vm instances ({obj.name}), instance_isolation_vm: VM Instance {obj.name} must use VM image, instance_creator_privilege: instance creator has no privileges on slice"; optional string instance_id = 1 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance id", null = True, db_index = False]; optional string instance_uuid = 2 [max_length = 200, content_type = "stripped", blank = True, help_text = "Nova instance uuid", null = True, db_index = False]; required string name = 3 [max_length = 200, content_type = "stripped", blank = False, help_text = "Instance name", null = False, db_index = False]; optional string instance_name = 4 [max_length = 200, content_type = "stripped", blank = True, help_text = "OpenStack generated name", null = True, db_index = False]; optional string ip = 5 [max_length = 39, content_type = "ip", blank = True, help_text = "Instance ip address", null = True, db_index = False, gui_hidden = True]; required manytoone image->Image:instances = 6 [db_index = True, null = False, blank = False]; optional manytoone creator->User:instances = 7 [db_index = True, null = True, blank = True]; required manytoone slice->Slice:instances = 8 [db_index = True, null = False, blank = False]; required manytoone deployment->Deployment:instance_deployment = 9 [db_index = True, null = False, blank = False]; required manytoone node->Node:instances = 10 [db_index = True, null = False, blank = False]; required int32 numberCores = 11 [help_text = "Number of cores for instance", default = 0, null = False, db_index = False, blank = False]; required manytoone flavor->Flavor:instance = 12 [help_text = "Flavor of this instance", null = False, db_index = True, blank = False]; optional string userData = 13 [help_text = "user_data passed to instance during creation", null = True, db_index = False, blank = True, varchar = True]; 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]; optional string volumes = 15 [help_text = "Comma-separated list of directories to expose to parent context", null = True, db_index = False, blank = True]; optional manytoone parent->Instance:instance = 16 [help_text = "Parent Instance for containers nested inside of VMs", null = True, db_index = True, blank = True]; } """ args = FakeArgs() args.inputs = xproto args.target = 'swagger.xtarget' args.output = "/Users/teone/Sites/opencord/orchestration/xos/lib/xos-genx" args.write_to_file = "single" args.dest_file = "swagger.yaml" args.quiet = False output = XOSGenerator.generate(args) self.assertEqual(True, False);
def test_write_multiple_files(self): """ [XOS-GenX] read multiple models as input, print one file per model """ args = FakeArgs() args.files = [TEST_XPROTO, VROUTER_XPROTO] args.target = TEST_TARGET args.output = OUTPUT_DIR args.dest_extension = 'txt' args.write_to_file = 'model' XOSGenerator.generate(args) generated_files = [ f for f in os.listdir(OUTPUT_DIR) if not f.startswith('.') ] self.assertEqual(len(generated_files), 2) xosmodel = open(os.path.join(OUTPUT_DIR, 'xosmodel.txt'), "r").read() vrouterport = open(os.path.join(OUTPUT_DIR, 'vrouterport.txt'), "r").read() self.assertIn("name: XOSModel", xosmodel) self.assertIn("name: VRouterPort", vrouterport)