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/>.
26
HERE = os.path.abspath(os.path.dirname(__file__))
29
'hostname': 'remote.com',
35
'private-address': '10.0.0.1',
40
'charmhelpers.core.hookenv.config',
41
'charmhelpers.core.hookenv.relation_get',
42
'charmhelpers.core.hookenv.remote_unit',
46
class RestishTestCase(unittest.TestCase):
48
"""Setup a testing hook environment.
50
Patches the required charmhelper and intercom objects and
51
initializes few mocks for tests.
53
super(RestishTestCase, self).setUp()
54
self.tmpdir = tempfile.mkdtemp()
55
self.addCleanup(shutil.rmtree, self.tmpdir)
58
attr_name = spec.split('.')[-1]
60
setattr(self, attr_name, _m.start())
61
self.addCleanup(_m.stop)
63
hooks.log = mock.Mock()
64
self.remote_unit.return_value = 'remote-test'
65
self._set_default_config()
67
def _set_default_config(self):
69
config = os.path.join(HERE, '../config.yaml')
70
if os.path.exists(config):
71
with open(config) as f:
72
config = yaml.safe_load(f)
73
for name, option in config['options'].iteritems():
74
defaults[name] = option.get('default')
75
defaults['health_path'] = self.tmpdir
76
self.config.return_value = defaults
79
class TestJSONStatusHooks(RestishTestCase):
81
def test_status_urls_flocking(self):
82
expected_data = {'foo': 'bar'}
83
with open(hooks.json_status_file(), 'w') as f:
84
json.dump(expected_data, f)
86
with hooks.status_urls() as data:
87
self.assertEqual(expected_data, data)
91
with open(hooks.json_status_file()) as f:
93
self.assertEqual(expected_data, data)
95
@mock.patch('hooks._add_proxy')
96
def test_json_status_joined(self, add_proxy):
97
with open(hooks.json_status_file(), 'w') as f:
100
self.relation_get.return_value = 'foo'
101
hooks.hooks.execute(['hooks/json_status-relation-joined'])
102
add_proxy.assert_called_once_with('json_status-remote-test', 'foo')
103
with hooks.status_urls() as data:
104
self.assertIn('remote-test', data)
106
@mock.patch('hooks._add_proxy')
107
def test_json_status_departed(self, add_proxy):
108
with open(hooks.json_status_file(), 'w') as f:
111
self.relation_get.return_value = 'foo'
112
hooks.hooks.execute(['hooks/json_status-relation-joined'])
113
add_proxy.assert_called_once_with('json_status-remote-test', 'foo')
114
with hooks.status_urls() as data:
115
self.assertIn('remote-test', data)
117
with hooks.status_urls() as data:
118
del data['remote-test']
120
with hooks.status_urls() as data:
121
self.assertEqual({}, data)