~viswesn/juju-ci-tools/juju-aws-add-credentials

« back to all changes in this revision

Viewing changes to assess_model_migration.py

Add wait for model to disappear check.

Show diffs side-by-side

added added

removed removed

Lines of Context:
124
124
    return new_log_dir
125
125
 
126
126
 
 
127
class ModelCheckFailed(Exception):
 
128
    """Exception used to signify a model status check failed or timed out."""
 
129
 
 
130
 
 
131
def _wait_for_model_check(client, model_check, timeout):
 
132
    """Wrapper to have a client wait for a model_check callable to succeed.
 
133
 
 
134
    :param client: ModelClient object to act on and pass into model_check
 
135
    :param model_check: Callable that takes a ModelClient object. When the
 
136
      callable reaches a success state it returns True. If model_check never
 
137
      returns True within `timeout`, the exception ModelCheckFailed will be
 
138
      raised.
 
139
    """
 
140
    with client.check_timeouts():
 
141
        with client.ignore_soft_deadline():
 
142
            for _ in until_timeout(timeout):
 
143
                if model_check(client):
 
144
                    return
 
145
                sleep(1)
 
146
    raise ModelCheckFailed()
 
147
 
 
148
 
 
149
def wait_until_model_disappears(client, model_name, timeout=60):
 
150
    """Waits for a while for 'model_name' model to no longer be listed.
 
151
 
 
152
    :raises JujuAssertionError: If the named model continues to be listed in
 
153
      list-models after specified timeout.
 
154
    """
 
155
    def model_check(client):
 
156
        try:
 
157
            models = client.get_controller_client().get_models()
 
158
        except CalledProcessError as e:
 
159
            # It's possible that we've tried to get status from the model as
 
160
            # it's being removed.
 
161
            if 'cannot get model details' in e.stderr:
 
162
                return True
 
163
            raise
 
164
        if model_name not in [m['name'] for m in models['models']]:
 
165
            return True
 
166
 
 
167
    try:
 
168
        _wait_for_model_check(client, model_check, timeout)
 
169
    except ModelCheckFailed:
 
170
        raise JujuAssertionError(
 
171
            'Model "{}" failed to be removed after {} seconds'.format(
 
172
                model_name, timeout))
 
173
 
 
174
 
127
175
def wait_for_model(client, model_name, timeout=60):
128
176
    """Wait for a given timeout for the client to see the model_name.
129
177
 
130
 
    Defaults to 10 seconds timeout.
131
 
 
132
 
    :raises AssertionError: If the named model does not appear in the specified
133
 
        timeout.
 
178
    :raises JujuAssertionError: If the named model does not appear in the
 
179
      specified timeout.
134
180
    """
135
 
    with client.check_timeouts():
136
 
        with client.ignore_soft_deadline():
137
 
            for _ in until_timeout(timeout):
138
 
                models = client.get_controller_client().get_models()
139
 
                if model_name in [m['name'] for m in models['models']]:
140
 
                    return
141
 
                sleep(1)
142
 
            raise JujuAssertionError(
143
 
                'Model \'{}\' failed to appear after {} seconds'.format(
144
 
                    model_name, timeout))
 
181
    def model_check(client):
 
182
        models = client.get_controller_client().get_models()
 
183
        if model_name in [m['name'] for m in models['models']]:
 
184
            return True
 
185
    try:
 
186
        _wait_for_model_check(client, model_check, timeout)
 
187
    except ModelCheckFailed:
 
188
        raise JujuAssertionError(
 
189
            'Model "{}" failed to appear after {} seconds'.format(
 
190
                model_name, timeout))
145
191
 
146
192
 
147
193
def wait_for_migrating(client, timeout=60):
298
344
    wait_for_model(
299
345
        migration_client, user_qualified_model_name)
300
346
    migration_client.wait_for_started()
 
347
    wait_until_model_disappears(source_client, user_qualified_model_name)
301
348
 
302
349
 
303
350
def deploy_simple_server_to_new_model(
355
402
        dest_client.env.clone(source_client.env.environment))
356
403
    wait_for_model(migration_target_client, source_client.env.environment)
357
404
    migration_target_client.wait_for_started()
 
405
    wait_until_model_disappears(source_client, source_client.env.environment)
358
406
    return migration_target_client
359
407
 
360
408