update TODO
[systemd.io.git] / test / test-shutdown.py
blobd19a03742c246f6c5cd2f2b40da28be601f38d21
1 #!/usr/bin/python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=broad-except
5 import argparse
6 import logging
7 import signal
8 import sys
9 import time
11 import pexpect
14 def run(args):
15 ret = 1
16 logger = logging.getLogger("test-shutdown")
17 logfile = None
19 if args.logfile:
20 logger.debug("Logging pexpect IOs to %s", args.logfile)
21 logfile = open(args.logfile, 'w')
22 elif args.verbose:
23 logfile = sys.stdout
25 logger.info("spawning test")
26 console = pexpect.spawn(args.command, args.arg, logfile=logfile, env={
27 "TERM": "dumb",
28 }, encoding='utf-8', timeout=60)
30 logger.debug("child pid %d", console.pid)
32 try:
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')
42 console.send('c')
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)
49 # console.interact()
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')
63 console.send('0')
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')
75 console.send('1')
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')
81 console.send('0')
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)
98 ret = 0
99 except Exception as e:
100 logger.error(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)
106 for _ in range(10):
107 if not console.isalive():
108 break
110 time.sleep(1)
111 else:
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)
117 return ret
119 def main():
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()
128 if args.verbose:
129 level = logging.DEBUG
130 else:
131 level = logging.INFO
133 logging.basicConfig(level=level)
135 return run(args)
137 if __name__ == '__main__':
138 sys.exit(main())
140 # vim: sw=4 et