From 54d793fb25e9f3fa7003a1a3a51e6cadf81a5b83 Mon Sep 17 00:00:00 2001 From: Tzanetos Balitsaris Date: Mon, 4 Aug 2014 03:57:01 +0300 Subject: [PATCH] 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 --- rally/benchmark/validation.py | 10 ++++++++++ tests/benchmark/test_validation.py | 16 ++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/rally/benchmark/validation.py b/rally/benchmark/validation.py index 3d25f13644..8c0b1d7cae 100644 --- a/rally/benchmark/validation.py +++ b/rally/benchmark/validation.py @@ -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) diff --git a/tests/benchmark/test_validation.py b/tests/benchmark/test_validation.py index 668143bead..2071c4df96 100644 --- a/tests/benchmark/test_validation.py +++ b/tests/benchmark/test_validation.py @@ -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,