]> git.proxmox.com Git - systemd.git/blob - test/test-shutdown.py
e181f976bea580e8fb5ed0f84b568c8a1e6dd257
[systemd.git] / test / test-shutdown.py
1 #!/usr/bin/python3
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3 #
4
5 import argparse
6 import logging
7 import sys
8
9 import pexpect
10
11
12 def run(args):
13
14 ret = 1
15 logger = logging.getLogger("test-shutdown")
16
17 logger.info("spawning test")
18 console = pexpect.spawn(args.command, args.arg, env={
19 "TERM": "linux",
20 }, encoding='utf-8', timeout=30)
21
22 if args.verbose:
23 console.logfile = sys.stdout
24
25 logger.debug("child pid %d", console.pid)
26
27 try:
28 logger.info("waiting for login prompt")
29 console.expect('H login: ', 10)
30
31 logger.info("log in and start screen")
32 console.sendline('root')
33 console.expect('bash.*# ', 10)
34 console.sendline('screen')
35 console.expect('screen0 ', 10)
36 console.sendcontrol('a')
37 console.send('c')
38 console.expect('screen1 ', 10)
39
40 # console.interact()
41
42 console.sendline('tty')
43 console.expect(r'/dev/(pts/\d+)')
44 pty = console.match.group(1)
45 logger.info("window 1 at line %s", pty)
46
47 logger.info("schedule reboot")
48 console.sendline('shutdown -r')
49 console.expect("Reboot scheduled for (?P<date>.*), use 'shutdown -c' to cancel", 2)
50 date = console.match.group('date')
51 logger.info("reboot scheduled for %s", date)
52
53 console.sendcontrol('a')
54 console.send('0')
55 logger.info("verify broadcast message")
56 console.expect('Broadcast message from root@H on %s' % pty, 2)
57 console.expect('The system will reboot at %s' % date, 2)
58
59 logger.info("check show output")
60 console.sendline('shutdown --show')
61 console.expect("Reboot scheduled for %s, use 'shutdown -c' to cancel" % date, 2)
62
63 logger.info("cancel shutdown")
64 console.sendline('shutdown -c')
65 console.sendcontrol('a')
66 console.send('1')
67 console.expect('System shutdown has been cancelled', 2)
68
69 logger.info("call for reboot")
70 console.sendline('sleep 10; shutdown -r now')
71 console.sendcontrol('a')
72 console.send('0')
73 console.expect("The system will reboot now!", 12)
74
75 logger.info("waiting for reboot")
76
77 console.expect('H login: ', 60)
78 console.sendline('root')
79 console.expect('bash.*# ', 10)
80
81 console.sendline('> /testok')
82
83 logger.info("power off")
84 console.sendline('poweroff')
85
86 logger.info("expect termination now")
87 console.expect(pexpect.EOF)
88
89 ret = 0
90 except Exception as e:
91 logger.error(e)
92 logger.info("killing child pid %d", console.pid)
93 console.terminate(force=True)
94
95 return ret
96
97 def main():
98 parser = argparse.ArgumentParser(description='test logind shutdown feature')
99 parser.add_argument("-v", "--verbose", action="store_true", help="verbose")
100 parser.add_argument("command", help="command to run")
101 parser.add_argument("arg", nargs='*', help="args for command")
102
103 args = parser.parse_args()
104
105 if args.verbose:
106 level = logging.DEBUG
107 else:
108 level = logging.INFO
109
110 logging.basicConfig(level=level)
111
112 return run(args)
113
114 if __name__ == '__main__':
115 sys.exit(main())
116
117 # vim: sw=4 et