Merge "Added --all flag to tuskar-delete-roles"

This commit is contained in:
Jenkins 2015-07-02 16:16:29 +00:00 committed by Gerrit Code Review
commit 64926ca8e0
3 changed files with 85 additions and 20 deletions

View File

@ -21,7 +21,7 @@ import sys
from oslo_config import cfg
from tuskar.common import service
from tuskar.storage.delete_roles import delete_roles
from tuskar.storage import delete_roles as dr
def _print_names(message, names):
@ -29,19 +29,31 @@ def _print_names(message, names):
cfg.CONF.register_cli_opt(cfg.BoolOpt('dryrun', default=False))
cfg.CONF.register_cli_opt(cfg.ListOpt(
'uuids', help='List of role uuid to delete'))
cfg.CONF.register_cli_opt(cfg.MultiStrOpt(
'uuid', short='u', help='List of role uuid to delete'))
cfg.CONF.register_cli_opt(cfg.BoolOpt(
'all', default=False,
help='If specified, all roles will be deleted; overrides the '
'--uuids argument'))
def main(argv=None):
if argv is None:
argv = sys.argv
index = argv.index('--uuids')
service.prepare_service(argv[:index])
roles = argv[index + 1:]
service.prepare_service(argv)
deleted = delete_roles(roles, noop=cfg.CONF.dryrun)
if not cfg.CONF.uuid and not cfg.CONF.all:
sys.stderr.write(
'Either specific roles must be specified using the --uuid '
'argument or --all must be specified\n')
sys.exit(1)
if cfg.CONF.uuid:
deleted = dr.delete_roles(cfg.CONF.uuid, noop=cfg.CONF.dryrun)
else:
deleted = dr.delete_all_roles(noop=cfg.CONF.dryrun)
if len(deleted):
_print_names("Deleted", deleted)

View File

@ -56,6 +56,12 @@ def _delete_role(role_id):
TemplateStore().delete(role_id)
def delete_all_roles(noop=False):
store = TemplateStore()
all_uuids = [role.uuid for role in store.list(only_latest=True)]
return delete_roles(all_uuids, noop=noop)
def delete_roles(role_ids=None, noop=False):
deleted = []
# if any of the roles are in use, or invalid, do nothing

View File

@ -12,7 +12,6 @@
# License for the specific language governing permissions and limitations
# under the License.
from mock import call
from mock import patch
from tuskar.cmd import delete_roles
@ -21,17 +20,65 @@ from tuskar.tests.base import TestCase
class DeleteRoleTests(TestCase):
CMD = """ tuskar-delete-roles --dryrun """
UUIDS = """ 3 4 5 """
@patch('tuskar.storage.delete_roles.delete_roles')
def test_main_single_uuid(self, mock_delete):
# Setup
cmd = """ tuskar-delete-roles --uuid foo """
@patch('tuskar.storage.stores.TemplateStore.retrieve', return_value="boo")
@patch('tuskar.cmd.delete_roles._print_names')
def test_main(self, mock_print, mock_read):
main_args = "%s --uuids %s" % (self.CMD, self.UUIDS)
expected_res = ['3', '4', '5', 'No deletions, dryrun']
# test
delete_roles.main(argv=(main_args).split())
# Test
delete_roles.main(argv=(cmd.split()))
# verify
self.assertEqual([call('Deleted', expected_res)],
mock_print.call_args_list)
# Verify
mock_delete.assert_called_once_with(['foo'], noop=False)
@patch('tuskar.storage.delete_roles.delete_roles')
def test_main_multiple_uuids(self, mock_delete):
# Setup
cmd = """ tuskar-delete-roles --uuid foo --uuid bar """
# Test
delete_roles.main(argv=(cmd.split()))
# Verify
mock_delete.assert_called_once_with(['foo', 'bar'], noop=False)
@patch('tuskar.storage.delete_roles.delete_all_roles')
def test_main_all(self, mock_delete):
# Setup
cmd = """ tuskar-delete-roles --all """
# Test
delete_roles.main(argv=(cmd.split()))
# Verify
mock_delete.assert_called_once_with(noop=False)
@patch('tuskar.storage.delete_roles.delete_all_roles')
def test_main_all_dryrun(self, mock_delete):
# Setup
cmd = """ tuskar-delete-roles --all --dryrun """
# Test
delete_roles.main(argv=(cmd.split()))
# Verify
mock_delete.assert_called_once_with(noop=True)
@patch('tuskar.storage.delete_roles.delete_roles')
def test_main_uuid_dryrun(self, mock_delete):
# Setup
cmd = """ tuskar-delete-roles --uuid foo --dryrun """
# Test
delete_roles.main(argv=(cmd.split()))
# Verify
mock_delete.assert_called_once_with(['foo'], noop=True)
@patch('tuskar.storage.delete_roles.delete_roles')
def test_main_no_roles(self, mock_delete):
# Setup
cmd = """ tuskar-delete-roles """
# Test
self.assertRaises(SystemExit, delete_roles.main, argv=(cmd.split()))