Merge "Nova aggregate-details should be more human friendly"
This commit is contained in:
commit
49be47367f
@ -197,3 +197,23 @@ class FlattenTestCase(test_utils.TestCase):
|
||||
'a2': ['l'],
|
||||
'a3': ('t',)},
|
||||
squashed)
|
||||
|
||||
def test_pretty_choice_list(self):
|
||||
l = []
|
||||
r = utils.pretty_choice_list(l)
|
||||
self.assertEqual(r, "")
|
||||
|
||||
l = ["v1", "v2", "v3"]
|
||||
r = utils.pretty_choice_list(l)
|
||||
self.assertEqual(r, "'v1', 'v2', 'v3'")
|
||||
|
||||
def test_pretty_choice_dict(self):
|
||||
d = {}
|
||||
r = utils.pretty_choice_dict(d)
|
||||
self.assertEqual(r, "")
|
||||
|
||||
d = {"k1": "v1",
|
||||
"k2": "v2",
|
||||
"k3": "v3"}
|
||||
r = utils.pretty_choice_dict(d)
|
||||
self.assertEqual(r, "'k1=v1', 'k2=v2', 'k3=v3'")
|
||||
|
168
novaclient/tests/v3/test_shell.py
Normal file
168
novaclient/tests/v3/test_shell.py
Normal file
@ -0,0 +1,168 @@
|
||||
# Copyright 2013 Cloudwatt
|
||||
# Copyright 2010 Jacob Kaplan-Moss
|
||||
# Copyright 2011 OpenStack Foundation
|
||||
# Copyright 2012 IBM Corp.
|
||||
# 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.
|
||||
|
||||
import fixtures
|
||||
import mock
|
||||
import six
|
||||
|
||||
from novaclient.openstack.common import timeutils
|
||||
import novaclient.shell
|
||||
from novaclient.tests import utils
|
||||
from novaclient.tests.v3 import fakes
|
||||
|
||||
|
||||
class ShellFixture(fixtures.Fixture):
|
||||
def setUp(self):
|
||||
super(ShellFixture, self).setUp()
|
||||
self.shell = novaclient.shell.OpenStackComputeShell()
|
||||
|
||||
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()
|
||||
super(ShellFixture, self).tearDown()
|
||||
|
||||
|
||||
class ShellTest(utils.TestCase):
|
||||
FAKE_ENV = {
|
||||
'NOVA_USERNAME': 'username',
|
||||
'NOVA_PASSWORD': 'password',
|
||||
'NOVA_PROJECT_ID': 'project_id',
|
||||
'OS_COMPUTE_API_VERSION': '3',
|
||||
'NOVA_URL': 'http://no.where',
|
||||
}
|
||||
|
||||
def setUp(self):
|
||||
"""Run before each test."""
|
||||
super(ShellTest, self).setUp()
|
||||
|
||||
for var in self.FAKE_ENV:
|
||||
self.useFixture(fixtures.EnvironmentVariable(var,
|
||||
self.FAKE_ENV[var]))
|
||||
self.shell = self.useFixture(ShellFixture()).shell
|
||||
|
||||
self.useFixture(fixtures.MonkeyPatch(
|
||||
'novaclient.client.get_client_class',
|
||||
lambda *_: fakes.FakeClient))
|
||||
self.addCleanup(timeutils.clear_time_override)
|
||||
|
||||
@mock.patch('sys.stdout', new_callable=six.StringIO)
|
||||
def run_command(self, cmd, mock_stdout):
|
||||
if isinstance(cmd, list):
|
||||
self.shell.main(cmd)
|
||||
else:
|
||||
self.shell.main(cmd.split())
|
||||
return mock_stdout.getvalue()
|
||||
|
||||
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)
|
||||
|
||||
def test_aggregate_list(self):
|
||||
self.run_command('aggregate-list')
|
||||
self.assert_called('GET', '/os-aggregates')
|
||||
|
||||
def test_aggregate_create(self):
|
||||
self.run_command('aggregate-create test_name nova1')
|
||||
body = {"aggregate": {"name": "test_name",
|
||||
"availability_zone": "nova1"}}
|
||||
self.assert_called('POST', '/os-aggregates', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_delete_by_id(self):
|
||||
self.run_command('aggregate-delete 1')
|
||||
self.assert_called('DELETE', '/os-aggregates/1')
|
||||
|
||||
def test_aggregate_delete_by_name(self):
|
||||
self.run_command('aggregate-delete test')
|
||||
self.assert_called('DELETE', '/os-aggregates/1')
|
||||
|
||||
def test_aggregate_update_by_id(self):
|
||||
self.run_command('aggregate-update 1 new_name')
|
||||
body = {"aggregate": {"name": "new_name"}}
|
||||
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_update_by_name(self):
|
||||
self.run_command('aggregate-update test new_name')
|
||||
body = {"aggregate": {"name": "new_name"}}
|
||||
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_update_with_availability_zone_by_id(self):
|
||||
self.run_command('aggregate-update 1 foo new_zone')
|
||||
body = {"aggregate": {"name": "foo", "availability_zone": "new_zone"}}
|
||||
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_update_with_availability_zone_by_name(self):
|
||||
self.run_command('aggregate-update test foo new_zone')
|
||||
body = {"aggregate": {"name": "foo", "availability_zone": "new_zone"}}
|
||||
self.assert_called('PUT', '/os-aggregates/1', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_set_metadata_by_id(self):
|
||||
self.run_command('aggregate-set-metadata 1 foo=bar delete_key')
|
||||
body = {"set_metadata": {"metadata": {"foo": "bar",
|
||||
"delete_key": None}}}
|
||||
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_set_metadata_by_name(self):
|
||||
self.run_command('aggregate-set-metadata test foo=bar delete_key')
|
||||
body = {"set_metadata": {"metadata": {"foo": "bar",
|
||||
"delete_key": None}}}
|
||||
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_add_host_by_id(self):
|
||||
self.run_command('aggregate-add-host 1 host1')
|
||||
body = {"add_host": {"host": "host1"}}
|
||||
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_add_host_by_name(self):
|
||||
self.run_command('aggregate-add-host test host1')
|
||||
body = {"add_host": {"host": "host1"}}
|
||||
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_remove_host_by_id(self):
|
||||
self.run_command('aggregate-remove-host 1 host1')
|
||||
body = {"remove_host": {"host": "host1"}}
|
||||
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_remove_host_by_name(self):
|
||||
self.run_command('aggregate-remove-host test host1')
|
||||
body = {"remove_host": {"host": "host1"}}
|
||||
self.assert_called('POST', '/os-aggregates/1/action', body, pos=-2)
|
||||
self.assert_called('GET', '/os-aggregates/1', pos=-1)
|
||||
|
||||
def test_aggregate_details_by_id(self):
|
||||
self.run_command('aggregate-details 1')
|
||||
self.assert_called('GET', '/os-aggregates/1')
|
||||
|
||||
def test_aggregate_details_by_name(self):
|
||||
self.run_command('aggregate-details test')
|
||||
self.assert_called('GET', '/os-aggregates')
|
@ -157,6 +157,11 @@ def pretty_choice_list(l):
|
||||
return ', '.join("'%s'" % i for i in l)
|
||||
|
||||
|
||||
def pretty_choice_dict(d):
|
||||
"""Returns a formatted dict as 'key=value'."""
|
||||
return pretty_choice_list(['%s=%s' % (k, d[k]) for k in sorted(d.keys())])
|
||||
|
||||
|
||||
def print_list(objs, fields, formatters={}, sortby_index=None):
|
||||
if sortby_index is None:
|
||||
sortby = None
|
||||
|
@ -2711,7 +2711,18 @@ def do_aggregate_details(cs, args):
|
||||
|
||||
def _print_aggregate_details(aggregate):
|
||||
columns = ['Id', 'Name', 'Availability Zone', 'Hosts', 'Metadata']
|
||||
utils.print_list([aggregate], columns)
|
||||
|
||||
def parser_metadata(fields):
|
||||
return utils.pretty_choice_dict(getattr(fields, 'metadata', {}) or {})
|
||||
|
||||
def parser_hosts(fields):
|
||||
return utils.pretty_choice_list(getattr(fields, 'hosts', []))
|
||||
|
||||
formatters = {
|
||||
'Metadata': parser_metadata,
|
||||
'Hosts': parser_hosts,
|
||||
}
|
||||
utils.print_list([aggregate], columns, formatters=formatters)
|
||||
|
||||
|
||||
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
|
||||
|
@ -2508,7 +2508,18 @@ def do_aggregate_details(cs, args):
|
||||
|
||||
def _print_aggregate_details(aggregate):
|
||||
columns = ['Id', 'Name', 'Availability Zone', 'Hosts', 'Metadata']
|
||||
utils.print_list([aggregate], columns)
|
||||
|
||||
def parser_metadata(fields):
|
||||
return utils.pretty_choice_dict(getattr(fields, 'metadata', {}) or {})
|
||||
|
||||
def parser_hosts(fields):
|
||||
return utils.pretty_choice_list(getattr(fields, 'hosts', []))
|
||||
|
||||
formatters = {
|
||||
'Metadata': parser_metadata,
|
||||
'Hosts': parser_hosts,
|
||||
}
|
||||
utils.print_list([aggregate], columns, formatters=formatters)
|
||||
|
||||
|
||||
@utils.arg('server', metavar='<server>', help='Name or ID of server.')
|
||||
|
Loading…
Reference in New Issue
Block a user