Browse Source

[libzookeeper] Fix zookeeper tests on kerberos cluster

Erick Tryzelaar 10 years ago
parent
commit
32cade7

+ 8 - 12
desktop/libs/libzookeeper/src/libzookeeper/models.py

@@ -56,38 +56,35 @@ class ZookeeperClient(object):
 
 
 
 
   def get_children_data(self, namespace):
   def get_children_data(self, namespace):
-    children_data = []
+    self.zk.start()
     try:
     try:
-      self.zk.start()
-
       children = self.zk.get_children(namespace)
       children = self.zk.get_children(namespace)
 
 
+      children_data = []
+
       for node in children:
       for node in children:
         data, stat = self.zk.get("%s/%s" % (namespace, node))
         data, stat = self.zk.get("%s/%s" % (namespace, node))
         children_data.append(data)
         children_data.append(data)
+
+      return children_data
     finally:
     finally:
       self.zk.stop()
       self.zk.stop()
 
 
-    return children_data
-
 
 
   def path_exists(self, namespace):
   def path_exists(self, namespace):
+    self.zk.start()
     try:
     try:
-      self.zk.start()
-
       return self.zk.exists(namespace) is not None
       return self.zk.exists(namespace) is not None
     finally:
     finally:
       self.zk.stop()
       self.zk.stop()
-    return False
 
 
 
 
   def copy_path(self, namespace, filepath):
   def copy_path(self, namespace, filepath):
     if self.read_only:
     if self.read_only:
       raise ReadOnlyClientException('Cannot execute copy_path when read_only is set to True.')
       raise ReadOnlyClientException('Cannot execute copy_path when read_only is set to True.')
 
 
+    self.zk.start()
     try:
     try:
-      self.zk.start()
-
       self.zk.ensure_path(namespace)
       self.zk.ensure_path(namespace)
       for dir, subdirs, files in os.walk(filepath):
       for dir, subdirs, files in os.walk(filepath):
         path = dir.replace(filepath, '').strip('/')
         path = dir.replace(filepath, '').strip('/')
@@ -107,9 +104,8 @@ class ZookeeperClient(object):
     if self.read_only:
     if self.read_only:
       raise ReadOnlyClientException('Cannot execute delete_path when read_only is set to True.')
       raise ReadOnlyClientException('Cannot execute delete_path when read_only is set to True.')
 
 
+    self.zk.start()
     try:
     try:
-      self.zk.start()
-
       self.zk.delete(namespace, recursive=True)
       self.zk.delete(namespace, recursive=True)
     finally:
     finally:
       self.zk.stop()
       self.zk.stop()

+ 45 - 26
desktop/libs/libzookeeper/src/libzookeeper/tests.py

@@ -90,50 +90,69 @@ class TestWithZooKeeper:
     # Don't want directories laying around
     # Don't want directories laying around
     shutil.rmtree(cls.local_directory)
     shutil.rmtree(cls.local_directory)
 
 
+  def teardown(self):
+    client = ZookeeperClient(hosts=zkensemble(), read_only=False)
+    # Delete the root_node first just in case it wasn't cleaned up in previous run
+    client.zk.start()
+    try:
+      if client.zk.exists(self.namespace):
+        client.zk.delete(self.namespace, recursive=True)
+    finally:
+      client.zk.stop()
+
   def test_get_children_data(self):
   def test_get_children_data(self):
-    client = ZookeeperClient(hosts=zkensemble())
-    db = client.get_children_data(namespace='')
+    root_node = '%s/%s' % (TestWithZooKeeper.namespace, 'test_path_exists')
+    client = ZookeeperClient(hosts=zkensemble(), read_only=False)
+
+    client.zk.start()
+    try:
+      client.zk.create(root_node, value='test_path_exists', makepath=True)
+    finally:
+      client.zk.stop()
+
+    db = client.get_children_data(namespace=TestWithZooKeeper.namespace)
     assert_true(len(db) > 0)
     assert_true(len(db) > 0)
 
 
   def test_path_exists(self):
   def test_path_exists(self):
