Add required_contexts validator

Add a validator that checks if a benchmark scenario has defined in its
configuration file the context(s) that it requires to run first.

Example:
@validation.required_contexts("context1", "context2")
def benchmark_scenario():
  pass

Change-Id: Id83a6ff93acde2abbb29ec8351210e38d725ce48
This commit is contained in:
Tzanetos Balitsaris 2014-08-04 03:57:01 +03:00 committed by Boris Pavlovic
parent 54dbf08b86
commit 54d793fb25
2 changed files with 26 additions and 0 deletions

View File

@ -371,3 +371,13 @@ def required_services(config, clients, task, *required_services):
if service not in available_services:
return ValidationResult(
False, _("Service is not available: %s") % service)
@validator
def required_contexts(config, clients, task, *context_names):
missing_contexts = set(context_names) - set(config.get("context", {}))
if missing_contexts:
message = (_("The following contexts are required but missing from "
"the benchmark configuration file: %s") %
", ".join(missing_contexts))
return ValidationResult(False, message)

View File

@ -80,6 +80,22 @@ class ValidationUtilsTestCase(test.TestCase):
validator, = self._get_scenario_validators(func_failure, scenario)
self.assertFalse(validator(None, None, None).is_valid)
def test_required_contexts(self):
config = {"context": {"context01": {}, "context02": {}}}
required_contexts = (lambda *contexts:
validation.required_contexts(*contexts)
(lambda: None).validators.pop()
(config, None, None))
# All the required contexts are defined
result = required_contexts("context01", "context02")
self.assertTrue(result.is_valid)
# A required context is not defined
result = required_contexts("context03")
self.assertFalse(result.is_valid)
def test_required_services(self):
available_services = {
consts.ServiceType.IDENTITY: consts.Service.KEYSTONE,