1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
3
# Copyright 2010-2011 ????
6
# Licensed under the Apache License, Version 2.0 (the "License"); you may
7
# not use this file except in compliance with the License. You may obtain
8
# a copy of the License at
10
# http://www.apache.org/licenses/LICENSE-2.0
12
# Unless required by applicable law or agreed to in writing, software
13
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
# License for the specific language governing permissions and limitations
17
# @author: Salvatore Orlando, Citrix Systems
19
""" Module containing unit tests for Quantum
20
command line interface
29
from quantum import api as server
30
from quantum.client import cli_lib as cli
31
from quantum.client import Client
32
from quantum.db import api as db
33
from quantum.tests.unit.client_tools import stubs as client_stubs
35
LOG = logging.getLogger('quantum.tests.test_cli')
39
class CLITest(unittest.TestCase):
42
"""Prepare the test environment"""
44
options['plugin_provider'] = \
45
'quantum.plugins.sample.SamplePlugin.FakePlugin'
46
#TODO: make the version of the API router configurable
47
self.api = server.APIRouterV11(options)
49
self.tenant_id = "test_tenant"
50
self.network_name_1 = "test_network_1"
51
self.network_name_2 = "test_network_2"
52
# Prepare client and plugin manager
53
self.client = Client(tenant=self.tenant_id, format=FORMAT,
54
testingStub=client_stubs.FakeHTTPConnection)
56
self.fake_stdout = client_stubs.FakeStdout()
57
sys.stdout = self.fake_stdout
60
"""Clear the test environment"""
62
sys.stdout = sys.__stdout__
64
def _verify_list_networks(self):
65
# Verification - get raw result from db
66
nw_list = db.network_list(self.tenant_id)
67
networks = [dict(id=nw.uuid, name=nw.name) for nw in nw_list]
69
output = cli.prepare_output('list_nets', self.tenant_id,
70
dict(networks=networks))
72
# Must add newline at the end to match effect of print call
73
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
75
def _verify_create_network(self):
76
# Verification - get raw result from db
77
nw_list = db.network_list(self.tenant_id)
79
self.fail("No network created")
80
network_id = nw_list[0].uuid
82
output = cli.prepare_output('create_net', self.tenant_id,
83
dict(network_id=network_id))
85
# Must add newline at the end to match effect of print call
86
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
88
def _verify_delete_network(self, network_id):
89
# Verification - get raw result from db
90
nw_list = db.network_list(self.tenant_id)
92
self.fail("DB should not contain any network")
94
output = cli.prepare_output('delete_net', self.tenant_id,
95
dict(network_id=network_id))
97
# Must add newline at the end to match effect of print call
98
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
100
def _verify_update_network(self):
101
# Verification - get raw result from db
102
nw_list = db.network_list(self.tenant_id)
103
network_data = {'id': nw_list[0].uuid,
104
'name': nw_list[0].name}
106
output = cli.prepare_output('update_net', self.tenant_id,
107
dict(network=network_data))
109
# Must add newline at the end to match effect of print call
110
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
112
def _verify_show_network(self):
113
# Verification - get raw result from db
114
nw = db.network_list(self.tenant_id)[0]
115
network = dict(id=nw.uuid, name=nw.name)
117
output = cli.prepare_output('show_net', self.tenant_id,
118
dict(network=network))
120
# Must add newline at the end to match effect of print call
121
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
123
def _verify_list_ports(self, network_id):
124
# Verification - get raw result from db
125
port_list = db.port_list(network_id)
126
ports = [dict(id=port.uuid, state=port.state)
127
for port in port_list]
129
output = cli.prepare_output('list_ports', self.tenant_id,
130
dict(network_id=network_id,
133
# Must add newline at the end to match effect of print call
134
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
136
def _verify_create_port(self, network_id):
137
# Verification - get raw result from db
138
port_list = db.port_list(network_id)
139
if len(port_list) != 1:
140
self.fail("No port created")
141
port_id = port_list[0].uuid
143
output = cli.prepare_output('create_port', self.tenant_id,
144
dict(network_id=network_id,
147
# Must add newline at the end to match effect of print call
148
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
150
def _verify_delete_port(self, network_id, port_id):
151
# Verification - get raw result from db
152
port_list = db.port_list(network_id)
153
if len(port_list) != 0:
154
self.fail("DB should not contain any port")
156
output = cli.prepare_output('delete_port', self.tenant_id,
157
dict(network_id=network_id,
160
# Must add newline at the end to match effect of print call
161
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
163
def _verify_update_port(self, network_id, port_id):
164
# Verification - get raw result from db
165
port = db.port_get(port_id, network_id)
166
port_data = {'id': port.uuid, 'state': port.state}
168
output = cli.prepare_output('update_port', self.tenant_id,
169
dict(network_id=network_id,
172
# Must add newline at the end to match effect of print call
173
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
175
def _verify_show_port(self, network_id, port_id):
176
# Verification - get raw result from db
177
# TODO(salvatore-orlando): Must resolve this issue with
178
# attachment in separate bug fix.
179
port = db.port_get(port_id, network_id)
180
port_data = {'id': port.uuid, 'state': port.state,
181
'attachment': "<none>"}
182
if port.interface_id is not None:
183
port_data['attachment'] = port.interface_id
186
output = cli.prepare_output('show_port', self.tenant_id,
187
dict(network_id=network_id,
190
# Must add newline at the end to match effect of print call
191
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
193
def _verify_plug_iface(self, network_id, port_id):
194
# Verification - get raw result from db
195
port = db.port_get(port_id, network_id)
197
output = cli.prepare_output("plug_iface", self.tenant_id,
198
dict(network_id=network_id,
199
port_id=port['uuid'],
200
attachment=port['interface_id']))
202
# Must add newline at the end to match effect of print call
203
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
205
def _verify_unplug_iface(self, network_id, port_id):
206
# Verification - get raw result from db
207
port = db.port_get(port_id, network_id)
209
output = cli.prepare_output("unplug_iface", self.tenant_id,
210
dict(network_id=network_id,
211
port_id=port['uuid']))
213
# Must add newline at the end to match effect of print call
214
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
216
def test_list_networks(self):
218
# Pre-populate data for testing using db api
219
db.network_create(self.tenant_id, self.network_name_1)
220
db.network_create(self.tenant_id, self.network_name_2)
222
cli.list_nets(self.client, self.tenant_id)
224
LOG.exception("Exception caught: %s", sys.exc_info())
225
self.fail("test_list_networks failed due to an exception")
227
LOG.debug("Operation completed. Verifying result")
228
LOG.debug(self.fake_stdout.content)
229
self._verify_list_networks()
231
def test_create_network(self):
233
cli.create_net(self.client, self.tenant_id, "test")
235
LOG.exception("Exception caught: %s", sys.exc_info())
236
self.fail("test_create_network failed due to an exception")
238
LOG.debug("Operation completed. Verifying result")
239
LOG.debug(self.fake_stdout.content)
240
self._verify_create_network()
242
def test_delete_network(self):
244
db.network_create(self.tenant_id, self.network_name_1)
245
network_id = db.network_list(self.tenant_id)[0]['uuid']
246
cli.delete_net(self.client, self.tenant_id, network_id)
248
LOG.exception("Exception caught: %s", sys.exc_info())
249
self.fail("test_delete_network failed due to an exception")
251
LOG.debug("Operation completed. Verifying result")
252
LOG.debug(self.fake_stdout.content)
253
self._verify_delete_network(network_id)
255
def test_show_network(self):
257
# Load some data into the datbase
258
net = db.network_create(self.tenant_id, self.network_name_1)
259
cli.show_net(self.client, self.tenant_id, net['uuid'])
261
LOG.exception("Exception caught: %s", sys.exc_info())
262
self.fail("test_detail_network failed due to an exception")
264
LOG.debug("Operation completed. Verifying result")
265
LOG.debug(self.fake_stdout.content)
266
self._verify_show_network()
268
def test_update_network(self):
270
net = db.network_create(self.tenant_id, self.network_name_1)
271
network_id = net['uuid']
272
cli.update_net(self.client, self.tenant_id,
273
network_id, 'name=%s' % self.network_name_2)
275
LOG.exception("Exception caught: %s", sys.exc_info())
276
self.fail("test_update_network failed due to an exception")
278
LOG.debug("Operation completed. Verifying result")
279
LOG.debug(self.fake_stdout.content)
280
self._verify_update_network()
282
def test_list_ports(self):
284
# Pre-populate data for testing using db api
285
net = db.network_create(self.tenant_id, self.network_name_1)
286
network_id = net['uuid']
287
db.port_create(network_id)
288
db.port_create(network_id)
289
cli.list_ports(self.client, self.tenant_id, network_id)
291
LOG.exception("Exception caught: %s", sys.exc_info())
292
self.fail("test_list_ports failed due to an exception")
294
LOG.debug("Operation completed. Verifying result")
295
LOG.debug(self.fake_stdout.content)
296
self._verify_list_ports(network_id)
298
def test_create_port(self):
301
# Pre-populate data for testing using db api
302
net = db.network_create(self.tenant_id, self.network_name_1)
303
network_id = net['uuid']
304
cli.create_port(self.client, self.tenant_id, network_id)
306
LOG.exception("Exception caught: %s", sys.exc_info())
307
self.fail("test_create_port failed due to an exception")
309
LOG.debug("Operation completed. Verifying result")
310
LOG.debug(self.fake_stdout.content)
311
self._verify_create_port(network_id)
313
def test_delete_port(self):
317
# Pre-populate data for testing using db api
318
net = db.network_create(self.tenant_id, self.network_name_1)
319
network_id = net['uuid']
320
port = db.port_create(network_id)
321
port_id = port['uuid']
322
cli.delete_port(self.client, self.tenant_id, network_id, port_id)
324
LOG.exception("Exception caught: %s", sys.exc_info())
325
self.fail("test_delete_port failed due to an exception")
327
LOG.debug("Operation completed. Verifying result")
328
LOG.debug(self.fake_stdout.content)
329
self._verify_delete_port(network_id, port_id)
331
def test_update_port(self):
333
net = db.network_create(self.tenant_id, self.network_name_1)
334
network_id = net['uuid']
335
port = db.port_create(network_id)
336
port_id = port['uuid']
337
# Default state is DOWN - change to ACTIVE.
338
cli.update_port(self.client, self.tenant_id, network_id,
339
port_id, 'state=ACTIVE')
341
LOG.exception("Exception caught: %s", sys.exc_info())
342
self.fail("test_update_port failed due to an exception")
344
LOG.debug("Operation completed. Verifying result")
345
LOG.debug(self.fake_stdout.content)
346
self._verify_update_port(network_id, port_id)
348
def test_show_port_no_attach(self):
352
# Pre-populate data for testing using db api
353
net = db.network_create(self.tenant_id, self.network_name_1)
354
network_id = net['uuid']
355
port = db.port_create(network_id)
356
port_id = port['uuid']
357
cli.show_port(self.client, self.tenant_id, network_id, port_id)
359
LOG.exception("Exception caught: %s", sys.exc_info())
360
self.fail("test_show_port_no_attach failed due to an exception")
362
LOG.debug("Operation completed. Verifying result")
363
LOG.debug(self.fake_stdout.content)
364
self._verify_show_port(network_id, port_id)
366
def test_show_port_with_attach(self):
369
iface_id = "flavor crystals"
371
# Pre-populate data for testing using db api
372
net = db.network_create(self.tenant_id, self.network_name_1)
373
network_id = net['uuid']
374
port = db.port_create(network_id)
375
port_id = port['uuid']
376
db.port_set_attachment(port_id, network_id, iface_id)
377
cli.show_port(self.client, self.tenant_id, network_id, port_id)
379
LOG.exception("Exception caught: %s", sys.exc_info())
380
self.fail("test_show_port_with_attach failed due to an exception")
382
LOG.debug("Operation completed. Verifying result")
383
LOG.debug(self.fake_stdout.content)
384
self._verify_show_port(network_id, port_id)
386
def test_plug_iface(self):
390
# Load some data into the datbase
391
net = db.network_create(self.tenant_id, self.network_name_1)
392
network_id = net['uuid']
393
port = db.port_create(net['uuid'])
394
port_id = port['uuid']
395
cli.plug_iface(self.client, self.tenant_id, network_id,
396
port_id, "test_iface_id")
398
LOG.exception("Exception caught: %s", sys.exc_info())
399
self.fail("test_plug_iface failed due to an exception")
401
LOG.debug("Operation completed. Verifying result")
402
LOG.debug(self.fake_stdout.content)
403
self._verify_plug_iface(network_id, port_id)
405
def test_unplug_iface(self):
409
# Load some data into the datbase
410
net = db.network_create(self.tenant_id, self.network_name_1)
411
network_id = net['uuid']
412
port = db.port_create(net['uuid'])
413
port_id = port['uuid']
414
db.port_set_attachment(port_id, network_id, "test_iface_id")
415
cli.unplug_iface(self.client, self.tenant_id, network_id, port_id)
417
LOG.exception("Exception caught: %s", sys.exc_info())
418
self.fail("test_plug_iface failed due to an exception")
420
LOG.debug("Operation completed. Verifying result")
421
LOG.debug(self.fake_stdout.content)
422
self._verify_unplug_iface(network_id, port_id)