~cprov/uci-engine/tarmac-multienv

« back to all changes in this revision

Viewing changes to charms/precise/webui/unit_tests/test_hooks.py

  • Committer: Evan Dandrea
  • Date: 2014-06-30 12:07:54 UTC
  • mfrom: (630 uci-engine)
  • mto: This revision was merged to the branch mainline in revision 655.
  • Revision ID: evan.dandrea@canonical.com-20140630120754-z6x5eqdahpp2qfrt
Merge with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Ubuntu CI Engine
 
2
# Copyright 2014 Canonical Ltd.
 
3
 
 
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.
 
7
 
 
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.
 
12
 
 
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/>.
 
15
import json
 
16
import mock
 
17
import os
 
18
import unittest
 
19
import shutil
 
20
import tempfile
 
21
 
 
22
import yaml
 
23
 
 
24
import hooks
 
25
 
 
26
HTTP_REL = {
 
27
    'hostname': 'remote.com',
 
28
    'port': '8080',
 
29
}
 
30
 
 
31
 
 
32
INFO_REL = {
 
33
    'private-address': '10.0.0.1',
 
34
}
 
35
 
 
36
 
 
37
PATCHES = [
 
38
    'charmhelpers.core.hookenv.config',
 
39
    'charmhelpers.core.hookenv.relation_get',
 
40
    'charmhelpers.core.hookenv.remote_unit',
 
41
]
 
42
 
 
43
 
 
44
class RestishTestCase(unittest.TestCase):
 
45
    def setUp(self):
 
46
        """Setup a testing hook environment.
 
47
 
 
48
        Patches the required charmhelper and intercom objects and
 
49
        initializes few mocks for tests.
 
50
        """
 
51
        super(RestishTestCase, self).setUp()
 
52
        self.tmpdir = tempfile.mkdtemp()
 
53
        self.addCleanup(shutil.rmtree, self.tmpdir)
 
54
 
 
55
        for spec in PATCHES:
 
56
            attr_name = spec.split('.')[-1]
 
57
            _m = mock.patch(spec)
 
58
            setattr(self, attr_name, _m.start())
 
59
            self.addCleanup(_m.stop)
 
60
 
 
61
        hooks.log = mock.Mock()
 
62
        self.remote_unit.return_value = 'remote-test'
 
63
        self._set_default_config()
 
64
 
 
65
    def _set_default_config(self):
 
66
        defaults = {}
 
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
 
75
 
 
76
 
 
77
class TestJSONStatusHooks(RestishTestCase):
 
78
 
 
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)
 
83
 
 
84
        with hooks.status_urls() as data:
 
85
            self.assertEqual(expected_data, data)
 
86
            data['blah'] = 123
 
87
            expected_data = data
 
88
 
 
89
        with open(hooks.json_status_file()) as f:
 
90
            data = json.load(f)
 
91
            self.assertEqual(expected_data, data)
 
92
 
 
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:
 
96
            f.write('{}')
 
97
 
 
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)