~codersquid/charms/precise/block-storage-broker/basenode-support

« back to all changes in this revision

Viewing changes to hooks/test_hooks.py

  • Committer: Chad Smith
  • Date: 2014-02-11 22:07:32 UTC
  • Revision ID: chad.smith@canonical.com-20140211220732-g3lhlk0z315hjv5u
unit test fixese for handling new persistent_file and get_vol_id(instance_id) calls

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
from charmhelpers import fetch
2
2
import hooks
 
3
import json
3
4
import mocker
 
5
import os
4
6
from testing import TestHookenv
5
7
 
6
8
 
79
81
 
80
82
    def test_block_storage_relation_changed_with_instance_id(self):
81
83
        """
82
 
        L{block_storage_relation_departed} calls L{attach_nova_volume} when
 
84
        L{block_storage_relation_changed} calls L{attach_nova_volume} when
83
85
        C{instance-id} is available in the relation data. To report
84
86
        a successful device attach, it sets the relation data
85
87
        C{block-device-path} to the attached volume's device path from nova.
86
88
        """
87
89
        device_path = "/dev/vdc"
 
90
        self.addCleanup(setattr, os, "environ", os.environ)
 
91
        os.environ = {"JUJU_REMOTE_UNIT": "storage/0"}
88
92
        self.addCleanup(
89
93
            setattr, hooks.hookenv, "_incoming_relation_data",
90
94
            hooks.hookenv._incoming_relation_data)
91
95
        hooks.hookenv._incoming_relation_data = (("instance-id", "i-123"),)
92
96
 
 
97
        persist = self.mocker.replace(hooks._persist_data)
 
98
        persist("storage/0", "i-123")
93
99
        nova_attach = self.mocker.replace("nova_util.attach_nova_volume")
94
100
        nova_attach(
95
101
            instance_id="i-123", volume_id=None, size=None, volume_label=None)
114
120
        """
115
121
        device_path = "/dev/vdc"
116
122
        volume_id = "123-123-123"
 
123
        self.addCleanup(setattr, os, "environ", os.environ)
 
124
        os.environ = {"JUJU_REMOTE_UNIT": "storage/0"}
117
125
        self.addCleanup(
118
126
            setattr, hooks.hookenv, "_incoming_relation_data",
119
127
            hooks.hookenv._incoming_relation_data)
120
128
        hooks.hookenv._incoming_relation_data = (
121
129
            ("instance-id", "i-123"), ("volume-id", volume_id))
122
130
 
 
131
        persist = self.mocker.replace(hooks._persist_data)
 
132
        persist("storage/0", "i-123")
123
133
        nova_attach = self.mocker.replace("nova_util.attach_nova_volume")
124
134
        nova_attach(
125
135
            instance_id="i-123", volume_id=volume_id, size=None,
145
155
        """
146
156
        device_path = "/dev/vdc"
147
157
        size = 12
 
158
        self.addCleanup(setattr, os, "environ", os.environ)
 
159
        os.environ = {"JUJU_REMOTE_UNIT": "storage/0"}
148
160
        self.addCleanup(
149
161
            setattr, hooks.hookenv, "_incoming_relation_data",
150
162
            hooks.hookenv._incoming_relation_data)
151
163
        hooks.hookenv._incoming_relation_data = (
152
164
            ("instance-id", "i-123"), ("size", size))
153
 
 
 
165
        persist = self.mocker.replace(hooks._persist_data)
 
166
        persist("storage/0", "i-123")
154
167
        nova_attach = self.mocker.replace("nova_util.attach_nova_volume")
155
168
        nova_attach(
156
169
            instance_id="i-123", volume_id=None, size=size, volume_label=None)
168
181
 
169
182
    def test_block_storage_relation_departed_fails_without_instance_id(self):
170
183
        """
171
 
        L{block_storage_relation_departed} will exit in error and log a message
172
 
        when the relation does not contain the required C{instance-id}.
 
184
        L{block_storage_relation_departed} will exit in error and logs a
 
185
        message when the persistent data does not contain the required
 
186
        C{instance-id} value for the C{hookenv.remote_unit}.
173
187
        """
174
 
        self.addCleanup(
175
 
            setattr, hooks.hookenv, "_incoming_relation_data",
176
 
            hooks.hookenv._incoming_relation_data)
177
 
        hooks.hookenv._incoming_relation_data = ()
 
188
        self.addCleanup(setattr, os, "environ", os.environ)
 
189
        charm_dir = self.makeDir()
 
190
        os.environ = {"JUJU_REMOTE_UNIT": "storage/0", "CHARM_DIR": charm_dir}
178
191
 
179
 
        self.assertIsNone(hooks.hookenv.relation_get("instance-id"))
180
192
        result = self.assertRaises(
181
193
            SystemExit, hooks.block_storage_relation_departed)
182
194
        self.assertEqual(result.code, 1)
189
201
        L{block_storage_relation_departed} calls L{detach_nova_volume} when
190
202
        C{instance-id} is available in the relation data.
191
203
        """
192
 
        self.addCleanup(
193
 
            setattr, hooks.hookenv, "_incoming_relation_data",
194
 
            hooks.hookenv._incoming_relation_data)
195
 
        hooks.hookenv._incoming_relation_data = (("instance-id", "i-123"),)
 
204
        self.addCleanup(setattr, os, "environ", os.environ)
 
205
        charm_dir = self.makeDir()
 
206
        os.environ = {"JUJU_REMOTE_UNIT": "storage/0", "CHARM_DIR": charm_dir}
 
207
        # Store our persistent instance-id info
 
208
        persist_path = "%s/%s" % (charm_dir, hooks.PERSIST_DATA_FILE)
 
209
        data = {"storage/0": "i-123"}
 
210
        with open(persist_path, "w") as outfile:
 
211
            outfile.write(unicode(json.dumps(data, ensure_ascii=False)))
196
212
 
197
213
        nova_detach = self.mocker.replace("nova_util.detach_nova_volume")
198
214
        nova_detach("i-123")
199
215
        self.mocker.replay()
200
216
 
201
 
        self.assertEqual(hooks.hookenv.relation_get("instance-id"), "i-123")
202
217
        hooks.block_storage_relation_departed()