Ejemplo n.º 1
0
    def test_must_exist_fail(self):
        """
        [TOSCA_Parser] Should throw an error if an object with 'must_exist' does not exist
        """
        recipe = """
        tosca_definitions_version: tosca_simple_yaml_1_0

        description: Create a new site with one user

        imports:
           - custom_types/site.yaml

        topology_template:
          node_templates:

            # Site
            site_onlab:
              type: tosca.nodes.Site
              properties:
                name: Open Networking Lab
                must-exist: True
        """

        parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)

        with self.assertRaises(Exception) as e:
            parser.execute()

        self.assertEqual(
            str(e.exception),
            "[XOS-TOSCA] Failed to save or delete model Site [site_onlab]: "
            "[XOS-TOSCA] Model of class Site and properties {'name': 'Open Networking Lab'} "
            "has property 'must-exist' but cannot be found")
Ejemplo n.º 2
0
    def test_number_param(self, mock_save):
        """
        [TOSCA_Parser] Should correctly parse number parameters
        """
        recipe = """
                tosca_definitions_version: tosca_simple_yaml_1_0

                description: Create a new site with one user

                imports:
                   - custom_types/privilege.yaml

                topology_template:
                  node_templates:

                    privilege#test_privilege:
                      type: tosca.nodes.Privilege
                      properties:
                        permission: whatever
                        accessor_id: 3
                """
        parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)
        parser.execute()

        # checking that the model has been saved
        mock_save.assert_called_with()

        # check that the model was saved with the expected values
        saved_model = parser.saved_model_by_name["privilege#test_privilege"]
        self.assertEqual(saved_model.permission, "whatever")
        self.assertEqual(saved_model.accessor_id, 3)
Ejemplo n.º 3
0
    def test_compute_dependencies(self):
        """
        [TOSCA_Parser] compute_dependencies: augment the TOSCA nodetemplate
        with information on requirements (aka related models)
        """

        parser = TOSCA_Parser("", "user", "pass")

        class FakeNode:
            def __init__(self, name, requirements):
                self.name = name
                self.requirements = requirements

        main = FakeNode("main", [])
        dep = FakeNode("dep", [{"relation": {"node": "main"}}])

        models_by_name = {"main": main, "dep": dep}

        class FakeTemplate:
            nodetemplates = [dep, main]

        parser.compute_dependencies(FakeTemplate, models_by_name)

        templates = FakeTemplate.nodetemplates
        augmented_dep = templates[0]
        augmented_main = templates[1]

        self.assertIsInstance(augmented_dep.dependencies[0], FakeNode)
        self.assertEqual(augmented_dep.dependencies[0].name, "main")
        self.assertEqual(augmented_dep.dependencies_names[0], "main")

        self.assertEqual(len(augmented_main.dependencies), 0)
        self.assertEqual(len(augmented_main.dependencies_names), 0)
Ejemplo n.º 4
0
    def test_translate_exception(self):
        """
        [TOSCA_Parser] translate_exception: convert a TOSCA Parser exception in a user readable string
        """
        e = TOSCA_Parser._translate_exception("Non tosca exception")
        self.assertEqual(e, "Non tosca exception")

        e = TOSCA_Parser._translate_exception("""
MissingRequiredFieldError: some message
    followed by unreadable
    and mystic
        python error
        starting at line
            38209834 of some file
UnknownFieldError: with some message
    followed by useless things
ImportError: with some message
    followed by useless things
InvalidTypeError: with some message
    followed by useless things
TypeMismatchError: with some message
    followed by useless things
        """)
        self.assertEqual(
            e,
            """MissingRequiredFieldError: some message
UnknownFieldError: with some message
ImportError: with some message
InvalidTypeError: with some message
TypeMismatchError: with some message
""",
        )
