Fix listing with --all error

When executing command "karbor xxx-list --all", it will raise
error as 'error: ambiguous option: --all could match --all-tenants,
--all_tenants'. The reason is we have both '--all-tenants' and
'--all_tenants' in the specify operations, '--all' matches two
args, so it can not work, but when using '--all-' or '--all_',
it return the correct result. We should fix it, so we remove the
arg '--all_tenants' which is not in the help info.
Story: 2005874
Task: 33686

Change-Id: Iafa70c35594af732435122ebd50c114fd7f0b9df
This commit is contained in:
Jiao Pengju 2019-06-13 12:27:43 +08:00
parent 6a5f46615c
commit 17f75a9c00
4 changed files with 169 additions and 39 deletions

View File

@ -400,9 +400,9 @@ class KarborShell(object):
if args.api_timeout:
kwargs['timeout'] = args.api_timeout
client = karbor_client.Client(api_version, endpoint, **kwargs)
self.cs = karbor_client.Client(api_version, endpoint, **kwargs)
args.func(client, args)
args.func(self.cs, args)
def do_bash_completion(self, args):
"""Prints all of the commands and options to stdout."""

View File

@ -37,12 +37,13 @@ class FakeClient(fakes.FakeClient, client.Client):
'project_id': PROJECT_ID,
}
client.Client.__init__(self, 'http://endpoint', **kwargs)
self.client = FakeHTTPClient(**kwargs)
self.client = self.http_client
class FakeHTTPClient(base_client.HTTPClient):
def __init__(self, **kwargs):
def __init__(self, endpoint, **kwargs):
super(FakeHTTPClient, self)
self.username = 'username'
self.password = 'password'
self.auth_url = 'auth_url'
@ -50,6 +51,9 @@ class FakeHTTPClient(base_client.HTTPClient):
self.management_url = 'http://10.0.2.15:8776/v1/fake'
self.osapi_max_limit = 1000
self.marker = None
self.project_id = 'project_id'
self.auth_token = 'auth_token'
self.region_name = 'region_name'
def _cs_request(self, url, method, **kwargs):
# Check that certain things are called correctly
@ -92,3 +96,27 @@ class FakeHTTPClient(base_client.HTTPClient):
"headers": headers,
})
return r, body
def json_request(self, method, url, **kwargs):
return self._cs_request(url, method, **kwargs)
def get_providers_1234_checkpoints(self, **kwargs):
return 200, {}, {"checkpoints": []}
def get_plans(self, **kwargs):
return 200, {}, {"plans": []}
def get_operation_logs(self, **kwargs):
return 200, {}, {"operation_logs": []}
def get_restores(self, **kwargs):
return 200, {}, {"restores": []}
def get_scheduled_operations(self, **kwargs):
return 200, {}, {"operations": []}
def get_triggers(self, **kwargs):
return 200, {}, {"triggers": []}
def get_verifications(self, **kwargs):
return 200, {}, {"verifications": []}

View File

