Exemple #1
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 #2
0
def RESTGetInfrastructureProperty(id=None, prop=None):
    try:
        auth = get_auth_header()
    except:
        return return_error(401, "No authentication data provided")

    try:
        if prop == "contmsg":
            res = InfrastructureManager.GetInfrastructureContMsg(id, auth)
        elif prop == "radl":
            res = InfrastructureManager.GetInfrastructureRADL(id, auth)
        elif prop == "state":
            accept = get_media_type('Accept')
            if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept:
                return return_error(415, "Unsupported Accept Media Types: %s" % accept)
            bottle.response.content_type = "application/json"
            res = InfrastructureManager.GetInfrastructureState(id, auth)
            return format_output(res, default_type="application/json", field_name="state")
        else:
            return return_error(404, "Incorrect infrastructure property")

        return format_output(res, field_name=prop)
    except DeletedInfrastructureException as ex:
        return return_error(404, "Error Getting Inf. prop: " + str(ex))
    except IncorrectInfrastructureException as ex:
        return return_error(404, "Error Getting Inf. prop: " + str(ex))
    except UnauthorizedUserException as ex:
        return return_error(403, "Error Getting Inf. prop: " + str(ex))
    except Exception as ex:
        logger.exception("Error Getting Inf. prop")
        return return_error(400, "Error Getting Inf. prop: " + str(ex))
Exemple #3
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 #4
0
def RESTGetInfrastructureProperty(infid=None, prop=None):
    try:
        auth = get_auth_header()
    except:
        return return_error(401, "No authentication data provided")

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

            res = InfrastructureManager.GetInfrastructureContMsg(
                infid, auth, headeronly)
        elif prop == "radl":
            res = InfrastructureManager.GetInfrastructureRADL(infid, auth)
        elif prop == "tosca":
            accept = get_media_type('Accept')
            if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept:
                return return_error(
                    415, "Unsupported Accept Media Types: %s" % accept)
            bottle.response.content_type = "application/json"
            auth = InfrastructureManager.check_auth_data(auth)
            sel_inf = InfrastructureManager.get_infrastructure(infid, auth)
            if "TOSCA" in sel_inf.extra_info:
                res = sel_inf.extra_info["TOSCA"].serialize()
            else:
                bottle.abort(
                    403,
                    "'tosca' infrastructure property is not valid in this infrastructure"
                )
        elif prop == "state":
            accept = get_media_type('Accept')
            if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept:
                return return_error(
                    415, "Unsupported Accept Media Types: %s" % accept)
            bottle.response.content_type = "application/json"
            res = InfrastructureManager.GetInfrastructureState(infid, auth)
            return format_output(res,
                                 default_type="application/json",
                                 field_name="state")
        elif prop == "outputs":
            accept = get_media_type('Accept')
            if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept:
                return return_error(
                    415, "Unsupported Accept Media Types: %s" % accept)
            bottle.response.content_type = "application/json"
            auth = InfrastructureManager.check_auth_data(auth)
            sel_inf = InfrastructureManager.get_infrastructure(infid, auth)
            if "TOSCA" in sel_inf.extra_info:
                res = sel_inf.extra_info["TOSCA"].get_outputs(sel_inf)
            else:
                bottle.abort(
                    403,
                    "'outputs' infrastructure property is not valid in this infrastructure"
                )
            return format_output(res,
                                 default_type="application/json",
                                 field_name="outputs")
        elif prop == "data":
            accept = get_media_type('Accept')
            if accept and "application/json" not in accept and "*/*" not in accept and "application/*" not in accept:
                return return_error(
                    415, "Unsupported Accept Media Types: %s" % accept)

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

            data = InfrastructureManager.ExportInfrastructure(
                infid, delete, auth)
            return format_output(data,
                                 default_type="application/json",
                                 field_name="data")
        else:
            return return_error(404, "Incorrect infrastructure property")

        return format_output(res, field_name=prop)
    except DeletedInfrastructureException as ex:
        return return_error(404, "Error Getting Inf. prop: %s" % ex.args[0])
    except IncorrectInfrastructureException as ex:
        return return_error(404, "Error Getting Inf. prop: %s" % ex.args[0])
    except UnauthorizedUserException as ex:
        return return_error(403, "Error Getting Inf. prop: %s" % ex.args[0])
    except Exception as ex:
        logger.exception("Error Getting Inf. prop")
        return return_error(400, "Error Getting Inf. prop: %s" % ex.args[0])
Exemple #5
0
    def test_contextualize(self):
        """Test Contextualization process"""
        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.dns_name = 'test' 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')
            )

configure step1 (
@begin
---
  - tasks:
      - shell:  echo "Hi"

@end
)

configure step2 (
@begin
---
  - tasks:
      - shell:  echo "Hi"

@end
)

            contextualize (
                system front configure step1 step 1
                system front configure step2 step 2
            )

            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.01
        Config.UPDATE_CTXT_LOG_INTERVAL = 1
        Config.CHECK_CTXT_PROCESS_INTERVAL = 1
        cloud0 = self.get_cloud_connector_mock("MyMock")
        self.register_cloudconnector("Mock", cloud0)

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

        time.sleep(10)

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

        InfrastructureList.infrastructure_list[infId].ansible_configured = True
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].get_ctxt_log = MagicMock()
        InfrastructureList.infrastructure_list[infId].vm_list[
            0].get_ctxt_log.return_value = "OK"

        IM.Reconfigure(infId, "", auth0)

        time.sleep(5)

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

        contmsg = IM.GetInfrastructureContMsg(infId, auth0)
        self.assertGreater(len(contmsg), 150)

        IM.DestroyInfrastructure(infId, auth0)
Exemple #6
0
    def test_contextualize_timeout(self):
        """Test Contextualization process timeout"""
        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.dns_name = 'test' 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'
            )

configure step1 (
@begin
---
  - tasks:
      - shell:  echo "Hi"

@end
)

configure step2 (
@begin
---
  - tasks:
      - shell:  echo "Hi"

@end
)

            contextualize 2 (
                system front configure step1 step 1
                system front configure step2 step 2
            )

            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 = 1
        Config.UPDATE_CTXT_LOG_INTERVAL = 1
        Config.CHECK_CTXT_PROCESS_INTERVAL = 1
        cloud0 = self.get_cloud_connector_mock("MyMock")
        self.register_cloudconnector("Mock", cloud0)

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

        time.sleep(4)

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

        contmsg = IM.GetInfrastructureContMsg(infId, auth0)
        self.assertIn("ERROR: Max contextualization time passed.", contmsg)

        IM.DestroyInfrastructure(infId, auth0)