Ejemplo n.º 1
0
    def test_pure_policies(self):
		xproto = \
"""
policy my_policy < exists x:a=b >
"""

		proto = \
"""
option my_policy = "policy:< exists x:a=b >";
"""
		target = XProtoTestHelpers.write_tmp_target(
"""
{{ policies }}
""")

		args_xproto = FakeArgs()
		args_xproto.inputs = xproto
		args_xproto.target = target
		xproto_gen = XOSGenerator.generate(args_xproto)

		args_proto = FakeArgs()
		args_proto.inputs = proto
		args_proto.target = target
		args_proto.rev = True
		proto_gen = XOSGenerator.generate(args_proto)

		self.assertEqual(proto_gen, xproto_gen)
Ejemplo n.º 2
0
    def test_equal(self):
        xproto = \
"""
    policy output < not (ctx.user = obj.user) >
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i2 = (ctx.user == obj.user)
            i1 = (not i2)
            if (not i1):
                raise Exception('Necessary Failure')
        """

        obj = FakeArgs()
	obj.user = 1
        ctx = FakeArgs()
	ctx.user = 1

        with self.assertRaises(Exception):
           policy_output_validator(obj, ctx)
Ejemplo n.º 3
0
    def test_basic_proto(self):
        xtarget = XProtoTestHelpers.write_tmp_target("{{ proto }}")

        xproto = \
"""
message Person {
  required string name = 1;
  required int32 id = 2;  // Unique ID number for this person.
  optional string email = 3 [symphony = "da da da dum"];

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  required  string number = 1;
  optional PhoneType type = 2;

  repeated PhoneNumber phones = 4;
}
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("PhoneNumber", output)
Ejemplo n.º 4
0
    def test_singularize(self):
        proto = \
"""
  message TestSingularize {
      // The following field has an explicitly specified singular
      required int many = 1 [singular = "one"];
      // The following fields have automatically computed singulars
      required int sheep = 2;
      required int radii = 2;
      required int slices = 2;
      required int networks = 2;
      required int omf_friendlies = 2;
  }
"""

        target = XProtoTestHelpers.write_tmp_target(
"""
{% for m in proto.messages.0.fields -%}
{{ xproto_singularize(m) }},
{%- endfor %}
""")
        args = FakeArgs()
        args.inputs = proto
        args.target = target
        output = XOSGenerator.generate(args)
        self.assertEqual("one,sheep,radius,slice,network,omf_friendly", output.lstrip().rstrip().rstrip(','))
Ejemplo n.º 5
0
    def test_slice_name_validation(self):
        xproto = \
"""
    policy test_policy < not obj.id -> {{ obj.name.startswith(obj.site.login_base) }} >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        obj = FakeArgs()
        obj.isolation = 'container'
        obj.kind = 'not a container'

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i3 = obj.id
            i4 = obj.name.startswith(obj.site.login_base)
            i2 = ((not i3) or i4)
            i1 = (not i2)
            if (not i1):
                raise ValidationError('Necessary Failure')
        """

        with self.assertRaises(Exception):
           policy_output_validator(obj, {})
Ejemplo n.º 6
0
    def test_field_graph(self):
        xproto = \
"""
message VRouterDevice (PlCoreBase){
     optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True, unique_with="openflow_id"];
     required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False, unique_with="name"];
     required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False, unique_with="driver"];
     required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False, unique_with="vrouter_service"];
     required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False];
     required string A = 6 [unique_with="B"];
     required string B = 7 [unique_with="C"];
     required string C = 8 [unique_with="A"];
     required string D = 9;
     required string E = 10 [unique_with="F,G"];
     required string F = 11;
     required string G = 12;
}
"""
	target = XProtoTestHelpers.write_tmp_target(
"""
{{ xproto_field_graph_components(proto.messages.0.fields) }}
""")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target
        output = XOSGenerator.generate(args)
        output =  eval(output)
        self.assertIn({'A','B','C'}, output)
        self.assertIn({'openflow_id','name'}, output)
        self.assertIn({'config_key','vrouter_service','driver'}, output)
        self.assertIn({'E','F','G'}, output)
        
        union = reduce(lambda acc,x: acc | x, output)
        self.assertNotIn('D', union) 
Ejemplo n.º 7
0
    def test_equal(self):
        xproto = \
"""
    policy output < ctx.user = obj.user >
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_enforcer(obj, ctx):
            i1 = (ctx.user == obj.user)
            return i1
        """

        obj = FakeArgs()
	obj.user = 1
        ctx = FakeArgs()
	ctx.user = 1

        verdict = policy_output_enforcer(obj, ctx)
Ejemplo n.º 8
0
    def test_one_to_many_in_modeldef(self):
        xproto = \
"""
option app_label = "test";

