~canonical-ci-engineering/uci-engine/trunk

« back to all changes in this revision

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

  • Committer: Ubuntu CI Bot
  • Author(s): Celso Providelo
  • Date: 2015-01-07 14:33:28 UTC
  • mfrom: (911.1.8 charms-cleanup)
  • Revision ID: ubuntu_ci_bot-20150107143328-q0d037ojtopm3n9d
Removing the charms from UCI-E since they live in isolated branches now and also adjusting the run-tests to stop loading/running charm tests.

Updating local j-d copy to trunk (r126) for proper support for charm branch pinning. [r=Evan Dandrea]

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
 
HERE = os.path.abspath(os.path.dirname(__file__))
27
 
 
28
 
HTTP_REL = {
29
 
    'hostname': 'remote.com',
30
 
    'port': '8080',
31
 
}
32
 
 
33
 
 
34
 
INFO_REL = {
35
 
    'private-address': '10.0.0.1',
36
 
}
37
 
 
38
 
 
39
 
PATCHES = [
40
 
    'charmhelpers.core.hookenv.config',
41
 
    'charmhelpers.core.hookenv.relation_get',
42
 
    'charmhelpers.core.hookenv.remote_unit',
43
 
]
44
 
 
45
 
 
46
 
class RestishTestCase(unittest.TestCase):
47
 
    def setUp(self):
48
 
        """Setup a testing hook environment.
49
 
 
50
 
        Patches the required charmhelper and intercom objects and
51
 
        initializes few mocks for tests.
52
 
        """
53
 
        super(RestishTestCase, self).setUp()
54
 
        self.tmpdir = tempfile.mkdtemp()
55
 
        self.addCleanup(shutil.rmtree, self.tmpdir)
56
 
 
57
 
        for spec in PATCHES:
58
 
            attr_name = spec.split('.')[-1]
59
 
            _m = mock.patch(spec)
60
 
            setattr(self, attr_name, _m.start())
61
 
            self.addCleanup(_m.stop)
62
 
 
63
 
        hooks.log = mock.Mock()
64
 
        self.remote_unit.return_value = 'remote-test'
65
 
        self._set_default_config()
66
 
 
67
 
    def _set_default_config(self):
68
 
        defaults = {}
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
77
 
 
78
 
 
79
 
class TestJSONStatusHooks(RestishTestCase):
80
 
 
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)
85
 
 
86
 
        with hooks.status_urls() as data:
87
 
            self.assertEqual(expected_data, data)
88
 
            data['blah'] = 123
89
 
            expected_data = data
90
 
 
91
 
        with open(hooks.json_status_file()) as f:
92
 
            data = json.load(f)
93
 
            self.assertEqual(expected_data, data)
94
 
 
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:
98
 
            f.write('{}')
99
 
 
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)
105
 
 
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:
109
 
            f.write('{}')
110
 
 
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)
116
 
 
117
 
        with hooks.status_urls() as data:
118
 
            del data['remote-test']
119
 
 
120
 
        with hooks.status_urls() as data:
121
 
            self.assertEqual({}, data)
122