Change assert statements to raises in CheckValue 85/60285/2
authorMiikka Koistinen <miikka.koistinen@nokia.com>
Fri, 27 Jul 2018 13:53:51 +0000 (16:53 +0300)
committerMiikka Koistinen <miikka.koistinen@nokia.com>
Tue, 31 Jul 2018 08:58:19 +0000 (11:58 +0300)
* Instead of using assert statements, raise a custom exception.
* Modify the unit tests so that they do not test unused parameters for
  equality.
* Fix pylint and pep8 errors.

JIRA: YARDSTICK-966

Change-Id: I41e13a5a22086827792eaf9de8da2e4ed6dd42de
Signed-off-by: Miikka Koistinen <miikka.koistinen@nokia.com>
yardstick/benchmark/scenarios/lib/check_value.py
yardstick/common/exceptions.py
yardstick/tests/unit/benchmark/scenarios/lib/test_check_value.py

index 7590760..4c9b27d 100644 (file)
@@ -13,6 +13,7 @@ from __future__ import absolute_import
 import logging
 
 from yardstick.benchmark.scenarios import base
+from yardstick.common import exceptions as y_exc
 
 LOG = logging.getLogger(__name__)
 
@@ -34,24 +35,18 @@ class CheckValue(base.Scenario):
         self.context_cfg = context_cfg
         self.options = self.scenario_cfg['options']
 
-    def run(self, result):
+    def run(self, _):
         """execute the test"""
 
         op = self.options.get("operator")
         LOG.debug("options=%s", self.options)
         value1 = str(self.options.get("value1"))
         value2 = str(self.options.get("value2"))
+        if (op == "eq" and value1 != value2) or (op == "ne" and
+                                                 value1 == value2):
+            raise y_exc.ValueCheckError(
+                value1=value1, operator=op, value2=value2)
         check_result = "PASS"
-        if op == "eq" and value1 != value2:
-            LOG.info("value1=%s, value2=%s, error: should equal!!!", value1,
-                     value2)
-            check_result = "FAIL"
-            assert value1 == value2, "Error %s!=%s" % (value1, value2)
-        elif op == "ne" and value1 == value2:
-            LOG.info("value1=%s, value2=%s, error: should not equal!!!",
-                     value1, value2)
-            check_result = "FAIL"
-            assert value1 != value2, "Error %s==%s" % (value1, value2)
         LOG.info("Check result is %s", check_result)
         keys = self.scenario_cfg.get('output', '').split()
         values = [check_result]
index 48f15c0..7e16e39 100644 (file)
@@ -408,3 +408,7 @@ class AclUnknownActionTemplate(YardstickException):
 
 class InvalidMacAddress(YardstickException):
     message = 'Mac address "%(mac_address)s" is invalid'
+
+
+class ValueCheckError(YardstickException):
+    message = 'Constraint "%(value1)s %(operator)s %(value2)s" does not hold'
index 7a2324b..b0488ba 100644 (file)
@@ -8,28 +8,56 @@
 ##############################################################################
 import unittest
 
-from yardstick.benchmark.scenarios.lib.check_value import CheckValue
+from yardstick.benchmark.scenarios.lib import check_value
+from yardstick.common import exceptions as y_exc
+
 
 class CheckValueTestCase(unittest.TestCase):
 
-    def setUp(self):
-        self.result = {}
+    def test_eq_pass(self):
+        scenario_cfg = {'options': {'operator': 'eq',
+                                    'value1': 1,
+                                    'value2': 1}}
+        obj = check_value.CheckValue(scenario_cfg, {})
+        result = obj.run({})
+
+        self.assertEqual({}, result)
+
+    def test_ne_pass(self):
+        scenario_cfg = {'options': {'operator': 'ne',
+                                    'value1': 1,
+                                    'value2': 2}}
+        obj = check_value.CheckValue(scenario_cfg, {})
+        result = obj.run({})
+
+        self.assertEqual({}, result)
+
+    def test_result(self):
+        scenario_cfg = {'options': {'operator': 'eq',
+                                    'value1': 1,
+                                    'value2': 1},
+                        'output': 'foo'}
+        obj = check_value.CheckValue(scenario_cfg, {})
+        result = obj.run({})
+
+        self.assertDictEqual(result, {'foo': 'PASS'})
 
-    def test_check_value_eq(self):
-        scenario_cfg = {'options': {'operator': 'eq', 'value1': 1, 'value2': 2}}
-        obj = CheckValue(scenario_cfg, {})
-        self.assertRaises(AssertionError, obj.run, self.result)
-        self.assertEqual({}, self.result)
+    def test_eq(self):
+        scenario_cfg = {'options': {'operator': 'eq',
+                                    'value1': 1,
+                                    'value2': 2}}
+        obj = check_value.CheckValue(scenario_cfg, {})
 
-    def test_check_value_eq_pass(self):
-        scenario_cfg = {'options': {'operator': 'eq', 'value1': 1, 'value2': 1}}
-        obj = CheckValue(scenario_cfg, {})
+        with self.assertRaises(y_exc.ValueCheckError):
+            result = obj.run({})
+            self.assertEqual({}, result)
 
-        obj.run(self.result)
-        self.assertEqual({}, self.result)
+    def test_ne(self):
+        scenario_cfg = {'options': {'operator': 'ne',
+                                    'value1': 1,
+                                    'value2': 1}}
+        obj = check_value.CheckValue(scenario_cfg, {})
 
-    def test_check_value_ne(self):
-        scenario_cfg = {'options': {'operator': 'ne', 'value1': 1, 'value2': 1}}
-        obj = CheckValue(scenario_cfg, {})
-        self.assertRaises(AssertionError, obj.run, self.result)
-        self.assertEqual({}, self.result)
+        with self.assertRaises(y_exc.ValueCheckError):
+            result = obj.run({})
+            self.assertEqual({}, result)