summaryrefslogtreecommitdiff
path: root/ironic/tests/unit/drivers/modules/network/test_flat.py
blob: 31462c3fb901fb967c0a6f5b08083d08404bcefc (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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#    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 oslo_config import cfg
from oslo_utils import uuidutils

from ironic.common import exception
from ironic.common import neutron
from ironic.conductor import task_manager
from ironic.drivers.modules.network import flat as flat_interface
from ironic.tests.unit.db import base as db_base
from ironic.tests.unit.objects import utils

CONF = cfg.CONF
VIFMIXINPATH = 'ironic.drivers.modules.network.common.NeutronVIFPortIDMixin'


class TestFlatInterface(db_base.DbTestCase):

    def setUp(self):
        super(TestFlatInterface, self).setUp()
        self.interface = flat_interface.FlatNetwork()
        self.node = utils.create_test_node(self.context)
        self.port = utils.create_test_port(
            self.context, node_id=self.node.id,
            internal_info={
                'cleaning_vif_port_id': uuidutils.generate_uuid()})

    @mock.patch('%s.vif_list' % VIFMIXINPATH)
    def test_vif_list(self, mock_vif_list):
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.vif_list(task)
            mock_vif_list.assert_called_once_with(task)

    @mock.patch('%s.vif_attach' % VIFMIXINPATH)
    def test_vif_attach(self, mock_vif_attach):
        vif = mock.MagicMock()
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.vif_attach(task, vif)
            mock_vif_attach.assert_called_once_with(task, vif)

    @mock.patch('%s.vif_detach' % VIFMIXINPATH)
    def test_vif_detach(self, mock_vif_detach):
        vif_id = "vif"
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.vif_detach(task, vif_id)
            mock_vif_detach.assert_called_once_with(task, vif_id)

    @mock.patch('%s.port_changed' % VIFMIXINPATH)
    def test_vif_port_changed(self, mock_p_changed):
        port = mock.MagicMock()
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.port_changed(task, port)
            mock_p_changed.assert_called_once_with(task, port)

    @mock.patch.object(flat_interface, 'LOG')
    def test_init_no_cleaning_network(self, mock_log):
        self.config(cleaning_network=None, group='neutron')
        flat_interface.FlatNetwork()
        self.assertTrue(mock_log.warning.called)

    @mock.patch.object(neutron, 'validate_network', autospec=True)
    def test_validate(self, validate_mock):
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.validate(task)
            validate_mock.assert_called_once_with(
                CONF.neutron.cleaning_network,
                'cleaning network', context=task.context)

    @mock.patch.object(neutron, 'validate_network', autospec=True)
    def test_validate_from_node(self, validate_mock):
        cleaning_network_uuid = '3aea0de6-4b92-44da-9aa0-52d134c83fdf'
        driver_info = self.node.driver_info
        driver_info['cleaning_network'] = cleaning_network_uuid
        self.node.driver_info = driver_info
        self.node.save()
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.validate(task)
        validate_mock.assert_called_once_with(
            cleaning_network_uuid,
            'cleaning network', context=task.context)

    @mock.patch.object(neutron, 'validate_network',
                       side_effect=lambda n, t, context=None: n)
    @mock.patch.object(neutron, 'add_ports_to_network')
    @mock.patch.object(neutron, 'rollback_ports')
    def test_add_cleaning_network(self, rollback_mock, add_mock,
                                  validate_mock):
        add_mock.return_value = {self.port.uuid: 'vif-port-id'}
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.add_cleaning_network(task)
            rollback_mock.assert_called_once_with(
                task, CONF.neutron.cleaning_network)
            add_mock.assert_called_once_with(
                task, CONF.neutron.cleaning_network)
            validate_calls = [
                mock.call(CONF.neutron.cleaning_network, 'cleaning network',
                          context=task.context),
                mock.call(CONF.neutron.cleaning_network, 'cleaning network',
                          context=task.context)
            ]
            validate_mock.assert_has_calls(validate_calls)
        self.port.refresh()
        self.assertEqual('vif-port-id',
                         self.port.internal_info['cleaning_vif_port_id'])

    @mock.patch.object(neutron, 'validate_network',
                       side_effect=lambda n, t, context=None: n)
    @mock.patch.object(neutron, 'add_ports_to_network')
    @mock.patch.object(neutron, 'rollback_ports')
    def test_add_cleaning_network_from_node(self, rollback_mock, add_mock,
                                            validate_mock):
        add_mock.return_value = {self.port.uuid: 'vif-port-id'}
        # Make sure that changing the network UUID works
        for cleaning_network_uuid in ['3aea0de6-4b92-44da-9aa0-52d134c83fdf',
                                      '438be438-6aae-4fb1-bbcb-613ad7a38286']:
            driver_info = self.node.driver_info
            driver_info['cleaning_network'] = cleaning_network_uuid
            self.node.driver_info = driver_info
            self.node.save()
            with task_manager.acquire(self.context, self.node.id) as task:
                self.interface.add_cleaning_network(task)
                rollback_mock.assert_called_with(
                    task, cleaning_network_uuid)
                add_mock.assert_called_with(task, cleaning_network_uuid)
                validate_mock.assert_called_with(
                    cleaning_network_uuid,
                    'cleaning network', context=task.context)
        self.port.refresh()
        self.assertEqual('vif-port-id',
                         self.port.internal_info['cleaning_vif_port_id'])

    @mock.patch.object(neutron, 'validate_network',
                       side_effect=lambda n, t, context=None: n)
    @mock.patch.object(neutron, 'remove_ports_from_network')
    def test_remove_cleaning_network(self, remove_mock, validate_mock):
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.remove_cleaning_network(task)
            remove_mock.assert_called_once_with(
                task, CONF.neutron.cleaning_network)
            validate_mock.assert_called_once_with(
                CONF.neutron.cleaning_network,
                'cleaning network', context=task.context)
        self.port.refresh()
        self.assertNotIn('cleaning_vif_port_id', self.port.internal_info)

    @mock.patch.object(neutron, 'validate_network',
                       side_effect=lambda n, t, context=None: n)
    @mock.patch.object(neutron, 'remove_ports_from_network')
    def test_remove_cleaning_network_from_node(self, remove_mock,
                                               validate_mock):
        cleaning_network_uuid = '3aea0de6-4b92-44da-9aa0-52d134c83fdf'
        driver_info = self.node.driver_info
        driver_info['cleaning_network'] = cleaning_network_uuid
        self.node.driver_info = driver_info
        self.node.save()
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.remove_cleaning_network(task)
            remove_mock.assert_called_once_with(task, cleaning_network_uuid)
            validate_mock.assert_called_once_with(
                cleaning_network_uuid,
                'cleaning network', context=task.context)
        self.port.refresh()
        self.assertNotIn('cleaning_vif_port_id', self.port.internal_info)

    @mock.patch.object(neutron, 'get_client')
    def test_add_provisioning_network_set_binding_host_id(
            self, client_mock):
        upd_mock = mock.Mock()
        client_mock.return_value.update_port = upd_mock
        extra = {'vif_port_id': 'foo'}
        utils.create_test_port(self.context, node_id=self.node.id,
                               address='52:54:00:cf:2d:33', extra=extra,
                               uuid=uuidutils.generate_uuid())
        exp_body = {'port': {'binding:host_id': self.node.uuid,
                             'binding:vnic_type': neutron.VNIC_BAREMETAL}}
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.add_provisioning_network(task)
        upd_mock.assert_called_once_with('foo', exp_body)

    @mock.patch.object(neutron, 'get_client')
    def test_add_provisioning_network_set_binding_host_id_portgroup(
            self, client_mock):
        upd_mock = mock.Mock()
        client_mock.return_value.update_port = upd_mock
        internal_info = {'tenant_vif_port_id': 'foo'}
        utils.create_test_portgroup(
            self.context, node_id=self.node.id, internal_info=internal_info,
            uuid=uuidutils.generate_uuid())
        utils.create_test_port(
            self.context, node_id=self.node.id, address='52:54:00:cf:2d:33',
            extra={'vif_port_id': 'bar'}, uuid=uuidutils.generate_uuid())
        exp_body = {'port': {'binding:host_id': self.node.uuid,
                             'binding:vnic_type': neutron.VNIC_BAREMETAL}}
        with task_manager.acquire(self.context, self.node.id) as task:
            self.interface.add_provisioning_network(task)
        upd_mock.assert_has_calls([
            mock.call('bar', exp_body), mock.call('foo', exp_body)
        ])

    @mock.patch.object(neutron, 'get_client')
    def test_add_provisioning_network_binding_host_id_raise(
            self, client_mock):
        client_mock.return_value.update_port.side_effect = \
            (neutron_exceptions.ConnectionFailed())
        extra = {'vif_port_id': 'foo'}
        utils.create_test_port(self.context, node_id=self.node.id,
                               address='52:54:00:cf:2d:33', extra=extra,
                               uuid=uuidutils.generate_uuid())
        with task_manager.acquire(self.context, self.node.id) as task:
            self.assertRaises(exception.NetworkError,
                              self.interface.add_provisioning_network,
                              task)