2
# Copyright 2014 Canonical Ltd.
4
# This program is free software: you can redistribute it and/or modify it
5
# under the terms of the GNU Affero General Public License version 3, as
6
# published by the Free Software Foundation.
8
# This program is distributed in the hope that it will be useful, but
9
# WITHOUT ANY WARRANTY; without even the implied warranties of
10
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11
# PURPOSE. See the GNU Affero General Public License for more details.
13
# You should have received a copy of the GNU Affero General Public License
14
# along with this program. If not, see <http://www.gnu.org/licenses/>.
28
'hostname': 'remote.com',
34
'private-address': '10.0.0.1',
39
'charmhelpers.core.hookenv.config',
40
'charmhelpers.core.hookenv.local_unit',
41
'charmhelpers.core.hookenv.log',
42
'charmhelpers.core.hookenv.remote_unit',
43
'charmhelpers.core.hookenv.relation_get',
44
'charmhelpers.core.hookenv.relation_id',
45
'charmhelpers.core.hookenv.relation_set',
46
'charmhelpers.core.hookenv.unit_get',
47
'charmhelpers.core.host.mkdir',
48
'charmhelpers.fetch.apt_install',
49
'charmhelpers.fetch.configure_sources',
53
class JujuTestCase(unittest.TestCase):
55
"""Setup a testing hook environment."""
56
super(JujuTestCase, self).setUp()
57
self.tmpdir = tempfile.mkdtemp()
58
self.addCleanup(shutil.rmtree, self.tmpdir)
61
attr_name = spec.split('.')[-1]
63
setattr(self, attr_name, _m.start())
64
self.addCleanup(_m.stop)
66
self.local_unit.return_value = 'local-test'
67
self.remote_unit.return_value = 'remote-test'
68
self.relation_get.side_effect = (
69
lambda rel_name: HTTP_REL.get(rel_name))
70
self.relation_id.return_value = '1'
72
self._set_default_config()
74
def _set_default_config(self):
76
config = os.path.join(os.path.dirname(__file__), '../config.yaml')
77
if os.path.exists(config):
78
with open(config) as f:
79
config = yaml.safe_load(f)
80
for name, option in config['options'].iteritems():
81
defaults[name] = option.get('default')
82
self.config.return_value = defaults
83
self.config.return_value['install_root'] = self.tmpdir
86
class TestLanderHook(JujuTestCase):
88
super(TestLanderHook, self).setUp()
89
p = os.path.join(self.tmpdir, self.local_unit.return_value)
91
self.relations = os.path.join(p, 'juju-relations')
93
def test_joined_broken(self):
94
hooks.hooks.execute(['hooks/lander-relation-joined'])
95
self.assertTrue(os.path.exists(self.relations))
96
rfile = os.path.join(self.relations, self.relation_id.return_value)
97
self.assertTrue(os.path.exists(rfile))
99
hooks.hooks.execute(['hooks/lander-relation-broken'])
100
self.assertFalse(os.path.exists(rfile))
103
class TestAmqpHook(JujuTestCase):
105
super(TestAmqpHook, self).setUp()
106
p = os.path.join(self.tmpdir, self.local_unit.return_value)
108
self.amqp_file = os.path.join(p, 'amqp_config.py')
109
with open(self.amqp_file, 'w') as f:
112
def test_joined(self):
113
hooks.hooks.execute(['hooks/amqp-relation-joined'])
114
self.assertEqual(1, self.relation_set.call_count)
116
def test_changed(self):
117
hooks.hooks.execute(['hooks/amqp-relation-changed'])
118
required = ['AMQP_USER', 'AMQP_VHOST', 'AMQP_HOST', 'AMQP_PASSWORD']
119
with open(self.amqp_file) as f:
120
for line in f.readlines():
121
key = line.split('=')[0].strip()
124
self.assertEqual([], required)
126
def test_broken(self):
127
hooks.hooks.execute(['hooks/amqp-relation-broken'])
128
self.assertFalse(os.path.exists(self.amqp_file))