1
# Copyright 2014 OneConvergence, Inc. All Rights Reserved.
3
# Licensed under the Apache License, Version 2.0 (the "License"); you may
4
# not use this file except in compliance with the License. You may obtain
5
# a copy of the License at
7
# http://www.apache.org/licenses/LICENSE-2.0
9
# Unless required by applicable law or agreed to in writing, software
10
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12
# License for the specific language governing permissions and limitations
17
from oslo_serialization import jsonutils
19
from neutron.plugins.oneconvergence.lib import nvsdlib
20
from neutron.tests import base
22
NETWORKS_URI = "/pluginhandler/ocplugin/tenant/%s/lnetwork/"
23
NETWORK_URI = NETWORKS_URI + "%s"
24
GET_ALL_NETWORKS = "/pluginhandler/ocplugin/tenant/getallnetworks"
26
SUBNETS_URI = NETWORK_URI + "/lsubnet/"
27
SUBNET_URI = SUBNETS_URI + "%s"
28
GET_ALL_SUBNETS = "/pluginhandler/ocplugin/tenant/getallsubnets"
30
PORTS_URI = NETWORK_URI + "/lport/"
31
PORT_URI = PORTS_URI + "%s"
33
EXT_URI = "/pluginhandler/ocplugin/ext/tenant/%s"
34
FLOATING_IPS_URI = EXT_URI + "/floatingip/"
35
FLOATING_IP_URI = FLOATING_IPS_URI + "%s"
37
ROUTERS_URI = EXT_URI + "/lrouter/"
38
ROUTER_URI = ROUTERS_URI + "%s"
40
TEST_NET = 'test-network'
41
TEST_SUBNET = 'test-subnet'
42
TEST_PORT = 'test-port'
43
TEST_FIP = 'test-floatingip'
44
TEST_ROUTER = 'test-router'
45
TEST_TENANT = 'test-tenant'
48
class TestNVSDApi(base.BaseTestCase):
51
super(TestNVSDApi, self).setUp()
52
self.nvsdlib = nvsdlib.NVSDApi()
54
def test_create_network(self):
57
"tenant_id": TEST_TENANT,
59
"admin_state_up": True,
60
"router:external": False
63
resp.json.return_value = {'id': 'uuid'}
64
with mock.patch.object(self.nvsdlib, 'send_request',
65
return_value=resp) as send_request:
66
uri = NETWORKS_URI % TEST_TENANT
67
net = self.nvsdlib.create_network(network_obj)
68
send_request.assert_called_once_with(
70
body=jsonutils.dumps(network_obj),
72
tenant_id=TEST_TENANT)
73
self.assertEqual(net, {'id': 'uuid'})
75
def test_update_network(self):
76
network = {'id': TEST_NET,
77
'tenant_id': TEST_TENANT}
78
update_network = {'name': 'new_name'}
79
uri = NETWORK_URI % (TEST_TENANT, TEST_NET)
80
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
81
self.nvsdlib.update_network(network, update_network)
82
send_request.assert_called_once_with(
83
"PUT", uri, body=jsonutils.dumps(update_network),
84
resource='network', tenant_id=TEST_TENANT,
87
def test_delete_network(self):
88
network = {'id': TEST_NET,
89
'tenant_id': TEST_TENANT}
91
uri = NETWORK_URI % (TEST_TENANT, TEST_NET)
93
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
94
with mock.patch.object(self.nvsdlib, '_get_ports'):
95
self.nvsdlib.delete_network(network)
96
send_request.assert_called_once_with(
97
"DELETE", uri, resource='network',
98
tenant_id=TEST_TENANT, resource_id=TEST_NET)
100
def test_create_port(self):
101
path = PORTS_URI % (TEST_TENANT, TEST_NET)
102
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
103
fixed_ips = [{'ip_address': '10.0.0.2',
104
'subnet_id': TEST_SUBNET}]
109
"device_id": "device_id",
110
"device_owner": "device_owner",
111
"mac_address": "mac_address",
112
"fixed_ips": fixed_ips,
113
"admin_state_up": True,
114
"network_id": TEST_NET,
117
self.nvsdlib.create_port(TEST_TENANT, lport)
118
expected = {"id": TEST_PORT, "name": 'test',
119
"device_id": "device_id",
120
"device_owner": "device_owner",
121
"mac_address": "mac_address",
122
"ip_address": '10.0.0.2',
123
"subnet_id": TEST_SUBNET,
124
"admin_state_up": True,
125
"network_id": TEST_NET,
127
send_request.assert_called_once_with(
129
body=jsonutils.dumps(expected),
131
tenant_id=TEST_TENANT)
133
def test_update_port(self):
134
port = {'id': TEST_PORT,
135
'network_id': TEST_NET}
137
port_update = {'name': 'new-name'}
138
uri = PORT_URI % (TEST_TENANT, TEST_NET, TEST_PORT)
140
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
141
self.nvsdlib.update_port(TEST_TENANT, port, port_update)
142
send_request.assert_called_once_with(
144
body=jsonutils.dumps(port_update),
146
resource_id='test-port',
147
tenant_id=TEST_TENANT)
149
def test_delete_port(self):
150
port = {'network_id': TEST_NET,
151
'tenant_id': TEST_TENANT}
152
uri = PORT_URI % (TEST_TENANT, TEST_NET, TEST_PORT)
154
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
155
self.nvsdlib.delete_port(TEST_PORT, port)
156
send_request.assert_called_once_with("DELETE", uri,
158
tenant_id=TEST_TENANT,
159
resource_id=TEST_PORT)
161
def test_create_subnet(self):
162
subnet = {'id': TEST_SUBNET,
163
'tenant_id': TEST_TENANT,
164
'network_id': TEST_NET}
165
uri = SUBNETS_URI % (TEST_TENANT, TEST_NET)
167
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
168
self.nvsdlib.create_subnet(subnet)
169
send_request.assert_called_once_with("POST", uri,
170
body=jsonutils.dumps(subnet),
172
tenant_id=TEST_TENANT)
174
def test_update_subnet(self):
175
subnet = {'id': TEST_SUBNET,
176
'tenant_id': TEST_TENANT,
177
'network_id': TEST_NET}
178
subnet_update = {'name': 'new-name'}
179
uri = SUBNET_URI % (TEST_TENANT, TEST_NET, TEST_SUBNET)
181
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
182
self.nvsdlib.update_subnet(subnet, subnet_update)
183
send_request.assert_called_once_with(
185
body=jsonutils.dumps(subnet_update), resource='subnet',
186
tenant_id=TEST_TENANT, resource_id=TEST_SUBNET)
188
def test_delete_subnet(self):
189
subnet = {'id': TEST_SUBNET,
190
'tenant_id': TEST_TENANT,
191
'network_id': TEST_NET}
192
uri = SUBNET_URI % (TEST_TENANT, TEST_NET, TEST_SUBNET)
194
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
195
self.nvsdlib.delete_subnet(subnet)
196
send_request.assert_called_once_with("DELETE", uri,
198
tenant_id=TEST_TENANT,
199
resource_id=TEST_SUBNET)
201
def test_create_floatingip(self):
202
floatingip = {'id': TEST_FIP,
203
'tenant_id': TEST_TENANT}
204
uri = FLOATING_IPS_URI % TEST_TENANT
206
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
207
self.nvsdlib.create_floatingip(floatingip)
208
send_request.assert_called_once_with(
210
body=jsonutils.dumps(floatingip),
211
resource='floating_ip',
212
tenant_id=TEST_TENANT)
214
def test_update_floatingip(self):
215
floatingip = {'id': TEST_FIP,
216
'tenant_id': TEST_TENANT}
217
uri = FLOATING_IP_URI % (TEST_TENANT, TEST_FIP)
219
floatingip_update = {'floatingip': {'router_id': TEST_ROUTER}}
220
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
221
self.nvsdlib.update_floatingip(floatingip, floatingip_update)
222
send_request.assert_called_once_with(
224
body=jsonutils.dumps(floatingip_update['floatingip']),
225
resource='floating_ip', tenant_id=TEST_TENANT,
226
resource_id=TEST_FIP)
228
def test_delete_floatingip(self):
229
floatingip = {'id': TEST_FIP,
230
'tenant_id': TEST_TENANT}
231
uri = FLOATING_IP_URI % (TEST_TENANT, TEST_FIP)
233
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
234
self.nvsdlib.delete_floatingip(floatingip)
235
send_request.assert_called_once_with(
236
"DELETE", uri, resource='floating_ip', tenant_id=TEST_TENANT,
237
resource_id=TEST_FIP)
239
def test_create_router(self):
240
router = {'id': TEST_ROUTER, 'tenant_id': TEST_TENANT}
241
uri = ROUTERS_URI % TEST_TENANT
243
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
244
self.nvsdlib.create_router(router)
245
send_request.assert_called_once_with(
246
"POST", uri, body=jsonutils.dumps(router), resource='router',
247
tenant_id=TEST_TENANT)
249
def test_update_router(self):
250
router = {'id': TEST_ROUTER, 'tenant_id': TEST_TENANT}
251
uri = ROUTER_URI % (TEST_TENANT, TEST_ROUTER)
253
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
254
self.nvsdlib.update_router(router)
255
send_request.assert_called_once_with(
256
"PUT", uri, body=jsonutils.dumps(router),
257
resource='router', tenant_id=TEST_TENANT,
258
resource_id=TEST_ROUTER)
260
def test_delete_router(self):
261
uri = ROUTER_URI % (TEST_TENANT, TEST_ROUTER)
263
with mock.patch.object(self.nvsdlib, 'send_request') as send_request:
264
self.nvsdlib.delete_router(TEST_TENANT, TEST_ROUTER)
265
send_request.assert_called_once_with(
266
"DELETE", uri, resource='router',
267
tenant_id=TEST_TENANT, resource_id=TEST_ROUTER)