~james-page/charms/trusty/swift-proxy/trunk

« back to all changes in this revision

Viewing changes to unit_tests/test_actions.py

  • Committer: Ryan Beisner
  • Date: 2016-01-19 12:47:49 UTC
  • mto: This revision was merged to the branch mainline in revision 135.
  • Revision ID: ryan.beisner@canonical.com-20160119124749-5uj102427wonbfmx
Fix typo in mitaka amulet test definition

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import argparse
 
2
import tempfile
 
3
import unittest
 
4
 
 
5
import mock
 
6
import yaml
 
7
 
 
8
from mock import patch
 
9
 
 
10
with patch('lib.swift_utils.is_paused') as is_paused:
 
11
    with patch('lib.swift_utils.register_configs') as configs:
 
12
        import actions.actions
 
13
 
 
14
 
 
15
class CharmTestCase(unittest.TestCase):
 
16
 
 
17
    def setUp(self, obj, patches):
 
18
        super(CharmTestCase, self).setUp()
 
19
        self.patches = patches
 
20
        self.obj = obj
 
21
        self.patch_all()
 
22
 
 
23
    def patch(self, method):
 
24
        _m = mock.patch.object(self.obj, method)
 
25
        mocked = _m.start()
 
26
        self.addCleanup(_m.stop)
 
27
        return mocked
 
28
 
 
29
    def patch_all(self):
 
30
        for method in self.patches:
 
31
            setattr(self, method, self.patch(method))
 
32
 
 
33
 
 
34
class PauseTestCase(CharmTestCase):
 
35
 
 
36
    def setUp(self):
 
37
        super(PauseTestCase, self).setUp(
 
38
            actions.actions, ["service_pause", "HookData", "kv",
 
39
                              "assess_status"])
 
40
 
 
41
        class FakeArgs(object):
 
42
            services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
 
43
        self.args = FakeArgs()
 
44
 
 
45
    def test_pauses_services(self):
 
46
        """Pause action pauses all of the Swift services."""
 
47
        pause_calls = []
 
48
 
 
49
        def fake_service_pause(svc):
 
50
            pause_calls.append(svc)
 
51
            return True
 
52
 
 
53
        self.service_pause.side_effect = fake_service_pause
 
54
 
 
55
        actions.actions.pause(self.args)
 
56
        self.assertEqual(
 
57
            pause_calls, ['swift-proxy', 'haproxy', 'memcached', 'apache2'])
 
58
 
 
59
    def test_bails_out_early_on_error(self):
 
60
        """Pause action fails early if there are errors stopping a service."""
 
61
        pause_calls = []
 
62
 
 
63
        def maybe_kill(svc):
 
64
            if svc == "haproxy":
 
65
                return False
 
66
            else:
 
67
                pause_calls.append(svc)
 
68
                return True
 
69
 
 
70
        self.service_pause.side_effect = maybe_kill
 
71
        self.assertRaisesRegexp(
 
72
            Exception, "haproxy didn't stop cleanly.",
 
73
            actions.actions.pause, self.args)
 
74
        self.assertEqual(pause_calls, ["swift-proxy"])
 
75
 
 
76
    def test_pause_sets_value(self):
 
77
        """Pause action sets the unit-paused value to True."""
 
78
        self.HookData()().return_value = True
 
79
 
 
80
        actions.actions.pause(self.args)
 
81
        self.kv().set.assert_called_with('unit-paused', True)
 
82
 
 
83
 
 
84
class ResumeTestCase(CharmTestCase):
 
85
 
 
86
    def setUp(self):
 
87
        super(ResumeTestCase, self).setUp(
 
88
            actions.actions, ["service_resume", "HookData", "kv",
 
89
                              "assess_status"])
 
90
 
 
91
        class FakeArgs(object):
 
92
            services = ['swift-proxy', 'haproxy', 'memcached', 'apache2']
 
93
        self.args = FakeArgs()
 
94
 
 
95
    def test_resumes_services(self):
 
96
        """Resume action resumes all of the Swift services."""
 
97
        resume_calls = []
 
