Merge branch 'testsuite-testsuer' into 'master'

See merge request SchedMD/dev/slurm!2046
diff --git a/testsuite/README b/testsuite/README
index c11087e..d353706 100644
--- a/testsuite/README
+++ b/testsuite/README
@@ -45,6 +45,11 @@
 SlurmInstallDir: Slurm Installation Directory (value of --prefix configure option)
 SlurmConfigDir:  Slurm Configuration Directory (compiled-in location of slurm.conf)
 
+If you are running in local-config, if the user running the test is SlurmUser or
+AdminLevel, you better specify an unprivileged user to run the test commands as:
+
+SlurmTestUser: Linux user without AdminLevel in the local-config Slurm.
+
 Tips:
 
 * the include and exclude filters use (perl 5) regular expressions
diff --git a/testsuite/python/conftest.py b/testsuite/python/conftest.py
index e6c929a..dab3d0c 100644
--- a/testsuite/python/conftest.py
+++ b/testsuite/python/conftest.py
@@ -14,7 +14,7 @@
 # import shutil
 import sys
 
-# from pathlib import Path
+from pathlib import Path
 
 sys.path.append(sys.path[0] + "/lib")
 import atf
@@ -125,7 +125,7 @@
         color_log_level(logging.TRACE, purple=True, bold=True)
 
 
-def update_tmp_path_exec_permissions():
+def update_tmp_path_exec_permissions(path):
     """
     For pytest versions 6+  the tmp path it uses no longer has
     public exec permissions for dynamically created directories by default.
@@ -140,15 +140,22 @@
     Bug 16568
     """
 
-    user_name = atf.get_user_name()
-    path = f"/tmp/pytest-of-{user_name}"
-
     if os.path.isdir(path):
         os.chmod(path, 0o777)
         for root, dirs, files in os.walk(path):
             for d in dirs:
                 os.chmod(os.path.join(root, d), 0o777)
 
+        # Ensure access for parent dirs too
+        path = path.resolve()
+        if not path.is_relative_to("/tmp"):
+            pytest.fail(f"Unexpected tmp path outside /tmp: {path}")
+
+        subdir = Path("/tmp")
+        for part in path.relative_to("/tmp").parts:
+            subdir = subdir / part
+            os.chmod(subdir, 0o777)
+
 
 @pytest.fixture(scope="module", autouse=True)
 def module_setup(request, tmp_path_factory):
@@ -179,7 +186,7 @@
     name = name[:30]
     atf.properties["test_name"] = name
     atf.module_tmp_path = tmp_path_factory.mktemp(name, numbered=True)
-    update_tmp_path_exec_permissions()
+    update_tmp_path_exec_permissions(atf.module_tmp_path)
 
     # Module-level fixtures should run from within the module_tmp_path
     os.chdir(atf.module_tmp_path)
@@ -318,6 +325,7 @@
         logging.info(request.function.__doc__)
 
     # Start each test inside the tmp_path
+    update_tmp_path_exec_permissions(tmp_path)
     monkeypatch.chdir(tmp_path)
 
 
diff --git a/testsuite/python/lib/atf.py b/testsuite/python/lib/atf.py
index 95ba0a2..99bcc0e 100644
--- a/testsuite/python/lib/atf.py
+++ b/testsuite/python/lib/atf.py
@@ -182,6 +182,10 @@
     if env_vars is not None:
         command = env_vars.strip() + " " + command
 
+    # If user is not specified but test-user is set, then set user to test-user
+    if not user and properties["test-user-set"]:
+        user = properties["test-user"]
+
     start_time = time.time()
     invocation_message = "Running command"
     if user is not None:
@@ -189,7 +193,7 @@
     invocation_message += f": {command}"
     logging.log(log_command_level, invocation_message)
     try:
-        if user is not None and user != properties["test-user"]:
+        if user is not None:
             if not properties["sudo-rights"]:
                 pytest.skip(
                     "This test requires the test user to have unprompted sudo rights",
@@ -1278,7 +1282,9 @@
             slurm_prefix = f"{properties['slurm-sbin-dir']}/.."
 
         version_str = (
-            run_command_output(f"sudo {slurm_prefix}/{component} -V", quiet=True)
+            run_command_output(
+                f"{slurm_prefix}/{component} -V", quiet=True, user="root"
+            )
             .strip()
             .replace("slurm ", "")
         )
@@ -2481,7 +2487,7 @@
         False
     """
 
-    user_name = get_user_name()
+    user_name = properties["test-user"]
 
     run_command(f"scancel -u {user_name}", fatal=fatal, quiet=quiet)
 
@@ -4125,7 +4131,9 @@
                         augmentation_dict[parameter_name] = parameter_value
             elif parameter_name == "Features":
                 required_features = set(parameter_value.split(","))
-                node_features = set(lower_node_dict.get("features", "").split(","))
+                features = lower_node_dict.get("features", [])
+                features = features[0] if features else ""
+                node_features = set(features.split(","))
                 if not required_features.issubset(node_features):
                     if node_qualifies:
                         node_qualifies = False
@@ -4224,7 +4232,16 @@
     with open(script_name, "w") as f:
         f.write("#!/bin/bash\n")
         f.write(script_contents)
-    os.chmod(script_name, 0o0700)
+
+    run_command(f"chmod 777 {script_name}", user="root", fatal=True, quiet=True)
+
+    if properties["test-user-set"]:
+        run_command(
+            f"chown {properties['test-user']} {script_name}",
+            user="root",
+            fatal=True,
+            quiet=True,
+        )
 
 
 def wait_for_file(file_name, **repeat_until_kwargs):
@@ -4883,7 +4900,7 @@
             if match := re.search(r"^\s*(?i:SlurmUser)\s*=\s*(.*)$", line):
                 properties["slurm-user"] = match.group(1)
 else:
-    # slurm.conf is not readable as test-user. We will try reading it as root
+    # slurm.conf is not readable, we will try reading it as root
     results = run_command(
         f"grep -i SlurmUser {slurm_config_file}", user="root", quiet=True
     )
@@ -4894,7 +4911,16 @@
             properties["slurm-user"] = match.group(1)
 
 properties["submitted-jobs"] = []
-properties["test-user"] = pwd.getpwuid(os.getuid()).pw_name
+if "slurmtestuser" in testsuite_config:
+    properties["test-user"] = testsuite_config["slurmtestuser"]
+    properties["test-user-set"] = True
+else:
+    properties["test-user"] = pwd.getpwuid(os.getuid()).pw_name
+    properties["test-user-set"] = False
+
+properties["test-user-uid"] = pwd.getpwnam(properties["test-user"]).pw_uid
+properties["test-user-gid"] = pwd.getpwnam(properties["test-user"]).pw_gid
+
 properties["auto-config"] = False
 properties["allow-slurmdbd-modify"] = False
 properties["slurmrestd-started"] = False
diff --git a/testsuite/python/tests/test_100_2.py b/testsuite/python/tests/test_100_2.py
index 5179193..456c3c4 100644
--- a/testsuite/python/tests/test_100_2.py
+++ b/testsuite/python/tests/test_100_2.py
@@ -281,7 +281,6 @@
             "-N1 --qos=qos1",
             "false",
             fatal=True,
-            user=atf.properties["test-user"],
         )
     )
     jobs.append(atf.submit_job("sbatch", "-N2 --qos=qos2", "false", fatal=True))
@@ -295,7 +294,6 @@
             "-N1 --qos=qos1",
             "sleep 300",
             fatal=True,
-            user=atf.properties["test-user"],
         )
     )
     jobs.append(atf.submit_job("sbatch", "-N2 --qos=qos2", "sleep 300", fatal=True))
diff --git a/testsuite/python/tests/test_102_1.py b/testsuite/python/tests/test_102_1.py
index 73647d5..bfc2aea 100644
--- a/testsuite/python/tests/test_102_1.py
+++ b/testsuite/python/tests/test_102_1.py
@@ -54,6 +54,15 @@
     )
 
 
+@pytest.fixture(scope="module", autouse=True)
+def clean_clusters():
+    yield
+    atf.run_command(
+        f"sacctmgr -i delete cluster {cluster_string}",
+        user=atf.properties["slurm-user"],
+    )
+
+
 @pytest.fixture(scope="function")
 def no_federations():
     atf.run_command(
diff --git a/testsuite/python/tests/test_105_5.py b/testsuite/python/tests/test_105_5.py
index 9ba1ccc..ad39540 100644
--- a/testsuite/python/tests/test_105_5.py
+++ b/testsuite/python/tests/test_105_5.py
@@ -8,7 +8,7 @@
 import pytest
 import re
 
-user_name = atf.get_user_name()
+user_name = atf.properties["test-user"]
 
 
 @pytest.fixture(scope="module", autouse=True)
diff --git a/testsuite/python/tests/test_115_1.py b/testsuite/python/tests/test_115_1.py
index 9a3d37b..c0cbb10 100644
--- a/testsuite/python/tests/test_115_1.py
+++ b/testsuite/python/tests/test_115_1.py
@@ -201,10 +201,10 @@
     # Verify they were all populated with an id
     for user in user1, user2:
         if user not in user_account_id:
-            atf.log_die(f"Account association for {user} was not created")
+            pytest.fail(f"Account association for {user} was not created")
         for account in account1, account2, account3:
             if account not in user_account_id[user]:
-                atf.log_die(f"Association for {user} and {account} was not created")
+                pytest.fail(f"Association for {user} and {account} was not created")
 
     # Populate user_wckey_id dictionary with user-wckey association ids
     output = atf.run_command_output(
@@ -221,9 +221,9 @@
     # Verify they were all populated with an id
     for user in user1, user2:
         if user not in user_wckey_id:
-            atf.log_die(f"WCKey association for {user} was not created")
+            pytest.fail(f"WCKey association for {user} was not created")
         if wckey1 not in user_wckey_id[user]:
-            atf.log_die(f"Association for {user} and {wckey1} was not created")
+            pytest.fail(f"Association for {user} and {wckey1} was not created")
 
 
 @pytest.fixture(scope="module")
@@ -284,17 +284,17 @@
             rf"{cluster}\|{account1}\|{user_account_id[user1][account1]}\|{wckey1}\|{user_wckey_id[user1][wckey1]}\|{job1_start_string}\|{job1_end_string}\|{job1_duration_string}",
             output,
         ):
-            atf.log_die("The job accounting data was not loaded correctly for job1")
+            pytest.fail("The job accounting data was not loaded correctly for job1")
         if not re.search(
             rf"{cluster}\|{account3}\|{user_account_id[user2][account3]}\|{wckey1}\|{user_wckey_id[user2][wckey1]}\|{job2_start_string}\|{job2_end_string}\|{job2_duration_string}",
             output,
         ):
-            atf.log_die("The job accounting data was not loaded correctly for job2")
+            pytest.fail("The job accounting data was not loaded correctly for job2")
         if not re.search(
             rf"{cluster}\|{account2}\|{user_account_id[user1][account2]}\|{wckey1}\|{user_wckey_id[user1][wckey1]}\|{job3_start_string}\|{job3_end_string}\|{job3_duration_string}",
             output,
         ):
-            atf.log_die("The job accounting data was not loaded correctly for job3")
+            pytest.fail("The job accounting data was not loaded correctly for job3")
 
         # Use sacctmgr to see if the node event loaded
         output = atf.run_command_output(
@@ -306,14 +306,14 @@
             rf"{cluster}\|\|{period_start_string}\|{period_end_string}\|{cluster_cpus}",
             output,
         ):
-            atf.log_die(
+            pytest.fail(
                 "The event accounting data was not loaded correctly for the cluster"
             )
         if not re.search(
             rf"{cluster}\|{node0}\|{node0_start_string}\|{node0_end_string}\|{node0_cpus}",
             output,
         ):
-            atf.log_die("The event accounting data was not loaded correctly for node0")
+            pytest.fail("The event accounting data was not loaded correctly for node0")
 
         # Use sacctmgr to roll up the time period
         atf.run_command_output(
diff --git a/testsuite/python/tests/test_116_12.py b/testsuite/python/tests/test_116_12.py
index 3014cd0..619a207 100644
--- a/testsuite/python/tests/test_116_12.py
+++ b/testsuite/python/tests/test_116_12.py
@@ -126,7 +126,7 @@
     fpc.remove_file(file_err)
 
     # Test %u puts the user name in the file name
-    user_name = atf.get_user_name()
+    user_name = atf.properties["test-user"]
     file_out = fpc.create_file_path("u")
     atf.run_job(f"--output={file_out} -N1 -O id")
     file_out = fpc.get_tmp_file()
@@ -175,7 +175,6 @@
     srun -O --output={file_out} true
 done""",
     )