message ServiceDependency {
    required manytoone provider_service->Service:provided_dependencies = 1;
    required manytoone subscriber_service->Service:subscribed_dependencies = 2;
}

message Service {
    required string name = 1;
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        # Service deps model
        self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)
        self.assertIn('{model: Service, type: manytoone, on_field: provider_service}', output)

        # Service model
        self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
        self.assertIn('{model: ServiceDependency, type: onetomany, on_field: provider_service}', output)
Ejemplo n.º 9
0
    def test_call_policy(self):
        xproto = \
"""
    policy sub_policy < ctx.user = obj.user >
    policy output < *sub_policy(child) >
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(output,globals()) # This loads the generated function, which should look like this:

        """
        def policy_sub_policy_enforcer(obj, ctx):
            i1 = (ctx.user == obj.user)
    	    return i1

	def policy_output_enforcer(obj, ctx):
	    i1 = policy_sub_policy_enforcer(obj.child, ctx)
	    return i1
        """

        obj = FakeArgs()
        obj.child = FakeArgs()
	obj.child.user = 1

        ctx = FakeArgs()
	ctx.user = 1

        verdict = policy_output_enforcer(obj, ctx)
        self.assertTrue(verdict)
Ejemplo n.º 10
0
    def test_instance_container(self):
        xproto = \
"""
    policy test_policy < (obj.isolation = "container" | obj.isolation = "container_vm" ) -> (obj.image.kind = "container") >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        obj = FakeArgs()
        obj.isolation = 'container'
        obj.kind = 'not a container'

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i4 = (obj.isolation == 'container')
            i5 = (self.isolation == 'container_vm')
            i2 = (i4 or i5)
            i3 = (obj.image.kind == 'container')
            i1 = (i2 or i3)
            return i1
        """

        with self.assertRaises(Exception):
           policy_output_validator(obj, {})
Ejemplo n.º 11
0
    def test_bin(self):
        xproto = \
"""
    policy output < ctx.is_admin = True | obj.empty = True>
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)
        exec(output) # This loads the generated function, which should look like this:

        """
	def policy_output_enforcer(obj, ctx):
	    i2 = (ctx.is_admin == True)
	    i3 = (obj.empty == True)
	    i1 = (i2 or i3)
	    return i1
        """

        obj = FakeArgs()
	obj.empty = True

	ctx = FakeArgs()
	ctx.is_admin = True

        verdict = policy_output_enforcer(obj, ctx)

        self.assertTrue(verdict)
Ejemplo n.º 12
0
    def test_pluralize(self):
        proto = \
"""
  message TestPluralize {
      // The following field has an explicitly specified plural
      required int anecdote = 1 [plural = "data"];
      // The following fields have automatically computed plurals
      required int sheep = 2;
      required int radius = 2;
      required int slice = 2;
      required int network = 2;
      required int omf_friendly = 2;
  }
"""

        target = XProtoTestHelpers.write_tmp_target(
"""
{% for m in proto.messages.0.fields -%}
{{ xproto_pluralize(m) }},
{%- endfor %}
""")
        args = FakeArgs()
        args.inputs = proto
        args.target = target
        output = XOSGenerator.generate(args)
        self.assertEqual("data,sheep,radii,slices,networks,omf_friendlies", output.lstrip().rstrip().rstrip(','))
Ejemplo n.º 13
0
    def test_bin(self):
        xproto = \
"""
    policy output < (ctx.is_admin = True | obj.empty = True) | False>
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)
        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i2 = (ctx.is_admin == True)
            i3 = (obj.empty == True)
            i1 = (i2 or i3)
            if (not i1):
                raise Exception('Necessary Failure')
        """

        obj = FakeArgs()
	obj.empty = False

	ctx = FakeArgs()
	ctx.is_admin = False

        with self.assertRaises(Exception):
            verdict = policy_output_validator(obj, ctx)
