]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-shutdown.py
test: wait until the test container is fully booted up
[thirdparty/systemd.git] / test / test-shutdown.py
1 #!/usr/bin/python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 # pylint: disable=broad-except
4
5 import argparse
6 import logging
7 import signal
8 import sys
9 import time
10
11 import pexpect
12
13
14 def run(args):
15
16 ret = 1
17 logger = logging.getLogger("test-shutdown")
18 logfile = None
19
20 if args.logfile:
21 logger.debug("Logging pexpect IOs to %s", args.logfile)
22 logfile = open(args.logfile, 'w')
23 elif args.verbose:
24 logfile = sys.stdout
25
26 logger.info("spawning test")
27 console = pexpect.spawn(args.command, args.arg, logfile=logfile, env={
28 "TERM": "linux",
29 }, encoding='utf-8', timeout=60)
30
31 logger.debug("child pid %d", console.pid)
32
33 try:
34 logger.info("waiting for login prompt")
35 console.expect('H login: ', 10)
36
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')
43 console.send('c')
44 console.expect('screen1 ', 10)
45
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)
49
50 # console.interact()
51
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)
56
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)
62
63 console.sendcontrol('a')
64 console.send('0')
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)
68
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)
72
73 logger.info("cancel shutdown")
74 console.sendline('shutdown -c')
75 console.sendcontrol('a')
76 console.send('1')
77 console.expect('System shutdown has been cancelled', 2)
78
79 logger.info("call for reboot")
80 console.sendline('sleep 10; shutdown -r now')
81 console.sendcontrol('a')
82 console.send('0')
83 console.expect("The system will reboot now!", 12)
84
85 logger.info("waiting for reboot")
86
87 console.expect('H login: ', 60)
88 console.sendline('root')
89 console.expect('bash.*# ', 10)
90
91 console.sendline('> /testok')
92
93 logger.info("power off")
94 console.sendline('poweroff')
95
96 logger.info("expect termination now")
97 console.expect(pexpect.EOF)
98
99 ret = 0
100 except Exception as e:
101 logger.error(e)
102 logger.info("killing child pid %d", console.pid)
103
104 # Ask systemd-nspawn to stop and release the container's resources properly.
105 console.kill(signal.SIGTERM)
106
107 for _ in range(10):
108 if not console.isalive():
109 break
110
111 time.sleep(1)
112 else:
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)
117
118 return ret
119
120 def main():
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")
126
127 args = parser.parse_args()
128
129 if args.verbose:
130 level = logging.DEBUG
131 else:
132 level = logging.INFO
133
134 logging.basicConfig(level=level)
135
136 return run(args)
137
138 if __name__ == '__main__':
139 sys.exit(main())
140
141 # vim: sw=4 et