Ejemplo n.º 5
0
    def test_related_models_creation(self, mock_save):
        """
        [TOSCA_Parser] Should save related models defined in a TOSCA recipe
        """

        recipe = """
tosca_definitions_version: tosca_simple_yaml_1_0

description: Create a new site with one user

imports:
   - custom_types/user.yaml
   - custom_types/site.yaml

topology_template:
  node_templates:

    # Site
    site_onlab:
      type: tosca.nodes.Site
      properties:
        name: Open Networking Lab
        site_url: http://onlab.us/
        hosts_nodes: True

    # User
    usertest:
      type: tosca.nodes.User
      properties:
        username: [email protected]
        email: [email protected]
        password: mypwd
        firstname: User
        lastname: Test
        is_admin: True
      requirements:
        - site:
            node: site_onlab
            relationship: tosca.relationships.BelongsToOne
"""

        parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)

        parser.execute()

        self.assertEqual(mock_save.call_count, 2)

        self.assertIsNotNone(parser.templates_by_model_name["site_onlab"])
        self.assertIsNotNone(parser.templates_by_model_name["usertest"])
        self.assertEqual(parser.ordered_models_name,
                         ["site_onlab", "usertest"])

        # check that the model was saved with the expected values
        saved_site = parser.saved_model_by_name["site_onlab"]
        self.assertEqual(saved_site.name, "Open Networking Lab")

        saved_user = parser.saved_model_by_name["usertest"]
        self.assertEqual(saved_user.firstname, "User")
        self.assertEqual(saved_user.site_id, 1)
Ejemplo n.º 6
0
    def test_populate_dependencies(self):
        """
        [TOSCA_Parser] populate_dependencies: if a recipe has dependencies, it
        should find the ID of the requirements and add it to the model
        """
        class FakeRecipe:
            requirements = [{
                "site": {
                    "node": "site_onlab",
                    "relationship": "tosca.relationship.BelongsToOne",
                }
            }]

        class FakeSite:
            id = 1
            name = "onlab"

        class FakeModel:
            name = "*****@*****.**"

        saved_models = {"site_onlab": FakeSite}

        model = TOSCA_Parser.populate_dependencies(FakeModel,
                                                   FakeRecipe.requirements,
                                                   saved_models)
        self.assertEqual(model.site_id, 1)
Ejemplo n.º 7
0
    def _handle_post(self, request, delete=False):

        headers = request.getAllHeaders()
        username = headers["xos-username"]
        password = headers["xos-password"]
        recipe = request.content.read()

        parser = TOSCA_Parser(recipe, username, password, delete=delete)
        d = GRPC_Client().create_secure_client(username, password, parser)
        tosca_execution = d.addCallback(self.execute_tosca)
        tosca_execution.addErrback(self.errorCallback, request)
        return d
Ejemplo n.º 8
0
    def test_basic_creation(self, mock_save):
        """
        [TOSCA_Parser] Should save models defined in a TOSCA recipe
        """
        recipe = """
tosca_definitions_version: tosca_simple_yaml_1_0

description: Persist xos-sample-gui-extension

imports:
   - custom_types/xosguiextension.yaml

topology_template:
  node_templates:

    # UI Extension
    test:
      type: tosca.nodes.XOSGuiExtension
      properties:
        name: test
        files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
"""

        parser = TOSCA_Parser(recipe, USERNAME, PASSWORD)

        parser.execute()

        # checking that the model has been saved
        mock_save.assert_called_with()

        self.assertIsNotNone(parser.templates_by_model_name["test"])
        self.assertEqual(parser.ordered_models_name, ["test"])

        # check that the model was saved with the expected values
        saved_model = parser.saved_model_by_name["test"]
        self.assertEqual(saved_model.name, "test")
        self.assertEqual(
            saved_model.files,
            "/spa/extensions/test/vendor.js, /spa/extensions/test/app.js",
        )