Ejemplo n.º 14
0
    def test_user_policy(self):
        xproto = \
"""
    policy test_policy <
         ctx.user.is_admin
         | ctx.user.id = obj.id
         | (exists Privilege: 
             Privilege.accessor_id = ctx.user.id
             & Privilege.accessor_type = "User"
             & Privilege.permission = "role:admin"
             & Privilege.object_type = "Site"
             & Privilege.object_id = ctx.user.site.id) >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_enforcer(obj, ctx):
            i2 = ctx.user.is_admin
            i4 = (ctx.user.id == obj.id)
            i5 = Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(permission='role:admin'), Q(object_type='Site'), Q(object_id=ctx.user.site.id))[0]
            i3 = (i4 or i5)
            i1 = (i2 or i3)
            return i1
        """

        # FIXME: Test this policy by executing it
        self.assertTrue(policy_output_enforcer is not None)
Ejemplo n.º 15
0
    def test_xproto_lib(self):
        target = XProtoTestHelpers.write_tmp_target(
"""
  {{ xproto_first_non_empty([None, None, None, None, None, None, "Eureka"]) }}
""")
        args = FakeArgs()
        args.inputs = ''
        args.target = target
        output = XOSGenerator.generate(args)
        self.assertIn("Eureka", output)
Ejemplo n.º 16
0
def generate_swagger_docs(xproto):

    # if not os.path.isfile(xproto):
    #     print "ERROR: Couldn't find xproto file for %s at: %s" % (service, xproto)
    #     return

    print "Generating swagger docs for %s" % (xproto)
    args = Args()
    args.files = xproto
    args.target = 'swagger.xtarget'
    args.output = SWAGGER_DOCS_DIR
    args.write_to_file = "single"
    args.dest_file = "swagger.yaml"
    args.quiet = False
    try:
        XOSGenerator.generate(args)
    except Exception, e:
        print "ERROR: Couldn't generate swagger specs"
        print e
Ejemplo n.º 17
0
    def test_context(self):
        target = XProtoTestHelpers.write_tmp_target(
"""
  {{ context.what }}
""")
        args = FakeArgs()
        args.inputs = ''
        args.target = target
        args.kv='what:what is what'
        output = XOSGenerator.generate(args)
        self.assertIn("what is what", output)
Ejemplo n.º 18
0
    def test_slice_policy(self):
        xproto = \
"""
   policy site_policy <
            ctx.user.is_admin
            | (ctx.write_access -> exists Privilege: Privilege.object_type = "Site" & Privilege.object_id = obj.id & Privilege.accessor_id = ctx.user.id & Privilege.permission_id = "role:admin") >

   policy test_policy <
         ctx.user.is_admin
         | (*site_policy(site)
         & ((exists Privilege:
             Privilege.accessor_id = ctx.user.id
             & Privilege.accessor_type = "User"
             & Privilege.object_type = "Slice"
             & Privilege.object_id = obj.id
             & (ctx.write_access->Privilege.permission="role:admin"))
           | (exists Privilege:
             Privilege.accessor_id = ctx.user.id
             & Privilege.accessor_type = "User"
             & Privilege.object_type = "Site"
             & Privilege.object_id = obj.site.id
             & Privilege.permission = "role:admin"))
            )>
    
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_enforcer(obj, ctx):
	    i2 = ctx.user.is_admin
	    i4 = policy_site_policy_enforcer(obj.site, ctx)
	    i10 = ctx.write_access
	    i11 = (not (not Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Slice'), Q(object_id=obj.id), Q(permission='role:admin'))))
	    i8 = (i10 and i11)
	    i14 = ctx.write_access
	    i12 = (not i14)
	    i13 = (not (not Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Slice'), Q(object_id=obj.id))))
	    i9 = (i12 and i13)
	    i6 = (i8 or i9)
	    i7 = (not (not Privilege.objects.filter(Q(accessor_id=ctx.user.id), Q(accessor_type='User'), Q(object_type='Site'), Q(object_id=obj.site.id), Q(permission='role:admin'))))
	    i5 = (i6 or i7)
	    i3 = (i4 and i5)
	    i1 = (i2 or i3)
	    return i1
        """

        # FIXME: Test this policy by executing it
        self.assertTrue(policy_output_enforcer is not None)
