File: service.py

package info (click to toggle)
pytest-testinfra 10.2.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 676 kB
  • sloc: python: 4,951; makefile: 152; sh: 2
file content (365 lines) | stat: -rw-r--r-- 10,699 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
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
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import functools

from testinfra.modules.base import Module


class Service(Module):
    """Test services

    Implementations:

    - Linux: detect Systemd, Upstart or OpenRC, fallback to SysV
    - FreeBSD: service(1)
    - OpenBSD: ``/etc/rc.d/$name check`` for ``is_running``
      ``rcctl ls on`` for ``is_enabled`` (only OpenBSD >= 5.8)
    - NetBSD: ``/etc/rc.d/$name onestatus`` for ``is_running``
      (``is_enabled`` is not yet implemented)

    """

    def __init__(self, name):
        self.name = name
        super().__init__()

    @property
    def exists(self):
        """Test if service is exists"""
        raise NotImplementedError

    @property
    def is_running(self):
        """Test if service is running"""
        raise NotImplementedError

    @property
    def is_enabled(self):
        """Test if service is enabled"""
        raise NotImplementedError

    @property
    def is_valid(self):
        """Test if service is valid

        This method is only available in the systemd implementation,
        it will raise ``NotImplementedError`` in others implementation
        """
        raise NotImplementedError

    @property
    def is_masked(self):
        """Test if service is masked

        This method is only available in the systemd implementation,
        it will raise ``NotImplementedError`` in others implementations
        """
        raise NotImplementedError

    @functools.cached_property
    def systemd_properties(self):
        """Properties of the service (unit).

        Return service properties as a `dict`,
        empty properties are not returned.

        >>> ntp = host.service("ntp")
        >>> ntp.systemd_properties["FragmentPath"]
        '/lib/systemd/system/ntp.service'

        This method is only available in the systemd implementation,
        it will raise ``NotImplementedError`` in others implementations

        Note: based on `systemctl show`_

        .. _systemctl show: https://man7.org/linux/man-pages/man1/systemctl.1.html
        """
        raise NotImplementedError

    @classmethod
    def get_module_class(cls, host):
        if host.system_info.type == "linux":
            if host.file("/run/systemd/system/").is_directory or (
                host.exists("systemctl")
                and "systemd" in host.file("/sbin/init").linked_to
            ):
                return SystemdService
            if (
                host.exists("initctl")
                and host.exists("status")
                and host.file("/etc/init").is_directory
            ):
                return UpstartService
            if host.exists("rc-service"):
                return OpenRCService
            return SysvService
        if host.system_info.type == "freebsd":
            return FreeBSDService
        if host.system_info.type == "openbsd":
            return OpenBSDService
        if host.system_info.type == "netbsd":
            return NetBSDService
        if host.system_info.type == "windows":
            return WindowsService
        raise NotImplementedError

    def __repr__(self):
        return f"<service {self.name}>"


class SysvService(Service):
    @functools.cached_property
    def _service_command(self):
        return self.find_command("service")

    @property
    def is_running(self):
        # based on /lib/lsb/init-functions
        # 0: program running
        # 1: program is dead and pid file exists
        # 3: not running and pid file does not exists
        # 4: Unable to determine status
        # 8: starting (alpine specific ?)
        return (
            self.run_expect(
                [0, 1, 3, 8], "%s %s status", self._service_command, self.name
            ).rc
            == 0
        )

    @property
    def is_enabled(self):
        return bool(
            self.check_output(
                "find -L /etc/rc?.d/ -name %s",
                "S??" + self.name,
            )
        )


