Пример #1
0
    def test_inf_addresources0(self):
        """Deploy single virtual machines and test reference."""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure("", auth0)

        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(vms, [0])

        # Test references
        radl = RADL()
        radl.add(system("s0", reference=True))
        radl.add(deploy("s0", 1))
        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(vms, [1])

        IM.DestroyInfrastructure(infId, auth0)
Пример #2
0
    def test_inf_addresources_without_credentials(self):
        """Deploy single virtual machine without credentials to check that it raises the correct exception."""

        radl = RADL()
        radl.add(
            system(
                "s0",
                [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure("", auth0)

        vms = IM.AddResource(infId, str(radl), auth0)

        self.assertEqual(vms, [0])

        res = IM.GetInfrastructureState(infId, auth0)
        self.assertEqual(res['state'], VirtualMachine.FAILED)

        res = IM.GetVMContMsg(infId, 0, auth0)
        self.assertEqual(res, (
            "Error launching the VMs of type s0 to cloud ID cloud0 of type Dummy."
            " No username for deploy: s0\n"))

        IM.DestroyInfrastructure(infId, auth0)
Пример #3
0
Файл: REST.py Проект: bbsyaya/im
def RESTAddResource(id=None):
    try:
        auth = get_auth_header()
    except:
        return return_error(401, "No authentication data provided")

    try:
        context = True
        if "context" in bottle.request.params.keys():
            str_ctxt = bottle.request.params.get("context").lower()
            if str_ctxt in ['yes', 'true', '1']:
                context = True
            elif str_ctxt in ['no', 'false', '0']:
                context = False
            else:
                return return_error(400,
                                    "Incorrect value in context parameter")

        content_type = get_media_type('Content-Type')
        radl_data = bottle.request.body.read().decode("utf-8")

        if content_type:
            if "application/json" in content_type:
                radl_data = parse_radl_json(radl_data)
            elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type:
                content_type = "text/plain"
            else:
                return return_error(415,
                                    "Unsupported Media Type %s" % content_type)

        vm_ids = InfrastructureManager.AddResource(id, radl_data, auth,
                                                   context)

        protocol = "http://"
        if Config.REST_SSL:
            protocol = "https://"
        res = []
        for vm_id in vm_ids:
            res.append(protocol + bottle.request.environ['HTTP_HOST'] +
                       "/infrastructures/" + str(id) + "/vms/" + str(vm_id))

        return format_output(res, "text/uri-list", "uri-list", "uri")
    except DeletedInfrastructureException as ex:
        return return_error(404, "Error Adding resources: " + str(ex))
    except IncorrectInfrastructureException as ex:
        return return_error(404, "Error Adding resources: " + str(ex))
    except UnauthorizedUserException as ex:
        return return_error(403, "Error Adding resources: " + str(ex))
    except Exception as ex:
        logger.exception("Error Adding resources")
        return return_error(400, "Error Adding resources: " + str(ex))
Пример #4
0
    def test_inf_creation_addition_clouds(self):
        """Add resources infrastructure with an incorrect RADL with 2 clouds."""

        radl = """"
            network publica (outbound = 'yes')
            network privada ()
            system front (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'publica' and
            net_interface.1.connection = 'privada' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )
            system wn (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'privada' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )
            deploy front 1 cloud0
            deploy wn 1
        """

        auth0 = self.getAuth([0], [], [("Dummy", 0), ("Dummy", 1)])
        infId = IM.CreateInfrastructure(radl, auth0)

        radl = """
            network privada
            system wn
            deploy wn 1 cloud1
        """

        with self.assertRaises(Exception) as ex:
            _ = IM.AddResource(infId, radl, auth0)
        self.assertIn(
            "Two deployments that have to be launched in the same cloud provider"
            " are asked to be deployed in different cloud providers",
            str(ex.exception))
Пример #5
0
    def test_inf_addresources_without_credentials(self):
        """Deploy single virtual machine without credentials to check that it raises the correct exception."""

        radl = RADL()
        radl.add(
            system(
                "s0",
                [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")]))
        radl.add(deploy("s0", 1))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure("", auth0)

        with self.assertRaises(Exception) as ex:
            IM.AddResource(infId, str(radl), auth0)

        self.assertIn("No username", str(ex.exception))

        IM.DestroyInfrastructure(infId, auth0)
Пример #6
0
    def test_inf_addresources3(self, suds_cli):
        """Test cloud selection."""

        n0, n1 = 2, 5  # Machines to deploy
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                SoftFeatures(10, [Feature("memory.size", "<=", 500)]),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(
            system("s1", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                SoftFeatures(10, [Feature("memory.size", ">=", 800)]),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", n0))
        radl.add(deploy("s1", n1))

        Config.MAX_SIMULTANEOUS_LAUNCHES = 10

        def concreteSystem(s, mem):
            return [system(s.name, [Feature("memory.size", "=", mem)])]

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        cloud0.concreteSystem = lambda _0, s, _1: concreteSystem(s, 500)
        self.register_cloudconnector("Mock0", cloud0)
        cloud1 = self.get_cloud_connector_mock("MyMock1")
        cloud1.concreteSystem = lambda _0, s, _1: concreteSystem(s, 1000)
        self.register_cloudconnector("Mock1", cloud1)
        auth0 = self.getAuth([0], [0], [("Mock0", 0), ("Mock1", 1)])
        infId = IM.CreateInfrastructure("", auth0)
        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(len(vms), n0 + n1)
        self.assertEqual(cloud0.launch.call_count, n0)
        self.assertEqual(cloud1.launch.call_count, n1)
        for call, _ in cloud0.launch.call_args_list + cloud1.launch.call_args_list:
            self.assertEqual(call[3], 1)
        IM.DestroyInfrastructure(infId, auth0)
Пример #7
0
    def test_inf_addresources2(self):
        """Deploy independent virtual machines in two cloud providers."""

        n0, n1 = 2, 5  # Machines to deploy
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(
            system("s1", [
                Feature("disk.0.image.url", "=", "mock1://wind.ows.suc.kz"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.private_key", "=",
                        "private_key")
            ]))
        radl.add(deploy("s0", n0))
        radl.add(deploy("s1", n1))

        Config.MAX_SIMULTANEOUS_LAUNCHES = 10

        def concreteSystem(s, cloud_id):
            url = s.getValue("disk.0.image.url")
            return [s.clone()] if url.partition(":")[0] == cloud_id else []

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        cloud0.concreteSystem = lambda _0, s, _1: concreteSystem(s, "mock0")
        self.register_cloudconnector("Mock0", cloud0)
        cloud1 = self.get_cloud_connector_mock("MyMock1")
        cloud1.concreteSystem = lambda _0, s, _1: concreteSystem(s, "mock1")
        self.register_cloudconnector("Mock1", cloud1)
        auth0 = self.getAuth([0], [], [("Mock0", 0), ("Mock1", 1)])
        infId = IM.CreateInfrastructure("", auth0)
        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(len(vms), n0 + n1)
        self.assertEqual(cloud0.launch.call_count, n0)
        self.assertEqual(cloud1.launch.call_count, n1)
        for call, _ in cloud0.launch.call_args_list + cloud1.launch.call_args_list:
            self.assertEqual(call[3], 1)
        IM.DestroyInfrastructure(infId, auth0)
Пример #8
0
    def test_inf_addresources1(self):
        """Deploy n independent virtual machines."""

        n = 20  # Machines to deploy
        Config.MAX_SIMULTANEOUS_LAUNCHES = int(n / 2)  # Test the pool
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", n))
        cloud = self.get_cloud_connector_mock()
        self.register_cloudconnector("Mock", cloud)
        auth0 = self.getAuth([0], [], [("Mock", 0)])
        infId = IM.CreateInfrastructure("", auth0)
        vms = IM.AddResource(infId, str(radl), auth0)
        self.assertEqual(len(vms), n)
        self.assertEqual(cloud.launch.call_count, n)
        for call, _ in cloud.launch.call_args_list:
            self.assertEqual(call[3], 1)
        IM.DestroyInfrastructure(infId, auth0)
Пример #9
0
    def test_inf_addresources4(self):
        """Deploy a virtual machine when the first cloud provider fails."""

        radl = RADL()
        radl.add(
            system("s0", [
                Feature(
                    "disk.0.image.url", "=",
                    ["mock0://linux.for.ev.er", "mock1://linux.for.ev.er"]),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))
        cloud0 = self.get_cloud_connector_mock("MyMock0")
        self.register_cloudconnector("Mock0", cloud0)
        cloud1 = type("MyMock1", (CloudConnector, object), {})
        cloud1.launch = Mock(return_value=[(False, "Error")])
        self.register_cloudconnector("Mock1", cloud1)
        auth0 = self.getAuth([0], [], [("Mock1", 1), ("Mock0", 0)])

        n = 3
        Config.MAX_VM_FAILS = n
        infId = IM.CreateInfrastructure("", auth0)
        vms = IM.AddResource(infId, str(radl), auth0)
        Config.MAX_VM_FAILS = 1

        self.assertEqual(len(vms), 1)
        # The first attempt in cloud 0 fails
        self.assertEqual(cloud0.launch.call_count, 1)
        # The rest of attempts are in cloud 1
        self.assertEqual(cloud1.launch.call_count, n)
        for call, _ in cloud0.launch.call_args_list:
            self.assertEqual(call[3], 1)
        for call, _ in cloud1.launch.call_args_list:
            self.assertEqual(call[3], 1)

        IM.DestroyInfrastructure(infId, auth0)
Пример #10
0
    def test_inf_lifecycle(self):
        """Test Infrastructure lifecycle"""
        radl = """"
            network publica (outbound = 'yes')

            system front (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'publica' and
            net_interface.0.ip = '10.0.0.1' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux' and
            disk.1.size=1GB and
            disk.1.device='hdb' and
            disk.1.fstype='ext4' and
            disk.1.mount_path='/mnt/disk' and
            disk.0.applications contains (name = 'ansible.modules.micafer.hadoop') and
            disk.0.applications contains (name='gmetad') and
            disk.0.applications contains (name='wget')
            )

            deploy front 1
        """

        auth0 = self.getAuth([0], [], [("Mock", 0)])
        IM._reinit()
        Config.PLAYBOOK_RETRIES = 1
        Config.CONTEXTUALIZATION_DIR = os.path.dirname(
            os.path.realpath(__file__)) + "/../../contextualization"
        Config.CONFMAMAGER_CHECK_STATE_INTERVAL = 0.001
        cloud0 = self.get_cloud_connector_mock("MyMock")
        self.register_cloudconnector("Mock", cloud0)

        infId = IM.CreateInfrastructure(str(radl), auth0)

        time.sleep(15)

        state = IM.GetInfrastructureState(infId, auth0)
        self.assertEqual(state["state"], "unconfigured")

        InfrastructureList.infrastructure_list[infId].ansible_configured = True

        IM.Reconfigure(infId, "", auth0)

        time.sleep(2)

        state = IM.GetInfrastructureState(infId, auth0)
        self.assertEqual(state["state"], "running")

        add_radl = RADL()
        add_radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        add_radl.add(deploy("s0", 1))

        vms = IM.AddResource(infId, str(add_radl), auth0)
        self.assertEqual(vms, [1])

        state = IM.GetVMProperty(infId, "1", "state", auth0)
        self.assertEqual(state, "running")

        contmsg = IM.GetVMContMsg(infId, "1", auth0)
        self.assertEqual(contmsg, "")

        cont = IM.RemoveResource(infId, ['1'], auth0)
        self.assertEqual(cont, 1)

        IM.DestroyInfrastructure(infId, auth0)
Пример #11
0
def RESTAddResource(infid=None):
    try:
        auth = get_auth_header()
    except:
        return return_error(401, "No authentication data provided")

    try:
        context = True
        if "context" in bottle.request.params.keys():
            str_ctxt = bottle.request.params.get("context").lower()
            if str_ctxt in ['yes', 'true', '1']:
                context = True
            elif str_ctxt in ['no', 'false', '0']:
                context = False
            else:
                return return_error(400,
                                    "Incorrect value in context parameter")

        content_type = get_media_type('Content-Type')
        radl_data = bottle.request.body.read().decode("utf-8")
        tosca_data = None
        remove_list = []

        if content_type:
            if "application/json" in content_type:
                radl_data = parse_radl_json(radl_data)
            elif "text/yaml" in content_type:
                tosca_data = Tosca(radl_data)
                auth = InfrastructureManager.check_auth_data(auth)
                sel_inf = InfrastructureManager.get_infrastructure(infid, auth)
                # merge the current TOSCA with the new one
                if isinstance(sel_inf.extra_info['TOSCA'], Tosca):
                    tosca_data = sel_inf.extra_info['TOSCA'].merge(tosca_data)
                remove_list, radl_data = tosca_data.to_radl(sel_inf)
            elif "text/plain" in content_type or "*/*" in content_type or "text/*" in content_type:
                content_type = "text/plain"
            else:
                return return_error(415,
                                    "Unsupported Media Type %s" % content_type)

        if remove_list:
            InfrastructureManager.RemoveResource(infid, remove_list, auth,
                                                 context)

        vm_ids = InfrastructureManager.AddResource(infid, radl_data, auth,
                                                   context)

        # Replace the TOSCA document
        if tosca_data:
            sel_inf = InfrastructureManager.get_infrastructure(infid, auth)
            sel_inf.extra_info['TOSCA'] = tosca_data

        res = []
        for vm_id in vm_ids:
            res.append(
                get_full_url("/infrastructures/" + str(infid) + "/vms/" +
                             str(vm_id)))

        return format_output(res, "text/uri-list", "uri-list", "uri")
    except DeletedInfrastructureException as ex:
        return return_error(404, "Error Adding resources: %s" % ex.args[0])
    except IncorrectInfrastructureException as ex:
        return return_error(404, "Error Adding resources: %s" % ex.args[0])
    except UnauthorizedUserException as ex:
        return return_error(403, "Error Adding resources: %s" % ex.args[0])
    except Exception as ex:
        logger.exception("Error Adding resources")
        return return_error(400, "Error Adding resources: %s" % ex.args[0])
Пример #12
0
    def test_inf_addresources5(self):
        """Deploy n independent virtual machines."""

        radl = """"
            network publica (outbound = 'yes')
            network privada ()

            system front (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'publica' and
            net_interface.1.connection = 'privada' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )

            system wn (
            cpu.arch='x86_64' and
            cpu.count>=1 and
            memory.size>=512m and
            net_interface.0.connection = 'privada' and
            disk.0.image.url = 'mock0://linux.for.ev.er' and
            disk.0.os.credentials.username = '******' and
            disk.0.os.credentials.password = '******' and
            disk.0.os.name = 'linux'
            )

            deploy front 1
            deploy wn 3
            deploy wn 2
        """
        cloud = type("MyMock0", (CloudConnector, object), {})
        cloud.launch = Mock(side_effect=self.sleep_and_create_vm)
        self.register_cloudconnector("Mock", cloud)
        auth0 = self.getAuth([0], [], [("Mock", 0)])
        infId = IM.CreateInfrastructure("", auth0)

        # in this case it will take aprox 20 secs
        before = int(time.time())
        Config.MAX_SIMULTANEOUS_LAUNCHES = 1
        vms = IM.AddResource(infId, str(radl), auth0)
        delay = int(time.time()) - before
        self.assertLess(delay, 17)
        self.assertGreater(delay, 14)

        self.assertEqual(len(vms), 6)
        self.assertEqual(cloud.launch.call_count, 3)
        self.assertEqual(cloud.launch.call_args_list[0][0][3], 1)
        self.assertEqual(cloud.launch.call_args_list[1][0][3], 3)
        self.assertEqual(cloud.launch.call_args_list[2][0][3], 2)

        cloud = type("MyMock0", (CloudConnector, object), {})
        cloud.launch = Mock(side_effect=self.sleep_and_create_vm)
        self.register_cloudconnector("Mock", cloud)

        # in this case it will take aprox 5 secs
        before = int(time.time())
        Config.MAX_SIMULTANEOUS_LAUNCHES = 3  # Test the pool
        vms = IM.AddResource(infId, str(radl), auth0)
        delay = int(time.time()) - before
        self.assertLess(delay, 7)
        self.assertGreater(delay, 4)
        Config.MAX_SIMULTANEOUS_LAUNCHES = 1

        self.assertEqual(len(vms), 6)
        self.assertEqual(cloud.launch.call_count, 3)
        self.assertEqual(cloud.launch.call_args_list[0][0][3], 1)
        self.assertEqual(cloud.launch.call_args_list[1][0][3], 3)
        self.assertEqual(cloud.launch.call_args_list[2][0][3], 2)

        IM.DestroyInfrastructure(infId, auth0)