Ejemplo n.º 19
0
    def test_through_extensions(self):
        xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.links.0.through }}")
        xproto = \
"""
message links {
    required manytomany vrouter_service->VRouterService/ServiceProxy:device_ports = 4 [db_index = True, null = False, blank = False];
}
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("ServiceProxy", output)
Ejemplo n.º 20
0
    def test_proto_generator(self):
        """
        [XOS-GenX] Generate DJANGO models, verify Fields and Foreign Keys
        """
        args = FakeArgs()
        args.files = [VROUTER_XPROTO]
        args.target = 'django.xtarget'
        output = XOSGenerator.generate(args)

        fields = filter(lambda s:'Field(' in s, output.splitlines())
        self.assertEqual(len(fields), 2)
        links = filter(lambda s:'Key(' in s, output.splitlines())
        self.assertEqual(len(links), 2)
Ejemplo n.º 21
0
    def test_message_base(self):
        xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.bases }}")
        xproto = \
"""
message base(Base) {
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("Base", output)
Ejemplo n.º 22
0
    def test_constant(self):
        xproto = \
"""
    policy true_policy < True >
"""

        target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.true_policy }}")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args).replace('t','T')
        self.assertTrue(eval(output)) 
Ejemplo n.º 23
0
    def test_policy_missing_function(self):
        xproto = \
"""
    policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
    policy network_slice_policy < *slice_policyX(slice) >
"""

        target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.network_slice_policy }} ")
        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        with self.assertRaises(Exception):
            output = XOSGenerator.generate(args)
Ejemplo n.º 24
0
    def test_not_default_value_in_modeldef(self):
        xproto = \
"""
option app_label = "test";

message Foo {
    required string name = 1 [ null = "False", blank="False"];
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        self.assertNotIn('default:', output)
Ejemplo n.º 25
0
    def test_global_options(self):

        xtarget = XProtoTestHelpers.write_tmp_target("{{ options }}")

        xproto = \
"""
    option kind = "vsg";
    option verbose_name = "vSG Service";
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("vsg", output)
        self.assertIn("vSG Service", output)
Ejemplo n.º 26
0
    def test_message_options(self):
        xtarget = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0.options.type }}")
        xproto = \
"""
message link {
    option type = "e1000";
}
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("e1000", output)

	pass
Ejemplo n.º 27
0
    def test_file_methods(self):
        target = XProtoTestHelpers.write_tmp_target(
"""
  {%% if file_exists("%s") %%}
    {{ include_file("%s") }}
  {%% endif %%}
"""%(TEST_FILE, TEST_FILE)
        )

        args = FakeArgs()
        args.inputs = ''
        args.target = target
        args.attic = OUTPUT_DIR
        output = XOSGenerator.generate(args)
        self.assertIn(TEST_OUTPUT, output)
Ejemplo n.º 28
0
    def test_constant(self):
        xproto = \
"""
    policy true_policy < True >
"""

        target = XProtoTestHelpers.write_tmp_target(
            "{{ proto.policies.true_policy }}")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args).replace('t', 'T')
        self.assertTrue(eval(output))
Ejemplo n.º 29
0
    def test_policy_missing_function(self):
        xproto = \
"""
    policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
    policy network_slice_policy < *slice_policyX(slice) >
"""

        target = XProtoTestHelpers.write_tmp_target(
            "{{ proto.policies.network_slice_policy }} ")
        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        with self.assertRaises(Exception):
            output = XOSGenerator.generate(args)
Ejemplo n.º 30
0
    def test_not_default_value_in_modeldef(self):
        xproto = \
"""
option app_label = "test";

message Foo {
    required string name = 1 [ null = "False", blank="False"];
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        self.assertNotIn('default:', output)
Ejemplo n.º 31
0
    def test_global_options(self):

        xtarget = XProtoTestHelpers.write_tmp_target("{{ options }}")

        xproto = \
"""
    option kind = "vsg";
    option verbose_name = "vSG Service";
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("vsg", output)
        self.assertIn("vSG Service", output)
Ejemplo n.º 32
0
    def test_message_options(self):
        xtarget = XProtoTestHelpers.write_tmp_target(
            "{{ proto.messages.0.options.type }}")
        xproto = \
"""
message link {
    option type = "e1000";
}
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("e1000", output)

        pass
Ejemplo n.º 33
0
    def test_forall(self):
        # This one we only parse
        xproto = \
"""
    policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id >
"""

        target = XProtoTestHelpers.write_tmp_target("{{ proto.policies.instance }}")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)
        (op, operands), = eval(output).items()

        self.assertEqual(op,'forall')
