~vila/uci-engine/enable-nova-and-swift

« back to all changes in this revision

Viewing changes to charms/precise/lander/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
 
 
27
HTTP_REL = {
 
28
    'hostname': 'remote.com',
 
29
    'port': '8080',
 
30
}
 
31
 
 
32
 
 
33
INFO_REL = {
 
34
    'private-address': '10.0.0.1',
 
35
}
 
36
 
 
37
 
 
38
PATCHES = [
 
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',
 
50
]
 
51
 
 
52
 
 
53
class JujuTestCase(unittest.TestCase):
 
54
    def setUp(self):
 
55
        """Setup a testing hook environment."""
 
56
        super(JujuTestCase, self).setUp()
 
57
        self.tmpdir = tempfile.mkdtemp()
 
58
        self.addCleanup(shutil.rmtree, self.tmpdir)
 
59
 
 
60
        for spec in PATCHES:
 
61
            attr_name = spec.split('.')[-1]
 
62
            _m = mock.patch(spec)
 
63
            setattr(self, attr_name, _m.start())
 
64
            self.addCleanup(_m.stop)
 
65
 
 
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'
 
71
 
 
72
        self._set_default_config()
 
73
 
 
74
    def _set_default_config(self):
 
75
        defaults = {}
 
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
 
84
 
 
85
 
 
86
class TestLanderHook(JujuTestCase):
 
87
    def setUp(self):
 
88
        super(TestLanderHook, self).setUp()
 
89
        p = os.path.join(self.tmpdir, self.local_unit.return_value)
 
90
        os.mkdir(p)
 
91
        self.relations = os.path.join(p, 'juju-relations')
 
92
 
 
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))
 
98
 
 
99
        hooks.hooks.execute(['hooks/lander-relation-broken'])
 
100
        self.assertFalse(os.path.exists(rfile))
 
101
 
 
102
 
 
103
class TestAmqpHook(JujuTestCase):
 
104
    def setUp(self):
 
105
        super(TestAmqpHook, self).setUp()
 
106
        p = os.path.join(self.tmpdir, self.local_unit.return_value)
 
107
        os.mkdir(p)
 
108
        self.amqp_file = os.path.join(p, 'amqp_config.py')
 
109
        with open(self.amqp_file, 'w') as f:
 
110
            f.write('\n')
 
111
 
 
112
    def test_joined(self):
 
113
        hooks.hooks.execute(['hooks/amqp-relation-joined'])
 
114
        self.assertEqual(1, self.relation_set.call_count)
 
115
 
 
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()
 
122
                if key in required:
 
123
                    required.remove(key)
 
124
        self.assertEqual([], required)
 
125
 
 
126
    def test_broken(self):
 
127
        hooks.hooks.execute(['hooks/amqp-relation-broken'])
 
128
        self.assertFalse(os.path.exists(self.amqp_file))