-    try:
-      root_node = '%s/%s' % (TestWithZooKeeper.namespace, 'test_path_exists')
-      client = ZookeeperClient(hosts=zkensemble(), read_only=False)
+    root_node = '%s/%s' % (TestWithZooKeeper.namespace, 'test_path_exists')
+    client = ZookeeperClient(hosts=zkensemble(), read_only=False)
 
 
-      # Delete the root_node first just in case it wasn't cleaned up in previous run
-      client.zk.start()
-      client.zk.create(root_node, value='test_path_exists')
-      client.zk.stop()
+    client.zk.start()
+    try:
+      client.zk.create(root_node, value='test_path_exists', makepath=True)
 
 
-      assert_true(client.path_exists(namespace=root_node))
-      assert_false(client.path_exists(namespace='bogus_path'))
+      try:
+        assert_true(client.path_exists(namespace=root_node))
+        assert_false(client.path_exists(namespace='bogus_path'))
+      finally:
+        client.delete_path(root_node)
     finally:
     finally:
-      client.delete_path(root_node)
+      client.zk.stop()
 
 
   def test_copy_and_delete_path(self):
   def test_copy_and_delete_path(self):
     root_node = '%s/%s' % (TestWithZooKeeper.namespace, 'test_copy_and_delete_path')
     root_node = '%s/%s' % (TestWithZooKeeper.namespace, 'test_copy_and_delete_path')
     client = ZookeeperClient(hosts=zkensemble(), read_only=False)
     client = ZookeeperClient(hosts=zkensemble(), read_only=False)
 
 
-    # Delete the root_node first just in case it wasn't cleaned up in previous run
-    client.zk.start()
-    client.zk.delete(root_node, recursive=True)
-    client.zk.stop()
-
     # Test copy_path
     # Test copy_path
     client.copy_path(root_node, TestWithZooKeeper.local_directory)
     client.copy_path(root_node, TestWithZooKeeper.local_directory)
 
 
     client.zk.start()
     client.zk.start()
-    assert_true(client.zk.exists('%s' % root_node))
-    assert_true(client.zk.exists('%s/%s' % (root_node, TestWithZooKeeper.subdir_name)))
-    assert_true(client.zk.exists('%s/%s/%s' % (root_node, TestWithZooKeeper.subdir_name, TestWithZooKeeper.filename)))
-    contents, stats = client.zk.get('%s/%s/%s' % (root_node, TestWithZooKeeper.subdir_name, TestWithZooKeeper.filename))
-    assert_equal(contents, TestWithZooKeeper.file_contents)
-    client.zk.stop()
+    try:
+      assert_true(client.zk.exists('%s' % root_node))
+      assert_true(client.zk.exists('%s/%s' % (root_node, TestWithZooKeeper.subdir_name)))
+      assert_true(client.zk.exists('%s/%s/%s' % (root_node, TestWithZooKeeper.subdir_name, TestWithZooKeeper.filename)))
+      contents, stats = client.zk.get('%s/%s/%s' % (root_node, TestWithZooKeeper.subdir_name, TestWithZooKeeper.filename))
+      assert_equal(contents, TestWithZooKeeper.file_contents)
+    finally:
+      client.zk.stop()
 
 
     # Test delete_path
     # Test delete_path
     client.delete_path(root_node)
     client.delete_path(root_node)
 
 
     client.zk.start()
     client.zk.start()
-    assert_equal(client.zk.exists('%s' % root_node), None)
-    assert_equal(client.zk.exists('%s/%s' % (root_node, TestWithZooKeeper.subdir_name)), None)
-    assert_equal(client.zk.exists('%s/%s/%s' % (root_node, TestWithZooKeeper.subdir_name, TestWithZooKeeper.filename)), None)
+    try:
+      assert_equal(client.zk.exists('%s' % root_node), None)
+      assert_equal(client.zk.exists('%s/%s' % (root_node, TestWithZooKeeper.subdir_name)), None)
+      assert_equal(client.zk.exists('%s/%s/%s' % (root_node, TestWithZooKeeper.subdir_name, TestWithZooKeeper.filename)), None)
+    finally:
+      client.zk.stop()