Ejemplo n.º 34
0
    def test_link_extensions(self):

        xtarget = XProtoTestHelpers.write_tmp_target(
            "{{ proto.messages.0.links }}")
        xproto = \
"""
message links {
    required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
}
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = xtarget
        output = XOSGenerator.generate(args)
        self.assertIn("VRouterService", output)

        pass
Ejemplo n.º 35
0
    def test_static_options(self):
        xproto = \
"""
option app_label = "test";

message Foo {
    required string name = 1 [ null = "False", blank="False"];
    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];
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        self.assertIn("options:", output)
        self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output)
Ejemplo n.º 36
0
    def test_static_options(self):
        xproto = \
"""
option app_label = "test";

message Foo {
    required string name = 1 [ null = "False", blank="False"];
    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];
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        self.assertIn("options:", output)
        self.assertIn(" {'id': 'container_vm', 'label': 'Container In VM'}", output)
Ejemplo n.º 37
0
    def test_gui_hidden_model_fields(self):
        xproto = \
"""
option app_label = "test";

message Foo {
    required string name = 1 [ null = "False", blank="False"];
    required string secret = 1 [ null = "False", blank="False", gui_hidden = "True"];
}
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        yaml_ir = yaml.load(output)
        self.assertEqual(len(yaml_ir['items']), 1)
        self.assertIn('name', output)
        self.assertNotIn('secret', output)
Ejemplo n.º 38
0
    def test_annotation(self):
        xproto = \
"""
    policy true_policy < True >

    message always::true_policy {
        required int still = 9;
    }
"""

        target = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0 }}")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)
        self.assertIn("true_policy", output)
Ejemplo n.º 39
0
    def test_annotation(self):
        xproto = \
"""
    policy true_policy < True >

    message always::true_policy {
        required int still = 9;
    }
"""

        target = XProtoTestHelpers.write_tmp_target("{{ proto.messages.0 }}")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)
        self.assertIn("true_policy", output)
Ejemplo n.º 40
0
    def test_forall(self):
        # This one we only parse
        xproto = \
"""
    policy instance < forall Instance: exists Credential: Credential.obj_id = Instance.obj_id >
"""

        target = XProtoTestHelpers.write_tmp_target(
            "{{ proto.policies.instance }}")

        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)
        (op, operands), = eval(output).items()

        self.assertEqual(op, 'forall')
Ejemplo n.º 41
0
    def test_xproto_fields_to_tosca_keys_default(self):
        """
        [XOS-GenX] if no "tosca_key" is specified, and a name attribute is present in the model, use that
        """
        xproto = \
"""
option app_label = "test";

message Foo {
    required string name = 1 [ null = "False", blank="False"];
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target_tosca_keys
        output = XOSGenerator.generate(args)
        self.assertIn('name', output)
Ejemplo n.º 42
0
    def test_policy_function(self):
        xproto = \
"""
    policy slice_policy < exists Privilege: Privilege.object_id = obj.id >
    policy network_slice_policy < *slice_policy(slice) >
"""

        target = XProtoTestHelpers.write_tmp_target(
            "{{ proto.policies.network_slice_policy }} ")
        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)

        (op, operands), = eval(output).items()

        self.assertIn('slice_policy', operands)
        self.assertIn('slice', operands)
Ejemplo n.º 43
0
    def test_forall(self):
        # This one we only parse
        xproto = \
"""
    policy output < forall Credential: Credential.obj_id = obj_id >
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)
        """
        def output_security_check(obj, ctx):
            i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
            i1 = (not i2)
            return i1
        """
        exec(output)
Ejemplo n.º 44
0
    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 = XOSGenerator.generate(args)
        self.assertIn("/xosapi/v1/core/instances/:", output)
        self.assertIn("/xosapi/v1/core/instances/{id}:", output)
        self.assertIn("Instance:", output)
Ejemplo n.º 45
0
    def test_term(self):
        xproto = \
