summaryrefslogtreecommitdiff
path: root/designate/tests/unit/network_api/test_neutron.py
blob: 14979005932316692ec8143e3aa168683b005e70 (plain)
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
# Copyright 2013 Hewlett-Packard Development Company, L.P.
#
# Author: Endre Karlson <endre.karlson@hpe.com>
#
# 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 mock
from neutronclient.common import exceptions as neutron_exceptions
from neutronclient.v2_0 import client as clientv20
from oslo_config import cfg
from oslo_config import fixture as cfg_fixture
import oslotest.base

from designate import context
from designate import exceptions
from designate.network_api import get_network_api
from designate.network_api import neutron

CONF = cfg.CONF


class NeutronNetworkAPITest(oslotest.base.BaseTestCase):
    def setUp(self):
        super(NeutronNetworkAPITest, self).setUp()
        self.useFixture(cfg_fixture.Config(CONF))

        CONF.set_override(
            'endpoints', ['RegionOne|http://localhost:9696'],
            'network_api:neutron'
        )

        self.api = get_network_api('neutron')
        self.context = context.DesignateContext(
            user_id='12345', project_id='54321',
        )

    @mock.patch.object(clientv20, 'Client')
    def test_get_client(self, mock_client):
        neutron.get_client(self.context, 'http://localhost:9696')

        kwargs = mock_client.call_args.kwargs

        self.assertIn('endpoint_url', kwargs)
        self.assertIn('timeout', kwargs)
        self.assertIn('insecure', kwargs)
        self.assertIn('ca_cert', kwargs)

        self.assertNotIn('token', kwargs)
        self.assertNotIn('username', kwargs)

        self.assertEqual('http://localhost:9696', kwargs['endpoint_url'])

    @mock.patch.object(clientv20, 'Client')
    def test_get_client_using_token(self, mock_client):
        self.context = context.DesignateContext(
            user_id='12345', project_id='54321', auth_token='token',
        )

        neutron.get_client(self.context, 'http://localhost:9696')

        kwargs = mock_client.call_args.kwargs

        self.assertIn('token', kwargs)
        self.assertIn('auth_strategy', kwargs)
        self.assertNotIn('username', kwargs)

        self.assertEqual('http://localhost:9696', kwargs['endpoint_url'])
        self.assertEqual(kwargs['token'], self.context.auth_token)

    @mock.patch.object(clientv20, 'Client')
    def test_get_client_using_admin(self, mock_client):
        CONF.set_override(
            'admin_username', 'test',
            'network_api:neutron'
        )

        neutron.get_client(self.context, 'http://localhost:9696')

        kwargs = mock_client.call_args.kwargs

        self.assertIn('auth_strategy', kwargs)
        self.assertIn('username', kwargs)
        self.assertIn('project_name', kwargs)
        self.assertIn('password', kwargs)
        self.assertIn('auth_url', kwargs)
        self.assertNotIn('token', kwargs)

        self.assertEqual('http://localhost:9696', kwargs['endpoint_url'])
        self.assertEqual(
            kwargs['username'], CONF['network_api:neutron'].admin_username
        )

    @mock.patch.object(neutron, 'get_client')
    def test_list_floatingips(self, get_client):
        driver = mock.Mock()
        driver.list_floatingips.return_value = {'floatingips': [
            {
                'id': '123',
                'floating_ip_address': '192.168.0.100',
                'region': 'RegionOne'
            },
            {
                'id': '456',
                'floating_ip_address': '192.168.0.200',
                'region': 'RegionOne'
            },
        ]}
        get_client.return_value = driver

        self.assertEqual(2, len(self.api.list_floatingips(self.context)))

    @mock.patch.object(neutron, 'get_client')
    def test_list_floatingips_unauthorized(self, get_client):
        driver = mock.Mock()
        driver.list_floatingips.side_effect = neutron_exceptions.Unauthorized
        get_client.return_value = driver

        self.assertEqual(0, len(self.api.list_floatingips(self.context)))

    @mock.patch.object(neutron, 'get_client')
    def test_list_floatingips_communication_failure(self, get_client):
        driver = mock.Mock()
        driver.list_floatingips.side_effect = (
            neutron_exceptions.NeutronException
        )
        get_client.return_value = driver

        self.assertRaises(
            exceptions.NeutronCommunicationFailure,
            self.api.list_floatingips, self.context
        )