Пример #1
0
    def test_GetInfrastructureInfo(self, bottle_request,
                                   GetInfrastructureInfo):
        """Test REST GetInfrastructureInfo."""
        bottle_request.environ = {'HTTP_HOST': 'imserver.com'}
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        GetInfrastructureInfo.return_value = ["1", "2"]
        res = RESTGetInfrastructureInfo("1")
        self.assertEqual(res, ("http://imserver.com/infrastructures/1/vms/1\n"
                               "http://imserver.com/infrastructures/1/vms/2"))

        GetInfrastructureInfo.side_effect = DeletedInfrastructureException()
        res = RESTGetInfrastructureInfo("1")
        self.assertEqual(res,
                         "Error Getting Inf. info: Deleted infrastructure.")

        GetInfrastructureInfo.side_effect = IncorrectInfrastructureException()
        res = RESTGetInfrastructureInfo("1")
        self.assertEqual(
            res,
            "Error Getting Inf. info: Invalid infrastructure ID or access not granted."
        )

        GetInfrastructureInfo.side_effect = UnauthorizedUserException()
        res = RESTGetInfrastructureInfo("1")
        self.assertEqual(
            res,
            "Error Getting Inf. info: Access to this infrastructure not granted."
        )
Пример #2
0
    def test_StopInfrastructure(self, bottle_request, StopInfrastructure):
        """Test REST StopInfrastructure."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }

        StopInfrastructure.return_value = ""

        res = RESTStopInfrastructure("1")
        self.assertEqual(res, "")

        StopInfrastructure.side_effect = DeletedInfrastructureException()
        res = RESTStopInfrastructure("1")
        self.assertEqual(
            res, "Error stopping infrastructure: Deleted infrastructure.")

        StopInfrastructure.side_effect = IncorrectInfrastructureException()
        res = RESTStopInfrastructure("1")
        self.assertEqual(
            res,
            "Error stopping infrastructure: Invalid infrastructure ID or access not granted."
        )

        StopInfrastructure.side_effect = UnauthorizedUserException()
        res = RESTStopInfrastructure("1")
        self.assertEqual(
            res,
            "Error stopping infrastructure: Access to this infrastructure not granted."
        )
Пример #3
0
    def test_Reconfigure(self, bottle_request, Reconfigure):
        """Test REST Reconfigure."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        bottle_request.body = BytesIO(b"radl")
        bottle_request.params = {'vm_list': '1,2'}

        Reconfigure.return_value = ""

        res = RESTReconfigureInfrastructure("1")
        self.assertEqual(res, "")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "application/json"
        }
        bottle_request.body = read_file_as_bytes("../files/test_simple.json")

        res = RESTReconfigureInfrastructure("1")
        self.assertEqual(res, "")

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        Reconfigure.side_effect = DeletedInfrastructureException()
        res = RESTReconfigureInfrastructure("1")
        self.assertEqual(
            res, "Error reconfiguring infrastructure: Deleted infrastructure.")

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        Reconfigure.side_effect = IncorrectInfrastructureException()
        res = RESTReconfigureInfrastructure("1")
        self.assertEqual(
            res,
            "Error reconfiguring infrastructure: Invalid infrastructure ID or access not granted."
        )

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        Reconfigure.side_effect = UnauthorizedUserException()
        res = RESTReconfigureInfrastructure("1")
        self.assertEqual(
            res,
            "Error reconfiguring infrastructure: Access to this infrastructure not granted."
        )
