Exemple #1
0
    def test_altervm(self):
        """Test AlterVM"""
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("cpu.count", "=", 1),
                Feature("memory.size", "=", 512, "M"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

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

        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("cpu.count", "=", 2),
                Feature("memory.size", "=", 1024, "M"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", 1))

        radl_info = IM.AlterVM(infId, "0", str(radl), auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("cpu.count"), 2)
        self.assertEqual(
            parsed_radl_info.systems[0].getFeature('memory.size').getValue(
                'M'), 1024)

        IM.DestroyInfrastructure(infId, auth0)
Exemple #2
0
    def test_inf_cloud_order(self, suds_cli):
        """Test cloud selection in base of the auth data order."""

        n0, n1 = 1, 1  # Machines to deploy
        radl = RADL()
        radl.add(
            system("s0", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("cpu.count", "=", 1),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s0", n0))
        radl.add(
            system("s1", [
                Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                Feature("cpu.count", "=", 1),
                Feature("disk.0.os.credentials.username", "=", "user"),
                Feature("disk.0.os.credentials.password", "=", "pass")
            ]))
        radl.add(deploy("s1", n1))

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        self.register_cloudconnector("Mock0", cloud0)
        cloud1 = self.get_cloud_connector_mock("MyMock1")
        self.register_cloudconnector("Mock1", cloud1)
        auth0 = self.getAuth([0], [0], [("Mock0", 0), ("Mock1", 1)])
        infId = IM.CreateInfrastructure(str(radl), auth0)
        self.assertEqual(cloud0.launch.call_count, n0 + n1)
        IM.DestroyInfrastructure(infId, auth0)
Exemple #3
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)
Exemple #4
0
    def test_altervm(self):
        """Test AlterVM"""
        radl = RADL()
        radl.add(system("s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                               Feature("disk.0.os.credentials.username", "=", "user"),
                               Feature("cpu.count", "=", 1),
                               Feature("memory.size", "=", 512, "M"),
                               Feature("disk.0.os.credentials.password", "=", "pass")]))
        radl.add(deploy("s0", 1))

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

        radl = RADL()
        radl.add(system("s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                               Feature("disk.0.os.credentials.username", "=", "user"),
                               Feature("cpu.count", "=", 2),
                               Feature("memory.size", "=", 1024, "M"),
                               Feature("disk.0.os.credentials.password", "=", "pass")]))
        radl.add(deploy("s0", 1))

        radl_info = IM.AlterVM(infId, "0", str(radl), auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("cpu.count"), 2)
        self.assertEqual(parsed_radl_info.systems[0].getFeature('memory.size').getValue('M'), 1024)

        IM.DestroyInfrastructure(infId, auth0)
Exemple #5
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)
Exemple #6
0
    def complete_radl(self, radl):
        """
        Update passed radl with the stored RADL.
        """

        with self._lock:
            # Replace references of systems, networks and configures by its
            # definitions
            for s in radl.networks + radl.systems + radl.configures + radl.ansible_hosts:
                if s.reference:
                    aspect = self.radl.get(s)
                    if aspect is None:
                        raise Exception(
                            "Unknown reference in RADL to %s '%s'" % (type(s), s.getId()))
                    radl.add(aspect.clone(), "replace")

            # Add fake deploys to indicate the cloud provider associated to a
            # private network.
            system_counter = 0
            for n in radl.networks:
                if n.id in self.private_networks:
                    system_id = self.FAKE_SYSTEM + str(system_counter)
                    system_counter += 1
                    radl.add(
                        system(system_id, [Feature("net_interface.0.connection", "=", n.id)]))
                    radl.add(deploy(system_id, 0, self.private_networks[n.id]))

        # Check the RADL
        radl.check()
Exemple #7
0
    def test_create_disk_snapshot(self):
        """Test CreateDiskSnapshot """
        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))

        new_url = "mock0://linux.for.ev.er/test"

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        cloud0.create_snapshot = Mock(return_value=(True, new_url))
        self.register_cloudconnector("Mock0", cloud0)
        auth0 = self.getAuth([0], [], [("Mock0", 0)])

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

        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector = cloud0

        res = IM.CreateDiskSnapshot(infId, 0, 0, "test", True, auth0)
        self.assertEqual(res, new_url)

        self.assertEqual(cloud0.create_snapshot.call_count, 1)