-    os.chmod(file_in, 0o0777)
     job_id = atf.submit_job_sbatch(f"-N{node_count} --output /dev/null {str(file_in)}")
     atf.wait_for_job_state(job_id, "DONE")
     tmp_dir_list = os.listdir(tmp_path)
@@ -192,7 +191,6 @@
     srun -O --error={file_err} true
 done""",
     )
-    os.chmod(file_in, 0o0777)
     job_id = atf.submit_job_sbatch(f"-N{node_count} --output /dev/null {str(file_in)}")
     atf.wait_for_job_state(job_id, "DONE")
     tmp_dir_list = os.listdir(tmp_path)
diff --git a/testsuite/python/tests/test_116_44.py b/testsuite/python/tests/test_116_44.py
index c58d8ff..8c5e007 100644
--- a/testsuite/python/tests/test_116_44.py
+++ b/testsuite/python/tests/test_116_44.py
@@ -25,7 +25,7 @@
 
     num_tasks = 4
     node_count = 1
-    my_uid = os.getuid()
+    test_uid = atf.properties["test-user-uid"]
 
     atf.make_bash_script(
         task_prolog,
@@ -65,10 +65,10 @@
 
     atf.wait_for_file(file_out_pre)
     output = atf.run_command_output(f"cat {file_out_pre}")
-    match_uid = re.findall(rf"uid={my_uid}", output)
+    match_uid = re.findall(rf"uid={test_uid}", output)
     assert len(match_uid) == num_tasks, "Task prolog output is missing or uid mismatch"
 
     atf.wait_for_file(file_out_post)
     output = atf.run_command_output(f"cat {file_out_post}")
-    match_uid = re.findall(rf"uid={my_uid}", output)
+    match_uid = re.findall(rf"uid={test_uid}", output)
     assert len(match_uid) == num_tasks, "Task epilog output is missing or uid mismatch"
diff --git a/testsuite/python/tests/test_116_46.py b/testsuite/python/tests/test_116_46.py
index 98d23ea..058507d 100644
--- a/testsuite/python/tests/test_116_46.py
+++ b/testsuite/python/tests/test_116_46.py
@@ -2,7 +2,6 @@
 # Copyright (C) SchedMD LLC.
 ############################################################################
 import atf
-import os
 import pytest
 import re
 
@@ -31,7 +30,6 @@
 sleep 20
 """,
     )