"""
    policy slice_user < slice.user.is_admin >
"""

        target = XProtoTestHelpers.write_tmp_target(
            "{{ proto.policies.slice_user }}")
        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)

        slice = FakeArgs()
        slice.user = FakeArgs()
        slice.user.is_admin = True

        expr = eval(output)
        self.assertTrue(expr)
Ejemplo n.º 46
0
    def test_xproto_fields_link_to_tosca_keys_custom(self):
        """
        [XOS-GenX] if "tosca_key" is specified, use it
        """
        xproto = \
            """
            option app_label = "test";

            message Foo {
                required string name = 1 [ null = "False", blank="False"];
                required manytoone provider_service_instance->ServiceInstance:provided_links = 1 [db_index = True, null = False, blank = False, tosca_key=True];
            }
            """

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target_tosca_keys
        output = XOSGenerator.generate(args)
        self.assertNotIn('name', output)
        self.assertIn('provider_service_instance_id', output)
Ejemplo n.º 47
0
    def test_python(self):
        xproto = \
"""
    policy output < {{ "jack" in ["the", "box"] }} = False >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target
        output = XOSGenerator.generate(args)
        exec(
            output
        )  # This loads the generated function, which should look like this:
        """
        def output_security_check(obj, ctx):
            i2 = ('jack' in ['the', 'box'])
            i1 = (i2 == False)
            return i1
        """

        self.assertTrue(output_security_check({}, {}) is True)
Ejemplo n.º 48
0
    def test_forall(self):
        # This one we only parse
        xproto = \
"""
    policy test_policy < forall Credential: Credential.obj_id = obj_id >
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)
        """
        def policy_output_enforcer(obj, ctx):
            i2 = Credential.objects.filter((~ Q(obj_id=obj_id)))[0]
            i1 = (not i2)
            return i1
        """

        self.assertIn('policy_output_validator', output)
Ejemplo n.º 49
0
    def test_exists(self):
        xproto = \
"""
    policy output < exists Privilege: Privilege.object_id = obj.id >
"""
	args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)
        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i1 = Privilege.objects.filter(Q(object_id=obj.id))[0]
            if (not i1):
                raise Exception('Necessary Failure')
        """

        self.assertTrue(policy_output_validator is not None)
Ejemplo n.º 50
0
    def test_exists(self):
        xproto = \
"""
    policy output < exists Privilege: Privilege.object_id = obj.id >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)
        exec(
            output
        )  # This loads the generated function, which should look like this:
        """
	def output_security_check(obj, ctx):
	    i1 = Privilege.objects.filter(object_id=obj.id)
    	    return i1
        """

        self.assertTrue(output_security_check is not None)
Ejemplo n.º 51
0
    def test_python(self):
        xproto = \
"""
    policy output < {{ "jack" in ["the", "box"] }} = True >
"""
	args = FakeArgs()
        args.inputs = xproto
        args.target = self.target
        output = XOSGenerator.generate(args)
        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i2 = ('jack' in ['the', 'box'])
            i1 = (i2 == True)
            if (not i1):
                raise Exception('Necessary Failure')
        """

        with self.assertRaises(Exception):
            self.assertTrue(policy_output_validator({}, {}) is True)
Ejemplo n.º 52
0
    def test_model_verbose_name(self):
        xproto = \
"""
option app_label = "test";

message Foo {
    option verbose_name="Verbose Foo Name";
    required string name = 1 [ null = "False", blank="False"];
    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];
}

