]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-systemd-tmpfiles.py
pkgconfig: define variables relative to ${prefix}/${rootprefix}/${sysconfdir}
[thirdparty/systemd.git] / src / test / test-systemd-tmpfiles.py
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: LGPL-2.1+
3 #
4 # systemd is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU Lesser General Public License as published by
6 # the Free Software Foundation; either version 2.1 of the License, or
7 # (at your option) any later version.
8
9 import os
10 import sys
11 import socket
12 import subprocess
13 import tempfile
14 import pwd
15 import grp
16
17 try:
18 from systemd import id128
19 except ImportError:
20 id128 = None
21
22 EX_DATAERR = 65 # from sysexits.h
23 EXIT_TEST_SKIP = 77
24
25 try:
26 subprocess.run
27 except AttributeError:
28 sys.exit(EXIT_TEST_SKIP)
29
30 exe_with_args = sys.argv[1:]
31
32 def test_line(line, *, user, returncode=EX_DATAERR, extra={}):
33 args = ['--user'] if user else []
34 print('Running {} on {!r}'.format(' '.join(exe_with_args + args), line))
35 c = subprocess.run(exe_with_args + ['--create', '-'] + args,
36 input=line, stdout=subprocess.PIPE, universal_newlines=True,
37 **extra)
38 assert c.returncode == returncode, c
39
40 def test_invalids(*, user):
41 test_line('asdfa', user=user)
42 test_line('f "open quote', user=user)
43 test_line('f closed quote""', user=user)
44 test_line('Y /unknown/letter', user=user)
45 test_line('w non/absolute/path', user=user)
46 test_line('s', user=user) # s is for short
47 test_line('f!! /too/many/bangs', user=user)
48 test_line('f++ /too/many/plusses', user=user)
49 test_line('f+!+ /too/many/plusses', user=user)
50 test_line('f!+! /too/many/bangs', user=user)
51 test_line('w /unresolved/argument - - - - "%Y"', user=user)
52 test_line('w /unresolved/argument/sandwich - - - - "%v%Y%v"', user=user)
53 test_line('w /unresolved/filename/%Y - - - - "whatever"', user=user)
54 test_line('w /unresolved/filename/sandwich/%v%Y%v - - - - "whatever"', user=user)
55 test_line('w - - - - - "no file specfied"', user=user)
56 test_line('C - - - - - "no file specfied"', user=user)
57 test_line('C non/absolute/path - - - - -', user=user)
58 test_line('b - - - - - -', user=user)
59 test_line('b 1234 - - - - -', user=user)
60 test_line('c - - - - - -', user=user)
61 test_line('c 1234 - - - - -', user=user)
62 test_line('t - - -', user=user)
63 test_line('T - - -', user=user)
64 test_line('a - - -', user=user)
65 test_line('A - - -', user=user)
66 test_line('h - - -', user=user)
67 test_line('H - - -', user=user)
68
69 def test_uninitialized_t():
70 if os.getuid() == 0:
71 return
72
73 test_line('w /foo - - - - "specifier for --user %t"',
74 user=True, returncode=0, extra={'env':{}})
75
76 def test_content(line, expected, *, user, extra={}):
77 d = tempfile.TemporaryDirectory(prefix='test-systemd-tmpfiles.')
78 arg = d.name + '/arg'
79 spec = line.format(arg)
80 test_line(spec, user=user, returncode=0, extra=extra)
81 content = open(arg).read()
82 print('expect: {!r}\nactual: {!r}'.format(expected, content))
83 assert content == expected
84
85 def test_valid_specifiers(*, user):
86 test_content('f {} - - - - two words', 'two words', user=user)
87 if id128:
88 try:
89 test_content('f {} - - - - %m', '{}'.format(id128.get_machine().hex), user=user)
90 except AssertionError as e:
91 print(e)
92 print('/etc/machine-id: {!r}'.format(open('/etc/machine-id').read()))
93 print('/proc/cmdline: {!r}'.format(open('/proc/cmdline').read()))
94 print('skipping')
95 test_content('f {} - - - - %b', '{}'.format(id128.get_boot().hex), user=user)
96 test_content('f {} - - - - %H', '{}'.format(socket.gethostname()), user=user)
97 test_content('f {} - - - - %v', '{}'.format(os.uname().release), user=user)
98 test_content('f {} - - - - %U', '{}'.format(os.getuid()), user=user)
99 test_content('f {} - - - - %G', '{}'.format(os.getgid()), user=user)
100
101 puser = pwd.getpwuid(os.getuid())
102 test_content('f {} - - - - %u', '{}'.format(puser.pw_name), user=user)
103
104 pgroup = grp.getgrgid(os.getgid())
105 test_content('f {} - - - - %g', '{}'.format(pgroup.gr_name), user=user)
106
107 # Note that %h is the only specifier in which we look the environment,
108 # because we check $HOME. Should we even be doing that?
109 home = os.path.expanduser("~")
110 test_content('f {} - - - - %h', '{}'.format(home), user=user)
111
112 xdg_runtime_dir = os.getenv('XDG_RUNTIME_DIR')
113 if xdg_runtime_dir is not None or not user:
114 test_content('f {} - - - - %t',
115 xdg_runtime_dir if user else '/run',
116 user=user)
117
118 xdg_config_home = os.getenv('XDG_CONFIG_HOME')
119 if xdg_config_home is not None or not user:
120 test_content('f {} - - - - %S',
121 xdg_config_home if user else '/var/lib',
122 user=user)
123
124 xdg_cache_home = os.getenv('XDG_CACHE_HOME')
125 if xdg_cache_home is not None or not user:
126 test_content('f {} - - - - %C',
127 xdg_cache_home if user else '/var/cache',
128 user=user)
129
130 if xdg_config_home is not None or not user:
131 test_content('f {} - - - - %L',
132 xdg_config_home + '/log' if user else '/var/log',
133 user=user)
134
135 test_content('f {} - - - - %%', '%', user=user)
136
137 if __name__ == '__main__':
138 test_invalids(user=False)
139 test_invalids(user=True)
140 test_uninitialized_t()
141
142 test_valid_specifiers(user=False)
143 test_valid_specifiers(user=True)