class SystemdService(SysvService):
    suffix_list = [
        "service",
        "socket",
        "device",
        "mount",
        "automount",
        "swap",
        "target",
        "path",
        "timer",
        "slice",
        "scope",
    ]
    """
    List of valid suffixes for systemd unit files

    See systemd.unit(5) for more details
    """

    def _has_systemd_suffix(self):
        """
        Check if service name has a known systemd unit suffix
        """
        unit_suffix = self.name.split(".")[-1]
        return unit_suffix in self.suffix_list

    @property
    def exists(self):
        cmd = self.run_test('systemctl list-unit-files | grep -q "^%s"', self.name)
        return cmd.rc == 0

    @property
    def is_running(self):
        # based on https://man7.org/linux/man-pages/man1/systemctl.1.html
        # 0: program running
        # 1: program is dead and pid file exists
        # 3: not running and pid file does not exists
        # 4: Unable to determine status (no such unit)
        out = self.run_expect([0, 1, 3, 4], "systemctl is-active %s", self.name)
        if out.rc == 1:
            # Failed to connect to bus: No such file or directory
            return super().is_running
        return out.rc == 0

    @property
    def is_enabled(self):
        cmd = self.run_test("systemctl is-enabled %s", self.name)
        if cmd.rc == 0:
            return True
        if cmd.stdout.strip() == "disabled":
            return False
        # Fallback on SysV - only for non-systemd units
        # https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760616
        if not self._has_systemd_suffix():
            return super().is_enabled
        raise RuntimeError(
            f"Unable to determine state of {self.name}. Does this service exist?"
        )

    @property
    def is_valid(self):
        # systemd-analyze requires a full unit name.
        name = self.name if self._has_systemd_suffix() else f"{self.name}.service"
        cmd = self.run("systemd-analyze verify %s", name)
        # A bad unit file still returns a rc of 0, so check the
        # stdout for anything.  Nothing means no warns/errors.
        # Docs at https://www.freedesktop.org/software/systemd/man/systemd
        # -analyze.html#Examples%20for%20verify
        assert (cmd.stdout, cmd.stderr) == ("", "")
        return True

    @property
    def is_masked(self):
        cmd = self.run_test("systemctl is-enabled %s", self.name)
        return cmd.stdout.strip() == "masked"

    @functools.cached_property
    def systemd_properties(self):
        out = self.check_output("systemctl show %s", self.name)
        out_d = {}
        if out:
            # maxsplit is required because values can contain `=`
            out_d = dict(
                map(lambda pair: pair.split("=", maxsplit=1), out.splitlines())
            )
        return out_d


class UpstartService(SysvService):
    @property
    def exists(self):
        return self._host.file(f"/etc/init/{self.name}.conf").exists

    @property
    def is_enabled(self):
        if (
            self.run(
                "grep -q '^start on' /etc/init/%s.conf",
                self.name,
            ).rc
            == 0
            and self.run(
                "grep -q '^manual' /etc/init/%s.override",
                self.name,
            ).rc
            != 0
        ):
            return True
        # Fallback on SysV
        return super().is_enabled

    @property
    def is_running(self):
        cmd = self.run_test("status %s", self.name)
        if cmd.rc == 0 and len(cmd.stdout.split()) > 1:
            return "running" in cmd.stdout.split()[1]
        return super().is_running


class OpenRCService(SysvService):
    @functools.cached_property
    def _service_command(self):
        return self.find_command("rc-service")

    @property
    def is_enabled(self):
        return bool(
            self.check_output(
                "find /etc/runlevels/ -name %s",
                self.name,
            )
        )


class FreeBSDService(Service):
    @property
    def exists(self):
        return self._host.file(f"/etc/rc.d/{self.name}").exists

    @property
    def is_running(self):
        return self.run_test("service %s onestatus", self.name).rc == 0

    @property
    def is_enabled(self):
        # Return list of enabled services like
        # /etc/rc.d/sshd
        # /etc/rc.d/sendmail
        for path in self.check_output("service -e").splitlines():
            if path and path.rsplit("/", 1)[1] == self.name:
                return True
        return False


class OpenBSDService(Service):
    @property
    def exists(self):
        return self._host.file(f"/etc/rc.d/{self.name}").exists

    @property
    def is_running(self):
        return self.run_test("/etc/rc.d/%s check", self.name).rc == 0

    @property
    def is_enabled(self):
        if self.name in self.check_output("rcctl ls on").splitlines():
            return True
        if self.name in self.check_output("rcctl ls off").splitlines():
            return False
        raise RuntimeError(
            f"Unable to determine state of {self.name}. Does this service exist?"
        )


class NetBSDService(Service):
    @property
    def exists(self):
        return self._host.file(f"/etc/rc.d/{self.name}").exists

    @property
    def is_running(self):
        return self.run_test("/etc/rc.d/%s onestatus", self.name).rc == 0

    @property
    def is_enabled(self):
        raise NotImplementedError


class WindowsService(Service):
    @property
    def exists(self):
        out = self.check_output(
            f"Get-Service -Name {self.name} -ErrorAction SilentlyContinue"
        )
        return self.name in out

    @property
    def is_running(self):
        return (
            self.check_output(
                "Get-Service '%s' | Select -ExpandProperty Status",
                self.name,
            )
            == "Running"
        )

    @property
    def is_enabled(self):
        return (
            self.check_output(
                "Get-Service '%s' | Select -ExpandProperty StartType",
                self.name,
            )
            == "Automatic"
        )