Create get_description and get_scenario_type for Scenario 77/46977/1
authorRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Tue, 7 Nov 2017 16:27:43 +0000 (16:27 +0000)
committerRodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
Tue, 7 Nov 2017 16:27:43 +0000 (16:27 +0000)
Created two new class functions for Scenario class:

* "get_description" will return the first line of the docstring,
  if exists.
* "get_scenario_type" will return the value of "__scenario_type__"
  variable.

Change-Id: I8e3b47e33e0bae101b874c3975bb9c383397d188
Signed-off-by: Rodolfo Alonso Hernandez <rodolfo.alonso.hernandez@intel.com>
tests/unit/benchmark/scenarios/test_base.py [new file with mode: 0644]
yardstick/benchmark/core/scenario.py
yardstick/benchmark/scenarios/base.py

diff --git a/tests/unit/benchmark/scenarios/test_base.py b/tests/unit/benchmark/scenarios/test_base.py
new file mode 100644 (file)
index 0000000..78e3429
--- /dev/null
@@ -0,0 +1,53 @@
+# Copyright 2017: Intel Ltd.
+# 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 unittest
+
+from yardstick.benchmark.scenarios import base
+
+
+class ScenarioTestCase(unittest.TestCase):
+
+    def test_get_scenario_type(self):
+        scenario_type = 'dummy scenario'
+
+        class DummyScenario(base.Scenario):
+            __scenario_type__ = scenario_type
+
+        self.assertEqual(scenario_type, DummyScenario.get_scenario_type())
+
+    def test_get_scenario_type_not_defined(self):
+        class DummyScenario(base.Scenario):
+            pass
+
+        self.assertEqual(str(None), DummyScenario.get_scenario_type())
+
+    def test_get_description(self):
+        docstring = """First line
+            Second line
+            Third line
+        """
+
+        class DummyScenario(base.Scenario):
+            __doc__ = docstring
+
+        self.assertEqual(docstring.splitlines()[0],
+                         DummyScenario.get_description())
+
+    def test_get_description_empty(self):
+        class DummyScenario(base.Scenario):
+            pass
+
+        self.assertEqual(str(None), DummyScenario.get_description())
index cd119c2..28eb652 100644 (file)
@@ -10,7 +10,6 @@
 """ Handler for yardstick command 'scenario' """
 
 from __future__ import absolute_import
-from __future__ import print_function
 from yardstick.benchmark.scenarios.base import Scenario
 from yardstick.benchmark.core import print_hbar
 
@@ -27,9 +26,9 @@ class Scenarios(object):    # pragma: no cover
         print_hbar(78)
         print("| %-16s | %-60s" % ("Type", "Description"))
         print_hbar(78)
-        for stype in types:
-            print("| %-16s | %-60s" % (stype.__scenario_type__,
-                                       stype.__doc__.split("\n")[0]))
+        for scenario_class in types:
+            print("| %-16s | %-60s" % (scenario_class.get_scenario_type(),
+                                       scenario_class.get_description()))
         print_hbar(78)
 
     def show(self, args):
index 3cb138d..7af8583 100644 (file)
@@ -64,6 +64,20 @@ class Scenario(object):
 
         raise RuntimeError("No such scenario type %s" % scenario_type)
 
+    @classmethod
+    def get_scenario_type(cls):
+        """Return a string with the scenario type, if defined"""
+        return str(getattr(cls, '__scenario_type__', None))
+
+    @classmethod
+    def get_description(cls):
+        """Return a single line string with the class description
+
+        This function will retrieve the class docstring and return the first
+        line, or 'None' if it's empty.
+        """
+        return cls.__doc__.splitlines()[0] if cls.__doc__ else str(None)
+
     def _push_to_outputs(self, keys, values):
         return dict(zip(keys, values))