~adam-collard/charms/trusty/landscape-client/trunk

« back to all changes in this revision

Viewing changes to hooks/test_hooks.py

  • Committer: Christopher Glass
  • Date: 2014-06-12 11:41:32 UTC
  • mfrom: (37.2.3 landscape-client)
  • Revision ID: christopher.glass@canonical.com-20140612114132-dwvthvhhlg14wzjy
Merge changes from the ~landscape/charms/landscape-client/trunk branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
import json
 
2
import socket
2
3
import os
3
4
from shutil import rmtree
4
5
from tempfile import mkdtemp
57
58
            data_path, "landscape.conf")
58
59
        self.landscape_broker.config.write()
59
60
        self.juju_broker = FakeJujuBroker()
60
 
        self.hooks = hooks.HookRunner(self.juju_broker, self.landscape_broker)
61
61
 
62
62
    def mkdtemp(self):
63
63
        directory = mkdtemp(prefix=self.__class__.__name__)
163
163
            "JUJU_API_ADDRESSES": "10.0.0.1:1234"})
164
164
        self.juju_broker.commands["relation-get"] = {
165
165
            "private-address": "10.0.0.99"}
166
 
        self.hooks.run("container-relation-joined")
 
166
        hooks.container_relation_joined(juju_broker=self.juju_broker,
 
167
                                        landscape_broker=self.landscape_broker)
167
168
        juju_info_path = os.path.join(
168
169
            self.landscape_broker.config.data_path, "juju-info.json")
169
170
        juju_info = json.loads(read_file(juju_info_path))
188
189
            registration_attempts.append(os.path.exists(juju_info_path))
189
190
 
190
191
        self.landscape_broker.try_to_register = record_registration_attempt
191
 
        self.hooks.run("container-relation-joined")
 
192
        hooks.container_relation_joined(juju_broker=self.juju_broker,
 
193
                                        landscape_broker=self.landscape_broker)
192
194
 
193
195
        self.assertEqual([True], registration_attempts)
194
196
 
 
197
    def test_computer_title_is_hostname(self):
 
198
        """
 
199
        The client adds the hostname as the "computer-title" field of the
 
200
        landscape broker config.
 
201
        """
 
202
        self.hooks.run("container-relation-joined")
 
203
        result = self.landscape_broker.config.computer_title
 
204
        self.assertEqual(result, socket.gethostname())
 
205
 
195
206
 
196
207
class ContainerRelationDepartedTest(HookTestCase):
197
208
    """Test for the container-relation-departed hook."""
201
212
        When the relation is removed, the Landscape registration data
202
213
        is removed.
203
214
        """
204
 
        self.hooks.run("container-relation-departed")
 
215
        hooks.container_relation_departed(
 
216
            landscape_broker=self.landscape_broker)
205
217
        self.assertTrue(self.landscape_broker.cleared_registration)
206
218
 
207
219
    def test_clears_computer_title(self):
211
223
        (which is now gone).
212
224
        """
213
225
        self.landscape_broker.config.computer_title = "unit/0"
214
 
        self.hooks.run("container-relation-departed")
 
226
        hooks.container_relation_departed(
 
227
            landscape_broker=self.landscape_broker)
215
228
        self.assertEqual("", self.landscape_broker.config.computer_title)
216
229
 
217
230
    def test_removes_juju_info(self):
227
240
                     "api-addresses": "10.0.0.1:1234",
228
241
                     "private-address": "10.0.0.99"}
229
242
        write_json_file(juju_info_path, juju_info)
230
 
        self.hooks.run("container-relation-departed")
 
243
        hooks.container_relation_departed(
 
244
            landscape_broker=self.landscape_broker)
231
245
        self.assertFalse(os.path.exists(juju_info_path))
232
246
 
233
247
 
255
269
        If there's no key in the relation information provided by ceph,
256
270
        log and do nothing.
