2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=broad-except
16 logger
= logging
.getLogger("test-shutdown")
20 logger
.debug("Logging pexpect IOs to %s", args
.logfile
)
21 logfile
= open(args
.logfile
, 'w')
25 logger
.info("spawning test")
26 console
= pexpect
.spawn(args
.command
, args
.arg
, logfile
=logfile
, env
={
28 }, encoding
='utf-8', timeout
=60)
30 logger
.debug("child pid %d", console
.pid
)
33 logger
.info("waiting for login prompt")
34 console
.expect('H login: ', 10)
36 logger
.info("log in and start screen")
37 console
.sendline('root')
38 console
.expect('bash.*# ', 10)
39 console
.sendline('screen')
40 console
.expect('screen0 ', 10)
41 console
.sendcontrol('a')
43 console
.expect('screen1 ', 10)
45 logger
.info('wait for the machine to fully boot')
46 console
.sendline('systemctl is-system-running --wait')
47 console
.expect(r
'\b(running|degraded)\b', 60)
51 console
.sendline('tty')
52 console
.expect(r
'/dev/(pts/\d+)')
53 pty
= console
.match
.group(1)
54 logger
.info("window 1 at tty %s", pty
)
56 logger
.info("schedule reboot")
57 console
.sendline('shutdown -r')
58 console
.expect("Reboot scheduled for (?P<date>.*), use 'shutdown -c' to cancel", 2)
59 date
= console
.match
.group('date')
60 logger
.info("reboot scheduled for %s", date
)
62 console
.sendcontrol('a')
64 logger
.info("verify broadcast message")
65 console
.expect(f
'Broadcast message from root@H on {pty}', 2)
66 console
.expect(f
'The system will reboot at {date}', 2)
68 logger
.info("check show output")
69 console
.sendline('shutdown --show')
70 console
.expect(f
"Reboot scheduled for {date}, use 'shutdown -c' to cancel", 2)
72 logger
.info("cancel shutdown")
73 console
.sendline('shutdown -c')
74 console
.sendcontrol('a')
76 console
.expect('System shutdown has been cancelled', 2)
78 logger
.info("call for reboot")
79 console
.sendline('sleep 10; shutdown -r now')
80 console
.sendcontrol('a')
82 console
.expect("The system will reboot now!", 12)
84 logger
.info("waiting for reboot")
86 console
.expect('H login: ', 60)
87 console
.sendline('root')
88 console
.expect('bash.*# ', 10)
90 console
.sendline('> /testok')
92 logger
.info("power off")
93 console
.sendline('poweroff')
95 logger
.info("expect termination now")
96 console
.expect(pexpect
.EOF
)
99 except Exception as e
:
101 logger
.info("killing child pid %d", console
.pid
)
103 # Ask systemd-nspawn to stop and release the container's resources properly.
104 console
.kill(signal
.SIGTERM
)
107 if not console
.isalive():
112 # We haven't exited the loop early, so check if the process is
113 # still alive - if so, force-kill it.
114 if console
.isalive():
115 console
.terminate(force
=True)
120 parser
= argparse
.ArgumentParser(description
='test logind shutdown feature')
121 parser
.add_argument("-v", "--verbose", action
="store_true", help="verbose")
122 parser
.add_argument("--logfile", metavar
='FILE', help="Save all test input/output to the given path")
123 parser
.add_argument("command", help="command to run")
124 parser
.add_argument("arg", nargs
='*', help="args for command")
126 args
= parser
.parse_args()
129 level
= logging
.DEBUG
133 logging
.basicConfig(level
=level
)
137 if __name__
== '__main__':