summaryrefslogtreecommitdiffstats
path: root/debian/tests/unit-config
blob: 1cfa4d432ae8182760a898f0b0473355fbaec7e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python3
# autopkgtest check: enable/disable/configure units
# (C) 2015 Canonical Ltd.
# Author: Martin Pitt <martin.pitt@ubuntu.com>

import unittest
import subprocess
import os
import sys
import tempfile
from glob import glob

system_unit_dir = subprocess.check_output(
    ['pkg-config', '--variable=systemdsystemunitdir', 'systemd'],
    universal_newlines=True).strip()
systemd_sysv_install = os.path.join(os.path.dirname(system_unit_dir),
                                    'systemd-sysv-install')


class EnableTests(unittest.TestCase):
    def tearDown(self):
        # remove all traces from our test unit
        f = glob(system_unit_dir + '/test_enable*.service')
        f += glob(system_unit_dir + '/*/test_enable*.service')
        f += glob('/etc/systemd/system/test_enable*.service')
        f += glob('/etc/systemd/system/*/test_enable*.service')
        f += glob('/etc/init.d/test_enable*')
        f += glob('/etc/rc?.d/???test_enable*')
        [os.unlink(i) for i in f]
        subprocess.check_call(['systemctl', 'daemon-reload'])

    def create_unit(self, suffix='', enable=False):
        '''Create a test unit'''

        unit = os.path.join(system_unit_dir,
                            'test_enable%s.service' % suffix)
        with open(unit, 'w') as f:
            f.write('''[Unit]
Description=Testsuite unit %s
[Service]
ExecStart=/bin/echo hello
[Install]
WantedBy=multi-user.target
''' % suffix)

        if enable:
            os.symlink(unit, '/etc/systemd/system/multi-user.target.wants/' +
                       os.path.basename(unit))

        return unit

    def create_sysv(self, suffix='', enable=False):
        '''Create a test SysV script'''

        script = '/etc/init.d/test_enable%s' % suffix
        with open(script, 'w') as f:
            f.write('''/bin/sh
### BEGIN INIT INFO
# Provides:          test_enable%s
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Testsuite script%s
### END INIT INFO

echo hello
''' % (suffix, suffix))
        os.chmod(script, 0o755)

        if enable:
            subprocess.check_call(
                [systemd_sysv_install, 'enable', os.path.basename(script)])

    def assertEnabled(self, enabled, unit='test_enable.service'):
        '''assert that given unit has expected state'''

        systemctl = subprocess.Popen(['systemctl', 'is-enabled', unit],
                                     stdout=subprocess.PIPE,
                                     universal_newlines=True)
        out = systemctl.communicate()[0].strip()
        if enabled:
            self.assertEqual(systemctl.returncode, 0)
            self.assertEqual(out, 'enabled')
        else:
            self.assertEqual(systemctl.returncode, 1)
            self.assertEqual(out, 'disabled')

    def test_unit_enable(self):
        '''no sysv: enable unit'''

        self.create_unit()
        self.assertEnabled(False)
        # also works without .service suffix
        self.assertEnabled(False, unit='test_enable')

        subprocess.check_call(['systemctl', 'enable', 'test_enable'])

        self.assertEnabled(True)
        # also works without .service suffix
        self.assertEnabled(True, unit='test_enable')

        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertTrue(os.path.islink(l))
        self.assertEqual(os.readlink(l),
                         system_unit_dir + '/test_enable.service')

        # enable should be idempotent
        subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])
        self.assertEnabled(True)

    def test_unit_disable(self):
        '''no sysv: disable unit'''

        self.create_unit(enable=True)
        self.assertEnabled(True)
        # also works without .service suffix
        self.assertEnabled(True, unit='test_enable')

        subprocess.check_call(['systemctl', 'disable', 'test_enable'])

        self.assertEnabled(False)
        # also works without .service suffix
        self.assertEnabled(False, unit='test_enable')

        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertFalse(os.path.islink(l))

        # disable should be idempotent
        subprocess.check_call(['systemctl', 'disable', 'test_enable.service'])
        self.assertEnabled(False)

    def test_unit_sysv_enable(self):
        '''with sysv: enable unit'''

        self.create_unit()
        self.create_sysv()
        self.assertEnabled(False)
        # also works without .service suffix
        self.assertEnabled(False, unit='test_enable')

        subprocess.check_call(['systemctl', 'enable', 'test_enable'])

        self.assertEnabled(True)
        # also works without .service suffix
        self.assertEnabled(True, unit='test_enable')

        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertTrue(os.path.islink(l))
        self.assertEqual(os.readlink(l),
                         system_unit_dir + '/test_enable.service')

        # enabled the sysv script
        l = glob('/etc/rc2.d/S??test_enable')
        self.assertEqual(len(l), 1, 'expect one symlink in %s' % repr(l))
        self.assertEqual(os.readlink(l[0]), '../init.d/test_enable')

        # enable should be idempotent
        subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])
        self.assertEnabled(True)

    def test_unit_sysv_disable(self):
        '''with sysv: disable unit'''

        self.create_unit(enable=True)
        self.create_sysv(enable=True)
        self.assertEnabled(True)
        # also works without .service suffix
        self.assertEnabled(True, unit='test_enable')

        subprocess.check_call(['systemctl', 'disable', 'test_enable'])

        self.assertEnabled(False)
        # also works without .service suffix
        self.assertEnabled(False, unit='test_enable')

        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertFalse(os.path.islink(l))

        # disabled the sysv script
        l = glob('/etc/rc2.d/S??test_enable')
        self.assertEqual(l, [])

        # disable should be idempotent
        subprocess.check_call(['systemctl', 'enable', 'test_enable.service'])
        self.assertEnabled(True)

    def test_unit_alias_enable(self):
        '''no sysv: enable unit with an alias'''

        u = self.create_unit()
        with open(u, 'a') as f:
            f.write('Alias=test_enablea.service\n')

        self.assertEnabled(False)

        subprocess.check_call(['systemctl', 'enable', 'test_enable'])

        self.assertEnabled(True)

        # enablement symlink
        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertTrue(os.path.islink(l))
        self.assertEqual(os.readlink(l),
                         system_unit_dir + '/test_enable.service')

        # alias symlink
        l = '/etc/systemd/system/test_enablea.service'
        self.assertTrue(os.path.islink(l))
        self.assertEqual(os.readlink(l),
                         system_unit_dir + '/test_enable.service')

    def test_unit_alias_disable(self):
        '''no sysv: disable unit with an alias'''

        u = self.create_unit()
        with open(u, 'a') as f:
            f.write('Alias=test_enablea.service\n')
        os.symlink(system_unit_dir + '/test_enable.service',
                   '/etc/systemd/system/test_enablea.service')

        subprocess.check_call(['systemctl', 'disable', 'test_enable'])

        self.assertEnabled(False)

        # enablement symlink
        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertFalse(os.path.islink(l))

        # alias symlink
        l = '/etc/systemd/system/test_enablea.service'
        self.assertFalse(os.path.islink(l))

    def test_unit_sysv_alias_enable(self):
        '''with sysv: enable unit with an alias'''

        u = self.create_unit()
        with open(u, 'a') as f:
            f.write('Alias=test_enablea.service\n')
        self.create_sysv()

        self.assertEnabled(False)

        subprocess.check_call(['systemctl', 'enable', 'test_enable'])

        # enablement symlink
        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertTrue(os.path.islink(l))
        self.assertEqual(os.readlink(l),
                         system_unit_dir + '/test_enable.service')

        # alias symlink
        l = '/etc/systemd/system/test_enablea.service'
        self.assertTrue(os.path.islink(l))
        self.assertEqual(os.readlink(l),
                         system_unit_dir + '/test_enable.service')

        # enabled the sysv script
        l = glob('/etc/rc2.d/S??test_enable')
        self.assertEqual(len(l), 1, 'expect one symlink in %s' % repr(l))
        self.assertEqual(os.readlink(l[0]), '../init.d/test_enable')

        self.assertEnabled(True)

    def test_unit_sysv_alias_disable(self):
        '''with sysv: disable unit with an alias'''

        u = self.create_unit(enable=True)
        with open(u, 'a') as f:
            f.write('Alias=test_enablea.service\n')
        os.symlink(system_unit_dir + '/test_enable.service',
                   '/etc/systemd/system/test_enablea.service')
        self.create_sysv(enable=True)

        subprocess.check_call(['systemctl', 'disable', 'test_enable'])

        # enablement symlink
        l = '/etc/systemd/system/multi-user.target.wants/test_enable.service'
        self.assertFalse(os.path.islink(l))

        # alias symlink
        l = '/etc/systemd/system/test_enablea.service'
        self.assertFalse(os.path.islink(l))

        # disabled the sysv script
        l = glob('/etc/rc2.d/S??test_enable')
        self.assertEqual(l, [])

        self.assertEnabled(False)

    def test_sysv_enable(self):
        '''only sysv: enable'''

        self.create_sysv()
        subprocess.check_call(['systemctl', 'enable', 'test_enable'])

        # enabled the sysv script
        l = glob('/etc/rc2.d/S??test_enable')
        self.assertEqual(len(l), 1, 'expect one symlink in %s' % repr(l))
        self.assertEqual(os.readlink(l[0]), '../init.d/test_enable')

        # enable should be idempotent
        subprocess.check_call(['systemctl', 'enable', 'test_enable'])
        self.assertEnabled(True)

    def test_sysv_disable(self):
        '''only sysv: disable'''

        self.create_sysv(enable=True)
        subprocess.check_call(['systemctl', 'disable', 'test_enable'])

        # disabled the sysv script
        l = glob('/etc/rc2.d/S??test_enable')
        self.assertEqual(l, [])

        # disable should be idempotent
        subprocess.check_call(['systemctl', 'disable', 'test_enable'])
        self.assertEnabled(False)

    def test_unit_link(self):
        '''systemctl link'''

        with tempfile.NamedTemporaryFile(suffix='.service') as f:
            f.write(b'[Unit]\n')
            f.flush()
            subprocess.check_call(['systemctl', 'link', f.name])

            unit = os.path.basename(f.name)
            l = os.path.join('/etc/systemd/system', unit)
            self.assertEqual(os.readlink(l), f.name)

            # disable it again
            subprocess.check_call(['systemctl', 'disable', unit])
            # this should also remove the unit symlink
            self.assertFalse(os.path.islink(l))

    def test_unit_enable_full_path(self):
        '''systemctl enable a unit in a non-default path'''

        with tempfile.NamedTemporaryFile(suffix='.service') as f:
            f.write(b'''[Unit]
Description=test
[Service]
ExecStart=/bin/true
[Install]
WantedBy=multi-user.target''')
            f.flush()
            unit = os.path.basename(f.name)

            # now enable it
            subprocess.check_call(['systemctl', 'enable', f.name])
            self.assertEnabled(True, unit=unit)
            l = os.path.join('/etc/systemd/system', unit)
            self.assertEqual(os.readlink(l), f.name)
            enable_l = '/etc/systemd/system/multi-user.target.wants/' + unit
            self.assertEqual(os.readlink(enable_l), f.name)

            # disable it again
            subprocess.check_call(['systemctl', 'disable', unit])
            # self.assertEnabled(False) does not work as now systemd does not
            # know about the unit at all any more
            self.assertFalse(os.path.islink(enable_l))
            # this should also remove the unit symlink
            self.assertFalse(os.path.islink(l))


if __name__ == '__main__':
    unittest.main(testRunner=unittest.TextTestRunner(stream=sys.stdout,
                                                     verbosity=2))