Ejemplo n.º 9
0
    def test_get_ordered_models_template(self):
        """
        [TOSCA_Parser] get_ordered_models_template: Create a list of templates based on topsorted models
        """
        ordered_models = ["foo", "bar"]

        templates = {"foo": "foo_template", "bar": "bar_template"}

        ordered_templates = TOSCA_Parser.get_ordered_models_template(
            ordered_models, templates)

        self.assertEqual(ordered_templates[0], "foo_template")
        self.assertEqual(ordered_templates[1], "bar_template")
Ejemplo n.º 10
0
    def test_basic_deletion(self, mock_model):
        """
        [TOSCA_Parser] Should delete models defined in a TOSCA recipe
        """
        recipe = """
    tosca_definitions_version: tosca_simple_yaml_1_0

    description: Persist xos-sample-gui-extension

    imports:
        - custom_types/node.yaml
        - custom_types/xosguiextension.yaml

    topology_template:
      node_templates:

        should_stay:
          type: tosca.nodes.Node
          properties:
            name: should_stay
            must-exist: true

        test:
          type: tosca.nodes.XOSGuiExtension
          properties:
            name: test
            files: /spa/extensions/test/vendor.js, /spa/extensions/test/app.js
    """

        parser = TOSCA_Parser(recipe, USERNAME, PASSWORD, delete=True)

        parser.execute()

        # checking that the model has been saved
        mock_model.assert_called_once_with()

        self.assertIsNotNone(parser.templates_by_model_name["test"])
        self.assertEqual(parser.ordered_models_name, ["should_stay", "test"])
Ejemplo n.º 11
0
    def test_populate_model(self):
        """
        [TOSCA_Parser] populate_model: augment the GRPC model with data from TOSCA
        """
        class FakeModel:
            pass

        data = {"name": "test", "foo": "bar", "number": 1}

        model = TOSCA_Parser.populate_model(FakeModel, data)

        self.assertEqual(model.name, "test")
        self.assertEqual(model.foo, "bar")
        self.assertEqual(model.number, 1)
Ejemplo n.º 12
0
    def test_get_tosca_models_by_name(self):
        """
        [TOSCA_Parser] get_tosca_models_by_name: should extract models from the TOSCA recipe and store them in a dict
        """
        class FakeNode:
            def __init__(self, name):
                self.name = name

        class FakeTemplate:
            nodetemplates = [FakeNode("model1"), FakeNode("model2")]

        res = TOSCA_Parser.get_tosca_models_by_name(FakeTemplate)
        self.assertIsInstance(res["model1"], FakeNode)
        self.assertIsInstance(res["model2"], FakeNode)

        self.assertEqual(res["model1"].name, "model1")
        self.assertEqual(res["model2"].name, "model2")
Ejemplo n.º 13
0
    def test_topsort_dependencies(self):
        """
        [TOSCA_Parser] topsort_dependencies: Create a list of models based on dependencies
        """
        class FakeTemplate:
            def __init__(self, name, deps):
                self.name = name
                self.dependencies_names = deps

        templates = {
            "deps": FakeTemplate("deps", ["main"]),
            "main": FakeTemplate("main", []),
        }

        sorted = TOSCA_Parser.topsort_dependencies(templates)

        self.assertEqual(sorted[0], "main")
        self.assertEqual(sorted[1], "deps")
Ejemplo n.º 14
0
    def test_populate_model_error(self):
        """
        [TOSCA_Parser] populate_model: should print a meaningful error message
        """
        class FakeModel:

            model_name = "FakeModel"

            def __setattr__(self, name, value):
                if name == "foo":
                    raise TypeError("reported exception")
                else:
                    super(FakeModel, self).__setattr__(name, value)

        data = {"name": "test", "foo": None, "number": 1}

        model = FakeModel()

        with self.assertRaises(Exception) as e:
            model = TOSCA_Parser.populate_model(model, data)
        self.assertEqual(
            str(e.exception),
            'Failed to set None on field foo for class FakeModel, Exception was: "reported exception"',
        )