]>
git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-shutdown.py
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=broad-except
17 logger
= logging
.getLogger("test-shutdown")
21 logger
.debug("Logging pexpect IOs to %s", args
.logfile
)
22 logfile
= open(args
.logfile
, 'w')
26 logger
.info("spawning test")
27 console
= pexpect
.spawn(args
.command
, args
.arg
, logfile
=logfile
, env
={
29 }, encoding
='utf-8', timeout
=60)
31 logger
.debug("child pid %d", console
.pid
)
34 logger
.info("waiting for login prompt")
35 console
.expect('H login: ', 10)
37 logger
.info("log in and start screen")
38 console
.sendline('root')
39 console
.expect('bash.*# ', 10)
40 console
.sendline('screen')
41 console
.expect('screen0 ', 10)
42 console
.sendcontrol('a')
44 console
.expect('screen1 ', 10)
46 logger
.info('wait for the machine to fully boot')
47 console
.sendline('systemctl is-system-running --wait')
48 console
.expect(r
'\b(running|degraded)\b', 60)
52 console
.sendline('tty')
53 console
.expect(r
'/dev/(pts/\d+)')
54 pty
= console
.match
.group(1)
55 logger
.info("window 1 at tty %s", pty
)
57 logger
.info("schedule reboot")
58 console
.sendline('shutdown -r')
59 console
.expect("Reboot scheduled for (?P<date>.*), use 'shutdown -c' to cancel", 2)
60 date
= console
.match
.group('date')
61 logger
.info("reboot scheduled for %s", date
)
63 console
.sendcontrol('a')
65 logger
.info("verify broadcast message")
66 console
.expect(f
'Broadcast message from root@H on {pty}', 2)
67 console
.expect(f
'The system will reboot at {date}', 2)
69 logger
.info("check show output")
70 console
.sendline('shutdown --show')
71 console
.expect(f
"Reboot scheduled for {date}, use 'shutdown -c' to cancel", 2)
73 logger
.info("cancel shutdown")
74 console
.sendline('shutdown -c')
75 console
.sendcontrol('a')
77 console
.expect('System shutdown has been cancelled', 2)
79 logger
.info("call for reboot")
80 console
.sendline('sleep 10; shutdown -r now')
81 console
.sendcontrol('a')
83 console
.expect("The system will reboot now!", 12)
85 logger
.info("waiting for reboot")
87 console
.expect('H login: ', 60)
88 console
.sendline('root')
89 console
.expect('bash.*# ', 10)
91 console
.sendline('> /testok')
93 logger
.info("power off")
94 console
.sendline('poweroff')
96 logger
.info("expect termination now")
97 console
.expect(pexpect
.EOF
)
100 except Exception as e
:
102 logger
.info("killing child pid %d", console
.pid
)
104 # Ask systemd-nspawn to stop and release the container's resources properly.
105 console
.kill(signal
.SIGTERM
)
108 if not console
.isalive():
113 # We haven't exited the loop early, so check if the process is
114 # still alive - if so, force-kill it.
115 if console
.isalive():
116 console
.terminate(force
=True)
121 parser
= argparse
.ArgumentParser(description
='test logind shutdown feature')
122 parser
.add_argument("-v", "--verbose", action
="store_true", help="verbose")
123 parser
.add_argument("--logfile", metavar
='FILE', help="Save all test input/output to the given path")
124 parser
.add_argument("command", help="command to run")
125 parser
.add_argument("arg", nargs
='*', help="args for command")
127 args
= parser
.parse_args()
130 level
= logging
.DEBUG
134 logging
.basicConfig(level
=level
)
138 if __name__
== '__main__':