about summary refs log tree commit diff
path: root/nixos
diff options
context:
space:
mode:
authorMarijan Petričević <marijan.petricevic94@gmail.com>2022-01-25 11:29:42 +0100
committerMarijan Petričević <marijan.petricevic94@gmail.com>2022-01-25 11:31:25 +0100
commit446c21fdc75c6a045bf5b715a315bdf87b5e71cb (patch)
treeaca847ba6873b9740371353be8576b96075c9884 /nixos
parent03c90974a74fd31dc03af58e6aeedb0c9d1f3a35 (diff)
factor out tmp-dir checks
Diffstat (limited to 'nixos')
-rw-r--r--nixos/lib/test-driver/test_driver/driver.py42
1 files changed, 25 insertions, 17 deletions
diff --git a/nixos/lib/test-driver/test_driver/driver.py b/nixos/lib/test-driver/test_driver/driver.py
index 10914a8c7aeea..880b1c5fdec0d 100644
--- a/nixos/lib/test-driver/test_driver/driver.py
+++ b/nixos/lib/test-driver/test_driver/driver.py
@@ -10,6 +10,28 @@ from test_driver.vlan import VLan
 from test_driver.polling_condition import PollingCondition
 
 
+def get_tmp_dir() -> Path:
+    """Returns a temporary directory that is defined by TMPDIR, TEMP, TMP or CWD
+    Raises an exception in case the retrieved temporary directory is not writeable
+    See https://docs.python.org/3/library/tempfile.html#tempfile.gettempdir
+    """
+    tmp_dir = Path(tempfile.gettempdir())
+    tmp_dir.mkdir(mode=0o700, exist_ok=True)
+    if not tmp_dir.is_dir():
+        raise NotADirectoryError(
+            "The directory defined by TMPDIR, TEMP, TMP or CWD: {0} is not a directory".format(
+                tmp_dir
+            )
+        )
+    if not os.access(tmp_dir, os.W_OK):
+        raise PermissionError(
+            "The directory defined by TMPDIR, TEMP, TMP, or CWD: {0} is not writeable".format(
+                tmp_dir
+            )
+        )
+    return tmp_dir
+
+
 class Driver:
     """A handle to the driver that sets up the environment
     and runs the tests"""
@@ -30,21 +52,7 @@ class Driver:
         self.tests = tests
         self.out_dir = out_dir
 
-        tmp_dir = Path(tempfile.gettempdir())
-        tmp_dir.mkdir(mode=0o700, exist_ok=True)
-
-        if not tmp_dir.is_dir():
-            raise NotADirectoryError(
-                "The directory defined by TMPDIR, TEMP, TMP or CWD: {0} is not a directory".format(
-                    tmp_dir
-                )
-            )
-        if not os.access(tmp_dir, os.W_OK):
-            raise PermissionError(
-                "The directory defined by TMPDIR, TEMP, TMP or CWD: {0} is not writeable".format(
-                    tmp_dir
-                )
-            )
+        tmp_dir = get_tmp_dir()
 
         with rootlog.nested("start all VLans"):
             self.vlans = [VLan(nr, tmp_dir) for nr in vlans]
@@ -157,8 +165,8 @@ class Driver:
             "Using legacy create_machine(), please instantiate the"
             "Machine class directly, instead"
         )
-        tmp_dir = Path(os.environ.get("TMPDIR", tempfile.gettempdir()))
-        tmp_dir.mkdir(mode=0o700, exist_ok=True)
+
+        tmp_dir = get_tmp_dir()
 
         if args.get("startCommand"):
             start_command: str = args.get("startCommand", "")