Debian Codegraph @

debian/tests/unit-config

raw
1 #!/usr/bin/python3
2 # autopkgtest check: enable/disable/configure units
3 # (C) 2015 Canonical Ltd.
4 # Author: Martin Pitt <[email protected]>
5
6 import unittest
7 import subprocess
8 import os
9 import sys
10 import tempfile
11 from glob import glob
12
13 system_unit_dir = subprocess.check_output(
14 ['pkg-config', '--variable=systemdsystemunitdir', 'systemd'],
15 universal_newlines=True).strip()
16
17
18 class EnableTests(unittest.TestCase):
19 def tearDown(self):
20 # remove all traces from our test unit
21 f = glob(system_unit_dir + '/test_enable*.service')
22 f += glob(system_unit_dir + '/*/test_enable*.service')
23 f += glob('/etc/systemd/system/test_enable*.service')
24 f += glob('/etc/systemd/system/*/test_enable*.service')
25 f += glob('/etc/init.d/test_enable*')
26 f += glob('/etc/rc?.d/???test_enable*')
27 [os.unlink(i) for i in f]
28 subprocess.check_call(['systemctl', 'daemon-reload'])
29
30 def create_unit(self, suffix='', enable=False):
31 '''Create a test unit'''
32
33 unit = os.path.join(system_unit_dir,
34 'test_enable%s.service' % suffix)
35 with open(unit, 'w') as f:
36 f.write('''[Unit]
37 Description=Testsuite unit %s
38 [Service]
39 ExecStart=/bin/echo hello
40 [Install]
41 WantedBy=multi-user.target
42 ''' % suffix)
43
44 if enable:
45 os.symlink(unit, '/etc/systemd/system/multi-user.target.wants/' +
46 os.path.basename(unit))
47
48 return unit
49
50 def assertEnabled(self, enabled, unit='test_enable.service'):
51 '''assert that given unit has expected state'''
52
53 systemctl = subprocess.Popen(['systemctl', 'is-enabled', unit],
54 stdout=subprocess.PIPE,
55 universal_newlines=True)
56 out = systemctl.communicate()[0].strip()
57 if enabled:
58 self.assertEqual(systemctl.returncode, 0)
59 self.assertEqual(out, 'enabled')
60 else:
61 self.assertEqual(systemctl.returncode, 1)
62 self.assertEqual(out, 'disabled')
63
64 def test_unit_enable(self):
65 '''enable unit'''
66
67 self.create_unit()
68 self.assertEnabled(False)
69 # also works without .service suffix
70 self.assertEnabled(False, unit='test_enable')
71
72 subprocess.check_call(['systemctl', 'enable', 'test_enable'])
73
74 self.assertEnabled(True)
75 # also works without .service suffix
76 self.assertEnabled(True, unit='test_enable')
77
78 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
79 self.assertTrue(os.path.islink(l))
80 self.assertTrue(os.readlink(l) == system_unit_dir + '/test_enable.service' or
81 os.readlink(l) == '../test_enable.service')
82
83 # enable should be idempotent
84 subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])
85 self.assertEnabled(True)
86
87 def test_unit_disable(self):
88 '''disable unit'''
89
90 self.create_unit(enable=True)
91 self.assertEnabled(True)
92 # also works without .service suffix
93 self.assertEnabled(True, unit='test_enable')
94
95 subprocess.check_call(['systemctl', 'disable', 'test_enable'])
96
97 self.assertEnabled(False)
98 # also works without .service suffix
99 self.assertEnabled(False, unit='test_enable')
100
101 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
102 self.assertFalse(os.path.islink(l))
103
104 # disable should be idempotent
105 subprocess.check_call(['systemctl', 'disable', 'test_enable.service'])
106 self.assertEnabled(False)
107
108 def test_unit_alias_enable(self):
109 '''enable unit with an alias'''
110
111 u = self.create_unit()
112 with open(u, 'a') as f:
113 f.write('Alias=test_enablea.service\n')
114
115 self.assertEnabled(False)
116
117 subprocess.check_call(['systemctl', 'enable', 'test_enable'])
118
119 self.assertEnabled(True)
120
121 # enablement symlink
122 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
123 self.assertTrue(os.path.islink(l))
124 self.assertTrue(os.readlink(l) == system_unit_dir + '/test_enable.service' or
125 os.readlink(l) == '../test_enable.service')
126
127 # alias symlink
128 l = '/etc/systemd/system/test_enablea.service'
129 self.assertTrue(os.path.islink(l))
130 self.assertTrue(os.readlink(l) == system_unit_dir + '/test_enable.service' or
131 os.readlink(l) == 'test_enable.service')
132
133 def test_unit_alias_disable(self):
134 '''disable unit with an alias'''
135
136 u = self.create_unit()
137 with open(u, 'a') as f:
138 f.write('Alias=test_enablea.service\n')
139 os.symlink(system_unit_dir + '/test_enable.service',
140 '/etc/systemd/system/test_enablea.service')
141
142 subprocess.check_call(['systemctl', 'disable', 'test_enable'])
143
144 self.assertEnabled(False)
145
146 # enablement symlink
147 l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
148 self.assertFalse(os.path.islink(l))
149
150 # alias symlink
151 l = '/etc/systemd/system/test_enablea.service'
152 self.assertFalse(os.path.islink(l))
153
154 def test_unit_link(self):
155 '''systemctl link'''
156
157 with tempfile.NamedTemporaryFile(suffix='.service') as f:
158 f.write(b'[Unit]\n')
159 f.flush()
160 subprocess.check_call(['systemctl', 'link', f.name])
161
162 unit = os.path.basename(f.name)
163 l = os.path.join('/etc/systemd/system', unit)
164 self.assertEqual(os.readlink(l), f.name)
165
166 # disable it again
167 subprocess.check_call(['systemctl', 'disable', unit])
168 # this should also remove the unit symlink
169 self.assertFalse(os.path.islink(l))
170
171 def test_unit_enable_full_path(self):
172 '''systemctl enable a unit in a non-default path'''
173
174 with tempfile.NamedTemporaryFile(suffix='.service') as f:
175 f.write(b'''[Unit]
176 Description=test
177 [Service]
178 ExecStart=/bin/true
179 [Install]
180 WantedBy=multi-user.target''')
181 f.flush()
182 unit = os.path.basename(f.name)
183
184 # now enable it
185 subprocess.check_call(['systemctl', 'enable', f.name])
186 self.assertEnabled(True, unit=unit)
187 l = os.path.join('/etc/systemd/system', unit)
188 self.assertEqual(os.readlink(l), f.name)
189 enable_l = '/etc/systemd/system/multi-user.target.wants/' + unit
190 self.assertTrue(os.readlink(enable_l) == f.name or
191 os.readlink(enable_l) == '../' + unit)
192
193 # disable it again
194 subprocess.check_call(['systemctl', 'disable', unit])
195 # self.assertEnabled(False) does not work as now systemd does not
196 # know about the unit at all any more
197 self.assertFalse(os.path.islink(enable_l))
198 # this should also remove the unit symlink
199 self.assertFalse(os.path.islink(l))
200
201
202 if __name__ == '__main__':
203 unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
204 verbosity=2))