~charmers/charms/precise/juju-gui/trunk

« back to all changes in this revision

Viewing changes to tests/helpers.py

  • Committer: Francesco Banconi
  • Date: 2013-10-09 10:48:53 UTC
  • mfrom: (60.13.41 trunk)
  • Revision ID: francesco.banconi@canonical.com-20131009104853-6b8oxx4k1ycmv2yk
Tags: 0.11.0
MergedĀ juju-guiĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import subprocess
25
25
import time
26
26
 
 
27
import websocket
 
28
import yaml
 
29
 
27
30
 
28
31
class ProcessError(subprocess.CalledProcessError):
29
32
    """Error running a shell command."""
99
102
    return decorator
100
103
 
101
104
 
 
105
def get_admin_secret():
 
106
    """Return the admin secret for the current environment.
 
107
 
 
108
    The environment name must be present in the JUJU_ENV env variable.
 
109
    Raise a ValueError if the environment is not found in the context or the
 
110
    given environment name is not included in ~/.juju/environments.yaml.
 
111
    """
 
112
    # Retrieve the current environment.
 
113
    env = juju_env()
 
114
    if env is None:
 
115
        raise ValueError('Unable to retrieve the current environment name.')
 
116
    # Load and parse the Juju environments file.
 
117
    path = os.path.expanduser('~/.juju/environments.yaml')
 
118
    try:
 
119
        environments_file = open(path)
 
120
    except IOError as err:
 
121
        raise ValueError('Unable to open environments file: {}'.format(err))
 
122
    try:
 
123
        environments = yaml.safe_load(environments_file)
 
124
    except Exception as err:
 
125
        raise ValueError('Unable to parse environments file: {}'.format(err))
 
126
    # Retrieve the admin secret for the current environment.
 
127
    try:
 
128
        environment = environments.get('environments', {}).get(env)
 
129
    except AttributeError as err:
 
130
        raise ValueError('Invalid YAML contents: {}'.format(environments))
 
131
    if environment is None:
 
132
        raise ValueError('Environment {} not found'.format(env))
 
133
    admin_secret = environment.get('admin-secret')
 
134
    if admin_secret is None:
 
135
        raise ValueError('Admin secret not found')
 
136
    return admin_secret
 
137
 
 
138
 
102
139
@retry(ProcessError)
103
140
def juju(command, *args):
104
141
    """Call the juju command, passing the environment parameters if required.
160
197
    return Version._make(map(to_int, match.groups()))
161
198
 
162
199
 
 
200
def stop_services(hostname, services):
 
201
    """Stop the given upstart services running on hostname."""
 
202
    target = 'ubuntu@{}'.format(hostname)
 
203
    for service in services:
 
204
        ssh(target, 'sudo', 'service', service, 'stop')
 
205
 
 
206
 
163
207
def wait_for_unit(sevice):
164
208
    """Wait for the first unit of the given service to be started.
165
209
 
183
227
                'the service unit is in an error state: {}'.format(state))
184
228
        if state == 'started':
185
229
            return unit
 
230
 
 
231
 
 
232
class WebSocketClient(object):
 
233
    """A simple blocking WebSocket client used in functional tests."""
 
234
 
 
235
    def __init__(self, url):
 
236
        self._url = url
 
237
        self._conn = None
 
238
 
 
239
    def connect(self):
 
240
        """Connect to the WebSocket server."""
 
241
        self._conn = websocket.create_connection(self._url)
 
242
 
 
243
    def send(self, request):
 
244
        """Send the given WebSocket request.
 
245
 
 
246
        Return the decoded WebSocket response returned by the server.
 
247
        Block until the server response is received.
 
248
        """
 
249
        self._conn.send(json.dumps(request))
 
250
        response = self._conn.recv()
 
251
        return json.loads(response)
 
252
 
 
253
    def close(self):
 
254
        """Close the WebSocket connection."""
 
255
        if self._conn is not None:
 
256
            self._conn.close()
 
257
            self._conn = None