Merge "openstack: make scenario able to reuse clients"
This commit is contained in:
commit
e736a18ced
@ -20,7 +20,7 @@ from rally.task.scenarios import base
|
|||||||
class OpenStackScenario(base.Scenario):
|
class OpenStackScenario(base.Scenario):
|
||||||
"""Base class for all OpenStack scenarios."""
|
"""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)
|
super(OpenStackScenario, self).__init__(context)
|
||||||
if context:
|
if context:
|
||||||
if "admin" in context:
|
if "admin" in context:
|
||||||
@ -28,6 +28,18 @@ class OpenStackScenario(base.Scenario):
|
|||||||
context["admin"]["endpoint"])
|
context["admin"]["endpoint"])
|
||||||
if "user" in context:
|
if "user" in context:
|
||||||
self._clients = osclients.Clients(context["user"]["endpoint"])
|
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):
|
def clients(self, client_type, version=None):
|
||||||
"""Returns a python openstack client of the requested type.
|
"""Returns a python openstack client of the requested type.
|
||||||
|
76
tests/unit/plugins/openstack/test_scenario.py
Normal file
76
tests/unit/plugins/openstack/test_scenario.py
Normal file
@ -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)
|
Loading…
Reference in New Issue
Block a user