def tearDown(test): """The matching tearDown for setUp. The single argument is the test case passed to setUp. """ setupstack.tearDown(test) real_zk = testing_with_real_zookeeper() if real_zk: zk = zc.zk.ZooKeeper(real_zk) root = setupstack.globs(test)['/zc.zk.testing.test-root'] if zk.exists(root): zk.delete_recursive(root) zk.close()
def setUp(test, tree=None, connection_string='zookeeper.example.com:2181'): """Set up zookeeper emulation. Standard (mock) testing ----------------------- The first argument is a test case object (either doctest or unittest). You can optionally pass: tree An initial ZooKeeper tree expressed as an import string. If not passed, an initial tree will be created with examples used in the zc.zk doctests. connection_string The connection string to use for the emulation server. This defaults to 'zookeeper.example.com:2181'. Testing with a real ZooKeeper Server ------------------------------------ You can test against a real ZooKeeper server, instead of a mock by setting the environment variable TEST_ZOOKEEPER_CONNECTION to the connection string of a test server. The tests will create a top-level node with a random name that starts with 'zc.zk.testing.test-roo', and use that as the virtual root for your tests. Although this is the virtual root, of the zookeeper tree in your tests, the presense of the node may be shown in your tests. In particularm ``zookeeper.create`` returns the path created and the string returned is real, not virtual. This node is cleaned up by the ``tearDown``. A doctest can determine if it's running with a stub ZooKeeper by checking whether the value of the ZooKeeper gloval variable is None. A regular unit test can check the ZooKeeper test attribute. """ globs = setupstack.globs(test) faux_zookeeper = None real_zk = testing_with_real_zookeeper() if real_zk: test_root = '/zc.zk.testing.test-root%s' % random.randint(0, sys.maxsize) globs['/zc.zk.testing.test-root'] = test_root setup_tree(tree, real_zk, test_root, True) orig_init = zookeeper.init @side_effect( setupstack.context_manager(test, mock.patch('zookeeper.init'))) def init(addr, watch=None, session_timeout=1000): if addr != connection_string: return orig_init(addr, watch, session_timeout) else: return orig_init(real_zk+test_root, watch, session_timeout) setupstack.register( test, lambda : setattr(zc.zk.ZooKeeper, 'test_sleep', 0)) zc.zk.ZooKeeper.test_sleep = .01 time.sleep(float(os.environ.get('TEST_ZOOKEEPER_SLEEP', 0))) else: if tree: faux_zookeeper = ZooKeeper(connection_string, Node()) else: faux_zookeeper = ZooKeeper( connection_string, Node( fooservice = Node( json.dumps(dict( database = "/databases/foomain", threads = 1, favorite_color= "red", )), providers = Node() ), zookeeper = Node('', quota=Node()), ), ) for name in ZooKeeper.__dict__: if name[0] == '_': continue m = setupstack.context_manager(test, mock.patch('zookeeper.'+name)) m.side_effect = getattr(faux_zookeeper, name) if tree: zk = zc.zk.ZooKeeper(connection_string) zk.import_tree(tree) zk.close() globs['wait_until'] = wait_until # BBB globs['ZooKeeper'] = faux_zookeeper globs.setdefault('assert_', assert_)
def setUp(test, tree=None, connection_string='zookeeper.example.com:2181'): """Set up zookeeper emulation. Standard (mock) testing ----------------------- The first argument is a test case object (either doctest or unittest). You can optionally pass: tree An initial ZooKeeper tree expressed as an import string. If not passed, an initial tree will be created with examples used in the zc.zk doctests. connection_string The connection string to use for the emulation server. This defaults to 'zookeeper.example.com:2181'. Testing with a real ZooKeeper Server ------------------------------------ You can test against a real ZooKeeper server, instead of a mock by setting the environment variable TEST_ZOOKEEPER_CONNECTION to the connection string of a test server. The tests will create a top-level node with a random name that starts with 'zc.zk.testing.test-root', and use that as the virtual root for your tests. Although this is the virtual root, of the zookeeper tree in your tests, the presense of the node may be shown in your tests. In particular, ``zookeeper.create`` returns the path created and the string returned is real, not virtual. This node is cleaned up by the ``tearDown``. A doctest can determine if it's running with a stub ZooKeeper by checking whether the value of the ZooKeeper global variable is None. A regular unit test can check the ZooKeeper test attribute. """ globs = setupstack.globs(test) faux_zookeeper = None real_zk = testing_with_real_zookeeper() if real_zk: test_root = '/zc.zk.testing.test-root%s' % random.randint(0, sys.maxint) globs['/zc.zk.testing.test-root'] = test_root @side_effect( setupstack.context_manager( test, mock.patch('kazoo.client.KazooClient'))) def client(addr, *a, **k): if addr != connection_string: return SlowClient(addr, *a, **k) else: return SlowClient(real_zk+test_root, *a, **k) else: faux_zookeeper = ZooKeeper(connection_string, Node()) test_root = '/' real_zk = connection_string @side_effect(setupstack.context_manager( test, mock.patch('kazoo.client.KazooClient'))) def client(*a, **k): return Client(faux_zookeeper, *a, **k) setup_tree(tree, real_zk, test_root, True) globs['wait_until'] = wait_until # BBB globs['ZooKeeper'] = faux_zookeeper globs.setdefault('assert_', assert_)