/usr/share/pyshared/txzookeeper/tests/test_node.py is in python-txzookeeper 0.9.5-0ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | #
# Copyright (C) 2010-2011 Canonical Ltd. All Rights Reserved
#
# This file is part of txzookeeper.
#
# Authors:
# Kapil Thangavelu
#
# txzookeeper is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# txzookeeper is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with txzookeeper. If not, see <http://www.gnu.org/licenses/>.
#
import base64
import hashlib
import zookeeper
from twisted.internet.defer import inlineCallbacks
from twisted.python.failure import Failure
from txzookeeper.node import ZNode
from txzookeeper.node import NodeEvent
from txzookeeper.tests import TestCase
from txzookeeper.tests.utils import deleteTree
from txzookeeper import ZookeeperClient
class NodeTest(TestCase):
def setUp(self):
super(NodeTest, self).setUp()
zookeeper.set_debug_level(zookeeper.LOG_LEVEL_ERROR)
self.client = ZookeeperClient("127.0.0.1:2181", 2000)
d = self.client.connect()
self.client2 = None
def create_zoo(client):
client.create("/zoo")
d.addCallback(create_zoo)
return d
def tearDown(self):
super(NodeTest, self).tearDown()
deleteTree(handle=self.client.handle)
if self.client.connected:
self.client.close()
if self.client2 and self.client2.connected:
self.client2.close()
zookeeper.set_debug_level(zookeeper.LOG_LEVEL_DEBUG)
def _make_digest_identity(self, credentials):
user, password = credentials.split(":")
digest = hashlib.new("sha1", credentials).digest()
return "%s:%s" % (user, base64.b64encode(digest))
def test_node_name_and_path(self):
"""
Each node has name and path.
"""
node = ZNode("/zoo/rabbit", self.client)
self.assertEqual(node.name, "rabbit")
self.assertEqual(node.path, "/zoo/rabbit")
def test_node_event_repr(self):
"""
Node events have a human-readable representation.
"""
node = ZNode("/zoo", self.client)
event = NodeEvent(4, None, node)
self.assertEqual(repr(event), "<NodeEvent child at '/zoo'>")
@inlineCallbacks
def test_node_exists_nonexistant(self):
"""
A node knows whether it exists or not.
"""
node = ZNode("/zoo/rabbit", self.client)
exists = yield node.exists()
self.assertFalse(exists)
@inlineCallbacks
def test_node_set_data_on_nonexistant(self):
"""
Setting data on a non existant node raises a no node exception.
"""
node = ZNode("/zoo/rabbit", self.client)
d = node.set_data("big furry ears")
self.failUnlessFailure(d, zookeeper.NoNodeException)
yield d
@inlineCallbacks
def test_node_create_set_data(self):
"""
A node can be created and have its data set.
"""
node = ZNode("/zoo/rabbit", self.client)
data = "big furry ears"
yield node.create(data)
exists = yield self.client.exists("/zoo/rabbit")
self.assertTrue(exists)
node_data = yield node.get_data()
self.assertEqual(data, node_data)
data = data*2
yield node.set_data(data)
node_data = yield node.get_data()
self.assertEqual(data, node_data)
@inlineCallbacks
def test_node_get_data(self):
"""
Data can be fetched from a node.
"""
yield self.client.create("/zoo/giraffe", "mouse")
data = yield ZNode("/zoo/giraffe", self.client).get_data()
self.assertEqual(data, "mouse")
@inlineCallbacks
def test_node_get_data_nonexistant(self):
"""
Attempting to fetch data from a nonexistant node returns
a non existant error.
"""
d = ZNode("/zoo/giraffe", self.client).get_data()
self.failUnlessFailure(d, zookeeper.NoNodeException)
yield d
@inlineCallbacks
def test_node_get_acl(self):
"""
The ACL for a node can be retrieved.
"""
yield self.client.create("/zoo/giraffe")
acl = yield ZNode("/zoo/giraffe", self.client).get_acl()
self.assertEqual(len(acl), 1)
self.assertEqual(acl[0]['scheme'], 'world')
def test_node_get_acl_nonexistant(self):
"""
The fetching the ACL for a non-existant node results in an error.
"""
node = ZNode("/zoo/giraffe", self.client)
def assert_failed(failed):
if not isinstance(failed, Failure):
self.fail("Should have failed")
self.assertTrue(
isinstance(failed.value, zookeeper.NoNodeException))
d = node.get_acl()
d.addBoth(assert_failed)
return d
@inlineCallbacks
def test_node_set_acl(self):
"""
The ACL for a node can be modified.
"""
path = yield self.client.create("/zoo/giraffe")
credentials = "zebra:moon"
acl = [{"id": self._make_digest_identity(credentials),
"scheme": "digest",
"perms":zookeeper.PERM_ALL}]
node = ZNode(path, self.client)
# little hack around slow auth issue 770 zookeeper
d = self.client.add_auth("digest", credentials)
yield node.set_acl(acl)
yield d
node_acl, stat = yield self.client.get_acl(path)
self.assertEqual(node_acl, acl)
@inlineCallbacks
def test_node_set_data_update_with_cached_exists(self):
"""
Data can be set on an existing node, updating it
in place.
"""
node = ZNode("/zoo/monkey", self.client)
yield self.client.create("/zoo/monkey", "stripes")
exists = yield node.exists()
self.assertTrue(exists)
yield node.set_data("banana")
data, stat = yield self.client.get("/zoo/monkey")
self.assertEqual(data, "banana")
@inlineCallbacks
def test_node_set_data_update_with_invalid_cached_exists(self):
"""
If a node is deleted, attempting to set data on it
raises a no node exception.
"""
node = ZNode("/zoo/monkey", self.client)
yield self.client.create("/zoo/monkey", "stripes")
exists = yield node.exists()
self.assertTrue(exists)
yield self.client.delete("/zoo/monkey")
d = node.set_data("banana")
self.failUnlessFailure(d, zookeeper.NoNodeException)
yield d
@inlineCallbacks
def test_node_set_data_update_with_exists(self):
"""
Data can be set on an existing node, updating it
in place.
"""
node = ZNode("/zoo/monkey", self.client)
yield self.client.create("/zoo/monkey", "stripes")
yield node.set_data("banana")
data, stat = yield self.client.get("/zoo/monkey")
self.assertEqual(data, "banana")
@inlineCallbacks
def test_node_exists_with_watch_nonexistant(self):
"""
The node's existance can be checked with the exist_watch api
a deferred will be returned and any node level events,
created, deleted, modified invoke the callback. You can
get these create event callbacks for non existant nodes.
"""
node = ZNode("/zoo/elephant", self.client)
exists, watch = yield node.exists_and_watch()
self.client.create("/zoo/elephant")
event = yield watch
self.assertEqual(event.type, zookeeper.CREATED_EVENT)
self.assertEqual(event.path, node.path)
@inlineCallbacks
def test_node_get_data_with_watch_on_update(self):
"""
Subscribing to a node will get node update events.
"""
yield self.client.create("/zoo/elephant")
node = ZNode("/zoo/elephant", self.client)
data, watch = yield node.get_data_and_watch()
yield self.client.set("/zoo/elephant")
event = yield watch
self.assertEqual(event.type, zookeeper.CHANGED_EVENT)
self.assertEqual(event.path, "/zoo/elephant")
@inlineCallbacks
def test_node_get_data_with_watch_on_delete(self):
"""
Subscribing to a node will get node deletion events.
"""
yield self.client.create("/zoo/elephant")
node = ZNode("/zoo/elephant", self.client)
data, watch = yield node.get_data_and_watch()
yield self.client.delete("/zoo/elephant")
event = yield watch
self.assertEqual(event.type, zookeeper.DELETED_EVENT)
self.assertEqual(event.path, "/zoo/elephant")
@inlineCallbacks
def test_node_children(self):
"""
A node's children can be introspected.
"""
node = ZNode("/zoo", self.client)
node_path_a = yield self.client.create("/zoo/lion")
node_path_b = yield self.client.create("/zoo/tiger")
children = yield node.get_children()
children.sort()
self.assertEqual(children[0].path, node_path_a)
self.assertEqual(children[1].path, node_path_b)
@inlineCallbacks
def test_node_children_by_prefix(self):
"""
A node's children can be introspected optionally with a prefix.
"""
node = ZNode("/zoo", self.client)
node_path_a = yield self.client.create("/zoo/lion")
yield self.client.create("/zoo/tiger")
children = yield node.get_children("lion")
children.sort()
self.assertEqual(children[0].path, node_path_a)
self.assertEqual(len(children), 1)
@inlineCallbacks
def test_node_get_children_with_watch_create(self):
"""
A node's children can explicitly be watched to given existance
events for node creation and destruction.
"""
node = ZNode("/zoo", self.client)
children, watch = yield node.get_children_and_watch()
yield self.client.create("/zoo/lion")
event = yield watch
self.assertEqual(event.path, "/zoo")
self.assertEqual(event.type, zookeeper.CHILD_EVENT)
self.assertEqual(event.type_name, "child")
@inlineCallbacks
def test_node_get_children_with_watch_delete(self):
"""
A node's children can explicitly be watched to given existance
events for node creation and destruction.
"""
node = ZNode("/zoo", self.client)
yield self.client.create("/zoo/lion")
children, watch = yield node.get_children_and_watch()
yield self.client.delete("/zoo/lion")
event = yield watch
self.assertEqual(event.path, "/zoo")
self.assertEqual(event.type, zookeeper.CHILD_EVENT)
@inlineCallbacks
def test_bad_version_error(self):
"""
The node captures the node version on any read operations, which
it utilizes for write operations. On a concurrent modification error
the node return a bad version error, this also clears the cached
state so subsequent modifications will be against the latest version,
unless the cache is seeded again by a read operation.
"""
node = ZNode("/zoo/lion", self.client)
self.client2 = ZookeeperClient("127.0.0.1:2181")
yield self.client2.connect()
yield self.client.create("/zoo/lion", "mouse")
yield node.get_data()
yield self.client2.set("/zoo/lion", "den2")
data = yield self.client.exists("/zoo/lion")
self.assertEqual(data['version'], 1)
d = node.set_data("zebra")
self.failUnlessFailure(d, zookeeper.BadVersionException)
yield d
# after failure the cache is deleted, and a set proceeds
yield node.set_data("zebra")
data = yield node.get_data()
self.assertEqual(data, "zebra")
|