File: unit-config

package info (click to toggle)
systemd 260.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 115,380 kB
  • sloc: ansic: 741,720; xml: 122,315; sh: 36,676; python: 36,497; cpp: 947; makefile: 277; awk: 126; lisp: 13; sed: 1
file content (204 lines) | stat: -rwxr-xr-x 7,231 bytes parent folder | download
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
#!/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()


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 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):
        '''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.assertTrue(os.readlink(l) == system_unit_dir + '/test_enable.service' or
                        os.readlink(l) == '../test_enable.service')

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

    def test_unit_disable(self):
        '''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_alias_enable(self):
        '''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.assertTrue(os.readlink(l) == system_unit_dir + '/test_enable.service' or
                        os.readlink(l) == '../test_enable.service')

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

    def test_unit_alias_disable(self):
        '''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_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.assertTrue(os.readlink(enable_l) == f.name or
                            os.readlink(enable_l) == '../' + unit)

            # 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))