~ubuntu-branches/ubuntu/utopic/kazoo/utopic-proposed

« back to all changes in this revision

Viewing changes to kazoo/testing/common.py

  • Committer: Package Import Robot
  • Author(s): Neil Williams
  • Date: 2013-08-26 06:26:20 UTC
  • mfrom: (1.1.1)
  • Revision ID: package-import@ubuntu.com-20130826062620-dv79eayvq78028jb
Tags: 1.2.1-1
* New upstream release.
* Fix sphinx documentation build on clean systems (added
  python-gevent to Build-Depends and patched config).
* Bumped standards version to 3.9.4. No changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
#
2
2
#  Copyright (C) 2010-2011, 2011 Canonical Ltd. All Rights Reserved
3
3
#
4
 
#  This file is part of txzookeeper.
 
4
#  This file was originally taken from txzookeeper and modified later.
5
5
#
6
6
#  Authors:
7
 
#   Kapil Thangavelu
 
7
#   Kapil Thangavelu and the Kazoo team
8
8
#
9
9
#  txzookeeper is free software: you can redistribute it and/or modify
10
10
#  it under the terms of the GNU Lesser General Public License as published by
18
18
#
19
19
#  You should have received a copy of the GNU Lesser General Public License
20
20
#  along with txzookeeper.  If not, see <http://www.gnu.org/licenses/>.
21
 
#
22
 
 
23
 
 
 
21
 
 
22
 
 
23
import code
24
24
import os
25
25
import os.path
26
26
import shutil
 
27
import signal
27
28
import subprocess
28
29
import tempfile
 
30
import traceback
29
31
 
30
32
from itertools import chain
31
33
from collections import namedtuple
32
34
from glob import glob
33
35
 
34
36
 
 
37
def debug(sig, frame):
 
38
    """Interrupt running process, and provide a python prompt for
 
39
    interactive debugging."""
 
40
    d = {'_frame': frame}         # Allow access to frame object.
 
41
    d.update(frame.f_globals)  # Unless shadowed by global
 
42
    d.update(frame.f_locals)
 
43
 
 
44
    i = code.InteractiveConsole(d)
 
45
    message = "Signal recieved : entering python shell.\nTraceback:\n"
 
46
    message += ''.join(traceback.format_stack(frame))
 
47
    i.interact(message)
 
48
 
 
49
 
 
50
def listen():
 
51
    if os.name != 'nt':  # SIGUSR1 is not supported on Windows
 
52
        signal.signal(signal.SIGUSR1, debug)  # Register handler
 
53
listen()
 
54
 
 
55
 
 
56
def to_java_compatible_path(path):
 
57
    if os.name == 'nt':
 
58
        path = path.replace('\\', '/')
 
59
    return path
 
60
 
35
61
ServerInfo = namedtuple(
36
62
    "ServerInfo", "server_id client_port election_port leader_port")
37
63
 
44
70
    future, we may want to do that, especially when run in a
45
71
    Hudson/Buildbot context, to ensure more test robustness."""
46
72
 
47
 
    def __init__(self, software_path, server_info, peers=()):
 
73
    def __init__(self, software_path, server_info, peers=(), classpath=None):
48
74
        """Define the ZooKeeper test instance.
49
75
 
50
76
        @param install_path: The path to the install for ZK
51
77
        @param port: The port to run the managed ZK instance
52
78
        """
53
79
        self.install_path = software_path
 
80
        self._classpath = classpath
54
81
        self.server_info = server_info
55
82
        self.host = "127.0.0.1"
56
83
        self.peers = peers
83
110
dataDir=%s
84
111
clientPort=%s
85
112
maxClientCnxns=0
86
 
""" % (data_path, self.server_info.client_port))
 
113
""" % (to_java_compatible_path(data_path), self.server_info.client_port))
87
114
 
88
115
        # setup a replicated setup if peers are specified
89
116
        if self.peers:
111
138
log4j.appender.ROLLINGFILE.layout.ConversionPattern=%d{ISO8601} [myid:%X{myid}] - %-5p [%t:%C{1}@%L] - %m%n
112
139
log4j.appender.ROLLINGFILE=org.apache.log4j.RollingFileAppender
113
140
log4j.appender.ROLLINGFILE.Threshold=DEBUG
114
 
log4j.appender.ROLLINGFILE.File=""" + (
 
141
log4j.appender.ROLLINGFILE.File=""" + to_java_compatible_path(
115
142
                self.working_path + os.sep + "zookeeper.log\n"))
116
143
 
117
144
        self.process = subprocess.Popen(
123
150
                  "-Dlog4j.configuration=file:%s" % log4j_path,
124
151
                  # "-Dlog4j.debug",
125
152
                  "org.apache.zookeeper.server.quorum.QuorumPeerMain",
126
 
                  config_path],
127
 
            )
 
153
                  config_path])
128
154
        self._running = True
129
155
 
130
156
    @property
131
157
    def classpath(self):
132
158
        """Get the classpath necessary to run ZooKeeper."""
133
159
 
 
160
        if self._classpath:
 
161
            return self._classpath
 
162
 
134
163
        # Two possibilities, as seen in zkEnv.sh:
135
164
        # Check for a release - top-level zookeeper-*.jar?
136
165
        jars = glob((os.path.join(
140
169
            jars.extend(glob(os.path.join(
141
170
                self.install_path,
142
171
                "lib/*.jar")))
 
172
            # support for different file locations on Debian/Ubuntu
 
173
            jars.extend(glob(os.path.join(
 
174
                self.install_path,
 
175
                "log4j-*.jar")))
 
176
            jars.extend(glob(os.path.join(
 
177
                self.install_path,
 
178
                "slf4j-*.jar")))
143
179
        else:
144
180
            # Development build (plain `ant`)
145
181
            jars = glob((os.path.join(
147
183
            jars.extend(glob(os.path.join(
148
184
                self.install_path,
149
185
                "build/lib/*.jar")))
150
 
        return ":".join(jars)
 
186
 
 
187
        return os.pathsep.join(jars)
151
188
 
152
189
    @property
153
190
    def address(self):
189
226
 
190
227
class ZookeeperCluster(object):
191
228
 
192
 
    def __init__(self, install_path, size=3, port_offset=20000):
 
229
    def __init__(self, install_path=None, classpath=None, size=3, port_offset=20000):
193
230
        self._install_path = install_path
 
231
        self._classpath = classpath
194
232
        self._servers = []
195
233
 
196
234
        # Calculate ports and peer group
198
236
        peers = []
199
237
 
200
238
        for i in range(size):
201
 
            port += i * 10
202
239
            info = ServerInfo(i + 1, port, port + 1, port + 2)
203
240
            peers.append(info)
 
241
            port += 10
204
242
 
205
243
        # Instantiate Managed ZK Servers
206
244
        for i in range(size):
208
246
            server_info = server_peers.pop(i)
209
247
            self._servers.append(
210
248
                ManagedZooKeeper(
211
 
                    self._install_path, server_info, server_peers))
 
249
                    self._install_path, server_info, server_peers, classpath=self._classpath))
212
250
 
213
251
    def __getitem__(self, k):
214
252
        return self._servers[k]