message Bar {
    required string name = 1;
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = 'modeldefs.xtarget'
        output = XOSGenerator.generate(args)
        self.assertIn('verbose_name: "Verbose Foo Name"', output)
Ejemplo n.º 53
0
    def test_proto_generator(self):
        target = XProtoTestHelpers.write_tmp_target("""
{% for m in proto.messages %}
   {% for r in m.rlinks %}
       {{ r }}
   {% endfor %}
{% endfor %}
""")

        xproto = \
"""
message VRouterPort (PlCoreBase){
     optional string name = 1 [help_text = "port friendly name", max_length = 20, null = True, db_index = False, blank = True];
     required string openflow_id = 2 [help_text = "port identifier in ONOS", max_length = 21, null = False, db_index = False, blank = False];
     required manytoone vrouter_device->VRouterDevice:ports = 3 [db_index = True, null = False, blank = False];
     required manytoone vrouter_service->VRouterService:device_ports = 4 [db_index = True, null = False, blank = False];
}

message VRouterService (Service) {
     optional string rest_hostname = 1 [db_index = False, max_length = 255, null = True, content_type = "stripped", blank = True];
     required int32 rest_port = 2 [default = 8181, null = False, db_index = False, blank = False];
     required string rest_user = 3 [default = "onos", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
     required string rest_pass = 4 [default = "rocks", max_length = 255, content_type = "stripped", blank = False, null = False, db_index = False];
}

message VRouterDevice (PlCoreBase){
     optional string name = 1 [help_text = "device friendly name", max_length = 20, null = True, db_index = False, blank = True];
     required string openflow_id = 2 [help_text = "device identifier in ONOS", max_length = 20, null = False, db_index = False, blank = False];
     required string config_key = 3 [default = "basic", max_length = 32, blank = False, help_text = "configuration key", null = False, db_index = False];
     required string driver = 4 [help_text = "driver type", max_length = 32, null = False, db_index = False, blank = False];
     required manytoone vrouter_service->VRouterService:devices = 5 [db_index = True, null = False, blank = False];
}
"""

        args = FakeArgs()
        args.inputs = xproto
        args.target = target
        output = XOSGenerator.generate(args)
        self.assertIn("'src_port': 'device_ports'", output)
        self.assertIn("'src_port': 'ports'", output)
Ejemplo n.º 54
0
    def test_constant(self):
        xproto = \
"""
    policy output < False >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(output) # This loads the generated function, which should look like this:

        """
        def policy_output_validator(obj, ctx):
            i1 = False
            if (not i1):
                raise Exception('Necessary Failure')
        """

        with self.assertRaises(Exception):
           policy_output_validator({}, {})
Ejemplo n.º 55
0
    def test_constant(self):
        xproto = \
"""
    policy output < True >
"""
        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target

        output = XOSGenerator.generate(args)

        exec(
            output
        )  # This loads the generated function, which should look like this:
        """
        def output_security_check(obj, ctx):
            i1 = True
            return i1
        """

        verdict = output_security_check({}, {})
        self.assertTrue(verdict)
Ejemplo n.º 56
0
    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)
Ejemplo n.º 57
0
    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)
Ejemplo n.º 58
0
    def test_bin(self):
        xproto = \
"""
    policy slice_admin < slice.is_admin | obj.empty >
"""
        target = XProtoTestHelpers.write_tmp_target(
            "{{ proto.policies.slice_admin }}")
        args = FakeArgs()
        args.inputs = xproto
        args.target = target

        output = XOSGenerator.generate(args)

        slice = FakeArgs()
        slice.is_admin = False
        obj = FakeArgs()
        obj.empty = []

        (op, operands), = eval(output).items()
        expr = op.join(operands).replace('|', ' or ')

        self.assertFalse(eval(expr))
Ejemplo n.º 59
0
    def test_tosca_fields(self):
        """
        [XOS-GenX] should convert xproto types to tosca know types
        """
        xproto = \
        """
        option app_label = "test";

        message Foo {
            required string name = 1 [ null = "False", blank="False"];
            required bool active = 1 [ null = "False", blank="False"];
            required int32 quantity = 1 [ null = "False", blank="False"];
        }
        """

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target_tosca_type
        output = XOSGenerator.generate(args)
        self.assertIn('string', output)
        self.assertIn('boolean', output)
        self.assertIn('integer', output)
Ejemplo n.º 60
0
    def test_xproto_fields_to_tosca_keys_custom(self):
        """
        [XOS-GenX] if "tosca_key" is specified, use it
        """
        xproto = \
            """
            option app_label = "test";
        
            message Foo {
                required string name = 1 [ null = "False", blank="False"];
                required string key_1 = 2 [ null = "False", blank="False", tosca_key=True];
                required string key_2 = 3 [ null = "False", blank="False", tosca_key=True];
            }
            """

        args = FakeArgs()
        args.inputs = xproto
        args.target = self.target_tosca_keys
        output = XOSGenerator.generate(args)
        self.assertNotIn('name', output)
        self.assertIn('key_1', output)
        self.assertIn('key_2', output)