~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
#!/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.
try:
    import ConfigParser as _conf_parser
except ImportError:
    import configparser as _conf_parser
import os
import tempfile
import unittest

from charmhelpers.core import templating
import mock
import pkg_resources
import yaml

TEMPLATES_DIR = pkg_resources.resource_filename(__name__, '../templates')


class TestTemplates(unittest.TestCase):
    def setUp(self):
        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']
        self.midonet = ['midonet_api_host', 8080]
        self.context = {
            'neutron_agents': [{
                '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]}],
            'config': {
                'shared_secret': '',
            }
        }

        self.charm_dir = pkg_resources.resource_filename(__name__, '')
        self._charm_dir_patch = mock.patch.object(templating.hookenv,
                                                  'charm_dir')
        self._charm_dir_mock = self._charm_dir_patch.start()
        self._charm_dir_mock.side_effect = lambda: self.charm_dir

    def tearDown(self):
        self._charm_dir_patch.stop()

    @mock.patch.object(templating.host.os, 'fchown')
    @mock.patch.object(templating.host, 'mkdir')
    @mock.patch.object(templating.host, 'log')
    def test_render_metadata_agent(self, log, mkdir, fchown):
        _, temp_path = tempfile.mkstemp()
        try:
            templating.render('metadata_agent.ini', temp_path,
                              self.context, templates_dir=TEMPLATES_DIR)
            config = _conf_parser.ConfigParser()
            with open(temp_path) as rendered_file:
                config.readfp(rendered_file)

            self.assertEqual(config.get('DEFAULT', 'auth_region'),
                             self.nova[11]),
            self.assertEqual(config.get('DEFAULT', 'admin_tenant_name'),
                             self.nova[10]),
            self.assertEqual(config.get('DEFAULT', 'admin_user'),
                             self.nova[5]),
            self.assertEqual(config.get('DEFAULT', 'admin_password'),
                             self.nova[6]),
            self.assertEqual(config.get('DEFAULT', 'nova_metadata_ip'),
                             self.nova[15]),
            self.assertEqual(config.get('DEFAULT',
                                        'metadata_proxy_shared_secret'),
                             self.context['config']['shared_secret']),
        finally:
            if os.path.exists(temp_path):
                os.remove(temp_path)