Debian Codegraph @

etcd3gw/tests/test_client.py

raw symbols SCIP
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import requests.exceptions
14 from unittest import mock
15
16 from etcd3gw.client import Etcd3Client
17 from etcd3gw import exceptions as exc
18 from etcd3gw.tests import base
19
20
21 class TestEtcd3Gateway(base.TestCase):
22
23 def test_client_version_discovery(self):
24 client = Etcd3Client()
25 with mock.patch.object(client, "session") as mock_session:
26 mock_response = mock.Mock()
27 mock_response.status_code = 200
28 mock_response.reason = "OK"
29 mock_response.json.return_value = {
30 "etcdserver": "3.4.0",
31 "etcdcluster": "3.0.0"
32 }
33 mock_session.get.return_value = mock_response
34 self.assertEqual("http://localhost:2379",
35 client.base_url)
36 self.assertEqual("http://localhost:2379/v3/lease/grant",
37 client.get_url("/lease/grant"))
38
39 def test_client_version_discovery_v3beta(self):
40 client = Etcd3Client()
41 with mock.patch.object(client, "session") as mock_session:
42 mock_response = mock.Mock()
43 mock_response.status_code = 200
44 mock_response.reason = "OK"
45 mock_response.json.return_value = {
46 "etcdserver": "3.3.0",
47 "etcdcluster": "3.0.0"
48 }
49 mock_session.get.return_value = mock_response
50 self.assertEqual("http://localhost:2379",
51 client.base_url)
52 self.assertEqual("http://localhost:2379/v3beta/lease/grant",
53 client.get_url("/lease/grant"))
54
55 def test_client_version_discovery_v3alpha(self):
56 client = Etcd3Client()
57 with mock.patch.object(client, "session") as mock_session:
58 mock_response = mock.Mock()
59 mock_response.status_code = 200
60 mock_response.reason = "OK"
61 mock_response.json.return_value = {
62 "etcdserver": "3.2.0",
63 "etcdcluster": "3.0.0"
64 }
65 mock_session.get.return_value = mock_response
66 self.assertEqual("http://localhost:2379",
67 client.base_url)
68 self.assertEqual("http://localhost:2379/v3alpha/lease/grant",
69 client.get_url("/lease/grant"))
70
71 def test_client_version_discovery_fail(self):
72 client = Etcd3Client()
73 with mock.patch.object(client, "session") as mock_session:
74 mock_response = mock.Mock()
75 mock_response.status_code = 500
76 mock_response.reason = "Internal Server Error"
77 mock_session.get.return_value = mock_response
78 self.assertRaises(
79 exc.Etcd3Exception,
80 client.get_url, "/lease/grant")
81
82 def test_client_version_discovery_version_absent(self):
83 client = Etcd3Client()
84 with mock.patch.object(client, "session") as mock_session:
85 mock_response = mock.Mock()
86 mock_response.status_code = 200
87 mock_response.reason = "OK"
88 mock_response.json.return_value = {}
89 mock_session.get.return_value = mock_response
90 self.assertRaises(
91 exc.ApiVersionDiscoveryFailedError,
92 client.get_url, "/lease/grant")
93
94 def test_client_version_discovery_version_malformed(self):
95 client = Etcd3Client()
96 with mock.patch.object(client, "session") as mock_session:
97 mock_response = mock.Mock()
98 mock_response.status_code = 200
99 mock_response.reason = "OK"
100 mock_response.json.return_value = {
101 "etcdserver": "3.2.a",
102 "etcdcluster": "3.0.0"
103 }
104 mock_session.get.return_value = mock_response
105 self.assertRaises(
106 exc.ApiVersionDiscoveryFailedError,
107 client.get_url, "/lease/grant")
108
109 def test_client_api_path(self):
110 client = Etcd3Client(api_path='/v3/')
111 self.assertEqual("http://localhost:2379",
112 client.base_url)
113 self.assertEqual("http://localhost:2379/v3/lease/grant",
114 client.get_url("/lease/grant"))
115
116 def test_client_ipv4(self):
117 client = Etcd3Client(host="127.0.0.1", api_path='/v3/')
118 self.assertEqual("http://127.0.0.1:2379",
119 client.base_url)
120 self.assertEqual("http://127.0.0.1:2379/v3/lease/grant",
121 client.get_url("/lease/grant"))
122
123 def test_client_ipv6(self):
124 client = Etcd3Client(host="::1", api_path='/v3/')
125 self.assertEqual("http://[::1]:2379",
126 client.base_url)
127 self.assertEqual("http://[::1]:2379/v3/lease/grant",
128 client.get_url("/lease/grant"))
129
130 def test_client_status(self):
131 client = Etcd3Client(host="127.0.0.1", api_path='/v3/')
132 with mock.patch.object(client, "session") as mock_session:
133 mock_response = mock.Mock()
134 mock_response.status_code = 200
135 mock_response.reason = "OK"
136 mock_response.text = "{}"
137 mock_session.post.return_value = mock_response
138 client.status()
139 mock_session.post.assert_has_calls([
140 mock.call('http://127.0.0.1:2379/v3/maintenance/status',
141 timeout=None, json={})
142 ])
143
144 def test_client_with_timeout(self):
145 client = Etcd3Client(host="127.0.0.1", api_path='/v3/', timeout=60)
146 with mock.patch.object(client, "session") as mock_session:
147 mock_response = mock.Mock()
148 mock_response.status_code = 200
149 mock_response.reason = "OK"
150 mock_response.text = "{}"
151 mock_session.post.return_value = mock_response
152 client.status()
153 mock_session.post.assert_has_calls([
154 mock.call('http://127.0.0.1:2379/v3/maintenance/status',
155 timeout=60, json={})
156 ])
157
158 def test_client_timed_out(self):
159 client = Etcd3Client(host="127.0.0.1", api_path='/v3/', timeout=60)
160 with mock.patch.object(client, "session") as mock_session:
161 mock_session.post.side_effect = requests.exceptions.Timeout()
162 self.assertRaises(exc.ConnectionTimeoutError, client.status)
163 mock_session.post.assert_has_calls([
164 mock.call('http://127.0.0.1:2379/v3/maintenance/status',
165 timeout=60, json={})
166 ])
167
168 def test_client_connection_error(self):
169 client = Etcd3Client(host="127.0.0.1", api_path='/v3/')
170 with mock.patch.object(client, "session") as mock_session:
171 mock_session.post.side_effect = \
172 requests.exceptions.ConnectionError()
173 self.assertRaises(exc.ConnectionFailedError, client.status)
174 mock_session.post.assert_has_calls([
175 mock.call('http://127.0.0.1:2379/v3/maintenance/status',
176 timeout=None, json={})
177 ])
178
179 def test_client_bad_request(self):
180 client = Etcd3Client(host="127.0.0.1", api_path='/v3/')
181 with mock.patch.object(client, "session") as mock_session:
182 mock_response = mock.Mock()
183 mock_response.status_code = 400
184 mock_response.reason = "Bad Request"
185 mock_response.text = '''{
186 "error": "etcdserver: mvcc: required revision has been compacted",
187 "code": 11
188 }'''
189 mock_session.post.return_value = mock_response
190 try:
191 client.status()
192 self.assertFalse(True)
193 except exc.Etcd3Exception as e:
194 self.assertEqual(str(e), "Bad Request")
195 self.assertEqual(e.detail_text, '''{
196 "error": "etcdserver: mvcc: required revision has been compacted",
197 "code": 11
198 }''')
199
200 def test_client_exceptions_by_code(self):
201 client = Etcd3Client(host="127.0.0.1", api_path='/v3/')
202 with mock.patch.object(client, "session") as mock_session:
203 mock_response = mock.Mock()
204 mock_response.status_code = 500
205 mock_response.reason = "Internal Server Error"
206 mock_response.text = '''{
207 "error": "etcdserver: unable to reach quorum"
208 }'''
209 mock_session.post.return_value = mock_response
210 try:
211 client.status()
212 self.assertFalse(True)
213 except exc.InternalServerError as e:
214 self.assertEqual(str(e), "Internal Server Error")
215 self.assertEqual(e.detail_text, '''{
216 "error": "etcdserver: unable to reach quorum"
217 }''')