Пример #4
0
    def test_GetVMInfo(self, bottle_request, GetVMInfo):
        """Test REST GetVMInfo."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Accept":
            "application/json"
        }

        GetVMInfo.return_value = parse_radl("system test (cpu.count = 1)")

        res = RESTGetVMInfo("1", "1")
        self.assertEqual(
            json.loads(res),
            json.loads(
                '{"radl": [{"cpu.count": 1, "class": "system", "id": "test"}]}'
            ))

        bottle_request.headers["Accept"] = "text/*"
        res = RESTGetVMInfo("1", "1")
        self.assertEqual(res, 'system test (\ncpu.count = 1\n)\n\n')

        GetVMInfo.side_effect = DeletedInfrastructureException()
        res = RESTGetVMInfo("1", "1")
        self.assertEqual(res,
                         "Error Getting VM. info: Deleted infrastructure.")

        GetVMInfo.side_effect = IncorrectInfrastructureException()
        res = RESTGetVMInfo("1", "1")
        self.assertEqual(
            res,
            "Error Getting VM. info: Invalid infrastructure ID or access not granted."
        )

        GetVMInfo.side_effect = UnauthorizedUserException()
        res = RESTGetVMInfo("1", "1")
        self.assertEqual(
            res,
            "Error Getting VM. info: Access to this infrastructure not granted."
        )

        GetVMInfo.side_effect = DeletedVMException()
        res = RESTGetVMInfo("1", "1")
        self.assertEqual(res, "Error Getting VM. info: Deleted VM.")

        GetVMInfo.side_effect = IncorrectVMException()
        res = RESTGetVMInfo("1", "1")
        self.assertEqual(res, "Error Getting VM. info: Invalid VM ID")
Пример #5
0
    def test_CreateDiskSnapshot(self, bottle_request, CreateDiskSnapshot):
        """Test REST StopVM."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }

        bottle_request.params = {
            'image_name': 'image_url',
            'auto_delete': 'yes'
        }
        CreateDiskSnapshot.return_value = "one://server.com/image_url"

        res = RESTCreateDiskSnapshot("1", "1", 0)
        self.assertEqual(res, "one://server.com/image_url")

        CreateDiskSnapshot.side_effect = DeletedInfrastructureException()
        res = RESTCreateDiskSnapshot("1", "1", 0)
        self.assertEqual(res,
                         "Error creating snapshot: Deleted infrastructure.")

        CreateDiskSnapshot.side_effect = IncorrectInfrastructureException()
        res = RESTCreateDiskSnapshot("1", "1", 0)
        self.assertEqual(
            res,
            "Error creating snapshot: Invalid infrastructure ID or access not granted."
        )

        CreateDiskSnapshot.side_effect = UnauthorizedUserException()
        res = RESTCreateDiskSnapshot("1", "1", 0)
        self.assertEqual(
            res,
            "Error creating snapshot: Access to this infrastructure not granted."
        )

        CreateDiskSnapshot.side_effect = DeletedVMException()
        res = RESTCreateDiskSnapshot("1", "1", 0)
        self.assertEqual(res, "Error creating snapshot: Deleted VM.")

        CreateDiskSnapshot.side_effect = IncorrectVMException()
        res = RESTCreateDiskSnapshot("1", "1", 0)
        self.assertEqual(res, "Error creating snapshot: Invalid VM ID")
Пример #6
0
    def test_GetVMProperty(self, bottle_request, GetVMContMsg, GetVMProperty):
        """Test REST GetVMProperty."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }

        GetVMProperty.return_value = "prop"
        GetVMContMsg.return_value = "contmsg"

        res = RESTGetVMProperty("1", "1", "prop")
        self.assertEqual(res, "prop")

        res = RESTGetVMProperty("1", "1", "contmsg")
        self.assertEqual(res, "contmsg")

        GetVMProperty.side_effect = DeletedInfrastructureException()
        res = RESTGetVMProperty("1", "1", "prop")
        self.assertEqual(
            res, "Error Getting VM. property: Deleted infrastructure.")

        GetVMProperty.side_effect = IncorrectInfrastructureException()
        res = RESTGetVMProperty("1", "1", "prop")
        self.assertEqual(
            res,
            "Error Getting VM. property: Invalid infrastructure ID or access not granted."
        )

        GetVMProperty.side_effect = UnauthorizedUserException()
        res = RESTGetVMProperty("1", "1", "prop")
        self.assertEqual(
            res,
            "Error Getting VM. property: Access to this infrastructure not granted."
        )

        GetVMProperty.side_effect = DeletedVMException()
        res = RESTGetVMProperty("1", "1", "prop")
        self.assertEqual(res, "Error Getting VM. property: Deleted VM.")

        GetVMProperty.side_effect = IncorrectVMException()
        res = RESTGetVMProperty("1", "1", "prop")
        self.assertEqual(res, "Error Getting VM. property: Invalid VM ID")
Пример #7
0
    def test_RemoveResource(self, bottle_request, RemoveResource):
        """Test REST RemoveResource."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        bottle_request.params = {'context': 'yes'}

        RemoveResource.return_value = 2

        res = RESTRemoveResource("1", "1,2")
        self.assertEqual(res, "")

        RemoveResource.side_effect = DeletedInfrastructureException()
        res = RESTRemoveResource("1", "1,2")
        self.assertEqual(res,
                         "Error Removing resources: Deleted infrastructure.")

        RemoveResource.side_effect = IncorrectInfrastructureException()
        res = RESTRemoveResource("1", "1,2")
        self.assertEqual(
            res,
            "Error Removing resources: Invalid infrastructure ID or access not granted."
        )

        RemoveResource.side_effect = UnauthorizedUserException()
        res = RESTRemoveResource("1", "1,2")
        self.assertEqual(
            res,
            "Error Removing resources: Access to this infrastructure not granted."
        )

        RemoveResource.side_effect = DeletedVMException()
        res = RESTRemoveResource("1", "1,2")
        self.assertEqual(res, "Error Removing resources: Deleted VM.")

        RemoveResource.side_effect = IncorrectVMException()
        res = RESTRemoveResource("1", "1,2")
        self.assertEqual(res, "Error Removing resources: Invalid VM ID")
