~hazmat/pyjuju/service-unit-state-upgrade-support

« back to all changes in this revision

Viewing changes to ensemble/tests/common.py

  • Committer: Jim Baker
  • Date: 2010-11-08 22:57:21 UTC
  • mto: (93.2.1 trunk)
  • mto: This revision was merged to the branch mainline in revision 94.
  • Revision ID: jim.baker@canonical.com-20101108225721-wy06vvqjnw2tmz1i
Remove use of global variables to manage ZK address

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
from contextlib import contextmanager
10
10
from glob import glob
11
11
 
12
 
import ensemble
13
12
 
14
 
__all__ = ("ManagedZooKeeper", "zookeeper_test_context")
 
13
__all__ = ("ManagedZooKeeper",
 
14
           "zookeeper_test_context",
 
15
           "get_zookeeper_test_address")
15
16
 
16
17
log = logging.getLogger("ensemble.tests.common")
17
18
 
114
115
        shutil.rmtree(self.working_path)
115
116
 
116
117
 
 
118
"""Global to manage the ZK test address - only for testing of course!"""
 
119
_zookeeper_address = "127.0.0.1:2181"
 
120
 
 
121
 
 
122
def get_test_zookeeper_address():
 
123
    """Get the current test ZK address, such as '127.0.0.1:2181'"""
 
124
    return _zookeeper_address
 
125
 
 
126
 
117
127
@contextmanager
118
128
def zookeeper_test_context(install_path, port=28181):
119
129
    """Manage context to run/stop a ZooKeeper for testing and related vars.
120
130
 
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
126
133
    """
127
 
    saved_zookeeper_port = ensemble.ZOOKEEPER_PORT
128
 
    saved_zookeeper_address = ensemble.ZOOKEEPER_LOCAL_ADDRESS
 
134
    global _zookeeper_address
 
135
    
 
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
133
141
    try:
134
142
        yield test_zookeeper
135
143
    finally:
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
 
146
        if saved_env:
 
147
            os.environ["ZOOKEEPER_ADDRESS"] = saved_env
 
148
        else:
 
149
            del os.environ["ZOOKEEPER_ADDRESS"]
 
150
 
139
151