|
| 1 | +# Databricks CLI |
| 2 | +# Copyright 2017 Databricks, Inc. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"), except |
| 5 | +# that the use of services to which certain application programming |
| 6 | +# interfaces (each, an "API") connect requires that the user first obtain |
| 7 | +# a license for the use of the APIs from Databricks, Inc. ("Databricks"), |
| 8 | +# by creating an account at www.databricks.com and agreeing to either (a) |
| 9 | +# the Community Edition Terms of Service, (b) the Databricks Terms of |
| 10 | +# Service, or (c) another written agreement between Licensee and Databricks |
| 11 | +# for the use of the APIs. |
| 12 | +# |
| 13 | +# You may not use this file except in compliance with the License. |
| 14 | +# You may obtain a copy of the License at |
| 15 | +# |
| 16 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 17 | +# |
| 18 | +# Unless required by applicable law or agreed to in writing, software |
| 19 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 20 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 21 | +# See the License for the specific language governing permissions and |
| 22 | +# limitations under the License. |
| 23 | + |
| 24 | +import mock |
| 25 | +import pytest |
| 26 | + |
| 27 | +from databricks_cli.cluster_policies.api import ClusterPolicyApi |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.parametrize( |
| 31 | + "policy, expected", |
| 32 | + [ |
| 33 | + ({"definition": "foo"}, {"definition": "foo"}), |
| 34 | + ({"definition": {"foo": "bar"}}, {"definition": '{"foo": "bar"}'}), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_format_policy_for_api(policy, expected): |
| 38 | + result = ClusterPolicyApi.format_policy_for_api(policy) |
| 39 | + assert result == expected |
| 40 | + |
| 41 | + |
| 42 | +@pytest.mark.parametrize( |
| 43 | + "fct_name, method, action", |
| 44 | + [ |
| 45 | + ("create_cluster_policy", "POST", "create"), |
| 46 | + ("edit_cluster_policy", "POST", "edit"), |
| 47 | + ], |
| 48 | +) |
| 49 | +@mock.patch( |
| 50 | + "databricks_cli.cluster_policies.api.ClusterPolicyApi.format_policy_for_api" |
| 51 | +) |
| 52 | +def test_create_and_edit_cluster_policy( |
| 53 | + mock_format_policy_for_api, fct_name, method, action, fixture_cluster_policies_api |
| 54 | +): |
| 55 | + mock_policy = mock.Mock() |
| 56 | + getattr(fixture_cluster_policies_api, fct_name)(mock_policy) |
| 57 | + mock_format_policy_for_api.assert_called_once_with(mock_policy) |
| 58 | + fixture_cluster_policies_api.client.client.perform_query.assert_called_once_with( |
| 59 | + method, |
| 60 | + "/policies/clusters/{}".format(action), |
| 61 | + data=mock_format_policy_for_api.return_value, |
| 62 | + ) |
0 commit comments