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