From d4309c0f31ef897d861a6d80fe9bcb87750ead28 Mon Sep 17 00:00:00 2001 From: Pavel Boldin Date: Mon, 20 Jul 2015 19:12:45 +0300 Subject: [PATCH] openstack: make scenario able to reuse clients Make rally.plugins.openstack.OpenStackScenario respect `admin_clients` and `clients` kwargs so the pre-existing clients can be reused. Change-Id: Ie93880b06ace24074a97802ab2c905622d2654e2 --- rally/plugins/openstack/scenario.py | 14 +++- tests/unit/plugins/openstack/test_scenario.py | 76 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tests/unit/plugins/openstack/test_scenario.py diff --git a/rally/plugins/openstack/scenario.py b/rally/plugins/openstack/scenario.py index 1efc5641..d781370c 100644 --- a/rally/plugins/openstack/scenario.py +++ b/rally/plugins/openstack/scenario.py @@ -20,7 +20,7 @@ from rally.task.scenarios import base class OpenStackScenario(base.Scenario): """Base class for all OpenStack scenarios.""" - def __init__(self, context=None): + def __init__(self, context=None, admin_clients=None, clients=None): super(OpenStackScenario, self).__init__(context) if context: if "admin" in context: @@ -28,6 +28,18 @@ class OpenStackScenario(base.Scenario): context["admin"]["endpoint"]) if "user" in context: self._clients = osclients.Clients(context["user"]["endpoint"]) + if admin_clients: + if hasattr(self, "_admin_clients"): + raise ValueError( + "Only one of context[\"admin\"] or admin_clients" + " must be supplied") + self._admin_clients = admin_clients + if clients: + if hasattr(self, "_clients"): + raise ValueError( + "Only one of context[\"user\"] or clients" + " must be supplied") + self._clients = clients def clients(self, client_type, version=None): """Returns a python openstack client of the requested type. diff --git a/tests/unit/plugins/openstack/test_scenario.py b/tests/unit/plugins/openstack/test_scenario.py new file mode 100644 index 00000000..0f699bae --- /dev/null +++ b/tests/unit/plugins/openstack/test_scenario.py @@ -0,0 +1,76 @@ +# 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 oslotest import mockpatch + +from rally.plugins.openstack import scenario as base_scenario +from tests.unit import test + + +class OpenStackScenarioTestCase(test.TestCase): + def setUp(self): + super(OpenStackScenarioTestCase, self).setUp() + self.osclients = mockpatch.Patch( + "rally.osclients.Clients") + self.useFixture(self.osclients) + + def test_init(self): + context = {"foo": "bar"} + scenario = base_scenario.OpenStackScenario(context) + self.assertEqual(context, scenario.context) + + def test_init_admin_context(self): + context = { + "foo": "bar", + "admin": {"endpoint": mock.Mock()} + } + scenario = base_scenario.OpenStackScenario(context) + self.assertEqual(context, scenario.context) + self.osclients.mock.assert_called_once_with( + context["admin"]["endpoint"]) + + self.assertRaises( + ValueError, base_scenario.OpenStackScenario, + context, admin_clients="foobar") + + def test_init_admin_clients(self): + context = {"foo": "bar"} + scenario = base_scenario.OpenStackScenario( + context, admin_clients="foobar") + self.assertEqual(context, scenario.context) + + self.assertEqual("foobar", scenario._admin_clients) + + def test_init_user_context(self): + context = { + "foo": "bar", + "user": {"endpoint": mock.Mock()} + } + scenario = base_scenario.OpenStackScenario(context) + self.assertEqual(context, scenario.context) + self.osclients.mock.assert_called_once_with( + context["user"]["endpoint"]) + + self.assertRaises( + ValueError, base_scenario.OpenStackScenario, + context, clients="foobar") + + def test_init_user_clients(self): + context = {"foo": "bar"} + scenario = base_scenario.OpenStackScenario( + context, clients="foobar") + self.assertEqual(context, scenario.context) + + self.assertEqual("foobar", scenario._clients)