]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/system/framework/roles/shadow.py: implement binding for `chpasswd`
authorIker Pedrosa <ipedrosa@redhat.com>
Mon, 8 Sep 2025 14:49:56 +0000 (16:49 +0200)
committerSerge Hallyn <serge@hallyn.com>
Wed, 25 Mar 2026 02:33:15 +0000 (21:33 -0500)
Signed-off-by: Iker Pedrosa <ipedrosa@redhat.com>
tests/system/framework/roles/shadow.py

index 4f9d3bf2f19c971fd943df1b7f7a8b397204fbf9..d267feb4e393e90c274a5d97623083a56845d9f8 100644 (file)
@@ -413,3 +413,24 @@ class Shadow(BaseLinuxRole[ShadowHost]):
             return self._passwd_as_root(*args, new_password=new_password)
         else:
             return self._passwd_as_user(*args, run_as=run_as, old_password=old_password, new_password=new_password)
+
+    def chpasswd(self, *args, passwords_data: str | None = None, file: str | None = None) -> ProcessResult:
+        """
+        Update passwords in batch.
+
+        Updates user passwords in batch by reading username:password pairs.
+        If `passwords_data` is provided, this data is used to simulate an interactive prompt.
+        Otherwise, the command reads from a file specified in `file`.
+        """
+        cmd_args = " ".join(args)
+
+        if passwords_data:
+            self.logger.info(f"Running chpasswd interactively on {self.host.hostname}")
+            cmd = self.host.conn.run(f"echo '{passwords_data}' | chpasswd {cmd_args}", log_level=ProcessLogLevel.Error)
+        else:
+            self.logger.info(f"Running chpasswd from file on {self.host.hostname}")
+            cmd = self.host.conn.run(f"chpasswd {cmd_args} < {file}", log_level=ProcessLogLevel.Error)
+
+        self.host.discard_file("/etc/shadow")
+
+        return cmd