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/>.
27
'hostname': 'remote.com',
33
'private-address': '10.0.0.1',
38
'charmhelpers.core.hookenv.config',
39
'charmhelpers.core.hookenv.relation_get',
40
'charmhelpers.core.hookenv.remote_unit',
44
class RestishTestCase(unittest.TestCase):
46
"""Setup a testing hook environment.
48
Patches the required charmhelper and intercom objects and
49
initializes few mocks for tests.
51
super(RestishTestCase, self).setUp()
52
self.tmpdir = tempfile.mkdtemp()
53
self.addCleanup(shutil.rmtree, self.tmpdir)
56
attr_name = spec.split('.')[-1]
58
setattr(self, attr_name, _m.start())
59
self.addCleanup(_m.stop)
61
hooks.log = mock.Mock()
62
self.remote_unit.return_value = 'remote-test'
63
self._set_default_config()
65
def _set_default_config(self):
67
config = os.path.join(os.path.dirname(__file__), '../config.yaml')
68
if os.path.exists(config):
69
with open(config) as f:
70
config = yaml.safe_load(f)
71
for name, option in config['options'].iteritems():
72
defaults[name] = option.get('default')
73
defaults['health_path'] = self.tmpdir
74
self.config.return_value = defaults
77
class TestJSONStatusHooks(RestishTestCase):
79
def test_status_urls_flocking(self):
80
expected_data = {'foo': 'bar'}
81
with open(hooks.json_status_file(), 'w') as f:
82
json.dump(expected_data, f)
84
with hooks.status_urls() as data:
85
self.assertEqual(expected_data, data)
89
with open(hooks.json_status_file()) as f:
91
self.assertEqual(expected_data, data)
93
@mock.patch('hooks._add_proxy')
94
def test_json_status_joined(self, add_proxy):
95
with open(hooks.json_status_file(), 'w') as f:
98
self.relation_get.return_value = 'foo'
99
hooks.hooks.execute(['hooks/json_status-relation-joined'])
100
add_proxy.assert_called_once_with('json_status-remote-test', 'foo')
101
with hooks.status_urls() as data:
102
self.assertIn('remote-test', data)