Exemple #8
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)
Exemple #9
0
    def test_start_stop(self):
        """Test Start and Stop operations"""
        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(str(radl), auth0)

        res = IM.StopInfrastructure(infId, auth0)
        self.assertEqual(res, "")
        res = IM.StartInfrastructure(infId, auth0)
        self.assertEqual(res, "")

        res = IM.StartVM(infId, "0", auth0)
        self.assertEqual(res, "")
        res = IM.StopVM(infId, "0", auth0)
        self.assertEqual(res, "")

        IM.DestroyInfrastructure(infId, auth0)
Exemple #10
0
    def test_create_async(self):
        """Create Inf. async"""

        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))

        cloud = type("MyMock0", (CloudConnector, object), {})
        cloud.launch = Mock(side_effect=self.sleep_and_create_vm)
        cloud.finalize = Mock(return_value=(True, ""))
        self.register_cloudconnector("Mock", cloud)
        auth0 = self.getAuth([0], [], [("Mock", 0)])

        before = int(time.time())
        infId = IM.CreateInfrastructure(str(radl), auth0, True)
        delay = int(time.time()) - before

        self.assertLess(delay, 2)

        time.sleep(6)

        IM.DestroyInfrastructure(infId, auth0)
Exemple #11
0
    def test_get_vm_info(self):
        """
        Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and
        GetInfrastructureContMsg and GetInfrastructureState.
        """
        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(str(radl), auth0)

        radl_info = IM.GetVMInfo(infId, "0", auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("state"), "running")

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

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

        contmsg = IM.GetInfrastructureContMsg(infId, auth0)

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

        radl_info = IM.GetInfrastructureRADL(infId, auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("disk.0.os.credentials.username"), "user")

        IM.DestroyInfrastructure(infId, auth0)
Exemple #12
0
    def complete_radl(self, radl):
        """
        Update passed radl with the stored RADL.
        """

        with self._lock:
            # Replace references of systems, networks and configures by its
            # definitions
            for s in radl.networks + radl.systems + radl.configures + radl.ansible_hosts:
                if s.reference:
                    aspect = self.radl.get(s)
                    if aspect is None:
                        raise Exception(
                            "Unknown reference in RADL to %s '%s'" %
                            (type(s), s.getId()))
                    radl.add(aspect.clone(), "replace")

            # Add fake deploys to indicate the cloud provider associated to a
            # private network.
            system_counter = 0
            for n in radl.networks:
                if n.id in self.private_networks:
                    system_id = self.FAKE_SYSTEM + str(system_counter)
                    system_counter += 1
                    radl.add(
                        system(
                            system_id,
                            [Feature("net_interface.0.connection", "=", n.id)
                             ]))
                    radl.add(deploy(system_id, 0, self.private_networks[n.id]))

        # Check the RADL
        radl.check()
