|
| 1 | +# Copyright (c) 2018 - 2023 Cloudify Platform Ltd. All rights reserved |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import mock |
| 16 | +import unittest |
| 17 | +from cloudify_common_sdk.deprecation import ( |
| 18 | + log_deprecation, |
| 19 | + deprecation_warning, |
| 20 | + check_deprecated_node_type, |
| 21 | + check_deprecated_relationship, |
| 22 | +) |
| 23 | + |
| 24 | + |
| 25 | +@mock.patch('cloudify.state.current_ctx.get_ctx') |
| 26 | +class TestDeprecation(unittest.TestCase): |
| 27 | + |
| 28 | + def test_log_deprecation(self, mock_get_ctx, *_): |
| 29 | + expected = 'The baz type foo is deprecated, ' \ |
| 30 | + 'please update your blueprint to use bar' |
| 31 | + mock_ctx = mock.Mock() |
| 32 | + mock_ctx.logger = mock.Mock() |
| 33 | + mock_get_ctx.return_value = mock_ctx |
| 34 | + log_deprecation( |
| 35 | + 'foo', 'bar', 'baz') |
| 36 | + mock_ctx.logger.error.assert_called_with(expected) |
| 37 | + |
| 38 | + @mock.patch('cloudify_common_sdk.deprecation.deprecated_node_types') |
| 39 | + @mock.patch('cloudify_common_sdk.deprecation.log_deprecation') |
| 40 | + def test_check_deprecated_node_type( |
| 41 | + self, mock_log, mock_deprecated, mock_get_ctx, *__): |
| 42 | + mock_deprecated.get.return_value = 'bar' |
| 43 | + |
| 44 | + mock_ctx = mock.Mock() |
| 45 | + mock_ctx.type = 'node-instance' |
| 46 | + mock_ctx.logger = mock.Mock() |
| 47 | + |
| 48 | + mock_ctx.node = mock.Mock(type='foo') |
| 49 | + mock_get_ctx.return_value = mock_ctx |
| 50 | + check_deprecated_node_type() |
| 51 | + mock_log.assert_called_with('foo', 'bar') |
| 52 | + |
| 53 | + @mock.patch( |
| 54 | + 'cloudify_common_sdk.deprecation.deprecated_relationship_types') |
| 55 | + @mock.patch('cloudify_common_sdk.deprecation.log_deprecation') |
| 56 | + def test_check_deprecated_relationship( |
| 57 | + self, mock_log, mock_deprecated, mock_get_ctx, *__): |
| 58 | + mock_deprecated.get.return_value = 'bar' |
| 59 | + |
| 60 | + mock_node_ctx = mock.Mock(type='foo', id='qux') |
| 61 | + |
| 62 | + mock_rel = mock.Mock() |
| 63 | + mock_rel.type = 'foo' |
| 64 | + mock_rel.target = mock.Mock(node=mock_node_ctx) |
| 65 | + mock_instance_ctx = mock.Mock(relationships=[mock_rel]) |
| 66 | + |
| 67 | + mock_source = mock.Mock() |
| 68 | + mock_source.instance = mock_instance_ctx |
| 69 | + mock_target = mock.Mock(node=mock_node_ctx) |
| 70 | + |
| 71 | + mock_ctx = mock.Mock() |
| 72 | + mock_ctx.type = 'relationship-instance' |
| 73 | + mock_ctx.logger = mock.Mock() |
| 74 | + |
| 75 | + mock_ctx.target = mock_target |
| 76 | + mock_ctx.source = mock_source |
| 77 | + |
| 78 | + mock_get_ctx.return_value = mock_ctx |
| 79 | + check_deprecated_relationship() |
| 80 | + mock_log.assert_called_with('foo', 'bar', 'relationship') |
| 81 | + |
| 82 | + @mock.patch( |
| 83 | + 'cloudify_common_sdk.deprecation.check_deprecated_node_type') |
| 84 | + @mock.patch( |
| 85 | + 'cloudify_common_sdk.deprecation.check_deprecated_relationship') |
| 86 | + def test_decorator(self, mock_a, mock_b, *_): |
| 87 | + |
| 88 | + @deprecation_warning |
| 89 | + def foo(*args, **kwargs): |
| 90 | + for v in args: |
| 91 | + v('foo') |
| 92 | + for k, v in kwargs.items(): |
| 93 | + v(k) |
| 94 | + |
| 95 | + args = [mock.Mock(), mock.Mock()] |
| 96 | + kwargs = { |
| 97 | + 'a': mock.Mock(), |
| 98 | + 'b': mock.Mock() |
| 99 | + } |
| 100 | + |
| 101 | + foo(*args, **kwargs) |
| 102 | + for v in args: |
| 103 | + v.assert_called_with('foo') |
| 104 | + for k, v in kwargs.items(): |
| 105 | + v.assert_called_with(k) |
| 106 | + mock_a.assert_called_once() |
| 107 | + mock_b.assert_called_once() |
0 commit comments