]> git.ipfire.org Git - thirdparty/systemd.git/blob - test/test-shutdown.py
test: set pexpect's logfile early
[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 # console.interact()
47
48 console.sendline('tty')
49 console.expect(r'/dev/(pts/\d+)')
50 pty = console.match.group(1)
51 logger.info("window 1 at tty %s", pty)
52
53 logger.info("schedule reboot")
54 console.sendline('shutdown -r')
55 console.expect("Reboot scheduled for (?P<date>.*), use 'shutdown -c' to cancel", 2)
56 date = console.match.group('date')
57 logger.info("reboot scheduled for %s", date)
58
59 console.sendcontrol('a')
60 console.send('0')
61 logger.info("verify broadcast message")
62 console.expect(f'Broadcast message from root@H on {pty}', 2)
63 console.expect(f'The system will reboot at {date}', 2)
64
65 logger.info("check show output")
66 console.sendline('shutdown --show')
67 console.expect(f"Reboot scheduled for {date}, use 'shutdown -c' to cancel", 2)
68
69 logger.info("cancel shutdown")
70 console.sendline('shutdown -c')
71 console.sendcontrol('a')
72 console.send('1')
73 console.expect('System shutdown has been cancelled', 2)
74
75 logger.info("call for reboot")
76 console.sendline('sleep 10; shutdown -r now')
77 console.sendcontrol('a')
78 console.send('0')
79 console.expect("The system will reboot now!", 12)
80
81 logger.info("waiting for reboot")
82
83 console.expect('H login: ', 60)
84 console.sendline('root')
85 console.expect('bash.*# ', 10)
86
87 console.sendline('> /testok')
88
89 logger.info("power off")
90 console.sendline('poweroff')
91
92 logger.info("expect termination now")
93 console.expect(pexpect.EOF)
94
95 ret = 0
96 except Exception as e:
97 logger.error(e)
98 logger.info("killing child pid %d", console.pid)
99
100 # Ask systemd-nspawn to stop and release the container's resources properly.
101 console.kill(signal.SIGTERM)
102
103 for _ in range(10):
104 if not console.isalive():
105 break
106
107 time.sleep(1)
108 else:
109 # We haven't exited the loop early, so check if the process is
110 # still alive - if so, force-kill it.
111 if console.isalive():
112 console.terminate(force=True)
113
114 return ret
115
116 def main():
117 parser = argparse.ArgumentParser(description='test logind shutdown feature')
118 parser.add_argument("-v", "--verbose", action="store_true", help="verbose")
119 parser.add_argument("--logfile", metavar='FILE', help="Save all test input/output to the given path")
120 parser.add_argument("command", help="command to run")
121 parser.add_argument("arg", nargs='*', help="args for command")
122
123 args = parser.parse_args()
124
125 if args.verbose:
126 level = logging.DEBUG
127 else:
128 level = logging.INFO
129
130 logging.basicConfig(level=level)
131
132 return run(args)
133
134 if __name__ == '__main__':
135 sys.exit(main())
136
137 # vim: sw=4 et