Esempio n. 1
0
class TestJSONTranformer(TestCase):
    @classmethod
    def setUpClass(self):
        file = 'data/tests/test_nodes.json'
        self.importer = JSONImporter()
        self.microtosca = self.importer.Import(file)
        self.tranformer = JSONExporter()

    def test_transform_service(self):
        s1 = self.microtosca['my_service']
        dict_s1 = self.tranformer.transform_node_to_json(s1)
        self.assertEqual(dict_s1["type"], JSON_NODE_SERVICE)
        self.assertEqual(dict_s1["name"], "my_service")

    def test_transform_database(self):
        db = self.microtosca['my_datastore']
        dict_db = self.tranformer.transform_node_to_json(db)
        self.assertEqual(dict_db["type"], JSON_NODE_DATABASE)
        self.assertEqual(dict_db["name"], "my_datastore")

    def test_transform_messagebroker(self):
        mb = self.microtosca['my_messagebroker']
        dict_mb = self.tranformer.transform_node_to_json(mb)
        self.assertEqual(dict_mb["type"], JSON_NODE_MESSAGE_BROKER)
        self.assertEqual(dict_mb["name"], "my_messagebroker")

    def test_transform_router(self):
        mr = self.microtosca['my_messagerouter']
        dict_mr = self.tranformer.transform_node_to_json(mr)
        self.assertEqual(dict_mr["type"], JSON_NODE_MESSAGE_ROUTER)
        self.assertEqual(dict_mr["name"], "my_messagerouter")
Esempio n. 2
0
def jsontoyml(jsonfile, ymlfile):
    """
    Transform a JSON file into microTosca YML file
    """

    model = JSONImporter().Import(jsonfile)
    ymlString = YMLExporter().Export(model)
    click.echo(ymlString)
    with open(ymlfile, 'w') as outfile:
        outfile.write(ymlString)
 def setUpClass(self):
     file = 'data/tests/test_relationships_properties.json'
     self.importer = JSONImporter()
     self.microtosca = self.importer.Import(file)
     self.tranformer = JSONExporter()
