forked from openstack/rally
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split user and admin cleanup to 2 separate classes
Partially implements: blueprint benchmark-context-cleanup-refactor Change-Id: I3ba0deeb5a2793b8c45f6160fe0eda22250a1ece
- Loading branch information
Aswad Rangnekar
authored and
Sergey Skripnick
committed
Aug 8, 2014
1 parent
b1ceff6
commit 2fb4ba4
Showing
8 changed files
with
269 additions
and
165 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# Copyright 2014: Mirantis Inc. | ||
# 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 sys | ||
|
||
import six | ||
|
||
from rally.benchmark.context import base | ||
from rally.benchmark.context.cleanup import utils | ||
from rally.openstack.common.gettextutils import _ | ||
from rally.openstack.common import log as logging | ||
from rally import osclients | ||
from rally import utils as rutils | ||
|
||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class AdminCleanup(base.Context): | ||
"""Context class for admin resource cleanup.""" | ||
|
||
__ctx_name__ = "admin_cleanup" | ||
__ctx_order__ = 200 | ||
__ctx_hidden__ = True | ||
|
||
CONFIG_SCHEMA = { | ||
"type": "array", | ||
"$schema": rutils.JSON_SCHEMA, | ||
"items": { | ||
"type": "string", | ||
"enum": ["keystone", "quotas"] | ||
}, | ||
"uniqueItems": True | ||
} | ||
|
||
def __init__(self, context): | ||
super(AdminCleanup, self).__init__(context) | ||
self.endpoint = None | ||
|
||
def _cleanup_resources(self): | ||
client = osclients.Clients(self.endpoint) | ||
|
||
cleanup_methods = { | ||
"keystone": (utils.delete_keystone_resources, client.keystone()), | ||
"quotas": (utils.delete_admin_quotas, client, | ||
self.context.get("tenants", [])), | ||
} | ||
|
||
for service_name in self.config: | ||
cleanup_method = cleanup_methods[service_name] | ||
method, client = cleanup_method[:2] | ||
try: | ||
method(client, *cleanup_method[2:]) | ||
except Exception as e: | ||
LOG.debug("Not all admin resources were cleaned.", | ||
exc_info=sys.exc_info()) | ||
LOG.warning(_('Unable to fully cleanup the cloud: %s') % | ||
(six.text_type(e))) | ||
|
||
@rutils.log_task_wrapper(LOG.info, _("Enter context: `admin cleanup`")) | ||
def setup(self): | ||
self.endpoint = self.context["admin"]["endpoint"] | ||
|
||
@rutils.log_task_wrapper(LOG.info, _("Exit context: `admin cleanup`")) | ||
def cleanup(self): | ||
self._cleanup_resources() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Copyright 2014: Mirantis Inc. | ||
# 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 mock | ||
|
||
from rally.benchmark.context.cleanup import admin_cleanup | ||
from tests import fakes | ||
from tests import test | ||
|
||
|
||
BASE = "rally.benchmark.context.cleanup.admin_cleanup" | ||
|
||
|
||
class AdminCleanupTestCase(test.TestCase): | ||
|
||
def test_with_statement(self): | ||
fake_admin_ctx = fakes.FakeUserContext({}).context | ||
fake_admin_ctx["config"] = {"admin_cleanup": ["keystone"]} | ||
admin_cleaner = admin_cleanup.AdminCleanup(fake_admin_ctx) | ||
admin_cleaner.setup() | ||
|
||
admin_cleaner._cleanup_resources = mock.MagicMock() | ||
|
||
with admin_cleaner as cleaner: | ||
self.assertEqual(admin_cleaner, cleaner) | ||
|
||
admin_cleaner._cleanup_resources.assert_called_once_with() | ||
|
||
@mock.patch("%s.osclients.Clients" % BASE) | ||
@mock.patch("%s.utils.delete_keystone_resources" % BASE) | ||
def test_cleaner_admin(self, mock_del_keystone, mock_clients): | ||
context = { | ||
"task": mock.MagicMock(), | ||
"config": {"admin_cleanup": ["keystone"]}, | ||
"admin": {"endpoint": mock.MagicMock()}, | ||
} | ||
res_cleaner = admin_cleanup.AdminCleanup(context) | ||
|
||
fake_keystone = mock.MagicMock() | ||
mock_clients.return_value.keystone.return_value = fake_keystone | ||
|
||
with res_cleaner: | ||
res_cleaner.setup() | ||
|
||
mock_clients.assert_called_once_with(context["admin"]["endpoint"]) | ||
mock_clients.return_value.keystone.assert_called_with() | ||
mock_del_keystone.assert_called_once_with(fake_keystone) |
Oops, something went wrong.