Moved preprocess function from base to types

The preprocess function was moved from benchmark.runners
.base to benchmark.types. Decoupling the preprocess
function from base removes a dependency in code.

A unit test for preprocess has also been added to
test_types.py.

Change-Id: I311316c667bb6256ea5f0a32e24328102b62c6e8
This commit is contained in:
Kevin Tsang 2014-09-09 18:14:44 -07:00 committed by Boris Pavlovic
parent 6d68d1d6d2
commit f109e72ced
5 changed files with 64 additions and 19 deletions

View File

@ -21,6 +21,7 @@ import jsonschema
from oslo.config import cfg
from rally.benchmark.scenarios import base as scenario_base
from rally.benchmark import types
from rally.benchmark import utils
from rally import consts
from rally import exceptions
@ -209,7 +210,7 @@ class ScenarioRunner(object):
cls = scenario_base.Scenario.get_by_name(cls_name)
# NOTE(boris-42): processing @types decorators
args = cls.preprocess(method_name, context, args)
args = types.preprocess(cls, method_name, context, args)
with rutils.Timer() as timer:
self._run_scenario(cls, method_name, context, args)

View File

@ -22,7 +22,6 @@ import time
from rally import consts
from rally import exceptions
from rally import osclients
from rally import utils
@ -171,22 +170,6 @@ class Scenario(object):
method = getattr(cls, method_name)
return copy.deepcopy(getattr(method, attr_name, default))
@classmethod
def preprocess(cls, method_name, context, args):
"""Run preprocessor on scenario arguments."""
preprocessors = Scenario.meta(cls, method_name=method_name,
attr_name="preprocessors", default={})
clients = osclients.Clients(context["admin"]["endpoint"])
for src, preprocessor in preprocessors.items():
resource_config = args.get(src)
if resource_config:
args[src] = preprocessor.transform(
clients=clients,
resource_config=resource_config)
return args
def context(self):
"""Returns the context of the current benchmark scenario."""
return self._context

View File

@ -14,10 +14,13 @@
# under the License.
import abc
import copy
import operator
import re
from rally.benchmark.scenarios import base
from rally import exceptions
from rally import osclients
def set(**kwargs):
@ -34,6 +37,31 @@ def set(**kwargs):
return wrapper
def preprocess(cls, method_name, context, args):
"""Run preprocessor on scenario arguments.
:param cls: class name of benchmark scenario
:param method_name: name of benchmark scenario method
:param context: dictionary object that must have admin and endpoint entries
:param args: args section of benchmark specification in rally task file
:returns processed_args: dictionary object with additional client
and resource configuration
"""
preprocessors = base.Scenario.meta(cls, method_name=method_name,
attr_name="preprocessors", default={})
clients = osclients.Clients(context["admin"]["endpoint"])
processed_args = copy.deepcopy(args)
for src, preprocessor in preprocessors.items():
resource_cfg = processed_args.get(src)
if resource_cfg:
processed_args[src] = preprocessor.transform(
clients=clients, resource_config=resource_cfg)
return processed_args
class ResourceType(object):
@classmethod

View File

@ -241,8 +241,9 @@ class ScenarioRunnerTestCase(test.TestCase):
cls_name, method_name = scenario_name.split(".", 1)
cls = scenario_base.Scenario.get_by_name(cls_name)
expected_config_kwargs = {"image": 1, "flavor": 1}
runner._run_scenario.assert_called_once_with(
cls, method_name, context_obj, config_kwargs)
cls, method_name, context_obj, expected_config_kwargs)
def test_runner_send_result_exception(self):
runner = serial.SerialScenarioRunner(

View File

@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
import mock
from rally.benchmark import types
from rally import exceptions
from tests import fakes
@ -165,3 +167,33 @@ class VolumeTypeResourceTypeTestCase(test.TestCase):
self.assertRaises(exceptions.InvalidScenarioArgument,
types.VolumeTypeResourceType.transform,
self.clients, resource_config)
class PreprocessTestCase(test.TestCase):
@mock.patch("rally.benchmark.types.base.Scenario.meta")
@mock.patch("rally.benchmark.types.osclients")
def test_preprocess(self, mock_osclients, mock_meta):
cls = "some_class"
method_name = "method_name"
context = {
"a": 1,
"b": 2,
"admin": {"endpoint": mock.MagicMock()}
}
args = {"a": 10, "b": 20}
class Preprocessor(types.ResourceType):
@classmethod
def transform(cls, clients, resource_config):
return resource_config * 2
mock_meta.return_value = {"a": Preprocessor}
result = types.preprocess(cls, method_name, context, args)
mock_meta.assert_called_once_with(cls, default={},
method_name=method_name,
attr_name="preprocessors")
mock_osclients.Clients.assert_called_once_with(
context["admin"]["endpoint"])
self.assertEqual({"a": 20, "b": 20}, result)