From 5700d49533332428f3e14143f43b39bfeb1b5e6c Mon Sep 17 00:00:00 2001 From: Stanislav Levin Date: May 22 2019 15:57:25 +0000 Subject: Respect TMPDIR, TEMP or TMP environment variables during testing The FreeIPA uses its own classes for managing temp files and directories for tests. One of its underlying low-level functions is `mkdtemp`. According to documentation for `mkdtemp`: ``` If dir is not None, the file will be created in that directory; otherwise, a default directory is used. The default directory is chosen from a platform-dependent list, but the user of the application can control the directory location by setting the TMPDIR, TEMP or TMP environment variables. ``` It's actually the truth, /usr/lib64/python3.7/tempfile.py: ``` def _candidate_tempdir_list(): """Generate a list of candidate temporary directories which _get_default_tempdir will try.""" dirlist = [] # First, try the environment. for envname in 'TMPDIR', 'TEMP', 'TMP': dirname = _os.getenv(envname) if dirname: dirlist.append(dirname) # Failing that, try OS-specific locations. if _os.name == 'nt': dirlist.extend([ _os.path.expanduser(r'~\AppData\Local\Temp'), _os.path.expandvars(r'%SYSTEMROOT%\Temp'), r'c:\temp', r'c:\tmp', r'\temp', r'\tmp' ]) else: dirlist.extend([ '/tmp', '/var/tmp', '/usr/tmp' ]) ``` For now, there is a hardcoded assertion of a temp directory (`/tmp`) in IPA tests. But some systems use the mentioned environment variables (for example, pam_mktemp https://www.openhub.net/p/pam_mktemp). It's easy to check an actual temp dir via `gettempdir`. Fixes: https://pagure.io/freeipa/issue/7956 Signed-off-by: Stanislav Levin Reviewed-By: Christian Heimes --- diff --git a/ipatests/util.py b/ipatests/util.py index 08a65a6..60c5ff7 100644 --- a/ipatests/util.py +++ b/ipatests/util.py @@ -102,7 +102,8 @@ class TempDir(object): def __get_path(self): assert path.abspath(self.__path) == self.__path - assert self.__path.startswith('/tmp/ipa.tests.') + assert self.__path.startswith(path.join(tempfile.gettempdir(), + 'ipa.tests.')) assert path.isdir(self.__path) and not path.islink(self.__path) return self.__path path = property(__get_path)