~celebdor/charms/trusty/neutron-agents-midonet/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# vim: tabstop=4 shiftwidth=4 softtabstop=4 filetype=python
#
# Copyright (c) 2015 Midokura Europe SARL, All Rights Reserved.
# All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import shutil
import tempfile

import mock

from .midonet_helpers import test

HOOKENV_PATCHES = [
    'charm_dir',
    'config',
    'is_relation_made',
    'log',
    'open_port',
    'relation_get',
    'relation_ids',
    'related_units',
    'relation_set',
    'unit_get']


class HookTests(test.CharmTestCase):
    def __init__(self, *args, **kwargs):
        self.nova = ['ks_host',
                     1111,
                     'http',
                     'http',
                     2222,
                     'me',
                     'mypass',
                     'tennant',
                     'https://ks_host:2222/v2.0',
                     'ks_host',
                     'tennant',
                     'RegionOne',
                     'http://quantum_host:3333',
                     'quantum_host',
                     3333,
                     '10.10.10.11']
        test.CharmTestCase.__init__(self, *args, **kwargs)

    @mock.patch('charmhelpers.fetch.apt_install')
    def setUp(self, apt_install):

        import services

        self.relations = test.FakeRelation(self._full_relation_data())
        self.services = services
        test.CharmTestCase.setUp(self, services.hookenv, HOOKENV_PATCHES)

        def lsb_release():
            ubuntu_trusty = {
                'DISTRIB_ID': 'Ubuntu',
                'DISTRIB_RELEASE': '14.04',
                'DISTRIB_CODENAME': 'trusty',
                'DISTRIB_DESCRIPTION': "Ubuntu 14.04.3 LTS",
            }

            try:
                data = services.host.lsb_release()
            except Exception:
                return ubuntu_trusty
            if data.get('DISTRIB_ID') != 'Ubuntu':
                return ubuntu_trusty
            return data

        def get_os_codename_package(pkg, fatal=True):
            version = 'liberty'
            try:
                ret_vers = services.utils.get_os_codename_package(pkg, fatal)
            except Exception:
                return version
            return ret_vers

        self.patch_all(services.utils,
                       ('get_os_codename_package',
                        'lsb_release',))
        self.lsb_release.side_effect = lsb_release
        self.get_os_codename_package.side_effect = get_os_codename_package

        self.patch_all(services.host, ('service_running',))
        self.service_running.return_value = True

        _templating = mock.patch.object(services.helpers, 'render_template')
        setattr(self, 'render_template', _templating.start())
        self.addCleanup(_templating.stop)

        _check_call = mock.patch.object(services.hookenv.subprocess,
                                        'check_call')
        setattr(self, 'check_call', _check_call.start())
        self.addCleanup(_check_call.stop)

        _service_restart = mock.patch.object(services.base, 'service_restart')
        setattr(self, 'service_restart', _service_restart.start())
        self.addCleanup(_service_restart.stop)

        _service_stop = mock.patch.object(services.base, 'service_stop')
        setattr(self, 'service_stop', _service_stop.start())
        self.addCleanup(_service_stop.stop)

        _relation_helpers = mock.patch.object(
            services.relations.helpers, 'hookenv', spec=[
                'relation_ids',
                'related_units',
                'relation_get'])
        setattr(self, 'relation_helpers', _relation_helpers.start())
        self.addCleanup(_relation_helpers.stop)

        self.test_config.config['openstack-origin'] = 'cloud:trusty-liberty'
        self.config.side_effect = self.test_config.get
        self.relation_get.side_effect = self.test_relation.get

        services.context.config = self.config

        self.charm_dir.return_value = tempfile.mkdtemp()

    def tearDown(self):
        shutil.rmtree(self.charm_dir.return_value)
        test.CharmTestCase.tearDown(self)

    def test_config_change(self):

        self.data = self._full_relation_data()

        self.relation_helpers.relation_ids.side_effect = (
            self.relations.relation_ids)

        import services
        services.context.relation_ids = self.relation_helpers.relation_ids

        self.relation_helpers.related_units.side_effect = (
            self.relations.related_units)
        self.relation_helpers.relation_get.side_effect = (
            self.relations.get)

        self.services.manage()

        self.assertTrue(self.render_template.called)
        self.assertTrue(self.service_restart.called)

    def _full_relation_data(self):
        return {
            'neutron_agents:4': {
                'nova-compute/0': {
                    'auth_host': self.nova[0],
                    'auth_port': self.nova[1],
                    'auth_protocol': self.nova[2],
                    'service_protocol': self.nova[3],
                    'service_port': self.nova[4],
                    'service_username': self.nova[5],
                    'service_password': self.nova[6],
                    'service_tenant_name': self.nova[7],
                    'keystone_host': self.nova[9],
                    'service_tenant': self.nova[10],
                    'region': self.nova[11],
                    'quantum_url': self.nova[12],
                    'quantum_host': self.nova[13],
                    'quantum_port': self.nova[14],
                    'private-address': self.nova[15],
                },
            },
            'midonet:8': {
                'midonet-api/0': {
                    'host': 'midonet_host',
                    'port': 8080,
                },
            },
        }