2012-05-21 16:32:35 -04:00
|
|
|
# Copyright 2010 Jacob Kaplan-Moss
|
|
|
|
|
|
|
|
# Copyright 2011 OpenStack LLC.
|
|
|
|
# All Rights Reserved.
|
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2012-12-24 21:49:34 -06:00
|
|
|
import fixtures
|
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
from cinderclient import client
|
|
|
|
from cinderclient import shell
|
2013-02-05 19:00:26 -05:00
|
|
|
from cinderclient.v1 import shell as shell_v1
|
2013-05-18 09:22:23 -07:00
|
|
|
from cinderclient.tests.v1 import fakes
|
|
|
|
from cinderclient.tests import utils
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
|
|
|
|
class ShellTest(utils.TestCase):
|
|
|
|
|
2012-12-24 21:49:34 -06:00
|
|
|
FAKE_ENV = {
|
|
|
|
'CINDER_USERNAME': 'username',
|
|
|
|
'CINDER_PASSWORD': 'password',
|
|
|
|
'CINDER_PROJECT_ID': 'project_id',
|
2013-02-02 13:26:18 -08:00
|
|
|
'OS_VOLUME_API_VERSION': '1.1',
|
2012-12-24 21:49:34 -06:00
|
|
|
'CINDER_URL': 'http://no.where',
|
|
|
|
}
|
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
# Patch os.environ to avoid required auth info.
|
|
|
|
def setUp(self):
|
|
|
|
"""Run before each test."""
|
2012-12-24 21:49:34 -06:00
|
|
|
super(ShellTest, self).setUp()
|
|
|
|
for var in self.FAKE_ENV:
|
|
|
|
self.useFixture(fixtures.EnvironmentVariable(var,
|
|
|
|
self.FAKE_ENV[var]))
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
self.shell = shell.OpenStackCinderShell()
|
|
|
|
|
|
|
|
#HACK(bcwaldon): replace this when we start using stubs
|
|
|
|
self.old_get_client_class = client.get_client_class
|
|
|
|
client.get_client_class = lambda *_: fakes.FakeClient
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# For some method like test_image_meta_bad_action we are
|
|
|
|
# testing a SystemExit to be thrown and object self.shell has
|
|
|
|
# no time to get instantatiated which is OK in this case, so
|
|
|
|
# we make sure the method is there before launching it.
|
|
|
|
if hasattr(self.shell, 'cs'):
|
|
|
|
self.shell.cs.clear_callstack()
|
|
|
|
|
|
|
|
#HACK(bcwaldon): replace this when we start using stubs
|
|
|
|
client.get_client_class = self.old_get_client_class
|
2012-12-24 21:49:34 -06:00
|
|
|
super(ShellTest, self).tearDown()
|
2012-05-21 16:32:35 -04:00
|
|
|
|
|
|
|
def run_command(self, cmd):
|
|
|
|
self.shell.main(cmd.split())
|
|
|
|
|
|
|
|
def assert_called(self, method, url, body=None, **kwargs):
|
|
|
|
return self.shell.cs.assert_called(method, url, body, **kwargs)
|
|
|
|
|
|
|
|
def assert_called_anytime(self, method, url, body=None):
|
|
|
|
return self.shell.cs.assert_called_anytime(method, url, body)
|
|
|
|
|
2013-02-05 19:00:26 -05:00
|
|
|
def test_extract_metadata(self):
|
|
|
|
# mimic the result of argparse's parse_args() method
|
|
|
|
class Arguments:
|
|
|
|
def __init__(self, metadata=[]):
|
|
|
|
self.metadata = metadata
|
|
|
|
|
|
|
|
inputs = [
|
|
|
|
([], {}),
|
|
|
|
(["key=value"], {"key": "value"}),
|
|
|
|
(["key"], {"key": None}),
|
|
|
|
(["k1=v1", "k2=v2"], {"k1": "v1", "k2": "v2"}),
|
|
|
|
(["k1=v1", "k2"], {"k1": "v1", "k2": None}),
|
|
|
|
(["k1", "k2=v2"], {"k1": None, "k2": "v2"})
|
|
|
|
]
|
|
|
|
|
|
|
|
for input in inputs:
|
|
|
|
args = Arguments(metadata=input[0])
|
|
|
|
self.assertEquals(shell_v1._extract_metadata(args), input[1])
|
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
def test_list(self):
|
|
|
|
self.run_command('list')
|
|
|
|
# NOTE(jdg): we default to detail currently
|
|
|
|
self.assert_called('GET', '/volumes/detail')
|
|
|
|
|
2012-09-05 14:51:57 +00:00
|
|
|
def test_list_filter_status(self):
|
|
|
|
self.run_command('list --status=available')
|
|
|
|
self.assert_called('GET', '/volumes/detail?status=available')
|
|
|
|
|
|
|
|
def test_list_filter_display_name(self):
|
|
|
|
self.run_command('list --display-name=1234')
|
|
|
|
self.assert_called('GET', '/volumes/detail?display_name=1234')
|
|
|
|
|
|
|
|
def test_list_all_tenants(self):
|
|
|
|
self.run_command('list --all-tenants=1')
|
|
|
|
self.assert_called('GET', '/volumes/detail?all_tenants=1')
|
|
|
|
|
2012-05-21 16:32:35 -04:00
|
|
|
def test_show(self):
|
|
|
|
self.run_command('show 1234')
|
|
|
|
self.assert_called('GET', '/volumes/1234')
|
|
|
|
|
|
|
|
def test_delete(self):
|
|
|
|
self.run_command('delete 1234')
|
2012-08-28 20:20:35 +00:00
|
|
|
self.assert_called('DELETE', '/volumes/1234')
|
2012-09-05 14:51:57 +00:00
|
|
|
|
|
|
|
def test_snapshot_list_filter_volume_id(self):
|
|
|
|
self.run_command('snapshot-list --volume-id=1234')
|
|
|
|
self.assert_called('GET', '/snapshots/detail?volume_id=1234')
|
|
|
|
|
|
|
|
def test_snapshot_list_filter_status_and_volume_id(self):
|
|
|
|
self.run_command('snapshot-list --status=available --volume-id=1234')
|
|
|
|
self.assert_called('GET', '/snapshots/detail?'
|
|
|
|
'status=available&volume_id=1234')
|
2012-08-28 20:20:35 +00:00
|
|
|
|
|
|
|
def test_rename(self):
|
|
|
|
# basic rename with positional agruments
|
|
|
|
self.run_command('rename 1234 new-name')
|
|
|
|
expected = {'volume': {'display_name': 'new-name'}}
|
|
|
|
self.assert_called('PUT', '/volumes/1234', body=expected)
|
|
|
|
# change description only
|
|
|
|
self.run_command('rename 1234 --display-description=new-description')
|
|
|
|
expected = {'volume': {'display_description': 'new-description'}}
|
|
|
|
self.assert_called('PUT', '/volumes/1234', body=expected)
|
|
|
|
# rename and change description
|
|
|
|
self.run_command('rename 1234 new-name '
|
|
|
|
'--display-description=new-description')
|
|
|
|
expected = {'volume': {
|
|
|
|
'display_name': 'new-name',
|
|
|
|
'display_description': 'new-description',
|
|
|
|
}}
|
|
|
|
self.assert_called('PUT', '/volumes/1234', body=expected)
|
|
|
|
# noop, the only all will be the lookup
|
|
|
|
self.run_command('rename 1234')
|
|
|
|
self.assert_called('GET', '/volumes/1234')
|
|
|
|
|
|
|
|
def test_rename_snapshot(self):
|
|
|
|
# basic rename with positional agruments
|
|
|
|
self.run_command('snapshot-rename 1234 new-name')
|
|
|
|
expected = {'snapshot': {'display_name': 'new-name'}}
|
|
|
|
self.assert_called('PUT', '/snapshots/1234', body=expected)
|
|
|
|
# change description only
|
|
|
|
self.run_command('snapshot-rename 1234 '
|
|
|
|
'--display-description=new-description')
|
|
|
|
expected = {'snapshot': {'display_description': 'new-description'}}
|
|
|
|
self.assert_called('PUT', '/snapshots/1234', body=expected)
|
|
|
|
# snapshot-rename and change description
|
|
|
|
self.run_command('snapshot-rename 1234 new-name '
|
|
|
|
'--display-description=new-description')
|
|
|
|
expected = {'snapshot': {
|
|
|
|
'display_name': 'new-name',
|
|
|
|
'display_description': 'new-description',
|
|
|
|
}}
|
|
|
|
self.assert_called('PUT', '/snapshots/1234', body=expected)
|
|
|
|
# noop, the only all will be the lookup
|
|
|
|
self.run_command('snapshot-rename 1234')
|
|
|
|
self.assert_called('GET', '/snapshots/1234')
|
2013-01-05 01:12:59 +00:00
|
|
|
|
|
|
|
def test_set_metadata_set(self):
|
|
|
|
self.run_command('metadata 1234 set key1=val1 key2=val2')
|
|
|
|
self.assert_called('POST', '/volumes/1234/metadata',
|
|
|
|
{'metadata': {'key1': 'val1', 'key2': 'val2'}})
|
|
|
|
|
|
|
|
def test_set_metadata_delete_dict(self):
|
|
|
|
self.run_command('metadata 1234 unset key1=val1 key2=val2')
|
|
|
|
self.assert_called('DELETE', '/volumes/1234/metadata/key1')
|
|
|
|
self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2)
|
|
|
|
|
|
|
|
def test_set_metadata_delete_keys(self):
|
|
|
|
self.run_command('metadata 1234 unset key1 key2')
|
|
|
|
self.assert_called('DELETE', '/volumes/1234/metadata/key1')
|
|
|
|
self.assert_called('DELETE', '/volumes/1234/metadata/key2', pos=-2)
|