1
"""Tests for assess_unregister module."""
11
from textwrap import dedent
13
import assess_unregister as a_unreg
18
from tests.test_jujupy import fake_juju_client
19
from utility import JujuAssertionError
22
class TestParseArgs(TestCase):
24
def test_common_args(self):
25
args = a_unreg.parse_args(
26
["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
27
self.assertEqual("an-env", args.env)
28
self.assertEqual("/bin/juju", args.juju_bin)
29
self.assertEqual("/tmp/logs", args.logs)
30
self.assertEqual("an-env-mod", args.temp_env_name)
31
self.assertEqual(False, args.debug)
34
fake_stdout = StringIO.StringIO()
35
with parse_error(self) as fake_stderr:
36
with patch("sys.stdout", fake_stdout):
37
a_unreg.parse_args(["--help"])
38
self.assertEqual("", fake_stderr.getvalue())
39
self.assertIn("Test unregister feature", fake_stdout.getvalue())
42
class TestMain(TestCase):
45
argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
46
client = Mock(spec=["is_jes_enabled"])
47
with patch("assess_unregister.configure_logging",
48
autospec=True) as mock_cl:
49
with patch("assess_unregister.BootstrapManager.booted_context",
50
autospec=True) as mock_bc:
51
with patch("deploy_stack.client_from_config",
52
return_value=client) as mock_c:
53
with patch("assess_unregister.assess_unregister",
54
autospec=True) as mock_assess:
56
mock_cl.assert_called_once_with(logging.DEBUG)
57
mock_c.assert_called_once_with('an-env', "/bin/juju", debug=False,
59
self.assertEqual(mock_bc.call_count, 1)
60
mock_assess.assert_called_once_with(client)
63
class TestAssess(TestCase):
65
def test_unregister(self):
67
with patch('jujupy.EnvJujuClient.register_user',
68
return_value=fake_user):
70
a_unreg, 'assert_controller_list',
71
autospec=True) as mock_assert_list:
73
a_unreg, 'assert_switch_raises_error', autospec=True):
74
fake_client = Mock(wraps=fake_juju_client())
75
fake_client.env.controller.name = 'testing-controller'
76
fake_client.bootstrap()
77
a_unreg.assess_unregister(fake_client)
79
mock_assert_list.mock_calls,
81
call(fake_user, ['testuser_controller']),
83
call(fake_client, ['testing-controller'])
87
class TestAssertControllerList(TestCase):
89
def test_passes_on_empty_controller_list_expecting_empty(self):
90
json_output = '{"controllers":null,"current-controller":""}\n'
91
fake_client = Mock(spec=['get_juju_output'])
92
fake_client.get_juju_output.return_value = json_output
93
a_unreg.assert_controller_list(fake_client, [])
95
def test_raises_when_no_controllers_with_expected_list(self):
96
json_output = '{"controllers":null,"current-controller":""}\n'
97
fake_client = Mock(spec=['get_juju_output'])
98
fake_client.get_juju_output.return_value = json_output
101
a_unreg.assert_controller_list,
105
def test_raises_when_list_controllers_expecting_none(self):
108
{"local-temp": {"current-model":"testing-model","user":"admin@local"}},
109
"current-controller":"local-temp"}\n"""
111
fake_client = Mock(spec=['get_juju_output'])
112
fake_client.get_juju_output.return_value = json_output
115
a_unreg.assert_controller_list,
119
def test_passes_when_expected_and_listed_match(self):
122
{"local-temp": {"current-model":"testing-model","user":"admin@local"}},
123
"current-controller":"local-temp"}\n"""
125
fake_client = Mock(spec=['get_juju_output'])
126
fake_client.get_juju_output.return_value = json_output
129
a_unreg.assert_controller_list,
133
def test_exception_message_is_correct(self):
134
json_output = '{"controllers":null,"current-controller":""}\n'
135
fake_client = Mock(spec=['get_juju_output'])
136
fake_client.get_juju_output.return_value = json_output
137
expected_message = dedent("""\
138
Unexpected controller names.
139
Expected: ['testing-model']
143
a_unreg.assert_controller_list(fake_client, ['testing-model'])
144
except JujuAssertionError as e:
145
if str(e) != expected_message:
149
class TestAssessSwitchRaisesError(TestCase):
151
def test_raises_exception_if_switch_doesnt_fail_at_all(self):
152
fake_client = Mock(spec=['get_juju_output'])
155
a_unreg.assert_switch_raises_error,
158
def test_raises_exception_when_switch_doesnt_fail_as_expected(self):
159
fake_client = Mock(spec=['get_juju_output'])
160
exception = subprocess.CalledProcessError(-1, '')
161
exception.stderr = ''
162
fake_client.get_juju_output.side_effect = exception
165
a_unreg.assert_switch_raises_error,
168
def test_passes_when_switch_errors_as_expected(self):
169
fake_client = Mock(spec=['get_juju_output'])
170
exception = subprocess.CalledProcessError(-1, '')
171
exception.stderr = 'no currently specified model'
172
fake_client.get_juju_output.side_effect = exception
174
a_unreg.assert_switch_raises_error(fake_client)