class TestJSONTranformer(TestCase):
    @classmethod
    def setUpClass(self):
        file = 'data/tests/test_relationships_properties.json'
        self.importer = JSONImporter()
        self.microtosca = self.importer.Import(file)
        self.tranformer = JSONExporter()

    def test_relationship(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            False)

    def test_relationship_has_id(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target")
        self.assertIn("id", rel_dict.keys())

    def test_relationship_t(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_t")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            False)

    def test_relationship_c(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_c")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            False)

    def test_relationship_d(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_d")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            True)

    def test_relationship_tc(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_tc")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            False)

    def test_relationship_td(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_td")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            True)

    def test_relationship_cd(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_cd")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            False)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            True)

    def test_relationship_tcd(self):
        rel_dict = self._export_link_to_json_from_source_to_target(
            "source", "target_tcd")
        self.assertEqual(
            rel_dict[MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_TIMEOUT_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_CIRCUIT_BREAKER_PROPERTY],
            True)
        self.assertEqual(
            rel_dict[
                MICROTOSCA_RELATIONSHIPS_INTERACT_WITH_DYNAMIC_DISCOVEY_PROPERTY],
            True)

    def _export_link_to_json_from_source_to_target(self, source_name,
                                                   target_name):
        source = self.microtosca[source_name]
        target = self.microtosca[target_name]
        link_to_target = [
            link for link in source.interactions if link.target == target
        ]
        self.assertEqual(len(link_to_target), 1)
        rel_dict = self.tranformer.export_link_to_json(link_to_target[0])
        return rel_dict
class TestJSONImporterNodes(TestCase):

    @classmethod
    def setUpClass(self):
        file = 'data/tests/test_nodes.json'
        self.importer = JSONImporter()
        self.microtosca = self.importer.Import(file)

    def test_service(self):
        s1 = self.microtosca['my_service']
        self.assertIsInstance(s1, Service)
        self.assertEqual(s1.name, "my_service")

    def test_database(self):
        db = self.microtosca['my_datastore']
        self.assertIsInstance(db, Datastore)
        self.assertEqual(db.name, "my_datastore")

    def test_messagebroker(self):
        mb = self.microtosca['my_messagebroker']
        self.assertIsInstance(mb, MessageBroker)
        self.assertEqual(mb.name, "my_messagebroker")

    def test_messagerouter(self):
        mr = self.microtosca['my_messagerouter']
        self.assertIsInstance(mr, MessageRouter)
        self.assertEqual(mr.name, "my_messagerouter")
    
    def test_kservice(self):
        mr = self.microtosca['my_kservice']
        self.assertIsInstance(mr, KService)
        self.assertEqual(mr.name, "my_kservice")
    
    def test_kproxy(self):
        mr = self.microtosca['my_kproxy']
        self.assertIsInstance(mr, KProxy)
        self.assertEqual(mr.name, "my_kproxy")
    
    def test_kingress(self):
        mr = self.microtosca['my_kingress']
        self.assertIsInstance(mr, KIngress)
        self.assertEqual(mr.name, "my_kingress")

    def load_test_exceptions(self):
        with self.assertRaises(ImporterError):
            self.importer.load_node_from_json({"notype": JSON_NODE_SERVICE, "name": "prova"})
        with self.assertRaises(ImporterError):
            self.importer.load_node_from_json({"type": JSON_NODE_SERVICE, "noname": "prova"})
        with self.assertRaises(ImporterError):
            self.importer.load_node_from_json({"notype": JSON_NODE_SERVICE, "noname": "prova"})
        with self.assertRaises(ImporterError):
            self.importer.load_node_from_json({})

    def test_load_node_service(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_SERVICE, "name": "prova"})
        self.assertIsInstance(node, Service)
        self.assertEqual(node.name, "prova")

    def test_load_node_database(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_DATABASE, "name": "provadb"})
        self.assertIsInstance(node, Datastore)
        self.assertEqual(node.name, "provadb")

    def test_load_node_message_broker(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_MESSAGE_BROKER, "name": "provamb"})
        self.assertIsInstance(node, MessageBroker)
        self.assertEqual(node.name, "provamb")

    def test_load_node_message_router(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_MESSAGE_ROUTER, "name": "provamr"})
        self.assertIsInstance(node, MessageRouter)
        self.assertEqual(node.name, "provamr")
    
    def test_load_node_kservice(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_MESSAGE_ROUTER_KSERVICE, "name": "prova-kservice"})
        self.assertIsInstance(node, KService)
        self.assertEqual(node.name, "prova-kservice")
    
    def test_load_node_kproxy(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_MESSAGE_ROUTER_KPROXY, "name": "provakproxy"})
        self.assertIsInstance(node, KProxy)
        self.assertEqual(node.name, "provakproxy")
    
    def test_load_node_kingress(self):
        node = self.importer.load_node_from_json(
            {"type": JSON_NODE_MESSAGE_ROUTER_KINGRESS, "name": "provakingress"})
        self.assertIsInstance(node, KIngress)
        self.assertEqual(node.name, "provakingress")
 def setUpClass(self):
     file = 'data/tests/test_nodes.json'
     self.importer = JSONImporter()
     self.microtosca = self.importer.Import(file)
Esempio n. 7
0
from rest_framework.decorators import api_view
from django.views.decorators.csrf import csrf_exempt
from rest_framework.response import Response
import os
import json

from microfreshener.core.analyser import MicroToscaAnalyser
from microfreshener.core.analyser import MicroToscaAnalyserBuilder
from microfreshener.core.importer import JSONImporter, YMLImporter
from microfreshener.core.exporter import JSONExporter, YMLExporter
from microfreshener.core.model import MicroToscaModel
from microfreshener.core.model import Service, Datastore, CommunicationPattern
from microfreshener.core.errors import ImporterError, MicroFreshenerError
from microfreshener.core.refiner import KubernetesRefiner, IstioRefiner

json_importer = JSONImporter()
yml_importer = YMLImporter()
json_exporter = JSONExporter()
yml_exporter = YMLExporter()

file_name = "default.json"
model_file_path = os.path.join(settings.MEDIA_ROOT, file_name)
examples_path = os.path.join(settings.MEDIA_ROOT, "examples")
uploads_path = os.path.join(settings.MEDIA_ROOT, "uploads")
file_uploads_path = os.path.join(uploads_path, "upload.yml")
file_refine_path = os.path.join(uploads_path, "refinekubernetes.yml")
file_refine_istio_path = os.path.join(uploads_path, "refine_istio.yml")


@api_view(['GET'])
def graph_analysis(request):
class TestJSONImporterRelationships(TestCase):
    @classmethod
    def setUpClass(self):
        self.file = 'data/tests/test_relationships.json'
        self.importer = JSONImporter()
        self.json_data = self.importer.load_json(self.file)
        self.importer._load_microtosca(self.json_data)
        self.importer._load_nodes(self.json_data)

    # def test_load_node_id_from_json(self):
    #     link = self.json_data['links'][0]
    #     id = load_node_id_from_json(link)

    def test_load_source_node_from_json(self):
        link = self.json_data['links'][0]
        source = self.importer.load_source_node_from_json(link)
        self.assertEqual(source.name, 's1')
        self.assertIsInstance(source, Service)

    def test_load_source_node_from_json_error(self):
        link = self.json_data['links'][1]
        with self.assertRaises(ImporterError):
            self.importer.load_source_node_from_json(link)

    def test_load_target_node_from_json(self):
        json_data = self.importer.load_json(self.file)
        link = json_data['links'][0]
        target = self.importer.load_target_node_from_json(link)
        self.assertEqual(target.name, 's2')
        self.assertIsInstance(target, Service)

    def test_load_target_node_from_json_error(self):
        link = self.json_data['links'][1]
        with self.assertRaises(ImporterError):
            self.importer.load_target_node_from_json(link)

    def test_load_interaction_from_json(self):
        link = self.json_data['links'][2]
        interaction = self.importer.load_interaction_from_json(link)
        self.assertIsInstance(interaction, InteractsWith)
        self.assertIsInstance(interaction.source, Service)
        self.assertIsInstance(interaction.target, Datastore)

    def test_import_link_from_json(self):
        link = self.json_data['links'][3]
        interaction = self.importer.import_link_from_json(link)
        self.assertIsInstance(interaction, InteractsWith)
        self.assertIn(interaction, interaction.source.interactions)
        self.assertIn(interaction, interaction.target.incoming_interactions)
 def setUpClass(self):
     self.file = 'data/tests/test_relationships.json'
     self.importer = JSONImporter()
     self.json_data = self.importer.load_json(self.file)
     self.importer._load_microtosca(self.json_data)
     self.importer._load_nodes(self.json_data)
class TestJSONImporterRelationshipProperties(TestCase):


    @classmethod
    def setUpClass(self):
        self.file = 'data/tests/test_relationships_properties.json'
        self.importer = JSONImporter()
        self.microtosca = self.importer.Import(self.file)
    
    def test_relationship_empty_(self):
        link = self._load_relationship_from_source_to_target("source", "target")
        self.assertFalse(link.timeout)
        self.assertFalse(link.circuit_breaker)
        self.assertFalse(link.dynamic_discovery)
    
    def test_relationship_t(self):
        link = self._load_relationship_from_source_to_target("source", "target_t")
        self.assertTrue(link.timeout)
        self.assertFalse(link.circuit_breaker)
        self.assertFalse(link.dynamic_discovery)
        
    def test_relationship_c(self):
        link = self._load_relationship_from_source_to_target("source", "target_c")
        self.assertFalse(link.timeout)
        self.assertTrue(link.circuit_breaker)
        self.assertFalse(link.dynamic_discovery)
    
    def test_relationship_d(self):
        link = self._load_relationship_from_source_to_target("source", "target_d")
        self.assertFalse(link.timeout)
        self.assertFalse(link.circuit_breaker)
        self.assertTrue(link.dynamic_discovery)
    
    def test_relationship_tc(self):
        link = self._load_relationship_from_source_to_target("source", "target_tc")
        self.assertTrue(link.timeout)
        self.assertTrue(link.circuit_breaker)
        self.assertFalse(link.dynamic_discovery)
    
    def test_relationship_td(self):
        link = self._load_relationship_from_source_to_target("source", "target_td")
        self.assertTrue(link.timeout)
        self.assertFalse(link.circuit_breaker)
        self.assertTrue(link.dynamic_discovery)
    
    def test_relationship_cd(self):
        link = self._load_relationship_from_source_to_target("source", "target_cd")
        self.assertFalse(link.timeout)
        self.assertTrue(link.circuit_breaker)
        self.assertTrue(link.dynamic_discovery)
    
    def test_relationship_tcd(self):
        link = self._load_relationship_from_source_to_target("source", "target_tcd")
        self.assertTrue(link.timeout)
        self.assertTrue(link.circuit_breaker)
        self.assertTrue(link.dynamic_discovery)
    
    def _load_relationship_from_source_to_target(self, source_name, target_name):
        source = self.microtosca[source_name]
        target = self.microtosca[target_name]
        links_to_target = [link for link in source.interactions if link.target == target]
        self.assertEqual(len(links_to_target), 1)
        return links_to_target[0]