2
"""Tests for the unregister command
4
Ensure that a users controller can be unregistered.
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"
17
from __future__ import print_function
24
from textwrap import dedent
26
from assess_user_grant_revoke import (
30
from deploy_stack import (
35
add_basic_testing_arguments,
44
log = logging.getLogger("assess_unregister")
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(
54
[user_controller_name])
57
'unregister', ('--yes', user_controller_name), include_e=False)
59
assert_controller_list(user_client, [])
61
assert_switch_raises_error(user_client)
63
# Ensure original controller still exists.
64
assert_controller_list(client, [client.env.controller.name])
67
def assert_switch_raises_error(client):
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')
77
raise JujuAssertionError('"juju switch failed to error as expected."')
80
def assert_controller_list(client, controller_list):
81
"""Assert that clients controller list only contains names provided.
83
:param client: EnvJujuClient to retrieve controllers of.
84
:param controller_list: list of strings for expected controller names.
87
json_output = client.get_juju_output(
88
'list-controllers', '--format', 'json', include_e=False)
89
output = json.loads(json_output)
92
controller_names = output['controllers'].keys()
93
except AttributeError:
94
# It's possible that there are 0 controllers for this client.
97
if controller_names != controller_list:
98
raise JujuAssertionError(
100
Unexpected controller names.
103
controller_list, controller_names)))
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)
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)
121
if __name__ == '__main__':