[Piglit] [PATCH 18/18] framework: Add support for setting default platform in piglit.conf

Dylan Baker baker.dylan.c at gmail.com
Tue Aug 19 13:25:12 PDT 2014


This patch adds support to piglit.conf to set a default platform.
---
 framework/programs/run.py           |  44 +++++++++++---
 framework/tests/run_parser_tests.py | 117 +++++++++++++++++++++++++++++++-----
 piglit.conf.example                 |   8 +++
 3 files changed, 148 insertions(+), 21 deletions(-)

diff --git a/framework/programs/run.py b/framework/programs/run.py
index ced5783..8bf11a7 100644
--- a/framework/programs/run.py
+++ b/framework/programs/run.py
@@ -26,6 +26,7 @@ import sys
 import os
 import os.path as path
 import time
+import ConfigParser
 
 import framework.core as core
 import framework.results
@@ -35,6 +36,40 @@ __all__ = ['run',
            'resume']
 
 
+_PLATFORMS = ["glx", "x11_egl", "wayland", "gbm", "mixed_glx_egl"]
+
+
+def _default_platform():
+    """ Logic to determine the default platform to use
+
+    This assumes that the platform can only be set on Linux, it probably works
+    on BSD. This is only relevant if piglit is built with waffle support. When
+    waffle support lands for Windows and if it ever happens for OSX, this will
+    need to be extended.
+
+    On Linux this will try in order,
+    1) An option provided via the -p/--platform option (this is handled in
+       argparse, not in this function)
+    2) PIGLIT_PLATFORM from the environment
+    3) [core]:platform from the config file
+    4) mixed_glx_egl
+
+    """
+    if os.environ.get('PIGLIT_PLATFORM'):
+        return os.environ.get('PIGLIT_PLATFORM')
+    else:
+        try:
+            plat = core.PIGLIT_CONFIG.get('core', 'platform')
+            if plat not in _PLATFORMS:
+                print('Platform is not valid\n'
+                      'valid platforms are: {}'.format(_PLATFORMS),
+                      file=sys.stderr)
+                sys.exit(1)
+            return plat
+        except (ConfigParser.NoOptionError, ConfigParser.NoSectionError):
+            return 'mixed_glx_egl'
+
+
 def _run_parser(input_):
     """ Parser for piglit run command """
     # Parse the config file before any other options, this allows the config
