~tribaal/charms/trusty/landscape-client/fix-unit-tests

« back to all changes in this revision

Viewing changes to hooks/hooks.py

  • Committer: Christopher Glass
  • Date: 2014-08-28 09:19:59 UTC
  • Revision ID: christopher.glass@canonical.com-20140828091959-5ra6zy4rh9oczmg8
Fixes unit tests by having better dependency injection.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import base64
7
7
 
8
8
from charmhelpers.core.hookenv import (
9
 
    Hooks, UnregisteredHookError, log, local_unit)
 
9
    Hooks, UnregisteredHookError, log)
10
10
from shutil import rmtree
11
11
from subprocess import CalledProcessError
12
12
 
20
20
CERT_FILE = "/etc/ssl/certs/landscape_server_ca.crt"
21
21
 
22
22
JUJU_BROKER = JujuBroker()
23
 
LANDSCAPE_BROKER = LandscapeBroker()
24
23
 
25
24
hooks = Hooks()
26
25
 
27
26
 
 
27
def get_landscape_broker():
 
28
    return LandscapeBroker()
 
29
 
 
30
 
28
31
@hooks.hook("registration-relation-joined", "registration-relation-changed")
29
 
def registration_relation(juju_broker=JUJU_BROKER,
30
 
                          landscape_broker=LANDSCAPE_BROKER):
 
32
def registration_relation(juju_broker=JUJU_BROKER, landscape_broker=None):
 
33
    if landscape_broker is None:
 
34
        landscape_broker = get_landscape_broker()
 
35
 
31
36
    data = juju_broker.get_relation_config()
32
37
    if "ping-url" in data:
33
38
        config_changed(relation_data=data, juju_broker=juju_broker,
36
41
 
37
42
@hooks.hook("config-changed")
38
43
def config_changed(relation_data=None, juju_broker=JUJU_BROKER,
39
 
                   landscape_broker=LANDSCAPE_BROKER):
 
44
                   landscape_broker=None):
40
45
    """
41
46
    @param relation_data: data from the context of a relation, it should
42
47
                          match the data in the config
43
48
    """
44
 
    log("In config-changed for %s" % local_unit())
 
49
    if landscape_broker is None:
 
50
        landscape_broker = get_landscape_broker()
 
51
 
 
52
    juju_broker.log("In config-changed for %s" % landscape_broker.local_unit)
 
53
 
45
54
    if relation_data is None:
46
55
        relation_data = {}
47
56
    service_config = juju_broker.get_service_config()
58
67
 
59
68
 
60
69
@hooks.hook("upgrade-charm")
61
 
def upgrade_charm(landscape_broker=LANDSCAPE_BROKER):
 
70
def upgrade_charm(landscape_broker=None):
62
71
    """Idempotently upgrades state from older charms."""
 
72
    if landscape_broker is None:
 
73
        landscape_broker = get_landscape_broker()
 
74
 
63
75
    landscape_broker.config.reload()
64
76
    return migrate_old_juju_info(landscape_broker.config)
65
77
 
66
78
 
67
79
@hooks.hook("container-relation-joined", "ceph-client-relation-joined")
68
 
def container_relation_joined(juju_broker=JUJU_BROKER,
69
 
                              landscape_broker=LANDSCAPE_BROKER):
70
 
    log("In container-relation-joined for %s" % local_unit())
 
80
def container_relation_joined(juju_broker=JUJU_BROKER, landscape_broker=None):
 
81
    if landscape_broker is None:
 
82
        landscape_broker = get_landscape_broker()
 
83
    juju_broker.log("In container-relation-joined for %s"
 
84
                    "" % landscape_broker.local_unit)
71
85
    landscape_broker.config.reload()
72
86
    relation_conf = juju_broker.get_relation_config()
73
87
    remote_unit_name = juju_broker.environment.get("JUJU_REMOTE_UNIT")
100
114
 
101
115
 
102
116
@hooks.hook("container-relation-departed")
103
 
def container_relation_departed(landscape_broker=LANDSCAPE_BROKER):
 
117
def container_relation_departed(landscape_broker=None):
104
118
    """Stop managing the removed unit with Landscape.
105
119
 
106
120
    If the relation is removed, the registration will be cleared, the
107
121
    computer title unset and the juju info file removed, so that we
108
122
    won't try to register again when the config-changed hook is fired.
109
123
    """
 
124
    if landscape_broker is None:
 
125
        landscape_broker = get_landscape_broker()
110
126
    landscape_broker.clear_registration()
111
127
 
112
128
    # Delete all of the juju-info files.
118
134
 
119
135
 
120
136
@hooks.hook("ceph-client-relation-changed")
121
 
def ceph_relation_changed(juju_broker=JUJU_BROKER,
122
 
                          landscape_broker=LANDSCAPE_BROKER):
 
137
def ceph_relation_changed(juju_broker=JUJU_BROKER, landscape_broker=None):
 
138
    if landscape_broker is None:
 
139
        landscape_broker = get_landscape_broker()
123
140
    relation_config = juju_broker.get_relation_config()
124
141
    # The key is only reported once the ceph ring has quorum (there are enough
125
142
    # nodes in the service). There's nothing to do until the key is provided.
155
172
 
156
173
 
157
174
@hooks.hook("ceph-client-relation-broken")
158
 
def ceph_relation_broken(landscape_broker=LANDSCAPE_BROKER):
 
175
def ceph_relation_broken(landscape_broker=None):
 
176
    if landscape_broker is None:
 
177
        landscape_broker = get_landscape_broker()
159
178
    rmtree(get_ceph_client_path(landscape_broker), ignore_errors=True)
160
179
 
161
180
 
162
181
@hooks.hook("ceph-client-relation-departed")
163
 
def ceph_relation_departed(landscape_broker=LANDSCAPE_BROKER):
 
182
def ceph_relation_departed(landscape_broker=None):
 
183
    if landscape_broker is None:
 
184
        landscape_broker = get_landscape_broker()
164
185
    landscape_broker.clear_registration()
165
186
 
166
187