| 1 |
import struct |
| 2 |
|
| 3 |
import pytest |
| 4 |
|
| 5 |
from consul import ConsulException |
| 6 |
|
| 7 |
|
| 8 |
class TestConsul: |
| 9 |
def test_kv(self, consul_obj) -> None: |
| 10 |
c, _consul_version = consul_obj |
| 11 |
_index, data = c.kv.get("foo") |
| 12 |
assert data is None |
| 13 |
assert c.kv.put("foo", "bar") is True |
| 14 |
_index, data = c.kv.get("foo") |
| 15 |
assert data["Value"] == b"bar" |
| 16 |
|
| 17 |
def test_kv_wait(self, consul_obj) -> None: |
| 18 |
c, _consul_version = consul_obj |
| 19 |
assert c.kv.put("foo", "bar") is True |
| 20 |
index, _data = c.kv.get("foo") |
| 21 |
check, _data = c.kv.get("foo", index=index, wait="20ms") |
| 22 |
assert index == check |
| 23 |
|
| 24 |
def test_kv_encoding(self, consul_obj) -> None: |
| 25 |
c, _consul_version = consul_obj |
| 26 |
|
| 27 |
|
| 28 |
c.kv.put("foo", struct.pack("i", 1000)) |
| 29 |
_index, data = c.kv.get("foo") |
| 30 |
assert struct.unpack("i", data["Value"]) == (1000,) |
| 31 |
|
| 32 |
|
| 33 |
c.kv.put("foo", "bar") |
| 34 |
_index, data = c.kv.get("foo") |
| 35 |
assert data["Value"] == b"bar" |
| 36 |
|
| 37 |
|
| 38 |
c.kv.put("foo", "") |
| 39 |
_index, data = c.kv.get("foo") |
| 40 |
assert data["Value"] is None |
| 41 |
|
| 42 |
|
| 43 |
c.kv.put("foo", None) |
| 44 |
_index, data = c.kv.get("foo") |
| 45 |
assert data["Value"] is None |
| 46 |
|
| 47 |
|
| 48 |
pytest.raises(AssertionError, c.kv.put, "foo", {1: 2}) |
| 49 |
|
| 50 |
def test_kv_put_cas(self, consul_obj) -> None: |
| 51 |
c, _consul_version = consul_obj |
| 52 |
assert c.kv.put("foo", "bar", cas=50) is False |
| 53 |
assert c.kv.put("foo", "bar", cas=0) is True |
| 54 |
_index, data = c.kv.get("foo") |
| 55 |
|
| 56 |
assert c.kv.put("foo", "bar2", cas=data["ModifyIndex"] - 1) is False |
| 57 |
assert c.kv.put("foo", "bar2", cas=data["ModifyIndex"]) is True |
| 58 |
_index, data = c.kv.get("foo") |
| 59 |
assert data["Value"] == b"bar2" |
| 60 |
|
| 61 |
def test_kv_put_flags(self, consul_obj) -> None: |
| 62 |
c, _consul_version = consul_obj |
| 63 |
c.kv.put("foo", "bar") |
| 64 |
_index, data = c.kv.get("foo") |
| 65 |
assert data["Flags"] == 0 |
| 66 |
|
| 67 |
assert c.kv.put("foo", "bar", flags=50) is True |
| 68 |
_index, data = c.kv.get("foo") |
| 69 |
assert data["Flags"] == 50 |
| 70 |
|
| 71 |
def test_kv_recurse(self, consul_obj) -> None: |
| 72 |
c, _consul_version = consul_obj |
| 73 |
_index, data = c.kv.get("foo/", recurse=True) |
| 74 |
assert data is None |
| 75 |
|
| 76 |
c.kv.put("foo/", None) |
| 77 |
_index, data = c.kv.get("foo/", recurse=True) |
| 78 |
assert len(data) == 1 |
| 79 |
|
| 80 |
c.kv.put("foo/bar1", "1") |
| 81 |
c.kv.put("foo/bar2", "2") |
| 82 |
c.kv.put("foo/bar3", "3") |
| 83 |
_index, data = c.kv.get("foo/", recurse=True) |
| 84 |
assert [x["Key"] for x in data] == ["foo/", "foo/bar1", "foo/bar2", "foo/bar3"] |
| 85 |
assert [x["Value"] for x in data] == [None, b"1", b"2", b"3"] |
| 86 |
|
| 87 |
def test_kv_delete(self, consul_obj) -> None: |
| 88 |
c, _consul_version = consul_obj |
| 89 |
c.kv.put("foo1", "1") |
| 90 |
c.kv.put("foo2", "2") |
| 91 |
c.kv.put("foo3", "3") |
| 92 |
_index, data = c.kv.get("foo", recurse=True) |
| 93 |
assert [x["Key"] for x in data] == ["foo1", "foo2", "foo3"] |
| 94 |
|
| 95 |
assert c.kv.delete("foo2") is True |
| 96 |
_index, data = c.kv.get("foo", recurse=True) |
| 97 |
assert [x["Key"] for x in data] == ["foo1", "foo3"] |
| 98 |
assert c.kv.delete("foo", recurse=True) is True |
| 99 |
_index, data = c.kv.get("foo", recurse=True) |
| 100 |
assert data is None |
| 101 |
|
| 102 |
def test_kv_delete_cas(self, consul_obj) -> None: |
| 103 |
c, _consul_version = consul_obj |
| 104 |
|
| 105 |
c.kv.put("foo", "bar") |
| 106 |
index, data = c.kv.get("foo") |
| 107 |
|
| 108 |
assert c.kv.delete("foo", cas=data["ModifyIndex"] - 1) is False |
| 109 |
assert c.kv.get("foo") == (index, data) |
| 110 |
|
| 111 |
assert c.kv.delete("foo", cas=data["ModifyIndex"]) is True |
| 112 |
index, data = c.kv.get("foo") |
| 113 |
assert data is None |
| 114 |
|
| 115 |
def test_kv_acquire_release(self, consul_obj) -> None: |
| 116 |
c, _consul_version = consul_obj |
| 117 |
|
| 118 |
pytest.raises(ConsulException, c.kv.put, "foo", "bar", acquire="foo") |
| 119 |
|
| 120 |
s1 = c.session.create() |
| 121 |
s2 = c.session.create() |
| 122 |
|
| 123 |
assert c.kv.put("foo", "1", acquire=s1) is True |
| 124 |
assert c.kv.put("foo", "2", acquire=s2) is False |
| 125 |
assert c.kv.put("foo", "1", acquire=s1) is True |
| 126 |
assert c.kv.put("foo", "1", release="foo") is False |
| 127 |
assert c.kv.put("foo", "2", release=s2) is False |
| 128 |
assert c.kv.put("foo", "2", release=s1) is True |
| 129 |
|
| 130 |
c.session.destroy(s1) |
| 131 |
c.session.destroy(s2) |
| 132 |
|
| 133 |
def test_kv_keys_only(self, consul_obj) -> None: |
| 134 |
c, _consul_version = consul_obj |
| 135 |
|
| 136 |
assert c.kv.put("bar", "4") is True |
| 137 |
assert c.kv.put("base/foo", "1") is True |
| 138 |
assert c.kv.put("base/base/foo", "5") is True |
| 139 |
|
| 140 |
_index, data = c.kv.get("base/", keys=True, separator="/") |
| 141 |
assert data == ["base/base/", "base/foo"] |