Skip to content

Commit 4e5dd28

Browse files
committed
Use api_execute for getting the version of the cluster
This will allow the same kind of safety net everything gets besides the fetch of machines.
1 parent b999001 commit 4e5dd28

2 files changed

Lines changed: 33 additions & 52 deletions

File tree

src/etcd/client.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,12 +214,8 @@ def _set_version_info(self):
214214
Sets the version information provided by the server.
215215
"""
216216
# Set the version
217-
version_info = json.loads(self.http.request(
218-
self._MGET,
219-
self._base_uri + '/version',
220-
headers=self._get_headers(),
221-
timeout=self.read_timeout,
222-
redirect=self.allow_redirect).data.decode('utf-8'))
217+
data = self.api_execute('/version', self._MGET).data
218+
version_info = json.loads(data.decode('utf-8'))
223219
self._version = version_info['etcdserver']
224220
self._cluster_version = version_info['etcdcluster']
225221

@@ -856,7 +852,7 @@ def wrapper(self, path, method, params=None, timeout=None):
856852
# Check the cluster ID hasn't changed under us. We use
857853
# preload_content=False above so we can read the headers
858854
# before we wait for the content of a watch.
859-
self._check_cluster_id(response)
855+
self._check_cluster_id(response, path)
860856
# Now force the data to be preloaded in order to trigger any
861857
# IO-related errors in this method rather than when we try to
862858
# access it later.
@@ -950,10 +946,11 @@ def api_execute_json(self, path, method, params=None, timeout=None):
950946
headers=headers,
951947
preload_content=False)
952948

953-
def _check_cluster_id(self, response):
949+
def _check_cluster_id(self, response, path):
954950
cluster_id = response.getheader("x-etcd-cluster-id")
955951
if not cluster_id:
956-
_log.warning("etcd response did not contain a cluster ID")
952+
if self.version_prefix in path:
953+
_log.warning("etcd response did not contain a cluster ID")
957954
return
958955
id_changed = (self.expected_cluster_id and
959956
cluster_id != self.expected_cluster_id)

src/etcd/tests/unit/test_client.py

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import dns.name
44
import dns.rdtypes.IN.SRV
55
import dns.resolver
6+
from etcd.tests.unit import TestClientApiBase
67
try:
78
import mock
89
except ImportError:
910
from unittest import mock
1011

1112

12-
class TestClient(unittest.TestCase):
13+
class TestClient(TestClientApiBase):
14+
1315

1416
def test_instantiate(self):
1517
""" client can be instantiated"""
@@ -123,55 +125,37 @@ def test_get_headers_with_auth(self):
123125

124126
def test__set_version_info(self):
125127
"""Verify _set_version_info makes the proper call to the server"""
126-
with mock.patch('urllib3.PoolManager') as _pm:
127-
_request = _pm().request
128-
# Return the expected data type
129-
_request.return_value = mock.MagicMock(
130-
data=b'{"etcdserver": "2.2.3", "etcdcluster": "2.3.0"}')
131-
132-
# Create the client and make the call.
133-
client = etcd.Client()
134-
client._set_version_info()
135-
136-
# Verify we call the proper endpoint
137-
_request.assert_called_once_with(
138-
client._MGET,
139-
client._base_uri + '/version',
140-
headers=mock.ANY,
141-
redirect=mock.ANY,
142-
timeout=mock.ANY)
143-
144-
# Verify the properties while we are here
145-
self.assertEquals('2.2.3', client.version)
146-
self.assertEquals('2.3.0', client.cluster_version)
128+
data = {"etcdserver": "2.2.3", "etcdcluster": "2.3.0"}
129+
self._mock_api(200, data)
130+
self.client.api_execute.return_value.getheader.return_value = None
131+
# Create the client and make the call.
132+
self.client._set_version_info()
133+
134+
# Verify we call the proper endpoint
135+
self.client.api_execute.assert_called_once_with(
136+
'/version',
137+
self.client._MGET
138+
)
139+
# Verify the properties while we are here
140+
self.assertEquals('2.2.3', self.client.version)
141+
self.assertEquals('2.3.0', self.client.cluster_version)
147142

148143
def test_version_property(self):
149144
"""Ensure the version property is set on first access."""
150-
with mock.patch('urllib3.PoolManager') as _pm:
151-
_request = _pm().request
152-
# Return the expected data type
153-
_request.return_value = mock.MagicMock(
154-
data=b'{"etcdserver": "2.2.3", "etcdcluster": "2.3.0"}')
155-
156-
# Create the client.
157-
client = etcd.Client()
145+
data = {"etcdserver": "2.2.3", "etcdcluster": "2.3.0"}
146+
self._mock_api(200, data)
147+
self.client.api_execute.return_value.getheader.return_value = None
158148

159-
# Verify the version property is set
160-
self.assertEquals('2.2.3', client.version)
149+
# Verify the version property is set
150+
self.assertEquals('2.2.3', self.client.version)
161151

162152
def test_cluster_version_property(self):
163153
"""Ensure the cluster version property is set on first access."""
164-
with mock.patch('urllib3.PoolManager') as _pm:
165-
_request = _pm().request
166-
# Return the expected data type
167-
_request.return_value = mock.MagicMock(
168-
data=b'{"etcdserver": "2.2.3", "etcdcluster": "2.3.0"}')
169-
170-
# Create the client.
171-
client = etcd.Client()
172-
173-
# Verify the cluster_version property is set
174-
self.assertEquals('2.3.0', client.cluster_version)
154+
data = {"etcdserver": "2.2.3", "etcdcluster": "2.3.0"}
155+
self._mock_api(200, data)
156+
self.client.api_execute.return_value.getheader.return_value = None
157+
# Verify the cluster_version property is set
158+
self.assertEquals('2.3.0', self.client.cluster_version)
175159

176160
def test_get_headers_without_auth(self):
177161
client = etcd.Client()

0 commit comments

Comments
 (0)