11
class CharmTestCase(unittest.TestCase):
13
def setUp(self, obj, patches):
14
super(CharmTestCase, self).setUp()
15
self.patches = patches
19
def patch(self, method):
20
_m = mock.patch.object(self.obj, method)
22
self.addCleanup(_m.stop)
26
for method in self.patches:
27
setattr(self, method, self.patch(method))
30
class PauseTestCase(CharmTestCase):
33
super(PauseTestCase, self).setUp(
34
actions.actions, ["service_pause", "status_set"])
36
class FakeArgs(object):
37
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
38
self.args = FakeArgs()
40
def test_pauses_services(self):
41
"""Pause action pauses all of the Swift services."""
44
def fake_service_pause(svc):
45
pause_calls.append(svc)
48
self.service_pause.side_effect = fake_service_pause
50
actions.actions.pause(self.args)
52
pause_calls, ['swift-proxy', 'haproxy', 'memcached', 'apache2'])
54
def test_bails_out_early_on_error(self):
55
"""Pause action fails early if there are errors stopping a service."""
62
pause_calls.append(svc)
65
self.service_pause.side_effect = maybe_kill
66
self.assertRaisesRegexp(
67
Exception, "haproxy didn't stop cleanly.",
68
actions.actions.pause, self.args)
69
self.assertEqual(pause_calls, ["swift-proxy"])
71
def test_status_mode(self):
72
"""Pause action sets the status to maintenance."""
74
self.status_set.side_effect = lambda state, msg: status_calls.append(
77
actions.actions.pause(self.args)
78
self.assertEqual(status_calls, ["maintenance"])
80
def test_status_message(self):
81
"""Pause action sets a status message reflecting that it's paused."""
83
self.status_set.side_effect = lambda state, msg: status_calls.append(
86
actions.actions.pause(self.args)
88
status_calls, ["Paused. "
89
"Use 'resume' action to resume normal service."])
92
class ResumeTestCase(CharmTestCase):
95
super(ResumeTestCase, self).setUp(
96
actions.actions, ["service_resume", "status_set"])
98
class FakeArgs(object):
99
services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
100
self.args = FakeArgs()
102
def test_resumes_services(self):
103
"""Resume action resumes all of the Swift services."""
106
def fake_service_resume(svc):
107
resume_calls.append(svc)
110
self.service_resume.side_effect = fake_service_resume
111
actions.actions.resume(self.args)
113
resume_calls, ['swift-proxy', 'haproxy', 'memcached', 'apache2'])
115
def test_bails_out_early_on_error(self):
116
"""Resume action fails early if there are errors starting a service."""
123
resume_calls.append(svc)
126
self.service_resume.side_effect = maybe_kill
127
self.assertRaisesRegexp(
128
Exception, "haproxy didn't start cleanly.",
129
actions.actions.resume, self.args)
130
self.assertEqual(resume_calls, ['swift-proxy'])
132
def test_status_mode(self):
133
"""Resume action sets the status to maintenance."""
135
self.status_set.side_effect = lambda state, msg: status_calls.append(
138
actions.actions.resume(self.args)
139
self.assertEqual(status_calls, ["active"])
141
def test_status_message(self):
142
"""Resume action sets an empty status message."""
144
self.status_set.side_effect = lambda state, msg: status_calls.append(
147
actions.actions.resume(self.args)
148
self.assertEqual(status_calls, [""])
151
class GetActionParserTestCase(unittest.TestCase):
153
def test_definition_from_yaml(self):
154
"""ArgumentParser is seeded from actions.yaml."""
155
actions_yaml = tempfile.NamedTemporaryFile(
156
prefix="GetActionParserTestCase", suffix="yaml")
157
actions_yaml.write(yaml.dump({"foo": {"description": "Foo is bar"}}))
159
parser = actions.actions.get_action_parser(actions_yaml.name, "foo",
160
get_services=lambda: [])
161
self.assertEqual(parser.description, 'Foo is bar')
164
class MainTestCase(CharmTestCase):
167
super(MainTestCase, self).setUp(
168
actions.actions, ["_get_action_name",
172
def test_invokes_pause(self):
175
def dummy_action(args):
176
dummy_calls.append(True)
178
self._get_action_name.side_effect = lambda: "foo"
179
self.get_action_parser = lambda: argparse.ArgumentParser()
180
with mock.patch.dict(actions.actions.ACTIONS, {"foo": dummy_action}):
181
actions.actions.main([])
182
self.assertEqual(dummy_calls, [True])
184
def test_unknown_action(self):
185
"""Unknown actions aren't a traceback."""
186
self._get_action_name.side_effect = lambda: "foo"
187
self.get_action_parser = lambda: argparse.ArgumentParser()
188
exit_string = actions.actions.main([])
189
self.assertEqual("Action foo undefined", exit_string)
191
def test_failing_action(self):
192
"""Actions which traceback trigger action_fail() calls."""
195
self.action_fail.side_effect = dummy_calls.append
196
self._get_action_name.side_effect = lambda: "foo"
198
def dummy_action(args):
199
raise ValueError("uh oh")
201
self.get_action_parser = lambda: argparse.ArgumentParser()
202
with mock.patch.dict(actions.actions.ACTIONS, {"foo": dummy_action}):
203
actions.actions.main([])
204
self.assertEqual(dummy_calls, ["uh oh"])