]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
Tests: Add a new user with a specified non-existing gid
authoraborah-sudo <aborah@redhat.com>
Fri, 13 Mar 2026 05:33:10 +0000 (11:03 +0530)
committerIker Pedrosa <ikerpedrosam@gmail.com>
Mon, 16 Mar 2026 13:21:59 +0000 (14:21 +0100)
This is the transformation to Python of the test located in
`tests/usertools/01/05_useradd_invalid_numeric_primary_group.test`, which checks that `useradd` fails to create a new user with non-existing group

tests/system/tests/test_useradd.py

index 01a5001c430ffc1f70b3b3aaf9a1187987c7e108..3d2610928bfe19536e2191e5b5a50a24158b0902 100644 (file)
@@ -328,3 +328,35 @@ def test_useradd__add_user_with_existing_uid(shadow: Shadow):
         shadow.useradd("test2 -u 4242")
 
     assert exc_info.value.rc == 4, f"Expected return code 4 (UID already in use), got {exc_info.value.rc}"
+
+
+@pytest.mark.topology(KnownTopology.Shadow)
+def test_useradd__invalid_numeric_primary_group(shadow: Shadow):
+    """
+    :title: Add a new user with a specified non-existing gid
+    :steps:
+        1. Attempt to create user with -g 4242 option
+        2. Verify command fails with appropriate error code and message
+        3. Check passwd and group entries
+        4. Check home directory
+    :expectedresults:
+        1. Useradd command fails
+        2. Return code is 6 (specified group doesn't exist) and error message indicates group doesn't exist
+        3. No entries are added to passwd and group
+        4. No home directory is created
+    :customerscenario: False
+    """
+    with pytest.raises(ProcessError) as exc_info:
+        shadow.useradd("test1 -g 4242")
+
+    actual_rc = getattr(exc_info.value, "rc", getattr(exc_info.value, "returncode", None))
+    assert actual_rc == 6, f"Expected return code 6 (specified group doesn't exist), got {actual_rc}"
+
+    error_output = exc_info.value.stderr.lower() if exc_info.value.stderr else ""
+    assert (
+        "group" in error_output and "does not exist" in error_output
+    ), f"Error message should indicate group doesn't exist. Got: {error_output}"
+
+    assert shadow.tools.getent.passwd("test1") is None, "User test1 should not be found in passwd"
+    assert shadow.tools.getent.group("test1") is None, "Group test1 should not be found"
+    assert not shadow.fs.exists("/home/test1"), "Home directory should not be created"