98
 
 
99
        def fake_service_resume(svc):
 
100
            resume_calls.append(svc)
 
101
            return True
 
102
 
 
103
        self.service_resume.side_effect = fake_service_resume
 
104
        actions.actions.resume(self.args)
 
105
        self.assertEqual(
 
106
            resume_calls, ['swift-proxy', 'haproxy', 'memcached', 'apache2'])
 
107
 
 
108
    def test_bails_out_early_on_error(self):
 
109
        """Resume action fails early if there are errors starting a service."""
 
110
        resume_calls = []
 
111
 
 
112
        def maybe_kill(svc):
 
113
            if svc == "haproxy":
 
114
                return False
 
115
            else:
 
116
                resume_calls.append(svc)
 
117
                return True
 
118
 
 
119
        self.service_resume.side_effect = maybe_kill
 
120
        self.assertRaisesRegexp(
 
121
            Exception, "haproxy didn't start cleanly.",
 
122
            actions.actions.resume, self.args)
 
123
        self.assertEqual(resume_calls, ['swift-proxy'])
 
124
 
 
125
    def test_resume_sets_value(self):
 
126
        """Resume action sets the unit-paused value to False."""
 
127
        self.HookData()().return_value = True
 
128
 
 
129
        actions.actions.resume(self.args)
 
130
        self.kv().set.assert_called_with('unit-paused', False)
 
131
 
 
132
 
 
133
class GetActionParserTestCase(unittest.TestCase):
 
134
 
 
135
    def test_definition_from_yaml(self):
 
136
        """ArgumentParser is seeded from actions.yaml."""
 
137
        actions_yaml = tempfile.NamedTemporaryFile(
 
138
            prefix="GetActionParserTestCase", suffix="yaml")
 
139
        actions_yaml.write(yaml.dump({"foo": {"description": "Foo is bar"}}))
 
140
        actions_yaml.seek(0)
 
141
        parser = actions.actions.get_action_parser(actions_yaml.name, "foo",
 
142
                                                   get_services=lambda: [])
 
143
        self.assertEqual(parser.description, 'Foo is bar')
 
144
 
 
145
 
 
146
class MainTestCase(CharmTestCase):
 
147
 
 
148
    def setUp(self):
 
149
        super(MainTestCase, self).setUp(
 
150
            actions.actions, ["_get_action_name",
 
151
                              "get_action_parser",
 
152
                              "action_fail"])
 
153
 
 
154
    def test_invokes_pause(self):
 
155
        dummy_calls = []
 
156
 
 
157
        def dummy_action(args):
 
158
            dummy_calls.append(True)
 
159
 
 
160
        self._get_action_name.side_effect = lambda: "foo"
 
161
        self.get_action_parser = lambda: argparse.ArgumentParser()
 
162
        with mock.patch.dict(actions.actions.ACTIONS, {"foo": dummy_action}):
 
163
            actions.actions.main([])
 
164
        self.assertEqual(dummy_calls, [True])
 
165
 
 
166
    def test_unknown_action(self):
 
167
        """Unknown actions aren't a traceback."""
 
168
        self._get_action_name.side_effect = lambda: "foo"
 
169
        self.get_action_parser = lambda: argparse.ArgumentParser()
 
170
        exit_string = actions.actions.main([])
 
171
        self.assertEqual("Action foo undefined", exit_string)
 
172
 
 
173
    def test_failing_action(self):
 
174
        """Actions which traceback trigger action_fail() calls."""
 
175
        dummy_calls = []
 
176
 
 
177
        self.action_fail.side_effect = dummy_calls.append
 
178
        self._get_action_name.side_effect = lambda: "foo"
 
179
 
 
180
        def dummy_action(args):
 
181
            raise ValueError("uh oh")
 
182
 
 
183
        self.get_action_parser = lambda: argparse.ArgumentParser()
 
184
        with mock.patch.dict(actions.actions.ACTIONS, {"foo": dummy_action}):
 
185
            actions.actions.main([])
 
186
        self.assertEqual(dummy_calls, ["uh oh"])