tests: treat all truthy env vars the same way 78/20478/7
authorPaul Vinciguerra <pvinci@vinciconsulting.com>
Wed, 3 Jul 2019 12:38:38 +0000 (08:38 -0400)
committerNeale Ranns <nranns@cisco.com>
Tue, 9 Jul 2019 07:16:16 +0000 (07:16 +0000)
Introduce a new class, that returns the truthiness of a env var.
Since an environment variable is just a string, it would normally
be true if not unset. The new class returns true when the env var is
set to a string that would be considered true.

Type: test
Depends-on: https://gerrit.fd.io/r/20484

Change-Id: I90ef010156f6fec246bde5c0e208ced1869b180f
Signed-off-by: Paul Vinciguerra <pvinci@vinciconsulting.com>
test/framework.py
test/run_tests.py

index 6bed1eb..2c03b43 100644 (file)
@@ -57,9 +57,28 @@ ERROR = 2
 SKIP = 3
 TEST_RUN = 4
 
-debug_framework = False
-if os.getenv('TEST_DEBUG', "0") == "1":
-    debug_framework = True
+
+class BoolEnvironmentVariable(object):
+
+    def __init__(self, env_var_name, default='n', true_values=None):
+        self.name = env_var_name
+        self.default = default
+        self.true_values = true_values if true_values is not None else \
+            ("y", "yes", "1")
+
+    def __bool__(self):
+        return os.getenv(self.name, self.default).lower() in self.true_values
+
+    if sys.version_info[0] == 2:
+        __nonzero__ = __bool__
+
+    def __repr__(self):
+        return 'BoolEnvironmentVariable(%r, default=%r, true_values=%r)' % \
+               (self.name, self.default, self.true_values)
+
+
+debug_framework = BoolEnvironmentVariable('TEST_DEBUG')
+if debug_framework:
     import debug_internal
 
 """
@@ -174,7 +193,7 @@ def pump_output(testclass):
 
 
 def _is_skip_aarch64_set():
-    return os.getenv('SKIP_AARCH64', 'n').lower() in ('yes', 'y', '1')
+    return BoolEnvironmentVariable('SKIP_AARCH64')
 
 
 is_skip_aarch64_set = _is_skip_aarch64_set()
@@ -188,8 +207,7 @@ is_platform_aarch64 = _is_platform_aarch64()
 
 
 def _running_extended_tests():
-    s = os.getenv("EXTENDED_TESTS", "n")
-    return True if s.lower() in ("y", "yes", "1") else False
+    return BoolEnvironmentVariable("EXTENDED_TESTS")
 
 
 running_extended_tests = _running_extended_tests()
@@ -243,6 +261,7 @@ class VppTestCase(unittest.TestCase):
     """This subclass is a base class for VPP test cases that are implemented as
     classes. It provides methods to create and run test case.
     """
+    get_truthy_envar = staticmethod(BoolEnvironmentVariable)
 
     extra_vpp_punt_config = []
     extra_vpp_plugin_config = []
@@ -314,9 +333,9 @@ class VppTestCase(unittest.TestCase):
     @classmethod
     def setUpConstants(cls):
         """ Set-up the test case class based on environment variables """
-        s = os.getenv("STEP", "n")
-        cls.step = True if s.lower() in ("y", "yes", "1") else False
+        cls.step = BoolEnvironmentVariable('STEP')
         d = os.getenv("DEBUG", None)
+        # inverted case to handle '' == True
         c = os.getenv("CACHE_OUTPUT", "1")
         cls.cache_vpp_output = False if c.lower() in ("n", "no", "0") else True
         cls.set_debug_flags(d)
index 58ca7d5..aee05c6 100644 (file)
@@ -14,6 +14,7 @@ import re
 from multiprocessing import Process, Pipe, cpu_count
 from multiprocessing.queues import Queue
 from multiprocessing.managers import BaseManager
+import framework
 from framework import VppTestRunner, running_extended_tests, VppTestCase, \
     get_testcase_doc_name, get_test_description, PASS, FAIL, ERROR, SKIP, \
     TEST_RUN
@@ -729,12 +730,10 @@ if __name__ == '__main__':
     debug = os.getenv("DEBUG", "n").lower() in ["gdb", "gdbserver"]
 
     debug_core = os.getenv("DEBUG", "").lower() == "core"
-    compress_core = os.getenv("CORE_COMPRESS", "").lower() in ("y", "yes", "1")
+    compress_core = framework.BoolEnvironmentVariable("CORE_COMPRESS")
 
-    step = os.getenv("STEP", "n").lower() in ("y", "yes", "1")
-
-    force_foreground = os.getenv("FORCE_FOREGROUND", "").lower() in \
-        ("y", "yes", "1")
+    step = framework.BoolEnvironmentVariable("STEP")
+    force_foreground = framework.BoolEnvironmentVariable("FORCE_FOREGROUND")
 
     run_interactive = debug or step or force_foreground