@@ -87,13 +122,8 @@ def _run_parser(input_):
                              dest="concurrency",
                              help="Disable concurrent test runs")
     parser.add_argument("-p", "--platform",
-                        choices=["glx", "x11_egl", "wayland", "gbm",
-                                 "mixed_glx_egl"],
-                        # If an explicit choice isn't made check the
-                        # environment, and if that fails select the glx/x11_egl
-                        # mixed profile
-                        default=os.environ.get("PIGLIT_PLATFORM",
-                                               "mixed_glx_egl"),
+                        choices=_PLATFORMS,
+                        default=_default_platform(),
                         help="Name of windows system passed to waffle")
     parser.add_argument("--valgrind",
                         action="store_true",
diff --git a/framework/tests/run_parser_tests.py b/framework/tests/run_parser_tests.py
index 88fcb0d..07d4750 100644
--- a/framework/tests/run_parser_tests.py
+++ b/framework/tests/run_parser_tests.py
@@ -20,39 +20,128 @@
 
 """ Module of tests for the run commandline parser """
 
+import sys
 import os
+import shutil
+import ConfigParser
 import nose.tools as nt
 import framework.tests.utils as utils
 import framework.programs.run as run
+import framework.core as core
 
 
 class TestPlatform(utils.TestWithEnvClean):
     """ Test piglitrun -p/--platform options """
+    _CONF = '[core]\nplatform=gbm'
+
+    def __unset_config(self):
+        """ Ensure that no config files are being accidently loaded """
+        self.add_teardown('HOME')
+        self.add_teardown('XDG_CONFIG_HOME')
+
+    def __move_piglit_conf(self):
+        """ Move piglit.conf from local and from piglit root
+
+        They are moved and put back so they aren't accidentally loaded
+
+        """
+        if os.path.exists('piglit.conf'):
+            shutil.move('piglit.conf', 'piglit.conf.restore')
+            self.defer(shutil.move, 'piglit.conf.restore', 'piglit.conf')
+
+        root = os.path.join(run.__file__, '..', '..', 'piglit.conf')
+        if os.path.exists(root):
+            shutil.move(root, root + '.restore')
+            self.defer(shutil.move, root + '.restore', root)
+
+    def __set_env(self):
+        """ Set PIGLIT_PLATFORM """
+        self.add_teardown('PIGLIT_PLATFORM')
+        os.environ['PIGLIT_PLATFORM'] = 'glx'
+
+    def setup(self):
+        # Set core.PIGLIT_CONFIG back to pristine between tests
+        core.PIGLIT_CONFIG = ConfigParser.SafeConfigParser()
+
     def test_default(self):
-        """ Run parser platform: When no option is passed the default option is
-        used """
+        """ run parser: platform final fallback path """
+        self.__unset_config()
+        self.__move_piglit_conf()
+
         args = run._run_parser(['quick.py', 'foo'])
         nt.assert_equal(args.platform, 'mixed_glx_egl')
 
-    def test_options(self):
-        """ Run parser platform: when an option is present it replaces default
-        """
+    def test_option_default(self):
+        """ Run parser: platform replaces default """
+        args = run._run_parser(['-p', 'x11_egl', 'quick.py', 'foo'])
+        nt.assert_equal(args.platform, 'x11_egl')
+
+    def test_option_env(self):
+        """ Run parser: platform option replaces env """
+        self.__set_env()
+
         args = run._run_parser(['-p', 'x11_egl', 'quick.py', 'foo'])
         nt.assert_equal(args.platform, 'x11_egl')
 
+    def test_option_conf(self):
+        """ Run parser: platform option replaces conf """
+        with utils.tempdir() as tdir:
+            os.environ['XDG_CONFIG_HOME'] = tdir
+            with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
+                f.write(self._CONF)
+
+            args = run._run_parser(['-p', 'x11_egl', 'quick.py', 'foo'])
+            nt.assert_equal(args.platform, 'x11_egl')
+
     def test_env_no_options(self):
-        """ Run parser platform: When no option is passed env overrides default
+        """ Run parser: When no platform is passed env overrides default
         """
-        self.add_teardown('PIGLIT_PLATFORM')
-        os.environ['PIGLIT_PLATFORM'] = 'glx'
+        self.__set_env()
 
         args = run._run_parser(['quick.py', 'foo'])
         nt.assert_equal(args.platform, 'glx')
 
-    def test_env_options(self):
-        """ Run parser platform: when an option is passed it overwrites env """
-        self.add_teardown('PIGLIT_PLATFORM')
-        os.environ['PIGLIT_PLATFORM'] = 'glx'
+    def test_conf_default(self):
+        """ Run parser platform: conf is used as a default when applicable """
+        self.__unset_config()
+        self.__move_piglit_conf()
 
-        args = run._run_parser(['-p', 'x11_egl', 'quick.py', 'foo'])
-        nt.assert_equal(args.platform, 'x11_egl')
+        with utils.tempdir() as tdir:
+            os.environ['XDG_CONFIG_HOME'] = tdir
+            with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
+                f.write(self._CONF)
+
+            args = run._run_parser(['quick.py', 'foo'])
+            nt.assert_equal(args.platform, 'gbm')
+
+    def test_env_conf(self):
+        """ Run parser: env overwrides a conf value """
+        self.__unset_config()
+        self.__move_piglit_conf()
+        self.__set_env()
+
+        with utils.tempdir() as tdir:
+            os.environ['XDG_CONFIG_HOME'] = tdir
+            with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
+                f.write(self._CONF)
+
+            args = run._run_parser(['quick.py', 'foo'])
+            nt.assert_equal(args.platform, 'glx')
+
+    @nt.raises(SystemExit)
+    def test_bad_value_in_conf(self):
+        """ run parser: an error is raised when the platform in conf is bad """
+        self.__unset_config()
+        self.__move_piglit_conf()
+
+        # This has sideffects, it shouldn't effect anything in this module, but
+        # it may cause later problems. But without this we get ugly error spew
+        # from this test.
+        sys.stderr = open(os.devnull, 'w')
+
+        with utils.tempdir() as tdir:
+            with open(os.path.join(tdir, 'piglit.conf'), 'w') as f:
+                f.write('[core]\nplatform=foobar')
+
+            run._run_parser(['-f', os.path.join(tdir, 'piglit.conf'),
+                             'quick.py', 'foo'])
diff --git a/piglit.conf.example b/piglit.conf.example
index 61a28cf..2e0fadf 100644
--- a/piglit.conf.example
+++ b/piglit.conf.example
@@ -14,3 +14,11 @@
 [oglconform]
 ; Set bindir equal to the absolute root of the oglconform directory
 ;path=/home/usr/src/oglconform
+
+[core]
+; Set the default platform to use.
+; Options can be found by running piglit -h and reading the section
+; for -p/--platform
+;
+; The default on Linux will be mixed_glx_egl
+;platform=gbm
-- 
2.0.4



More information about the Piglit mailing list