test: reduce further influence from the environment
authorPeter Wu <peter@lekensteyn.nl>
Fri, 12 Oct 2018 16:12:01 +0000 (18:12 +0200)
committerAnders Broman <a.broman58@gmail.com>
Fri, 12 Oct 2018 18:55:04 +0000 (18:55 +0000)
Some tests used the default home directory which can have side-effects
(such as loading plugins, loading deprecated preferences). These could
cause tests to fail. Always use a sane environment to fix this.

Change getTsharkInfo to use this clean environment as well
(WIRESHARK_CONFIG_DIR does not exist with master-2.6 and would also not
propagate things like ASAN_OPTIONS=detect_leaks=0).

Change-Id: I1674f71972d35de91d191e0c29fdb59b8a0a56ce
Reviewed-on: https://code.wireshark.org/review/30165
Petri-Dish: Peter Wu <peter@lekensteyn.nl>
Tested-by: Petri Dish Buildbot
Reviewed-by: Anders Broman <a.broman58@gmail.com>
test/config.py
test/suite_clopts.py
test/suite_text2pcap.py

index 240875140c0622f88a501f7e12d68c7c8a5d0de1..b1a76772ffdf7294ce6fc36643dc4a700513136b 100644 (file)
@@ -104,7 +104,7 @@ def getTsharkInfo():
             (cmd_tshark, '--version'),
             stderr=subprocess.PIPE,
             universal_newlines=True,
-            env={'WIRESHARK_CONFIG_DIR': '/dummy/non/existing'}
+            env=baseEnv()
         ).replace('\n', ' ')
     except subprocess.CalledProcessError as e:
         logging.warning("Failed to detect tshark features: %s", e)
@@ -172,8 +172,18 @@ def setProgramPath(path):
     setUpHostFiles()
     return retval
 
-def testEnvironment():
-    return test_env
+def baseEnv(home=None):
+    """A modified environment to ensure reproducible tests."""
+    env = os.environ.copy()
+    env['TZ'] = 'UTC'
+    home_env = 'APPDATA' if sys.platform.startswith('win32') else 'HOME'
+    if home:
+        env[home_env] = home
+    else:
+        # This directory is supposed not to be written and is used by
+        # "readonly" tests that do not read any other preferences.
+        env[home_env] = "/wireshark-tests-unused"
+    return env
 
 def setUpTestEnvironment():
     global home_path
@@ -185,10 +195,8 @@ def setUpTestEnvironment():
     test_confdir = tempfile.mkdtemp(prefix='wireshark-tests.')
     home_path = os.path.join(test_confdir, 'home')
     if sys.platform.startswith('win32'):
-        home_env = 'APPDATA'
         conf_path = os.path.join(home_path, 'Wireshark')
     else:
-        home_env = 'HOME'
         conf_path = os.path.join(home_path, '.config', 'wireshark')
     os.makedirs(conf_path)
     # Test spaces while we're here.
@@ -209,11 +217,9 @@ def setUpTestEnvironment():
         setUpUatFile(uat)
 
     # Set up our environment
-    test_env = os.environ.copy()
+    test_env = baseEnv(home=home_path)
     test_env['WIRESHARK_RUN_FROM_BUILD_DIRECTORY'] = 'True'
     test_env['WIRESHARK_QUIT_AFTER_CAPTURE'] = 'True'
-    test_env['TZ'] = 'UTC'
-    test_env[home_env] = home_path
 
 def setUpUatFile(conf_file):
     global home_path
index 02cfd97f68422f68b862e3faf457a89ee8f63284..aa6ec95cccd7b8443fa68c75ba8a7e1a3b8e5a97 100644 (file)
@@ -178,7 +178,7 @@ class case_tshark_dump_glossaries(subprocesstest.SubprocessTestCase):
 
     def test_tshark_glossary_valid_utf8(self):
         for glossary in glossaries:
-            env = os.environ.copy()
+            env = config.baseEnv()
             env['LANG'] = 'en_US.UTF-8'
             g_contents = subprocess.check_output((config.cmd_tshark, '-G', glossary), env=env, stderr=subprocess.PIPE)
             decoded = True
@@ -189,7 +189,7 @@ class case_tshark_dump_glossaries(subprocesstest.SubprocessTestCase):
             self.assertTrue(decoded, '{} is not valid UTF-8'.format(glossary))
 
     def test_tshark_glossary_plugin_count(self):
-        self.runProcess((config.cmd_tshark, '-G', 'plugins'), env=os.environ.copy())
+        self.runProcess((config.cmd_tshark, '-G', 'plugins'), env=config.baseEnv())
         self.assertGreaterEqual(self.countOutput('dissector'), 10, 'Fewer than 10 dissector plugins found')
 
 
index e4ff07bd0e559e995732b388cf56a329c8baf784..0702e51d7ab673b7d2c08e0b7cfb298445d4190b 100644 (file)
@@ -129,7 +129,7 @@ def check_text2pcap(self, cap_file, file_type, expected_packets=None, expected_d
         cf = cf_path,
         of = testin_file,
     )
-    self.assertRun(tshark_cmd, shell=True, env=os.environ.copy())
+    self.assertRun(tshark_cmd, shell=True, env=config.baseEnv())
 
     testout_fname = file_type_to_testout[file_type]
     testout_file = self.filename_from_id(testout_fname)