~james-w/charms/precise/postgresql/metrics

« back to all changes in this revision

Viewing changes to hooks/test_hooks.py

  • Committer: Stuart Bishop
  • Date: 2014-05-29 13:08:35 UTC
  • mfrom: (75.2.27 postgresql-storage)
  • Revision ID: stuart@stuartbishop.net-20140529130835-afvqvwn11kp5aw0v
[chad.smith] Use storage broker and storage subordinate charm for non-ephemeral disk, per https://code.launchpad.net/~chad.smith/charms/precise/postgresql/postgresql-using-storage-subordinate/+merge/206078

Show diffs side-by-side

added added

removed removed

Lines of Context:
43
43
    certain data is set.
44
44
    """
45
45
 
46
 
    _relation_data = {}
 
46
    _incoming_relation_data = ()
 
47
    _outgoing_relation_data = ()
47
48
    _relation_ids = {}
48
49
    _relation_list = ("postgres/0",)
 
50
    _log = ()
 
51
 
 
52
    _log_DEBUG = ()
 
53
    _log_INFO = ()
 
54
    _log_WARNING = ()
 
55
    _log_ERROR = ()
 
56
    _log_CRITICAL = ()
 
57
 
 
58
    DEBUG = "DEBUG"
 
59
    INFO = "INFO"
 
60
    WARNING = "WARNING"
 
61
    ERROR = "ERROR"
 
62
    CRITICAL = "CRITICAL"
49
63
 
50
64
    def __init__(self):
51
65
        self._config = {
95
109
            "wal_buffers": "-1",
96
110
            "checkpoint_segments": 3,
97
111
            "random_page_cost": 4.0,
98
 
            "volume_ephemeral_storage": True,
99
112
            "volume_map": "",
100
113
            "volume_dev_regexp": "/dev/db[b-z]",
101
114
            "backup_dir": "/var/lib/postgresql/backups",
102
115
            "backup_schedule": "13 4 * * *",
103
116
            "backup_retention_count": 7,
104
117
            "nagios_context": "juju",
 
118
            "pgdg": False,
 
119
            "install_sources": "",
 
120
            "install_keys": "",
105
121
            "extra_archives": "",
106
122
            "advisory_lock_restart_key": 765}
107
123
 
108
124
    def relation_set(self, *args, **kwargs):
109
125
        """
110
 
        Capture result of relation_set into _relation_data, which
 
126
        Capture result of relation_set into _outgoing_relation_data, which
111
127
        can then be checked later.
112
128
        """
113
129
        if "relation_id" in kwargs:
114
130
            del kwargs["relation_id"]
115
 
        self._relation_data = dict(self._relation_data, **kwargs)
 
131
 
116
132
        for arg in args:
117
133
            (key, value) = arg.split("=")
118
 
            self._relation_data[key] = value
 
134
            self._outgoing_relation_data = (
 
135
                self._outgoing_relation_data + ((key, value),))
119
136
 
120
137
    def relation_ids(self, relation_name="db-admin"):
121
138
        """
156
173
    def juju_log(self, *args, **kwargs):
157
174
        pass
158
175
 
159
 
    def log(self, *args, **kwargs):
160
 
        pass
 
176
    def log(self, message, level=None):
 
177
        if level is None:
 
178
            level = self.INFO
 
179
        log = getattr(self, "_log_%s" % level)
 
180
        setattr(self, "_log_%s" % level, log + (message,))
161
181
 
162
182
    def config(self, scope=None):
163
183
        if scope is None:
166
186
            return self._config[scope]
167
187
 
168
188
    def relation_get(self, scope=None, unit_name=None, relation_id=None):
169
 
        pass
 
189
        if scope:
 
190
            for (key, value) in self._incoming_relation_data:
 
191
                if key == scope:
 
192
                    return value
 
193
            return None
170
194
 
171
195
 
172
196
class TestHooks(mocker.MockerTestCase):
209
233
 
210
234
class TestHooksService(TestHooks):
211
235
 
 
236
    def test_data_relation_departed_stops_postgresql(self):
 
237
        """
 
238
        When the storage subordinate charm relation departs firing the
 
239
        C{data-relation-departed} hook, the charm stops the postgresql service
 
240
        and logs a message.
 
241
        """
 
242
        postgresql_stop = self.mocker.replace(hooks.postgresql_stop)
 
243
        postgresql_stop()
 
244
        self.mocker.replay()
 
245
        hooks.stop_postgres_on_data_relation_departed()
 
246
        message = "Data relation departing. Stopping PostgreSQL"
 
247
        self.assertIn(
 
248
            message, hooks.hookenv._log_DEBUG, "Not logged- %s" % message)
 
249
 
 
250
    def test_data_relation_joined_requests_configured_mountpoint(self):
 
251
        """
 
252
        When postgresql is related to the storage subordinate charm via the
 
253
        'data' relation it will read the configured C{storage_mount_point} and
 
254
        set C{mountpoint} in the relation in order to request a specific
 
255
        mountpoint from the storage charm.
 
256
        """
 
257
        mount = hooks.external_volume_mount
 
258
        hooks.data_relation_joined()
 
259
        message = "Setting mount point in the relation: %s" % mount
 
260
        self.assertIn(
 
261
            message, hooks.hookenv._log_DEBUG, "Not logged- %s" % message)
 
262
 
 
263
    def test_data_relation_changed_waits_for_data_relation_mountpoint(self):
 
264
        """
 
265
        C{data_relation_changed} will wait for the storage charm to respond
 
266
        with the properly configured C{mountpoint} in the 'data' relation
 
267
        before calling C{config_changed}.
 
268
        """
 
269
        mount = hooks.external_volume_mount
 
270
        hooks.hookenv._config["storage_mount_point"] = mount
 
271
        self.assertEqual(hooks.hookenv._incoming_relation_data, ())
 
272
        hooks.data_relation_changed()
 
273
        message = "Waiting for mountpoint from the relation: %s" % mount
 
274
        self.assertIn(
 
275
            message, hooks.hookenv._log_DEBUG, "Not logged- %s" % message)
 
276
 
 
277
    def test_data_relation_changed_mountpoint_present(self):
 
278
        """
 
279
        C{data_relation_changed} will call C{config_changed} when it receives
 
280
        the successfuly mounted C{mountpoint} from storage charm.
 
281
        """
 
282
        mount = hooks.external_volume_mount
 
283
        self.addCleanup(
 
284
            setattr, hooks.hookenv, "_incoming_relation_data", ())
 
285
        hooks.hookenv._incoming_relation_data = (("mountpoint", mount),)
 
286
        config_changed = self.mocker.replace(hooks.config_changed)
 
287
        config_changed(mount_point=mount)
 
288
        self.mocker.replay()
 
289
 
 
290
        hooks.data_relation_changed()
 
291
        message = "Storage ready and mounted"
 
292
        self.assertIn(
 
293
            message, hooks.hookenv._log_DEBUG, "Not logged- %s" % message)
 
294
 
212
295
    def test_create_postgresql_config_wal_no_replication(self):
213
296
        """
214
297
        When postgresql is in C{standalone} mode, and participates in no