-    os.chmod(file_in, 0o0755)
     run_error = atf.run_command_error(
         f"srun -n{task_count} -N{node_count} -O -W2 {file_in}"
     )
diff --git a/testsuite/python/tests/test_116_47.py b/testsuite/python/tests/test_116_47.py
index 13c100f..22a0a11 100644
--- a/testsuite/python/tests/test_116_47.py
+++ b/testsuite/python/tests/test_116_47.py
@@ -2,7 +2,6 @@
 # Copyright (C) SchedMD LLC.
 ############################################################################
 import atf
-import os
 import pytest
 import re
 
@@ -21,7 +20,7 @@
 # Verify that a job executes as the appropriate user and group
 def test_uid(srun_output):
     """Verify that a job executes as the invoking user"""
-    test_uid = os.geteuid()
+    test_uid = atf.properties["test-user-uid"]
     job_uid = (
         int(match.group(1)) if (match := re.search(r"uid=(\d+)", srun_output)) else None
     )
@@ -30,7 +29,7 @@
 
 def test_gid(srun_output):
     """Verify that a job executes as the invoking group"""
-    test_gid = os.getegid()
+    test_gid = atf.properties["test-user-gid"]
     job_gid = (
         int(match.group(1)) if (match := re.search(r"gid=(\d+)", srun_output)) else None
     )
diff --git a/testsuite/python/tests/test_123_4.py b/testsuite/python/tests/test_123_4.py
index c1e7b45..75e9036 100644
--- a/testsuite/python/tests/test_123_4.py
+++ b/testsuite/python/tests/test_123_4.py
@@ -32,10 +32,8 @@
     result = atf.run_command(create_res, user=atf.properties["slurm-user"])
     assert result["exit_code"] == 0, "Couldn't create the reservation!"
 
-    # Try to run a job as atf
-    result = atf.run_command(
-        f"srun -N1 --reservation={res_name} true", user=atf.properties["test-user"]
-    )
+    # Try to run a job as test-user
+    result = atf.run_command(f"srun -N1 --reservation={res_name} true")
     assert result["exit_code"] != 0, "The job should have been denied for user!"
     assert (
         "Access denied" in result["stderr"]
diff --git a/testsuite/python/tests/test_123_5.py b/testsuite/python/tests/test_123_5.py
index 9b20de8..adddcae 100644
--- a/testsuite/python/tests/test_123_5.py
+++ b/testsuite/python/tests/test_123_5.py
@@ -86,7 +86,6 @@
     # Try to run a job as in the wrong QOS
     result = atf.run_command(
         f"srun -N1 --reservation={res_name} --account={acct1} --qos=normal true",
-        user=atf.properties["test-user"],
     )
     assert (
         result["exit_code"] != 0
@@ -98,7 +97,6 @@
     # Try to run a job as in the correct QOS
     result = atf.run_command(
         f"srun -N1 --reservation={res_name} --account={acct1} --qos=normal,{qos1} true",
-        user=atf.properties["test-user"],
     )
     assert (
         result["exit_code"] == 0
diff --git a/testsuite/python/tests/test_123_6.py b/testsuite/python/tests/test_123_6.py
index 1f940c8..2f52140 100644
--- a/testsuite/python/tests/test_123_6.py
+++ b/testsuite/python/tests/test_123_6.py
@@ -80,7 +80,6 @@
     # Try to run a job as in the wrong partition
     result = atf.run_command(
         f"srun -N1 --reservation={res_name} --partition={partitions[1]} true",
-        user=atf.properties["test-user"],
     )
     assert (
         result["exit_code"] != 0
@@ -92,7 +91,6 @@
     # Try to run a job that can use either partition
     result = atf.run_command(
         f"srun -N1 --reservation={res_name} --partition={partitions[1]},{partitions[0]} true",
-        user=atf.properties["test-user"],
     )
     assert (
         result["exit_code"] == 0