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 import cli_lib as cli
31
from quantum.client import Client
32
from quantum.db import api as db
33
from 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'] = 'quantum.plugins.SamplePlugin.FakePlugin'
45
self.api = server.APIRouterV01(options)
47
self.tenant_id = "test_tenant"
48
self.network_name_1 = "test_network_1"
49
self.network_name_2 = "test_network_2"
50
# Prepare client and plugin manager
51
self.client = Client(tenant=self.tenant_id, format=FORMAT,
52
testingStub=client_stubs.FakeHTTPConnection)
54
self.fake_stdout = client_stubs.FakeStdout()
55
sys.stdout = self.fake_stdout
58
"""Clear the test environment"""
60
sys.stdout = sys.__stdout__
62
def _verify_list_networks(self):
63
# Verification - get raw result from db
64
nw_list = db.network_list(self.tenant_id)
65
networks = [dict(id=nw.uuid, name=nw.name) for nw in nw_list]
67
output = cli.prepare_output('list_nets', self.tenant_id,
68
dict(networks=networks))
70
# Must add newline at the end to match effect of print call
71
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
73
def _verify_create_network(self):
74
# Verification - get raw result from db
75
nw_list = db.network_list(self.tenant_id)
77
self.fail("No network created")
78
network_id = nw_list[0].uuid
80
output = cli.prepare_output('create_net', self.tenant_id,
81
dict(network_id=network_id))
83
# Must add newline at the end to match effect of print call
84
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
86
def _verify_delete_network(self, network_id):
87
# Verification - get raw result from db
88
nw_list = db.network_list(self.tenant_id)
90
self.fail("DB should not contain any network")
92
output = cli.prepare_output('delete_net', self.tenant_id,
93
dict(network_id=network_id))
95
# Must add newline at the end to match effect of print call
96
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
98
def _verify_rename_network(self):
99
# Verification - get raw result from db
100
nw_list = db.network_list(self.tenant_id)
101
network_data = {'id': nw_list[0].uuid,
102
'name': nw_list[0].name}
104
output = cli.prepare_output('rename_net', self.tenant_id,
105
dict(network=network_data))
107
# Must add newline at the end to match effect of print call
108
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
110
def _verify_show_network(self):
111
# Verification - get raw result from db
112
nw = db.network_list(self.tenant_id)[0]
113
network = dict(id=nw.uuid, name=nw.name)
115
output = cli.prepare_output('show_net', self.tenant_id,
116
dict(network=network))
118
# Must add newline at the end to match effect of print call
119
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
121
def _verify_list_ports(self, network_id):
122
# Verification - get raw result from db
123
port_list = db.port_list(network_id)
124
ports = [dict(id=port.uuid, state=port.state)
125
for port in port_list]
127
output = cli.prepare_output('list_ports', self.tenant_id,
128
dict(network_id=network_id,
131
# Must add newline at the end to match effect of print call
132
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
134
def _verify_create_port(self, network_id):
135
# Verification - get raw result from db
136
port_list = db.port_list(network_id)
137
if len(port_list) != 1:
138
self.fail("No port created")
139
port_id = port_list[0].uuid
141
output = cli.prepare_output('create_port', self.tenant_id,
142
dict(network_id=network_id,
145
# Must add newline at the end to match effect of print call
146
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
148
def _verify_delete_port(self, network_id, port_id):
149
# Verification - get raw result from db
150
port_list = db.port_list(network_id)
151
if len(port_list) != 0:
152
self.fail("DB should not contain any port")
154
output = cli.prepare_output('delete_port', self.tenant_id,
155
dict(network_id=network_id,
158
# Must add newline at the end to match effect of print call
159
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
161
def _verify_set_port_state(self, network_id, port_id):
162
# Verification - get raw result from db
163
port = db.port_get(port_id, network_id)
164
port_data = {'id': port.uuid, 'state': port.state}
166
output = cli.prepare_output('set_port_state', self.tenant_id,
167
dict(network_id=network_id,
170
# Must add newline at the end to match effect of print call
171
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
173
def _verify_show_port(self, network_id, port_id):
174
# Verification - get raw result from db
175
# TODO(salvatore-orlando): Must resolve this issue with
176
# attachment in separate bug fix.
177
port = db.port_get(port_id, network_id)
178
port_data = {'id': port.uuid, 'state': port.state,
179
'attachment': "<none>"}
180
if port.interface_id is not None:
181
port_data['attachment'] = port.interface_id
184
output = cli.prepare_output('show_port', self.tenant_id,
185
dict(network_id=network_id,
188
# Must add newline at the end to match effect of print call
189
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
191
def _verify_plug_iface(self, network_id, port_id):
192
# Verification - get raw result from db
193
port = db.port_get(port_id, network_id)
195
output = cli.prepare_output("plug_iface", self.tenant_id,
196
dict(network_id=network_id,
197
port_id=port['uuid'],
198
attachment=port['interface_id']))
200
# Must add newline at the end to match effect of print call
201
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
203
def _verify_unplug_iface(self, network_id, port_id):
204
# Verification - get raw result from db
205
port = db.port_get(port_id, network_id)
207
output = cli.prepare_output("unplug_iface", self.tenant_id,
208
dict(network_id=network_id,
209
port_id=port['uuid']))
211
# Must add newline at the end to match effect of print call
212
self.assertEquals(self.fake_stdout.make_string(), output + '\n')
214
def test_list_networks(self):
216
# Pre-populate data for testing using db api
217
db.network_create(self.tenant_id, self.network_name_1)
218
db.network_create(self.tenant_id, self.network_name_2)
220
cli.list_nets(self.client, self.tenant_id)
222
LOG.exception("Exception caught: %s", sys.exc_info())
223
self.fail("test_list_networks failed due to an exception")
225
LOG.debug("Operation completed. Verifying result")
226
LOG.debug(self.fake_stdout.content)
227
self._verify_list_networks()
229
def test_create_network(self):
231
cli.create_net(self.client, self.tenant_id, "test")
233
LOG.exception("Exception caught: %s", sys.exc_info())
234
self.fail("test_create_network failed due to an exception")
236
LOG.debug("Operation completed. Verifying result")
237
LOG.debug(self.fake_stdout.content)
238
self._verify_create_network()
240
def test_delete_network(self):
242
db.network_create(self.tenant_id, self.network_name_1)
243
network_id = db.network_list(self.tenant_id)[0]['uuid']
244
cli.delete_net(self.client, self.tenant_id, network_id)
246
LOG.exception("Exception caught: %s", sys.exc_info())
247
self.fail("test_delete_network failed due to an exception")
249
LOG.debug("Operation completed. Verifying result")
250
LOG.debug(self.fake_stdout.content)
251
self._verify_delete_network(network_id)
253
def test_show_network(self):
255
# Load some data into the datbase
256
net = db.network_create(self.tenant_id, self.network_name_1)
257
cli.show_net(self.client, self.tenant_id, net['uuid'])
259
LOG.exception("Exception caught: %s", sys.exc_info())
260
self.fail("test_detail_network failed due to an exception")
262
LOG.debug("Operation completed. Verifying result")
263
LOG.debug(self.fake_stdout.content)
264
self._verify_show_network()
266
def test_rename_network(self):
268
net = db.network_create(self.tenant_id, self.network_name_1)
269
network_id = net['uuid']
270
cli.rename_net(self.client, self.tenant_id,
271
network_id, self.network_name_2)
273
LOG.exception("Exception caught: %s", sys.exc_info())
274
self.fail("test_rename_network failed due to an exception")
276
LOG.debug("Operation completed. Verifying result")
277
LOG.debug(self.fake_stdout.content)
278
self._verify_rename_network()
280
def test_list_ports(self):
282
# Pre-populate data for testing using db api
283
net = db.network_create(self.tenant_id, self.network_name_1)
284
network_id = net['uuid']
285
db.port_create(network_id)
286
db.port_create(network_id)
287
cli.list_ports(self.client, self.tenant_id, network_id)
289
LOG.exception("Exception caught: %s", sys.exc_info())
290
self.fail("test_list_ports failed due to an exception")
292
LOG.debug("Operation completed. Verifying result")
293
LOG.debug(self.fake_stdout.content)
294
self._verify_list_ports(network_id)
296
def test_create_port(self):
299
# Pre-populate data for testing using db api
300
net = db.network_create(self.tenant_id, self.network_name_1)
301
network_id = net['uuid']
302
cli.create_port(self.client, self.tenant_id, network_id)
304
LOG.exception("Exception caught: %s", sys.exc_info())
305
self.fail("test_create_port failed due to an exception")
307
LOG.debug("Operation completed. Verifying result")
308
LOG.debug(self.fake_stdout.content)
309
self._verify_create_port(network_id)
311
def test_delete_port(self):
315
# Pre-populate data for testing using db api
316
net = db.network_create(self.tenant_id, self.network_name_1)
317
network_id = net['uuid']
318
port = db.port_create(network_id)
319
port_id = port['uuid']
320
cli.delete_port(self.client, self.tenant_id, network_id, port_id)
322
LOG.exception("Exception caught: %s", sys.exc_info())
323
self.fail("test_delete_port failed due to an exception")
325
LOG.debug("Operation completed. Verifying result")
326
LOG.debug(self.fake_stdout.content)
327
self._verify_delete_port(network_id, port_id)
329
def test_set_port_state(self):
331
net = db.network_create(self.tenant_id, self.network_name_1)
332
network_id = net['uuid']
333
port = db.port_create(network_id)
334
port_id = port['uuid']
335
# Default state is DOWN - change to ACTIVE.
336
cli.set_port_state(self.client, self.tenant_id, network_id,
339
LOG.exception("Exception caught: %s", sys.exc_info())
340
self.fail("test_set_port_state failed due to an exception")
342
LOG.debug("Operation completed. Verifying result")
343
LOG.debug(self.fake_stdout.content)
344
self._verify_set_port_state(network_id, port_id)
346
def test_show_port_no_attach(self):
350
# Pre-populate data for testing using db api
351
net = db.network_create(self.tenant_id, self.network_name_1)
352
network_id = net['uuid']
353
port = db.port_create(network_id)
354
port_id = port['uuid']
355
cli.show_port(self.client, self.tenant_id, network_id, port_id)
357
LOG.exception("Exception caught: %s", sys.exc_info())
358
self.fail("test_show_port_no_attach failed due to an exception")
360
LOG.debug("Operation completed. Verifying result")
361
LOG.debug(self.fake_stdout.content)
362
self._verify_show_port(network_id, port_id)
364
def test_show_port_with_attach(self):
367
iface_id = "flavor crystals"
369
# Pre-populate data for testing using db api
370
net = db.network_create(self.tenant_id, self.network_name_1)
371
network_id = net['uuid']
372
port = db.port_create(network_id)
373
port_id = port['uuid']
374
db.port_set_attachment(port_id, network_id, iface_id)
375
cli.show_port(self.client, self.tenant_id, network_id, port_id)
377
LOG.exception("Exception caught: %s", sys.exc_info())
378
self.fail("test_show_port_with_attach failed due to an exception")
380
LOG.debug("Operation completed. Verifying result")
381
LOG.debug(self.fake_stdout.content)
382
self._verify_show_port(network_id, port_id)
384
def test_plug_iface(self):
388
# Load some data into the datbase
389
net = db.network_create(self.tenant_id, self.network_name_1)
390
network_id = net['uuid']
391
port = db.port_create(net['uuid'])
392
port_id = port['uuid']
393
cli.plug_iface(self.client, self.tenant_id, network_id,
394
port_id, "test_iface_id")
396
LOG.exception("Exception caught: %s", sys.exc_info())
397
self.fail("test_plug_iface failed due to an exception")
399
LOG.debug("Operation completed. Verifying result")
400
LOG.debug(self.fake_stdout.content)
401
self._verify_plug_iface(network_id, port_id)
403
def test_unplug_iface(self):
407
# Load some data into the datbase
408
net = db.network_create(self.tenant_id, self.network_name_1)
409
network_id = net['uuid']
410
port = db.port_create(net['uuid'])
411
port_id = port['uuid']
412
db.port_set_attachment(port_id, network_id, "test_iface_id")
413
cli.unplug_iface(self.client, self.tenant_id, network_id, port_id)
415
LOG.exception("Exception caught: %s", sys.exc_info())
416
self.fail("test_plug_iface failed due to an exception")
418
LOG.debug("Operation completed. Verifying result")
419
LOG.debug(self.fake_stdout.content)
420
self._verify_unplug_iface(network_id, port_id)