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

« back to all changes in this revision

Viewing changes to tests/test_assess_unregister.py

  • Committer: Christopher Lee
  • Date: 2016-06-21 05:59:02 UTC
  • mto: (1465.1.15 trunk)
  • mto: This revision was merged to the branch mainline in revision 1470.
  • Revision ID: chris.lee@canonical.com-20160621055902-07fpazw7t8wgadif
juju unregister assess test with unit tests

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""Tests for assess_unregister module."""
 
2
 
 
3
from contextlib import nested
 
4
import logging
 
5
from mock import (
 
6
    Mock,
 
7
    call,
 
8
    patch,
 
9
)
 
10
import StringIO
 
11
import subprocess
 
12
from textwrap import dedent
 
13
 
 
14
import assess_unregister as a_unreg
 
15
from tests import (
 
16
    parse_error,
 
17
    TestCase,
 
18
)
 
19
from tests.test_jujupy import fake_juju_client
 
20
from utility import JujuAssertionError
 
21
 
 
22
 
 
23
class TestParseArgs(TestCase):
 
24
 
 
25
    def test_common_args(self):
 
26
        args = a_unreg.parse_args(
 
27
            ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod"])
 
28
        self.assertEqual("an-env", args.env)
 
29
        self.assertEqual("/bin/juju", args.juju_bin)
 
30
        self.assertEqual("/tmp/logs", args.logs)
 
31
        self.assertEqual("an-env-mod", args.temp_env_name)
 
32
        self.assertEqual(False, args.debug)
 
33
 
 
34
    def test_help(self):
 
35
        fake_stdout = StringIO.StringIO()
 
36
        with parse_error(self) as fake_stderr:
 
37
            with patch("sys.stdout", fake_stdout):
 
38
                a_unreg.parse_args(["--help"])
 
39
        self.assertEqual("", fake_stderr.getvalue())
 
40
        self.assertIn("Test unregister feature", fake_stdout.getvalue())
 
41
 
 
42
 
 
43
class TestMain(TestCase):
 
44
 
 
45
    def test_main(self):
 
46
        argv = ["an-env", "/bin/juju", "/tmp/logs", "an-env-mod", "--verbose"]
 
47
        env = object()
 
48
        client = Mock(spec=["is_jes_enabled"])
 
49
        with nested(
 
50
            patch("assess_unregister.configure_logging", autospec=True),
 
51
            patch("assess_unregister.BootstrapManager.booted_context",
 
52
                  autospec=True),
 
53
            patch("jujupy.SimpleEnvironment.from_config", return_value=env),
 
54
            patch("jujupy.EnvJujuClient.by_version", return_value=client),
 
55
            patch("assess_unregister.assess_unregister", autospec=True),
 
56
        ) as (mock_cl, mock_bc, mock_e, mock_c, mock_assess):
 
57
            a_unreg.main(argv)
 
58
        mock_cl.assert_called_once_with(logging.DEBUG)
 
59
        mock_e.assert_called_once_with("an-env")
 
60
        mock_c.assert_called_once_with(env, "/bin/juju", debug=False)
 
61
        self.assertEqual(mock_bc.call_count, 1)
 
62
        mock_assess.assert_called_once_with(client)
 
63
 
 
64
 
 
65
class TestAssess(TestCase):
 
66
 
 
67
    def test_unregister(self):
 
68
        fake_user = Mock()
 
69
        with nested(
 
70
            patch.object(a_unreg, 'register_user', return_value=fake_user),
 
71
            patch.object(a_unreg, 'assert_controller_list', autospec=True),
 
72
            patch.object(a_unreg, 'assert_switch_raises_error', autospec=True)
 
73
        ) as (mock_reg_user, mock_assert_list, mock_assert_switch):
 
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)
 
78
        self.assertEqual(
 
79
            mock_assert_list.mock_calls,
 
80
            [
 
81
                call(fake_user, ['testuser_controller']),
 
82
                call(fake_user, []),
 
83
                call(fake_client, ['testing-controller'])
 
84
            ])
 
85
 
 
86
 
 
87
class TestAssertControllerList(TestCase):
 
88
 
 
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, [])
 
94
 
 
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
 
99
        self.assertRaises(
 
100
            JujuAssertionError,
 
101
            a_unreg.assert_controller_list,
 
102
            fake_client,
 
103
            ['some_controller'])
 
104
 
 
105
    def test_raises_when_list_controllers_expecting_none(self):
 
106
        json_output = """\
 
107
        {"controllers":
 
108
        {"local-temp": {"current-model":"testing-model","user":"admin@local"}},
 
109
        "current-controller":"local-temp"}\n"""
 
110
 
 
111
        fake_client = Mock(spec=['get_juju_output'])
 
112
        fake_client.get_juju_output.return_value = json_output
 
113
        self.assertRaises(
 
114
            JujuAssertionError,
 
115
            a_unreg.assert_controller_list,
 
116
            fake_client,
 
117
            [])
 
118
 
 
119
    def test_passes_when_expected_and_listed_match(self):
 
120
        json_output = """\
 
121
        {"controllers":
 
122
        {"local-temp": {"current-model":"testing-model","user":"admin@local"}},
 
123
        "current-controller":"local-temp"}\n"""
 
124
 
 
125
        fake_client = Mock(spec=['get_juju_output'])
 
126
        fake_client.get_juju_output.return_value = json_output
 
127
        self.assertRaises(
 
128
            JujuAssertionError,
 
129
            a_unreg.assert_controller_list,
 
130
            fake_client,
 
131
            ['testing-model'])
 
132
 
 
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']
 
140
        Got: []""")
 
141
 
 
142
        try:
 
143
            a_unreg.assert_controller_list(fake_client, ['testing-model'])
 
144
        except JujuAssertionError as e:
 
145
            if str(e) != expected_message:
 
146
                raise
 
147
 
 
148
 
 
149
class TestAssessSwitchRaisesError(TestCase):
 
150
 
 
151
    def test_raises_exception_if_switch_doesnt_fail_at_all(self):
 
152
        fake_client = Mock(spec=['get_juju_output'])
 
153
        self.assertRaises(
 
154
            JujuAssertionError,
 
155
            a_unreg.assert_switch_raises_error,
 
156
            fake_client)
 
157
 
 
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
 
163
        self.assertRaises(
 
164
            JujuAssertionError,
 
165
            a_unreg.assert_switch_raises_error,
 
166
            fake_client)
 
167
 
 
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
 
173
 
 
174
        a_unreg.assert_switch_raises_error(fake_client)