Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

5.6 KB
Raw Blame
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

import asyncio
import unittest

from fastmcp import Client

from mcp_server.client.factory import RESTClientFactory
from mcp_server.core import Setting
from mcp_server.server import GravitinoMCPServer
from tests.unit.tools import MockOperation


class TestPolicyTool(unittest.TestCase):

    def setUp(self):
        RESTClientFactory.set_rest_client(MockOperation)
        server = GravitinoMCPServer(Setting("mock_metalake"))
        self.mcp = server.mcp

    def test_list_policies(self):
        async def _test_list_policies(mcp_server):
            async with Client(mcp_server) as client:
                result = await client.call_tool("get_list_of_policies", {})
                self.assertEqual("mock_policies", result.content[0].text)

        asyncio.run(_test_list_policies(self.mcp))

    def test_get_policy_detail_information(self):
        async def _test_get_policy_detail_information(mcp_server):
            async with Client(mcp_server) as client:
                result = await client.call_tool(
                    "get_policy_detail_information",
                    {"policy_name": "mock_name"},
                )
                self.assertEqual(
                    "mock_policy: mock_name", result.content[0].text
                )

        asyncio.run(_test_get_policy_detail_information(self.mcp))

    def test_list_policies_for_tag(self):
        async def _test():
            async with Client(self.mcp) as client:
                result = await client.call_tool(
                    "list_policies_for_tag", {"tag_name": "data_domain"}
                )
                self.assertEqual(
                    "list_policies_for_tag: data_domain",
                    result.content[0].text,
                )

        asyncio.run(_test())

    def test_associate_policy_with_tag(self):
        async def _test():
            async with Client(self.mcp) as client:
                result = await client.call_tool(
                    "associate_policy_with_tag",
                    {
                        "tag_name": "data_domain",
                        "policy_name": "retention_policy",
                        "selector": {
                            "type": "TAG_VALUE",
                            "value": "finance",
                        },
                    },
                )
                self.assertEqual(
                    "associate_policy_with_tag: data_domain, "
                    "retention_policy, {'type': 'TAG_VALUE', "
                    "'value': 'finance'}",
                    result.content[0].text,
                )

        asyncio.run(_test())

    def test_disassociate_policy_from_tag(self):
        async def _test():
            async with Client(self.mcp) as client:
                result = await client.call_tool(
                    "disassociate_policy_from_tag",
                    {
                        "tag_name": "data_domain",
                        "policy_name": "retention_policy",
                    },
                )
                self.assertEqual(
                    "disassociate_policy_from_tag: data_domain, "
                    "retention_policy",
                    result.content[0].text,
                )

        asyncio.run(_test())

    def test_list_tags_for_policy(self):
        async def _test():
            async with Client(self.mcp) as client:
                result = await client.call_tool(
                    "list_tags_for_policy",
                    {"policy_name": "retention_policy"},
                )
                self.assertEqual(
                    "list_tags_for_policy: retention_policy",
                    result.content[0].text,
                )

        asyncio.run(_test())

    def test_policy_tag_tools_are_exposed(self):
        async def _test():
            async with Client(self.mcp) as client:
                names = {tool.name for tool in await client.list_tools()}
                self.assertTrue(
                    {
                        "list_policies_for_tag",
                        "associate_policy_with_tag",
                        "disassociate_policy_from_tag",
                        "list_tags_for_policy",
                    }.issubset(names)
                )

        asyncio.run(_test())

    def test_removed_object_policy_tools_are_not_exposed(self):
        async def _test():
            async with Client(self.mcp) as client:
                names = {tool.name for tool in await client.list_tools()}
                for name in (
                    "list_policies_for_metadata",
                    "associate_policy_with_metadata",
                    "disassociate_policy_from_metadata",
                    "get_policy_for_metadata",
                    "list_metadata_by_policy",
                ):
                    with self.subTest(tool=name):
                        self.assertNotIn(name, names)

        asyncio.run(_test())