Debian Codegraph @

monascaclient/tests/test_client.py

raw
1 # (C) Copyright 2014-2017 Hewlett Packard Enterprise Development LP
2 # Copyright 2017 FUJITSU LIMITED
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13 # implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 from unittest import mock
18 import warnings
19
20 from oslotest import base
21
22 from monascaclient import client
23
24
25 class TestMonascaClient(base.BaseTestCase):
26
27 @mock.patch('monascaclient.client.migration')
28 @mock.patch('monascaclient.client._get_auth_handler')
29 @mock.patch('monascaclient.client._get_session')
30 def test_should_warn_when_passing_args(self, _, __, ___):
31
32 api_version = mock.Mock()
33 endpoint = mock.Mock()
34
35 with warnings.catch_warnings(record=True) as w:
36 warnings.simplefilter('always')
37
38 client.Client(api_version, endpoint)
39
40 self.assertEqual(1, len(w))
41 self.assertEqual(DeprecationWarning, w[0].category)
42 self.assertRegex(
43 str(w[0].message),
44 'explicit configuration of the client using'
45 )
46
47 @mock.patch('monascaclient.client.migration')
48 @mock.patch('monascaclient.client._get_auth_handler')
49 @mock.patch('monascaclient.client._get_session')
50 def test_should_not_warn_when_passing_no_args(self, _, __, ___):
51 api_version = mock.Mock()
52
53 with warnings.catch_warnings(record=True) as w:
54 warnings.simplefilter('always')
55 client.Client(api_version)
56 self.assertEqual(0, len(w))
57
58 @mock.patch('monascaclient.client.migration')
59 @mock.patch('monascaclient.client._get_auth_handler')
60 @mock.patch('monascaclient.client._get_session')
61 def test_should_override_endpoint_if_passed_as_arg(self, get_session,
62 get_auth, _):
63 api_version = mock.Mock()
64 endpoint = mock.Mock()
65 endpoint_fake = mock.Mock()
66 auth_val = mock.Mock()
67
68 get_auth.return_value = auth_val
69
70 with warnings.catch_warnings(record=True) as w:
71 warnings.simplefilter('always')
72 client.Client(api_version, endpoint, endpoint=endpoint_fake)
73 self.assertEqual(1, len(w))
74
75 get_auth.assert_called_once_with({
76 'endpoint': endpoint
77 })
78 get_session.assert_called_once_with(auth_val, {
79 'endpoint': endpoint
80 })
81
82 @mock.patch('monascaclient.client.migration')
83 @mock.patch('monascaclient.client._get_auth_handler')
84 @mock.patch('monascaclient.client._get_session')
85 def test_should_override_tenant_name_with_project_name(self,
86 _,
87 get_auth,
88 __):
89 api_version = mock.Mock()
90 auth_val = mock.Mock()
91 tenant_name = mock.Mock()
92 project_name = tenant_name
93
94 get_auth.return_value = auth_val
95
96 with warnings.catch_warnings(record=True) as w:
97 warnings.simplefilter('always')
98 client.Client(api_version, tenant_name=tenant_name)
99
100 self.assertEqual(1, len(w))
101 self.assertEqual(DeprecationWarning, w[0].category)
102 self.assertRegex(
103 str(w[0].message),
104 'Usage of tenant_name has been deprecated in favour '
105 )
106
107 get_auth.assert_called_once_with({
108 'project_name': project_name
109 })
110
111 @mock.patch('monascaclient.client.migration')
112 @mock.patch('monascaclient.client._get_auth_handler')
113 @mock.patch('monascaclient.client._get_session')
114 def test_should_override_insecure_with_negated_verify(self,
115 _,
116 get_auth,
117 __):
118 api_version = mock.Mock()
119 auth_val = mock.Mock()
120 get_auth.return_value = auth_val
121
122 for insecure in [True, False]:
123 warnings.resetwarnings()
124 with warnings.catch_warnings(record=True) as w:
125 warnings.simplefilter('always')
126 client.Client(api_version, insecure=insecure)
127
128 self.assertEqual(1, len(w))
129 self.assertEqual(DeprecationWarning, w[0].category)
130 self.assertRegex(
131 str(w[0].message),
132 'Usage of insecure has been deprecated in favour of'
133 )
134
135 get_auth.assert_called_once_with({
136 'verify': not insecure
137 })
138 get_auth.reset_mock()
139
140 @mock.patch('monascaclient.client.migration')
141 @mock.patch('monascaclient.client._get_auth_handler')
142 @mock.patch('monascaclient.client._get_session')
143 def test_should_reuse_the_session_if_initialized_with_one(self,
144 get_session,
145 get_auth,
146 _):
147 from keystoneauth1 import session as k_session
148
149 api_version = mock.Mock()
150 session = mock.Mock(spec=k_session.Session)
151
152 with warnings.catch_warnings(record=True) as w:
153 warnings.simplefilter('always')
154 client.Client(api_version, session=session)
155 self.assertEqual(0, len(w))
156
157 get_auth.assert_not_called()
158 get_session.assert_not_called()
159
160 @mock.patch('monascaclient.client.migration')
161 @mock.patch('monascaclient.client._get_auth_handler')
162 @mock.patch('monascaclient.client._get_session')
163 def test_should_error_if_session_is_not_in_correct_type(self,
164 _,
165 __,
166 ___):
167
168 api_version = mock.Mock()
169 for cls in [str, int, float]:
170 session = mock.Mock(spec=cls)
171 self.assertRaises(RuntimeError, client.Client,
172 api_version, session=session)