#!/usr/bin/env python3
"""Test script for Unity Catalog tools in the Databricks MCP server.
Tests all UC tools including volumes, functions, models, external locations, etc.
"""
import os
import sys
from databricks.sdk import WorkspaceClient
def test_uc_tools():
"""Test all Unity Catalog tools."""
# Get configuration from environment
host = os.environ.get('DATABRICKS_HOST')
token = os.environ.get('DATABRICKS_TOKEN')
if not host or not token:
print('β Error: DATABRICKS_HOST and DATABRICKS_TOKEN environment variables must be set')
print("Please run: export DATABRICKS_HOST='https://your-workspace.cloud.databricks.com'")
print("Please run: export DATABRICKS_TOKEN='your-token'")
sys.exit(1)
print(f'π§ Testing Unity Catalog tools against workspace: {host}')
try:
# Test connection
w = WorkspaceClient(host=host, token=token)
user = w.current_user.me()
print(f'β
Connected as: {user.user_name}')
# Test describing a common catalog
print('\nπ Testing describe_uc_catalog with common catalog...')
catalog_names = ['hive_metastore', 'main', 'samples']
catalog_found = False
for catalog_name in catalog_names:
try:
print(f' Trying catalog: {catalog_name}')
catalog_details = w.catalogs.get(catalog_name)
print(f'β
Found catalog: {catalog_details.name}')
catalog_found = True
# List schemas
schemas = list(w.schemas.list(catalog_name))
print(f"β
Found {len(schemas)} schema(s) in catalog '{catalog_name}'")
if schemas:
schema_name = schemas[0].name
full_schema = f'{catalog_name}.{schema_name}'
print(f'π First schema: {full_schema}')
# Test describing schema
print(f"\nπ Testing describe_uc_schema for '{full_schema}'...")
try:
schema_details = w.schemas.get(full_schema)
print(f'β
Got schema details: {schema_details.name}')
# List tables
tables = list(w.tables.list(catalog_name=catalog_name, schema_name=schema_name))
print(f"β
Found {len(tables)} table(s) in schema '{full_schema}'")
if tables:
table_name = tables[0].name
full_table = f'{full_schema}.{table_name}'
print(f'π First table: {full_table}')
# Test describing table
print(f"\nπ Testing describe_uc_table for '{full_table}'...")
try:
table_details = w.tables.get(full_table)
print(f'β
Got table details: {table_details.name}')
if hasattr(table_details, 'columns') and table_details.columns:
print(f'β
Table has {len(table_details.columns)} columns')
else:
print('βΉοΈ Table has no column information available')
except Exception as e:
print(f"β οΈ Could not describe table '{full_table}': {e}")
else:
print('βΉοΈ No tables found in schema')
# Test listing volumes
print(f"\nπ¦ Testing list_uc_volumes for '{full_schema}'...")
try:
volumes = list(w.volumes.list(catalog_name=catalog_name, schema_name=schema_name))
print(f"β
Found {len(volumes)} volume(s) in schema '{full_schema}'")
if volumes:
volume_name = volumes[0].name
full_volume = f'{full_schema}.{volume_name}'
print(f'π¦ First volume: {full_volume}')
# Test describing volume
print(f"\nπ Testing describe_uc_volume for '{full_volume}'...")
try:
volume_details = w.volumes.get(full_volume)
print(f'β
Got volume details: {volume_details.name}')
except Exception as e:
print(f"β οΈ Could not describe volume '{full_volume}': {e}")
except Exception as e:
print(f"β οΈ Could not list volumes for schema '{full_schema}': {e}")
# Test listing functions
print(f"\nπ§ Testing list_uc_functions for '{full_schema}'...")
try:
functions = list(w.functions.list(catalog_name=catalog_name, schema_name=schema_name))
print(f"β
Found {len(functions)} function(s) in schema '{full_schema}'")
if functions:
function_name = functions[0].name
full_function = f'{full_schema}.{function_name}'
print(f'π§ First function: {full_function}')
# Test describing function
print(f"\nπ Testing describe_uc_function for '{full_function}'...")
try:
function_details = w.functions.get(full_function)
print(f'β
Got function details: {function_details.name}')
except Exception as e:
print(f"β οΈ Could not describe function '{full_function}': {e}")
except Exception as e:
print(f"β οΈ Could not list functions for schema '{full_schema}': {e}")
# Test listing models
print(f"\nπ€ Testing list_uc_models for '{full_schema}'...")
try:
models = list(w.models.list(catalog_name=catalog_name, schema_name=schema_name))
print(f"β
Found {len(models)} model(s) in schema '{full_schema}'")
if models:
model_name = models[0].name
full_model = f'{full_schema}.{model_name}'
print(f'π€ First model: {full_model}')
# Test describing model
print(f"\nπ Testing describe_uc_model for '{full_model}'...")
try:
model_details = w.models.get(full_model)
print(f'β
Got model details: {model_details.name}')
except Exception as e:
print(f"β οΈ Could not describe model '{full_model}': {e}")
except Exception as e:
print(f"β οΈ Could not list models for schema '{full_schema}': {e}")
except Exception as e:
print(f"β οΈ Could not describe schema '{full_schema}': {e}")
else:
print('βΉοΈ No schemas found in catalog')
break # Found a working catalog, exit loop
except Exception as e:
print(f"β οΈ Could not describe catalog '{catalog_name}': {e}")
if not catalog_found:
print('βΉοΈ No accessible catalogs found')
# Test external locations
print('\nπ Testing list_external_locations...')
try:
locations = list(w.external_locations.list())
print(f'β
Found {len(locations)} external location(s)')
if locations:
location_name = locations[0].name
print(f'π First location: {location_name}')
# Test describing external location
print(f"\nπ Testing describe_external_location for '{location_name}'...")
try:
location_details = w.external_locations.get(location_name)
print(f'β
Got location details: {location_details.name}')
except Exception as e:
print(f"β οΈ Could not describe location '{location_name}': {e}")
except Exception as e:
print(f'β οΈ Could not list external locations: {e}')
# Test storage credentials
print('\nπ Testing list_storage_credentials...')
try:
credentials = list(w.storage_credentials.list())
print(f'β
Found {len(credentials)} storage credential(s)')
if credentials:
credential_name = credentials[0].name
print(f'π First credential: {credential_name}')
# Test describing storage credential
print(f"\nπ Testing describe_storage_credential for '{credential_name}'...")
try:
credential_details = w.storage_credentials.get(credential_name)
print(f'β
Got credential details: {credential_details.name}')
except Exception as e:
print(f"β οΈ Could not describe credential '{credential_name}': {e}")
except Exception as e:
print(f'β οΈ Could not list storage credentials: {e}')
# Test metastores
print('\nποΈ Testing list_metastores...')
try:
metastores = list(w.metastores.list())
print(f'β
Found {len(metastores)} metastore(s)')
if metastores:
metastore_name = metastores[0].name
print(f'ποΈ First metastore: {metastore_name}')
# Test describing metastore
print(f"\nπ Testing describe_metastore for '{metastore_name}'...")
try:
metastore_details = w.metastores.get(metastore_name)
print(f'β
Got metastore details: {metastore_details.name}')
except Exception as e:
print(f"β οΈ Could not describe metastore '{metastore_name}': {e}")
except Exception as e:
print(f'β οΈ Could not list metastores: {e}')
# Test search functionality
print('\nπ Testing search_uc_objects...')
try:
# Search for objects containing the catalog name
search_results = []
for catalog in w.catalogs.list():
if catalog.name.lower() in catalog.name.lower():
search_results.append(
{'name': catalog.name, 'type': 'catalog', 'full_name': catalog.name}
)
break # Just get one result for testing
if search_results:
print(f'β
Search found {len(search_results)} result(s)')
for result in search_results:
print(f' - {result["type"]}: {result["full_name"]}')
else:
print('βΉοΈ Search returned no results')
except Exception as e:
print(f'β οΈ Could not perform search: {e}')
print('\nπ Unity Catalog tools testing completed successfully!')
print('β
All core UC operations are working')
print(
'βΉοΈ Some advanced features (tags, data quality) are placeholders for future implementation'
)
except Exception as e:
print(f'β Error during testing: {e}')
sys.exit(1)
if __name__ == '__main__':
test_uc_tools()