Handle key has only metadata and no value
Some other etcd clients may add key without providing 'value'. This patch adds compatibility, so etcd can be used together with another clients Closes-bug: #1967674 Change-Id: I6319d58462481aac4b15048aad57de77094e8769
This commit is contained in:
parent
bbd14c466e
commit
e00c9f2ec4
@ -231,12 +231,11 @@ class Etcd3Client(object):
|
||||
if metadata:
|
||||
def value_with_metadata(item):
|
||||
item['key'] = _decode(item['key'])
|
||||
value = _decode(item.pop('value'))
|
||||
value = _decode(item.pop('value', ''))
|
||||
return value, item
|
||||
|
||||
return [value_with_metadata(item) for item in result['kvs']]
|
||||
else:
|
||||
return [_decode(item['value']) for item in result['kvs']]
|
||||
|
||||
return [_decode(item.get('value', '')) for item in result['kvs']]
|
||||
|
||||
def get_all(self, sort_order=None, sort_target='key'):
|
||||
"""Get all keys currently stored in etcd.
|
||||
|
@ -31,6 +31,7 @@ import urllib3
|
||||
from etcd3gw.client import Etcd3Client
|
||||
from etcd3gw import exceptions
|
||||
from etcd3gw.tests import base
|
||||
from etcd3gw import utils
|
||||
|
||||
try:
|
||||
# Python 3.8 : time.clock was deprecated and removed.
|
||||
@ -448,3 +449,44 @@ class TestEtcd3Gateway(base.TestCase):
|
||||
self.fail("watch timed out when server responded with unicode")
|
||||
|
||||
self.assertEqual(res, {'kv': {'key': b'value'}})
|
||||
|
||||
def _post_key(self, key_name, provide_value=True):
|
||||
payload = {"key": utils._encode(key_name)}
|
||||
if provide_value:
|
||||
payload["value"] = utils._encode(key_name)
|
||||
self.client.post(self.client.get_url("/kv/put"), json=payload)
|
||||
|
||||
@unittest.skipUnless(
|
||||
_is_etcd3_running(), "etcd3 is not available")
|
||||
def test_client_keys_with_metadata_and_value(self):
|
||||
test_key_value = b"some_key"
|
||||
self._post_key(test_key_value)
|
||||
result = self.client.get(test_key_value, metadata=True)
|
||||
self.assertTrue(
|
||||
len(result) > 0,
|
||||
str(test_key_value) + " key is not found in etcd"
|
||||
)
|
||||
value, metadata = result[0]
|
||||
self.assertEqual(
|
||||
value,
|
||||
test_key_value,
|
||||
"unable to get value for " + str(test_key_value)
|
||||
)
|
||||
|
||||
@unittest.skipUnless(
|
||||
_is_etcd3_running(), "etcd3 is not available")
|
||||
def test_client_keys_with_metadata_and_no_value(self):
|
||||
value_is_not_set_default = b""
|
||||
test_key = b"some_key"
|
||||
self._post_key(test_key, provide_value=False)
|
||||
result = self.client.get(test_key, metadata=True)
|
||||
self.assertTrue(
|
||||
len(result) > 0,
|
||||
str(test_key) + " key is not found in etcd"
|
||||
)
|
||||
value, metadata = result[0]
|
||||
self.assertEqual(
|
||||
value,
|
||||
value_is_not_set_default,
|
||||
"unable to get value for " + str(test_key)
|
||||
)
|
||||
|
4
releasenotes/notes/bug-1967674-102b70275b1627cd.yaml
Normal file
4
releasenotes/notes/bug-1967674-102b70275b1627cd.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
---
|
||||
fixes:
|
||||
- |
|
||||
Add compatibility with another etcd clients, that may have not provide value while posting a key
|
Loading…
Reference in New Issue
Block a user