@ -0,0 +1,137 @@
# 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
from karborclient import shell
from karborclient.tests.unit import base
from karborclient.tests.unit.v1 import fakes
FAKE_PROVIDER_ID = '1234'
FAKE_ENDPOINT = 'http://127.0.0.1/identity'
class ShellFixture(fixtures.Fixture):
def setUp(self):
super(ShellFixture, self).setUp()
self.shell = shell.KarborShell()
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 instantiated 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(base.TestCaseShell):
FAKE_ENV = {
'OS_USERNAME': 'username',
'OS_PASSWORD': 'password',
'OS_TENANT_NAME': 'project_id',
'OS_AUTH_URL': 'http://no.where/v2.0',
'OS_AUTH_TOKEN': 'fake_token'
}
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
get_endpoint = mock.MagicMock()
get_endpoint.return_value = FAKE_ENDPOINT
self.useFixture(fixtures.MonkeyPatch(
'keystoneauth1.identity.generic.token.Token.get_endpoint',
get_endpoint))
self.useFixture(fixtures.MonkeyPatch('karborclient.client.Client',
fakes.FakeClient))
self.useFixture(fixtures.MonkeyPatch(
'karborclient.common.http._construct_http_client',
fakes.FakeHTTPClient))
def run_command(self, cmd):
if not isinstance(cmd, list):
cmd = cmd.split()
self.shell.main(cmd)
def assert_called(self, method, url, body=None, **kwargs):
return self.shell.cs.assert_called(method, url, body, **kwargs)
def test_checkpoint_list_with_all_tenants(self):
self.run_command(
'checkpoint-list ' + FAKE_PROVIDER_ID + ' --all-tenants 1')
self.assert_called('GET',
'/providers/1234/'
'checkpoints?all_tenants=1')
def test_checkpoint_list_with_all(self):
self.run_command(
'checkpoint-list ' + FAKE_PROVIDER_ID + ' --all')
self.assert_called('GET',
'/providers/1234/'
'checkpoints?all_tenants=1')
def test_plan_list_with_all_tenants(self):
self.run_command('plan-list --all-tenants 1')
self.assert_called('GET', '/plans?all_tenants=1')
def test_plan_list_with_all(self):
self.run_command('plan-list --all')
self.assert_called('GET', '/plans?all_tenants=1')
def test_resotre_list_with_all_tenants(self):
self.run_command('restore-list --all-tenants 1')
self.assert_called('GET', '/restores?all_tenants=1')
def test_resotre_list_with_all(self):
self.run_command('restore-list --all')
self.assert_called('GET', '/restores?all_tenants=1')
def test_verification_list_with_all_tenants(self):
self.run_command('verification-list --all-tenants 1')
self.assert_called('GET', '/verifications?all_tenants=1')
def test_verification_list_with_all(self):
self.run_command('verification-list --all')
self.assert_called('GET', '/verifications?all_tenants=1')
def test_trigger_list_with_all_tenants(self):
self.run_command('trigger-list --all-tenants 1')
self.assert_called('GET', '/triggers?all_tenants=1')
def test_trigger_list_with_all(self):
self.run_command('trigger-list --all')
self.assert_called('GET', '/triggers?all_tenants=1')
def test_scheduledoperation_list_with_all_tenants(self):
self.run_command('scheduledoperation-list --all-tenants 1')
self.assert_called('GET', '/scheduled_operations?all_tenants=1')
def test_scheduledoperation_list_with_all(self):
self.run_command('scheduledoperation-list --all')
self.assert_called('GET', '/scheduled_operations?all_tenants=1')
def test_operationlog_list_with_all_tenants(self):
self.run_command('operationlog-list --all-tenants 1')
self.assert_called('GET', '/operation_logs?all_tenants=1')
def test_operationlog_list_with_all(self):
self.run_command('operationlog-list --all')
self.assert_called('GET', '/operation_logs?all_tenants=1')

View File

@ -31,11 +31,6 @@ from karborclient import utils as arg_utils
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('--name',
metavar='<name>',
default=None,
@ -286,11 +281,6 @@ def do_restore_create(cs, args):
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('--status',
metavar='<status>',
default=None,
@ -416,11 +406,6 @@ def do_verification_create(cs, args):
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('--status',
metavar='<status>',
default=None,
@ -724,11 +709,6 @@ def do_checkpoint_create(cs, args):
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('provider_id',
metavar='<provider_id>',
help='ID of provider.')
@ -917,11 +897,6 @@ def do_checkpoint_reset_state(cs, args):
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('--name',
metavar='<name>',
default=None,
@ -1073,11 +1048,6 @@ def do_trigger_delete(cs, args):
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('--name',
metavar='<name>',
default=None,
@ -1225,11 +1195,6 @@ def do_scheduledoperation_delete(cs, args):
const=1,
default=0,
help='Shows details for all tenants. Admin only.')
@utils.arg('--all_tenants',
nargs='?',
type=int,
const=1,
help=argparse.SUPPRESS)
@utils.arg('--status',
metavar='<status>',
default=None,