257
271
        """
258
 
        self.assertEqual(0, self.hooks.run("ceph-client-relation-changed"))
 
272
        self.assertEqual(0, hooks.ceph_relation_changed(
 
273
            juju_broker=self.juju_broker,
 
274
            landscape_broker=self.landscape_broker))
259
275
 
260
276
        [log] = self.juju_broker.logs
261
277
        self.assertEqual(
273
289
        hooks.write_ceph_client_keyring = stub_ceph_write
274
290
        hooks.write_ceph_client_config = stub_ceph_write
275
291
 
276
 
        self.assertEqual(0, self.hooks.run("ceph-client-relation-changed"))
 
292
        self.assertEqual(0, hooks.ceph_relation_changed(
 
293
            juju_broker=self.juju_broker,
 
294
            landscape_broker=self.landscape_broker))
277
295
 
278
296
        ceph_client_dir = os.path.join(
279
297
            self.landscape_broker.config.data_path, "ceph-client")
299
317
        hooks.write_ceph_client_keyring = stub_write_ceph_client_keyring
300
318
        hooks.write_ceph_client_config = stub_ceph_write
301
319
 
302
 
        self.assertEqual(0, self.hooks.run("ceph-client-relation-changed"))
 
320
        self.assertEqual(0, hooks.ceph_relation_changed(
 
321
            juju_broker=self.juju_broker,
 
322
            landscape_broker=self.landscape_broker))
303
323
        [args] = called_args
304
324
        self.assertEqual(
305
325
            (self.landscape_broker, "landscape-client", "foo"), args)
324
344
            called_args.append(args)
325
345
        hooks.write_ceph_client_config = stub_write_ceph_client_config
326
346
 
327
 
        self.assertEqual(0, self.hooks.run("ceph-client-relation-changed"))
 
347
        self.assertEqual(0, hooks.ceph_relation_changed(
 
348
            juju_broker=self.juju_broker,
 
349
            landscape_broker=self.landscape_broker))
328
350
        [args] = called_args
329
351
        self.assertEqual(
330
352
            (self.landscape_broker, "bar", keyring_file, "home"), args)
338
360
        hooks.write_ceph_client_keyring = stub_ceph_write
339
361
        hooks.write_ceph_client_config = stub_ceph_write
340
362
 
341
 
        self.assertEqual(0, self.hooks.run("ceph-client-relation-changed"))
 
363
        self.assertEqual(0, hooks.ceph_relation_changed(
 
364
            juju_broker=self.juju_broker,
 
365
            landscape_broker=self.landscape_broker))
342
366
 
343
367
        [log] = self.juju_broker.logs
344
368
        self.assertEqual("Ceph configuration written to file.", log)
351
375
        ceph_client_path = os.path.join(
352
376
            self.landscape_broker.config.data_path, "ceph-client")
353
377
        os.mkdir(ceph_client_path)
354
 
        self.hooks.run("ceph-client-relation-broken")
 
378
        hooks.ceph_relation_broken(landscape_broker=self.landscape_broker)
355
379
        self.assertFalse(os.path.exists(ceph_client_path))
356
380
 
357
381
 
372
396
            "ssl-public-key": "base64:%s" % "foo".encode("base64"),
373
397
            "account-name": "standalone",
374
398
            "ping-url": "http://127.0.0.1"}
375
 
        self.hooks.run("config-changed")
 
399
 
 
400
        hooks.config_changed(juju_broker=self.juju_broker,
 
401
                             landscape_broker=self.landscape_broker)
376
402
        self.assertEqual(
377
403
            hooks.CERT_FILE, self.landscape_broker.config.ssl_public_key)
378
404
        with open(hooks.CERT_FILE) as fh:
396
422
            "ssl-public-key": "base64:%s" % "foo".encode("base64"),
397
423
            "account-name": "standalone",
398
424
            "ping-url": "http://127.0.0.1/ping"}
399
 
        self.hooks.run("registration-relation-joined")
 
425
        hooks.registration_relation(juju_broker=self.juju_broker,
 
426
                                    landscape_broker=self.landscape_broker)
400
427
        self.assertEqual(
401
428
            hooks.CERT_FILE, self.landscape_broker.config.ssl_public_key)
402
429
        with open(hooks.CERT_FILE) as fh:
410
437
            "ssl-public-key": "/etc/ssl/my-cert.pem",
411
438
            "account-name": "standalone",
412
439
            "ping-url": "http://127.0.0.1/ping"}
413
 
        self.hooks.run("registration-relation-joined")
 
440
        hooks.registration_relation(juju_broker=self.juju_broker,
 
441
                                    landscape_broker=self.landscape_broker)
414
442
        self.assertEqual("/etc/ssl/my-cert.pem",
415
443
                         self.landscape_broker.config.ssl_public_key)