~abentley/juju-ci-tools/client-from-config-4

« back to all changes in this revision

Viewing changes to assess_unregister.py

  • Committer: Aaron Bentley
  • Date: 2014-02-24 17:18:29 UTC
  • mto: This revision was merged to the branch mainline in revision 252.
  • Revision ID: aaron.bentley@canonical.com-20140224171829-sz644yhoygu7m9dm
Use tags to identify and shut down instances.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
"""Tests for the unregister command
3
 
 
4
 
Ensure that a users controller can be unregistered.
5
 
 
6
 
Test plan:
7
 
 - add user
8
 
 - run juju register command from previous step
9
 
 - verify controller shows up in juju list-controllers
10
 
 - 'juju unregister' the controller
11
 
 - verify the controller is no longer listed in juju list-controllers
12
 
 - verify that juju switch does not show the unregistered controller as
13
 
   the current controller; will show "ERROR no currently specified model"
14
 
 
15
 
"""
16
 
 
17
 
from __future__ import print_function
18
 
 
19
 
import argparse
20
 
import json
21
 
import logging
22
 
import subprocess
23
 
import sys
24
 
from textwrap import dedent
25
 
 
26
 
from assess_user_grant_revoke import (
27
 
    User,
28
 
    register_user,
29
 
)
30
 
from deploy_stack import (
31
 
    BootstrapManager,
32
 
)
33
 
from utility import (
34
 
    JujuAssertionError,
35
 
    add_basic_testing_arguments,
36
 
    configure_logging,
37
 
    temp_dir,
38
 
)
39
 
 
40
 
 
41
 
__metaclass__ = type
42
 
 
43
 
 
44
 
log = logging.getLogger("assess_unregister")
45
 
 
46
 
 
47
 
def assess_unregister(client):
48
 
    user = User('testuser', 'read', [])
49
 
    user_controller_name = '{}_controller'.format(user.name)
50
 
    with temp_dir() as fake_home:
51
 
        user_client = register_user(user, client, fake_home)
52
 
        assert_controller_list(
53
 
            user_client,
54
 
            [user_controller_name])
55
 
 
56
 
        user_client.juju(
57
 
            'unregister', ('--yes', user_controller_name), include_e=False)
58
 
 
59
 
        assert_controller_list(user_client, [])
60
 
 
61
 
        assert_switch_raises_error(user_client)
62
 
 
63
 
    # Ensure original controller still exists.
64
 
    assert_controller_list(client, [client.env.controller.name])
65
 
 
66
 
 
67
 
def assert_switch_raises_error(client):
68
 
    try:
69
 
        client.get_juju_output('switch', include_e=False)
70
 
    except subprocess.CalledProcessError as e:
71
 
        if 'no currently specified model' not in e.stderr:
72
 
            raise JujuAssertionError(
73
 
                '"juju switch" command failed for an unexpected reason: '
74
 
                '{}'.format(e.stderr))
75
 
        log.info('"juju switch" failed as expected')
76
 
        return
77
 
    raise JujuAssertionError('"juju switch failed to error as expected."')
78
 
 
79
 
 
80
 
def assert_controller_list(client, controller_list):
81
 
    """Assert that clients controller list only contains names provided.
82
 
 
83
 
    :param client: EnvJujuClient to retrieve controllers of.
84
 
    :param controller_list: list of strings for expected controller names.
85
 
 
86
 
    """
87
 
    json_output = client.get_juju_output(
88
 
        'list-controllers', '--format', 'json', include_e=False)
89
 
    output = json.loads(json_output)
90
 
 
91
 
    try:
92
 
        controller_names = output['controllers'].keys()
93
 
    except AttributeError:
94
 
        # It's possible that there are 0 controllers for this client.
95
 
        controller_names = []
96
 
 
97
 
    if controller_names != controller_list:
98
 
        raise JujuAssertionError(
99
 
            dedent("""\
100
 
            Unexpected controller names.
101
 
            Expected: {}
102
 
            Got: {}""".format(
103
 
                controller_list, controller_names)))
104
 
 
105
 
 
106
 
def parse_args(argv):
107
 
    parser = argparse.ArgumentParser(description="Test unregister feature.")
108
 
    add_basic_testing_arguments(parser)
109
 
    return parser.parse_args(argv)
110
 
 
111
 
 
112
 
def main(argv=None):
113
 
    args = parse_args(argv)
114
 
    configure_logging(args.verbose)
115
 
    bs_manager = BootstrapManager.from_args(args)
116
 
    with bs_manager.booted_context(args.upload_tools):
117
 
        assess_unregister(bs_manager.client)
118
 
    return 0
119
 
 
120
 
 
121
 
if __name__ == '__main__':
122
 
    sys.exit(main())