Пример #8
0
    def test_GetInfrastructureList(self, bottle_request,
                                   GetInfrastructureList):
        """Test REST GetInfrastructureList."""
        bottle_request.environ = {'HTTP_HOST': 'imserver.com'}
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Accept":
            "application/json"
        }

        GetInfrastructureList.return_value = ["1", "2"]
        res = RESTGetInfrastructureList()
        self.assertEqual(
            res,
            ('{"uri-list": [{"uri": "http://imserver.com/infrastructures/1"},'
             ' {"uri": "http://imserver.com/infrastructures/2"}]}'))

        GetInfrastructureList.side_effect = InvaliddUserException()
        res = RESTGetInfrastructureList()
        res = json.loads(res)
        self.assertEqual(
            res, {
                "message":
                "Error Getting Inf. List: Invalid InfrastructureManager credentials",
                "code": 401
            })

        GetInfrastructureList.side_effect = UnauthorizedUserException()
        res = RESTGetInfrastructureList()
        res = json.loads(res)
        self.assertEqual(
            res, {
                "message":
                "Error Getting Inf. List: Access to this infrastructure not granted.",
                "code": 400
            })
Пример #9
0
    def test_AlterVM(self, bottle_request, AlterVM):
        """Test REST AlterVM."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        bottle_request.body = BytesIO(b"radl")
        bottle_request.params = {'context': 'yes'}

        AlterVM.return_value = "vm_info"

        res = RESTAlterVM("1", "1")
        self.assertEqual(res, "vm_info")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "text/yaml"
        }
        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")

        res = RESTAlterVM("1", "1")
        self.assertEqual(res, "vm_info")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "application/json"
        }
        bottle_request.body = read_file_as_bytes("../files/test_simple.json")

        res = RESTAlterVM("1", "1")
        self.assertEqual(res, "vm_info")

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        AlterVM.side_effect = DeletedInfrastructureException()
        res = RESTAlterVM("1", "1")
        self.assertEqual(res,
                         "Error modifying resources: Deleted infrastructure.")

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        AlterVM.side_effect = IncorrectInfrastructureException()
        res = RESTAlterVM("1", "1")
        self.assertEqual(
            res,
            "Error modifying resources: Invalid infrastructure ID or access not granted."
        )

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        AlterVM.side_effect = UnauthorizedUserException()
        res = RESTAlterVM("1", "1")
        self.assertEqual(
            res,
            "Error modifying resources: Access to this infrastructure not granted."
        )

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        AlterVM.side_effect = DeletedVMException()
        res = RESTAlterVM("1", "1")
        self.assertEqual(res, "Error modifying resources: Deleted VM.")

        bottle_request.body = read_file_as_bytes("../files/test_simple.json")
        AlterVM.side_effect = IncorrectVMException()
        res = RESTAlterVM("1", "1")
        self.assertEqual(res, "Error modifying resources: Invalid VM ID")
Пример #10
0
    def test_AddResource(self, bottle_request, get_infrastructure,
                         AddResource):
        """Test REST AddResource."""
        bottle_request.environ = {'HTTP_HOST': 'imserver.com'}
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        bottle_request.body = BytesIO(b"radl")
        bottle_request.params = {'context': 'yes'}

        AddResource.return_value = "1"

        res = RESTAddResource("1")
        self.assertEqual(res, "http://imserver.com/infrastructures/1/vms/1")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "application/json"
        }
        bottle_request.body = read_file_as_bytes("../files/test_simple.json")

        res = RESTAddResource("1")
        self.assertEqual(res, "http://imserver.com/infrastructures/1/vms/1")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "text/yaml"
        }
        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")

        res = RESTAddResource("1")
        self.assertEqual(res, "http://imserver.com/infrastructures/1/vms/1")

        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
        AddResource.side_effect = DeletedInfrastructureException()
        res = RESTAddResource("1")
        self.assertEqual(res,
                         "Error Adding resources: Deleted infrastructure.")

        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
        AddResource.side_effect = IncorrectInfrastructureException()
        res = RESTAddResource("1")
        self.assertEqual(
            res,
            "Error Adding resources: Invalid infrastructure ID or access not granted."
        )

        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
        AddResource.side_effect = UnauthorizedUserException()
        res = RESTAddResource("1")
        self.assertEqual(
            res,
            "Error Adding resources: Access to this infrastructure not granted."
        )
Пример #11
0
    def test_CreateInfrastructure(self, bottle_request, get_infrastructure,
                                  CreateInfrastructure):
        """Test REST CreateInfrastructure."""
        bottle_request.environ = {'HTTP_HOST': 'imserver.com'}
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }
        bottle_request.body = BytesIO(b"radl")

        CreateInfrastructure.return_value = "1"

        res = RESTCreateInfrastructure()
        self.assertEqual(res, "http://imserver.com/infrastructures/1")

        bottle_request.params = {"async": "yes"}
        res = RESTCreateInfrastructure()
        self.assertEqual(res, "http://imserver.com/infrastructures/1")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "application/json"
        }
        bottle_request.body = read_file_as_bytes("../files/test_simple.json")

        CreateInfrastructure.return_value = "1"

        res = RESTCreateInfrastructure()
        self.assertEqual(res, "http://imserver.com/infrastructures/1")

        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass"),
            "Content-Type":
            "text/yaml"
        }
        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")

        CreateInfrastructure.return_value = "1"

        res = RESTCreateInfrastructure()
        self.assertEqual(res, "http://imserver.com/infrastructures/1")

        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
        CreateInfrastructure.side_effect = InvaliddUserException()
        res = RESTCreateInfrastructure()
        self.assertEqual(
            res,
            "Error Getting Inf. info: Invalid InfrastructureManager credentials"
        )

        bottle_request.body = read_file_as_bytes("../files/tosca_create.yml")
        CreateInfrastructure.side_effect = UnauthorizedUserException()
        res = RESTCreateInfrastructure()
        self.assertEqual(
            res,
            "Error Creating Inf.: Access to this infrastructure not granted.")
Пример #12
0
    def test_GetInfrastructureProperty(self, bottle_request,
                                       get_infrastructure,
                                       GetInfrastructureState,
                                       GetInfrastructureRADL,
                                       GetInfrastructureContMsg):
        """Test REST GetInfrastructureProperty."""
        bottle_request.return_value = MagicMock()
        bottle_request.headers = {
            "AUTHORIZATION":
            ("type = InfrastructureManager; username = user; password = pass\n"
             "id = one; type = OpenNebula; host = onedock.i3m.upv.es:2633; "
             "username = user; password = pass")
        }

        GetInfrastructureState.return_value = {
            'state': "running",
            'vm_states': {
                "vm1": "running",
                "vm2": "running"
            }
        }
        GetInfrastructureRADL.return_value = "radl"
        GetInfrastructureContMsg.return_value = "contmsg"

        inf = MagicMock()
        get_infrastructure.return_value = inf
        tosca = MagicMock()
        inf.extra_info = {"TOSCA": tosca}
        tosca.get_outputs.return_value = "outputs"
        tosca.serialize.return_value = "tosca"

        res = RESTGetInfrastructureProperty("1", "state")
        self.assertEqual(json.loads(res)["state"]["state"], "running")

        res = RESTGetInfrastructureProperty("1", "contmsg")
        self.assertEqual(res, "contmsg")

        bottle_request.params = {'headeronly': 'yes'}
        res = RESTGetInfrastructureProperty("1", "contmsg")
        self.assertEqual(res, "contmsg")

        bottle_request.params = {'headeronly': 'no'}
        res = RESTGetInfrastructureProperty("1", "contmsg")
        self.assertEqual(res, "contmsg")

        res = RESTGetInfrastructureProperty("1", "radl")
        self.assertEqual(res, "radl")

        res = RESTGetInfrastructureProperty("1", "outputs")
        self.assertEqual(res, '{"outputs": "outputs"}')

        res = RESTGetInfrastructureProperty("1", "tosca")
        self.assertEqual(res, "tosca")

        GetInfrastructureRADL.side_effect = DeletedInfrastructureException()
        res = RESTGetInfrastructureProperty("1", "radl")
        self.assertEqual(res,
                         "Error Getting Inf. prop: Deleted infrastructure.")

        GetInfrastructureRADL.side_effect = IncorrectInfrastructureException()
        res = RESTGetInfrastructureProperty("1", "radl")
        self.assertEqual(
            res,
            "Error Getting Inf. prop: Invalid infrastructure ID or access not granted."
        )

        GetInfrastructureRADL.side_effect = UnauthorizedUserException()
        res = RESTGetInfrastructureProperty("1", "radl")
        self.assertEqual(
            res,
            "Error Getting Inf. prop: Access to this infrastructure not granted."
        )