9
9
from contextlib import contextmanager
10
10
from glob import glob
14
__all__ = ("ManagedZooKeeper", "zookeeper_test_context")
13
__all__ = ("ManagedZooKeeper",
14
"zookeeper_test_context",
15
"get_zookeeper_test_address")
16
17
log = logging.getLogger("ensemble.tests.common")
114
115
shutil.rmtree(self.working_path)
118
"""Global to manage the ZK test address - only for testing of course!"""
119
_zookeeper_address = "127.0.0.1:2181"
122
def get_test_zookeeper_address():
123
"""Get the current test ZK address, such as '127.0.0.1:2181'"""
124
return _zookeeper_address
118
128
def zookeeper_test_context(install_path, port=28181):
119
129
"""Manage context to run/stop a ZooKeeper for testing and related vars.
121
Besides managing the actual execution of the ZooKeeper instance,
122
the context manager is also used to create a dynamic scope on
123
ZOOKEEPER_LOCAL_ADDRESS and ZOOKEEPER_PORT (not actually used, but
124
exposed if there's a more complex setup required). This is needed
125
because non-test code was assuming settings like 127.0.0.1:2181.
131
@param install_path: The path to the install for ZK
132
@param port: The port to run the managed ZK instance
127
saved_zookeeper_port = ensemble.ZOOKEEPER_PORT
128
saved_zookeeper_address = ensemble.ZOOKEEPER_LOCAL_ADDRESS
134
global _zookeeper_address
136
saved_zookeeper_address = _zookeeper_address
137
saved_env = os.environ.get("ZOOKEEPER_ADDRESS")
129
138
test_zookeeper = ManagedZooKeeper(install_path, port)
130
139
test_zookeeper.run()
131
ensemble.ZOOKEEPER_PORT = test_zookeeper.port
132
ensemble.ZOOKEEPER_LOCAL_ADDRESS = test_zookeeper.address
140
os.environ["ZOOKEEPER_ADDRESS"] = _zookeeper_address = test_zookeeper.address
134
142
yield test_zookeeper
136
144
test_zookeeper.stop()
137
ensemble.ZOOKEEPER_PORT = saved_zookeeper_port
138
ensemble.ZOOKEEPER_LOCAL_ADDRESS = saved_zookeeper_address
145
_zookeeper_address = saved_zookeeper_address
147
os.environ["ZOOKEEPER_ADDRESS"] = saved_env
149
del os.environ["ZOOKEEPER_ADDRESS"]