|
| 1 | +import etcd |
| 2 | +import unittest |
| 3 | +import mock |
| 4 | + |
| 5 | +from etcd import EtcdException |
| 6 | + |
| 7 | + |
| 8 | +class TestClientRequest(unittest.TestCase): |
| 9 | + |
| 10 | + def test_machines(self): |
| 11 | + """ Can request machines """ |
| 12 | + client = etcd.Client() |
| 13 | + client.api_execute = mock.Mock( |
| 14 | + return_value= |
| 15 | + "http://127.0.0.1:4002," |
| 16 | + " http://127.0.0.1:4001," |
| 17 | + " http://127.0.0.1:4003," |
| 18 | + " http://127.0.0.1:4001" |
| 19 | + ) |
| 20 | + |
| 21 | + assert client.machines == [ |
| 22 | + 'http://127.0.0.1:4002', |
| 23 | + 'http://127.0.0.1:4001', |
| 24 | + 'http://127.0.0.1:4003', |
| 25 | + 'http://127.0.0.1:4001' |
| 26 | + ] |
| 27 | + |
| 28 | + def test_leader(self): |
| 29 | + """ Can request the leader """ |
| 30 | + client = etcd.Client() |
| 31 | + client.api_execute = mock.Mock(return_value="http://127.0.0.1:7002") |
| 32 | + result = client.leader |
| 33 | + self.assertEquals('http://127.0.0.1:7002', result) |
| 34 | + |
| 35 | + def test_set(self): |
| 36 | + """ Can set a value """ |
| 37 | + client = etcd.Client() |
| 38 | + client.api_execute = mock.Mock( |
| 39 | + return_value= |
| 40 | + '{"action":"SET",' |
| 41 | + '"key":"/testkey",' |
| 42 | + '"value":"test",' |
| 43 | + '"newKey":true,' |
| 44 | + '"expiration":"2013-09-14T00:56:59.316195568+02:00",' |
| 45 | + '"ttl":19,"index":183}') |
| 46 | + |
| 47 | + result = client.set('/testkey', 'test', ttl=19) |
| 48 | + |
| 49 | + self.assertEquals( |
| 50 | + etcd.EtcdResult( |
| 51 | + **{u'action': u'SET', |
| 52 | + u'expiration': u'2013-09-14T00:56:59.316195568+02:00', |
| 53 | + u'index': 183, |
| 54 | + u'key': u'/testkey', |
| 55 | + u'newKey': True, |
| 56 | + u'ttl': 19, |
| 57 | + u'value': u'test'}), result) |
| 58 | + |
| 59 | + def test_test_and_set(self): |
| 60 | + """ Can test and set a value """ |
| 61 | + client = etcd.Client() |
| 62 | + client.api_execute = mock.Mock( |
| 63 | + return_value= |
| 64 | + '{"action":"SET",' |
| 65 | + '"key":"/testkey",' |
| 66 | + '"prevValue":"test",' |
| 67 | + '"value":"newvalue",' |
| 68 | + '"expiration":"2013-09-14T02:09:44.24390976+02:00",' |
| 69 | + '"ttl":49,"index":203}') |
| 70 | + |
| 71 | + result = client.test_and_set('/testkey', 'newvalue', 'test', ttl=19) |
| 72 | + self.assertEquals( |
| 73 | + etcd.EtcdResult( |
| 74 | + **{u'action': u'SET', |
| 75 | + u'expiration': u'2013-09-14T02:09:44.24390976+02:00', |
| 76 | + u'index': 203, |
| 77 | + u'key': u'/testkey', |
| 78 | + u'prevValue': u'test', |
| 79 | + u'ttl': 49, |
| 80 | + u'value': u'newvalue'}), result) |
| 81 | + |
| 82 | + def test_test_and_test_failure(self): |
| 83 | + """ Exception will be raised if prevValue != value in test_set """ |
| 84 | + |
| 85 | + client = etcd.Client() |
| 86 | + client.api_execute = mock.Mock( |
| 87 | + side_effect=ValueError( |
| 88 | + 'The given PrevValue is not equal' |
| 89 | + ' to the value of the key : TestAndSet: 1!=3')) |
| 90 | + try: |
| 91 | + result = client.test_and_set( |
| 92 | + '/testkey', |
| 93 | + 'newvalue', |
| 94 | + 'test', ttl=19) |
| 95 | + except ValueError, e: |
| 96 | + #from ipdb import set_trace; set_trace() |
| 97 | + self.assertEquals( |
| 98 | + 'The given PrevValue is not equal' |
| 99 | + ' to the value of the key : TestAndSet: 1!=3', e.message) |
| 100 | + |
| 101 | + def test_delete(self): |
| 102 | + """ Can delete a value """ |
| 103 | + client = etcd.Client() |
| 104 | + client.api_execute = mock.Mock( |
| 105 | + return_value= |
| 106 | + '{"action":"DELETE",' |
| 107 | + '"key":"/testkey",' |
| 108 | + '"prevValue":"test",' |
| 109 | + '"expiration":"2013-09-14T01:06:35.5242587+02:00",' |
| 110 | + '"index":189}') |
| 111 | + |
| 112 | + result = client.delete('/testkey') |
| 113 | + self.assertEquals(etcd.EtcdResult( |
| 114 | + **{u'action': u'DELETE', |
| 115 | + u'expiration': u'2013-09-14T01:06:35.5242587+02:00', |
| 116 | + u'index': 189, |
| 117 | + u'key': u'/testkey', |
| 118 | + u'prevValue': u'test'}), result) |
| 119 | + |
| 120 | + def test_get(self): |
| 121 | + """ Can get a value """ |
| 122 | + client = etcd.Client() |
| 123 | + client.api_execute = mock.Mock( |
| 124 | + return_value= |
| 125 | + '{"action":"GET",' |
| 126 | + '"key":"/testkey",' |
| 127 | + '"value":"test",' |
| 128 | + '"index":190}') |
| 129 | + |
| 130 | + result = client.get('/testkey') |
| 131 | + self.assertEquals(etcd.EtcdResult( |
| 132 | + **{u'action': u'GET', |
| 133 | + u'index': 190, |
| 134 | + u'key': u'/testkey', |
| 135 | + u'value': u'test'}), result) |
| 136 | + |
| 137 | + def test_not_in(self): |
| 138 | + """ Can check if key is not in client """ |
| 139 | + client = etcd.Client() |
| 140 | + client.get = mock.Mock(side_effect=KeyError()) |
| 141 | + result = '/testkey' not in client |
| 142 | + self.assertEquals(True, result) |
| 143 | + |
| 144 | + def test_in(self): |
| 145 | + """ Can check if key is in client """ |
| 146 | + client = etcd.Client() |
| 147 | + client.api_execute = mock.Mock( |
| 148 | + return_value= |
| 149 | + '{"action":"GET",' |
| 150 | + '"key":"/testkey",' |
| 151 | + '"value":"test",' |
| 152 | + '"index":190}') |
| 153 | + result = '/testkey' in client |
| 154 | + |
| 155 | + self.assertEquals(True, result) |
| 156 | + |
| 157 | + def test_simple_watch(self): |
| 158 | + """ Can watch values """ |
| 159 | + client = etcd.Client() |
| 160 | + client.api_execute = mock.Mock( |
| 161 | + return_value= |
| 162 | + '{"action":"SET",' |
| 163 | + '"key":"/testkey",' |
| 164 | + '"value":"test",' |
| 165 | + '"newKey":true,' |
| 166 | + '"expiration":"2013-09-14T01:35:07.623681365+02:00",' |
| 167 | + '"ttl":19,' |
| 168 | + '"index":192}') |
| 169 | + result = client.watch('/testkey') |
| 170 | + self.assertEquals( |
| 171 | + etcd.EtcdResult( |
| 172 | + **{u'action': u'SET', |
| 173 | + u'expiration': u'2013-09-14T01:35:07.623681365+02:00', |
| 174 | + u'index': 192, |
| 175 | + u'key': u'/testkey', |
| 176 | + u'newKey': True, |
| 177 | + u'ttl': 19, |
| 178 | + u'value': u'test'}), result) |
| 179 | + |
| 180 | + def test_index_watch(self): |
| 181 | + """ Can watch values from index """ |
| 182 | + client = etcd.Client() |
| 183 | + client.api_execute = mock.Mock( |
| 184 | + return_value= |
| 185 | + '{"action":"SET",' |
| 186 | + '"key":"/testkey",' |
| 187 | + '"value":"test",' |
| 188 | + '"newKey":true,' |
| 189 | + '"expiration":"2013-09-14T01:35:07.623681365+02:00",' |
| 190 | + '"ttl":19,' |
| 191 | + '"index":180}') |
| 192 | + result = client.watch('/testkey', index=180) |
| 193 | + self.assertEquals( |
| 194 | + etcd.EtcdResult( |
| 195 | + **{u'action': u'SET', |
| 196 | + u'expiration': u'2013-09-14T01:35:07.623681365+02:00', |
| 197 | + u'index': 180, |
| 198 | + u'key': u'/testkey', |
| 199 | + u'newKey': True, |
| 200 | + u'ttl': 19, |
| 201 | + u'value': u'test'}), result) |
| 202 | + |
| 203 | + |
| 204 | +class TestEventGenerator(object): |
| 205 | + def check_watch(self, result): |
| 206 | + assert etcd.EtcdResult( |
| 207 | + **{u'action': u'SET', |
| 208 | + u'expiration': u'2013-09-14T01:35:07.623681365+02:00', |
| 209 | + u'index': 180, |
| 210 | + u'key': u'/testkey', |
| 211 | + u'newKey': True, |
| 212 | + u'ttl': 19, |
| 213 | + u'value': u'test'}) == result |
| 214 | + |
| 215 | + def test_ethernal_watch(self): |
| 216 | + """ Can watch values from generator """ |
| 217 | + client = etcd.Client() |
| 218 | + client.api_execute = mock.Mock( |
| 219 | + return_value= |
| 220 | + '{"action":"SET",' |
| 221 | + '"key":"/testkey",' |
| 222 | + '"value":"test",' |
| 223 | + '"newKey":true,' |
| 224 | + '"expiration":"2013-09-14T01:35:07.623681365+02:00",' |
| 225 | + '"ttl":19,' |
| 226 | + '"index":180}') |
| 227 | + for result in range(1, 5): |
| 228 | + result = client.ethernal_watch('/testkey', index=180).next() |
| 229 | + yield self.check_watch, result |
| 230 | + |
| 231 | + |
| 232 | +class FakeHTTPResponse(object): |
| 233 | + def __init__(self, status, data=''): |
| 234 | + self.status = status |
| 235 | + self.data = data |
| 236 | + |
| 237 | + |
| 238 | +class TestClientApiExecutor(unittest.TestCase): |
| 239 | + |
| 240 | + def test_get(self): |
| 241 | + """ http get request """ |
| 242 | + client = etcd.Client() |
| 243 | + response = FakeHTTPResponse(status=200, data='arbitrary json data') |
| 244 | + client.http.request = mock.Mock(return_value=response) |
| 245 | + result = client.api_execute('/v1/keys/testkey', client._MGET) |
| 246 | + self.assertEquals('arbitrary json data', result) |
| 247 | + |
| 248 | + def test_delete(self): |
| 249 | + """ http delete request """ |
| 250 | + client = etcd.Client() |
| 251 | + response = FakeHTTPResponse(status=200, data='arbitrary json data') |
| 252 | + client.http.request = mock.Mock(return_value=response) |
| 253 | + result = client.api_execute('/v1/keys/testkey', client._MDELETE) |
| 254 | + self.assertEquals('arbitrary json data', result) |
| 255 | + |
| 256 | + def test_get_error(self): |
| 257 | + """ http get error request 101""" |
| 258 | + client = etcd.Client() |
| 259 | + response = FakeHTTPResponse(status=400, |
| 260 | + data='{"message": "message",' |
| 261 | + ' "cause": "cause",' |
| 262 | + ' "errorCode": 100}') |
| 263 | + client.http.request = mock.Mock(return_value=response) |
| 264 | + try: |
| 265 | + client.api_execute('v1/keys/testkey', client._MGET) |
| 266 | + assert False |
| 267 | + except KeyError, e: |
| 268 | + self.assertEquals(e.message, "message : cause") |
| 269 | + |
| 270 | + def test_post(self): |
| 271 | + """ http post request """ |
| 272 | + client = etcd.Client() |
| 273 | + response = FakeHTTPResponse(status=200, data='arbitrary json data') |
| 274 | + client.http.request_encode_body = mock.Mock(return_value=response) |
| 275 | + result = client.api_execute('v1/keys/testkey', client._MPOST) |
| 276 | + self.assertEquals('arbitrary json data', result) |
| 277 | + |
| 278 | + def test_test_and_set_error(self): |
| 279 | + """ http post error request 101 """ |
| 280 | + client = etcd.Client() |
| 281 | + response = FakeHTTPResponse( |
| 282 | + status=400, |
| 283 | + data='{"message": "message", "cause": "cause", "errorCode": 101}') |
| 284 | + client.http.request_encode_body = mock.Mock(return_value=response) |
| 285 | + payload = {'value': 'value', 'prevValue': 'oldValue', 'ttl': '60'} |
| 286 | + try: |
| 287 | + client.api_execute('v1/keys/testkey', client._MPOST, payload) |
| 288 | + self.fail() |
| 289 | + except ValueError, e: |
| 290 | + self.assertEquals('message : cause', e.message) |
| 291 | + |
| 292 | + def test_set_error(self): |
| 293 | + """ http post error request 102 """ |
| 294 | + client = etcd.Client() |
| 295 | + response = FakeHTTPResponse( |
| 296 | + status=400, |
| 297 | + data='{"message": "message", "cause": "cause", "errorCode": 102}') |
| 298 | + client.http.request_encode_body = mock.Mock(return_value=response) |
| 299 | + payload = {'value': 'value', 'prevValue': 'oldValue', 'ttl': '60'} |
| 300 | + try: |
| 301 | + client.api_execute('v1/keys/testkey', client._MPOST, payload) |
| 302 | + self.fail() |
| 303 | + except KeyError, e: |
| 304 | + self.assertEquals('message : cause', e.message) |
| 305 | + |
| 306 | + def test_set_error(self): |
| 307 | + """ http post error request 102 """ |
| 308 | + client = etcd.Client() |
| 309 | + response = FakeHTTPResponse( |
| 310 | + status=400, |
| 311 | + data='{"message": "message", "cause": "cause", "errorCode": 102}') |
| 312 | + client.http.request_encode_body = mock.Mock(return_value=response) |
| 313 | + payload = {'value': 'value', 'prevValue': 'oldValue', 'ttl': '60'} |
| 314 | + try: |
| 315 | + client.api_execute('v1/keys/testkey', client._MPOST, payload) |
| 316 | + self.fail() |
| 317 | + except KeyError, e: |
| 318 | + self.assertEquals('message : cause', e.message) |
| 319 | + |
| 320 | + def test_get_error_unknown(self): |
| 321 | + """ http get error request unknown """ |
| 322 | + client = etcd.Client() |
| 323 | + response = FakeHTTPResponse(status=400, |
| 324 | + data='{"message": "message",' |
| 325 | + ' "cause": "cause",' |
| 326 | + ' "errorCode": 42}') |
| 327 | + client.http.request = mock.Mock(return_value=response) |
| 328 | + try: |
| 329 | + client.api_execute('v1/keys/testkey', client._MGET) |
| 330 | + assert False |
| 331 | + except EtcdException, e: |
| 332 | + self.assertEquals(e.message, "Unable to decode server response") |
| 333 | + |
| 334 | + def test_get_error_request_invalid(self): |
| 335 | + """ http get error request invalid """ |
| 336 | + client = etcd.Client() |
| 337 | + response = FakeHTTPResponse(status=200, |
| 338 | + data='{){){)*garbage*') |
| 339 | + client.http.request = mock.Mock(return_value=response) |
| 340 | + try: |
| 341 | + client.get('/testkey') |
| 342 | + assert False |
| 343 | + except EtcdException, e: |
| 344 | + self.assertEquals(e.message, "Unable to decode server response") |
| 345 | + |
| 346 | + def test_get_error_invalid(self): |
| 347 | + """ http get error request invalid """ |
| 348 | + client = etcd.Client() |
| 349 | + response = FakeHTTPResponse(status=400, |
| 350 | + data='{){){)*garbage*') |
| 351 | + client.http.request = mock.Mock(return_value=response) |
| 352 | + try: |
| 353 | + client.api_execute('v1/keys/testkey', client._MGET) |
| 354 | + assert False |
| 355 | + except EtcdException, e: |
| 356 | + self.assertEquals(e.message, "Unable to decode server response") |
0 commit comments