|
| 1 | +import time |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from databricks.sdk.core import Config |
| 6 | + |
| 7 | +from .conftest import raises |
| 8 | + |
| 9 | +default_auth_base_error_message = \ |
| 10 | + "default auth: cannot configure default credentials, " \ |
| 11 | + "please check https://docs.databricks.com/en/dev-tools/auth.html#databricks-client-unified-authentication " \ |
| 12 | + "to configure credentials for your preferred authentication method" |
| 13 | + |
| 14 | + |
| 15 | +@pytest.mark.parametrize( |
| 16 | + "env_values, oauth_file_name", |
| 17 | + [([('IS_IN_DB_MODEL_SERVING_ENV', 'true'), |
| 18 | + ('DB_MODEL_SERVING_HOST_URL', 'x')], "tests/testdata/model-serving-test-token"), |
| 19 | + ([('IS_IN_DATABRICKS_MODEL_SERVING_ENV', 'true'), |
| 20 | + ('DB_MODEL_SERVING_HOST_URL', 'x')], "tests/testdata/model-serving-test-token"), |
| 21 | + ([('IS_IN_DB_MODEL_SERVING_ENV', 'true'), |
| 22 | + ('DATABRICKS_MODEL_SERVING_HOST_URL', 'x')], "tests/testdata/model-serving-test-token"), |
| 23 | + ([('IS_IN_DATABRICKS_MODEL_SERVING_ENV', 'true'), |
| 24 | + ('DATABRICKS_MODEL_SERVING_HOST_URL', 'x')], "tests/testdata/model-serving-test-token"), ]) |
| 25 | +def test_model_serving_auth(env_values, oauth_file_name, monkeypatch): |
| 26 | + ## In mlflow we check for these two environment variables to return the correct config |
| 27 | + for (env_name, env_value) in env_values: |
| 28 | + monkeypatch.setenv(env_name, env_value) |
| 29 | + # patch mlflow to read the file from the test directory |
| 30 | + monkeypatch.setattr( |
| 31 | + "databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH", |
| 32 | + oauth_file_name) |
| 33 | + |
| 34 | + cfg = Config() |
| 35 | + |
| 36 | + assert cfg.auth_type == 'model-serving' |
| 37 | + headers = cfg.authenticate() |
| 38 | + assert (cfg.host == 'x') |
| 39 | + # Token defined in the test file |
| 40 | + assert headers.get("Authorization") == 'Bearer databricks_sdk_unit_test_token' |
| 41 | + |
| 42 | + |
| 43 | +@pytest.mark.parametrize("env_values, oauth_file_name", [ |
| 44 | + ([], "invalid_file_name"), # Not in Model Serving and Invalid File Name |
| 45 | + ([('IS_IN_DB_MODEL_SERVING_ENV', 'true')], "invalid_file_name"), # In Model Serving and Invalid File Name |
| 46 | + ([('IS_IN_DATABRICKS_MODEL_SERVING_ENV', 'true') |
| 47 | + ], "invalid_file_name"), # In Model Serving and Invalid File Name |
| 48 | + ([], "tests/testdata/model-serving-test-token") # Not in Model Serving and Valid File Name |
| 49 | +]) |
| 50 | +@raises(default_auth_base_error_message) |
| 51 | +def test_model_serving_auth_errors(env_values, oauth_file_name, monkeypatch): |
| 52 | + for (env_name, env_value) in env_values: |
| 53 | + monkeypatch.setenv(env_name, env_value) |
| 54 | + monkeypatch.setattr( |
| 55 | + "databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH", |
| 56 | + oauth_file_name) |
| 57 | + |
| 58 | + Config() |
| 59 | + |
| 60 | + |
| 61 | +def test_model_serving_auth_refresh(monkeypatch): |
| 62 | + ## In mlflow we check for these two environment variables to return the correct config |
| 63 | + monkeypatch.setenv('IS_IN_DB_MODEL_SERVING_ENV', 'true') |
| 64 | + monkeypatch.setenv('DB_MODEL_SERVING_HOST_URL', 'x') |
| 65 | + |
| 66 | + # patch mlflow to read the file from the test directory |
| 67 | + monkeypatch.setattr( |
| 68 | + "databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH", |
| 69 | + "tests/testdata/model-serving-test-token") |
| 70 | + |
| 71 | + cfg = Config() |
| 72 | + assert cfg.auth_type == 'model-serving' |
| 73 | + |
| 74 | + current_time = time.time() |
| 75 | + headers = cfg.authenticate() |
| 76 | + assert (cfg.host == 'x') |
| 77 | + assert headers.get( |
| 78 | + "Authorization") == 'Bearer databricks_sdk_unit_test_token' # Token defined in the test file |
| 79 | + |
| 80 | + # Simulate refreshing the token by patching to to a new file |
| 81 | + monkeypatch.setattr( |
| 82 | + "databricks.sdk.credentials_provider.ModelServingAuthProvider._MODEL_DEPENDENCY_OAUTH_TOKEN_FILE_PATH", |
| 83 | + "tests/testdata/model-serving-test-token-v2") |
| 84 | + |
| 85 | + monkeypatch.setattr('databricks.sdk.credentials_provider.time.time', lambda: current_time + 10) |
| 86 | + |
| 87 | + headers = cfg.authenticate() |
| 88 | + assert (cfg.host == 'x') |
| 89 | + # Read from cache even though new path is set because expiry is still not hit |
| 90 | + assert headers.get("Authorization") == 'Bearer databricks_sdk_unit_test_token' |
| 91 | + |
| 92 | + # Expiry is 300 seconds so this should force an expiry and re read from the new file path |
| 93 | + monkeypatch.setattr('databricks.sdk.credentials_provider.time.time', lambda: current_time + 600) |
| 94 | + |
| 95 | + headers = cfg.authenticate() |
| 96 | + assert (cfg.host == 'x') |
| 97 | + # Read V2 now |
| 98 | + assert headers.get("Authorization") == 'Bearer databricks_sdk_unit_test_token_v2' |
0 commit comments