Exemple #13
0
    def test_db(self):
        """ Test DB data access """
        inf = InfrastructureInfo()
        inf.id = "1"
        inf.auth = self.getAuth([0], [], [("Dummy", 0)])
        cloud = CloudInfo()
        cloud.type = "Dummy"
        radl = RADL()
        radl.add(
            system(
                "s0",
                [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")]))
        radl.add(deploy("s0", 1))
        vm1 = VirtualMachine(inf, "1", cloud, radl, radl)
        vm2 = VirtualMachine(inf, "2", cloud, radl, radl)
        inf.vm_list = [vm1, vm2]
        inf.vm_master = vm1
        # first create the DB table
        Config.DATA_DB = "sqlite:///tmp/ind.dat"
        InfrastructureList.load_data()

        success = InfrastructureList._save_data_to_db(Config.DATA_DB,
                                                      {"1": inf})
        self.assertTrue(success)

        res = InfrastructureList._get_data_from_db(Config.DATA_DB)
        self.assertEqual(len(res), 1)
        self.assertEqual(len(res['1'].vm_list), 2)
        self.assertEqual(res['1'].vm_list[0], res['1'].vm_master)
        self.assertEqual(
            res['1'].vm_master.info.systems[0].getValue("disk.0.image.url"),
            "mock0://linux.for.ev.er")
        self.assertTrue(res['1'].auth.compare(inf.auth,
                                              "InfrastructureManager"))
Exemple #14
0
    def test_get_vm_info(self):
        """
        Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and
        GetInfrastructureContMsg and GetInfrastructureState.
        """
        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(str(radl), auth0)

        radl_info = IM.GetVMInfo(infId, "0", auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("state"),
                         "running")

        radl_info = IM.GetVMInfo(infId, "0", auth0, True)
        parsed_radl_info = parse_radl_json(radl_info)
        self.assertEqual(parsed_radl_info.systems[0].getValue("state"),
                         "running")

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

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

        InfrastructureList.infrastructure_list[infId].cont_out = "Header"
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector = MagicMock()
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector.error_messages = "TESTMSG"
        contmsg = IM.GetInfrastructureContMsg(infId, auth0)
        header_contmsg = IM.GetInfrastructureContMsg(infId, auth0, True)
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].cloud_connector = None

        self.assertIn("TESTMSG", contmsg)
        self.assertNotIn("TESTMSG", header_contmsg)
        self.assertIn("Header", header_contmsg)

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

        radl_info = IM.GetInfrastructureRADL(infId, auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(
            parsed_radl_info.systems[0].getValue(
                "disk.0.os.credentials.username"), "user")

        IM.DestroyInfrastructure(infId, auth0)
Exemple #15
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)
Exemple #16
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)
Exemple #17
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)
Exemple #18
0
    def test_inf_addresources3(self):
        """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)
Exemple #19
0
    def test_inf_cloud_order(self):
        """Test cloud selection in base of the auth data order."""

        n0, n1 = 1, 1  # Machines to deploy
        radl = RADL()
        radl.add(system("s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                               Feature("cpu.count", "=", 1),
                               Feature("disk.0.os.credentials.username", "=", "user"),
                               Feature("disk.0.os.credentials.password", "=", "pass")]))
        radl.add(deploy("s0", n0))
        radl.add(system("s1", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"),
                               Feature("cpu.count", "=", 1),
                               Feature("disk.0.os.credentials.username", "=", "user"),
                               Feature("disk.0.os.credentials.password", "=", "pass")]))
        radl.add(deploy("s1", n1))

        cloud0 = self.get_cloud_connector_mock("MyMock0")
        self.register_cloudconnector("Mock0", cloud0)
        cloud1 = self.get_cloud_connector_mock("MyMock1")
        self.register_cloudconnector("Mock1", cloud1)
        auth0 = self.getAuth([0], [0], [("Mock0", 0), ("Mock1", 1)])
        infId = IM.CreateInfrastructure(str(radl), auth0)
        self.assertEqual(cloud0.launch.call_count, n0 + n1)
        IM.DestroyInfrastructure(infId, auth0)
Exemple #20
0
    def test_export_import(self):
        """Test ExportInfrastructure and ImportInfrastructure operations"""
        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(str(radl), auth0)

        res = IM.ExportInfrastructure(infId, True, auth0)
        new_inf_id = IM.ImportInfrastructure(res, auth0)

        IM.DestroyInfrastructure(new_inf_id, auth0)
Exemple #21
0
    def test_inf_removeresources(self):
        """Deploy 4 VMs and remove 2"""
        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", 4))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)
        cont = IM.RemoveResource(infId, ['0', '1'], auth0)
        self.assertEqual(cont, 2)
        vms = IM.GetInfrastructureInfo(infId, auth0)
        self.assertEqual(sorted(vms), ['2', '3'])

        IM.DestroyInfrastructure(infId, auth0)
Exemple #22
0
    def test_reconfigure(self):
        """Reconfigure."""
        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(str(radl), auth0)

        reconf_radl = """configure test (\n@begin\n---\n  - tasks:\n      - debug: msg="RECONFIGURERADL"\n@end\n)"""
        IM.Reconfigure(infId, reconf_radl, auth0)
        IM.Reconfigure(infId, reconf_radl, auth0, ['0'])

        IM.DestroyInfrastructure(infId, auth0)
Exemple #23
0
    def addresources(infid=None):

        access_token = oidc_blueprint.session.token['access_token']

        auth_data = utils.getUserAuthData(access_token, cred,
                                          session["userid"])
        headers = {"Authorization": auth_data, "Accept": "text/plain"}

        form_data = request.form.to_dict()

        url = "%s/infrastructures/%s/radl" % (settings.imUrl, infid)
        response = requests.get(url, headers=headers)

        if response.ok:
            radl = None
            try:
                radl = radl_parse.parse_radl(response.text)
                radl.deploys = []
                for system in radl.systems:
                    if "%s_num" % system.name in form_data:
                        vm_num = int(form_data["%s_num" % system.name])
                        if vm_num > 0:
                            radl.deploys.append(deploy(system.name, vm_num))
            except Exception as ex:
                flash(
                    "Error parsing RADL: \n%s\n%s" % (str(ex), response.text),
                    'error')

            if radl:
                headers = {
                    "Authorization": auth_data,
                    "Accept": "application/json"
                }
                url = "%s/infrastructures/%s" % (settings.imUrl, infid)
                response = requests.post(url, headers=headers, data=str(radl))

                if response.ok:
                    num = len(response.json()["uri-list"])
                    flash("%d nodes added successfully" % num, 'info')
                else:
                    flash("Error adding nodesL: \n%s" % (response.text),
                          'error')

            return redirect(url_for('showinfrastructures'))
        else:
            flash("Error getting RADL: \n%s" % (response.text), 'error')
            return redirect(url_for('showinfrastructures'))
Exemple #24
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", ex.exception.message)

        IM.DestroyInfrastructure(infId, auth0)
Exemple #25
0
    def test_export_import(self):
        """Test ExportInfrastructure and ImportInfrastructure operations"""
        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(str(radl), auth0)

        res = IM.ExportInfrastructure(infId, True, auth0)
        new_inf_id = IM.ImportInfrastructure(res, auth0)

        IM.DestroyInfrastructure(new_inf_id, auth0)
Exemple #26
0
    def addresources(infid=None):

        access_token = oidc_blueprint.session.token['access_token']
        form_data = request.form.to_dict()

        auth_data = utils.getUserAuthData(access_token, cred,
                                          session["userid"])
        try:
            response = im.get_inf_property(infid, 'radl', auth_data)
        except Exception as ex:
            flash("Error: %s." % ex, 'error')

        if response.ok:
            radl = None
            try:
                radl = radl_parse.parse_radl(response.text)
                radl.deploys = []
                for system in radl.systems:
                    sys_dep = deploy(system.name, 0)
                    if "%s_num" % system.name in form_data:
                        vm_num = int(form_data["%s_num" % system.name])
                        if vm_num > 0:
                            sys_dep.vm_number = vm_num
                    radl.deploys.append(sys_dep)
            except Exception as ex:
                flash(
                    "Error parsing RADL: \n%s\n%s" % (str(ex), response.text),
                    'error')

            if radl:
                try:
                    response = im.addresource_inf(infid, str(radl), auth_data)
                    if not response.ok:
                        raise Exception(response.text)
                    num = len(response.json()["uri-list"])
                    flash("%d nodes added successfully" % num, 'info')
                except Exception as ex:
                    flash("Error adding nodes: \n%s\n%s" % (ex, response.text),
                          'error')

            return redirect(url_for('showinfrastructures'))
        else:
            flash("Error getting RADL: \n%s" % (response.text), 'error')
            return redirect(url_for('showinfrastructures'))
Exemple #27
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)
Exemple #28
0
    def test_reconfigure(self):
        """Reconfigure."""
        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(str(radl), auth0)

        reconf_radl = """configure test (\n@begin\n---\n  - tasks:\n      - debug: msg="RECONFIGURERADL"\n@end\n)"""
        IM.Reconfigure(infId, reconf_radl, auth0)
        IM.Reconfigure(infId, reconf_radl, auth0, ['0'])

        IM.DestroyInfrastructure(infId, auth0)
Exemple #29
0
    def test_inf_removeresources(self):
        """Deploy 4 VMs and remove 2"""
        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", 4))

        auth0 = self.getAuth([0], [], [("Dummy", 0)])
        infId = IM.CreateInfrastructure(str(radl), auth0)
        cont = IM.RemoveResource(infId, ['0', '1'], auth0)
        self.assertEqual(cont, 2)
        vms = IM.GetInfrastructureInfo(infId, auth0)
        self.assertEqual(sorted(vms), ['2', '3'])

        IM.DestroyInfrastructure(infId, auth0)
Exemple #30
0
    def test_inf_addresources1(self):
        """Deploy n independent virtual machines."""

        n = 20  # Machines to deploy
        Config.MAX_SIMULTANEOUS_LAUNCHES = 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)
Exemple #31
0
    def test_get_vm_info(self):
        """
        Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and
        GetInfrastructureContMsg and GetInfrastructureState.
        """
        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(str(radl), auth0)

        radl_info = IM.GetVMInfo(infId, "0", auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(parsed_radl_info.systems[0].getValue("state"),
                         "running")

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

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

        contmsg = IM.GetInfrastructureContMsg(infId, auth0)

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

        radl_info = IM.GetInfrastructureRADL(infId, auth0)
        parsed_radl_info = parse_radl(str(radl_info))
        self.assertEqual(
            parsed_radl_info.systems[0].getValue(
                "disk.0.os.credentials.username"), "user")

        IM.DestroyInfrastructure(infId, auth0)
Exemple #32
0
    def test_start_stop(self):
        """Test Start and Stop operations"""
        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(str(radl), auth0)

        res = IM.StopInfrastructure(infId, auth0)
        self.assertEqual(res, "")
        res = IM.StartInfrastructure(infId, auth0)
        self.assertEqual(res, "")

        res = IM.StartVM(infId, "0", auth0)
        self.assertEqual(res, "")
        res = IM.StopVM(infId, "0", auth0)
        self.assertEqual(res, "")

        IM.DestroyInfrastructure(infId, auth0)
Exemple #33
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)
Exemple #34
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)
Exemple #35
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)
Exemple #36
0
    def test_inf_creation_errors(self):
        """Create infrastructure with errors"""

        radl = """"
            network publica (outbound = 'yes')
            network privada ()
            system front (
            net_interface.0.connection = 'publica' and
            net_interface.1.connection = 'privada' and
            disk.0.image.url = ['one://localhost/image', 'http://localhost:443/image'] and
            disk.0.os.credentials.username = '******'
            )
            system wn (
            net_interface.0.connection = 'privada' and
            disk.0.image.url = ['one://localhost/image', 'http://localhost:443/image'] and
            disk.0.os.credentials.username = '******'
            )
            deploy front 1
            deploy wn 1
        """

        # this case raises an exception
        auth0 = Authentication([{
            'type': 'InfrastructureManager',
            'username': '******',
            'password': '******'
        }])
        with self.assertRaises(Exception) as ex:
            IM.CreateInfrastructure(radl, auth0)

        self.assertEqual(str(ex.exception), "No cloud provider available")

        # this case must fail with "no concrete system" error
        auth0 = Authentication([{
            'id': 'ost',
            'type': 'OpenStack',
            'username': '******',
            'password': '******',
            'tenant': 'ten',
            'host': 'localhost:5000'
        }, {
            'type': 'InfrastructureManager',
            'username': '******',
            'password': '******'
        }])

        with self.assertRaises(Exception) as ex:
            IM.CreateInfrastructure(radl, auth0)
        self.assertIn(
            'Error launching the VMs of type front to cloud ID ost of type OpenStack.'
            ' Error, no concrete system to deploy: front in cloud: ost. '
            'Check if a correct image is being used', str(ex.exception))

        # this case must work OK
        auth0 = Authentication([{
            'id': 'dummy',
            'type': 'Dummy'
        }, {
            'type': 'InfrastructureManager',
            'username': '******',
            'password': '******'
        }])
        IM.CreateInfrastructure(radl, auth0)

        Config.MAX_VM_FAILS = 3
        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))
        cloud = type("MyMock0", (CloudConnector, object), {})
        cloud.launch = Mock(return_value=[(False, "e1")])
        cloud.finalize = Mock(return_value=(True, ""))
        self.register_cloudconnector("Mock", cloud)
        auth0 = self.getAuth([0], [], [("Mock", 0)])

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

        self.assertIn((
            "Error launching the VMs of type s0 to cloud ID cloud0 of type Mock. "
            "Attempt 1: e1\nAttempt 2: e1\nAttempt 3: e1"), str(ex.exception))

        self.assertEqual(cloud.finalize.call_count, 1)