Skip to content

service is_enabled does not work with exit code 4 #801

Open
@pfuntner

Description

@pfuntner

I'm finding that when systemctl is-enabled is used for a non-existent service and the target has an exit code of 4 (such as on an Ubuntu 24 target), an exception is raised.

pytest script

(venv) $ cat service-test.py
def test_faux_service(host):
        assert not host.service('foo').is_enabled
(venv) $

Ubuntu 24

(venv) $ pytest --hosts ubuntu24 service-test.py
==================================================================================== test session starts =====================================================================================
platform linux -- Python 3.11.0rc1, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/jpfuntne/sto/issues/CL9-746
plugins: testinfra-10.2.2
collected 1 item

service-test.py F                                                                                                                                                                      [100%]

========================================================================================== FAILURES ==========================================================================================
___________________________________________________________________________ test_faux_service[paramiko://ubuntu24] ___________________________________________________________________________

host = <testinfra.host.Host paramiko://ubuntu24>

    def test_faux_service(host):
>       assert not host.service('foo').is_enabled

service-test.py:2:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv/lib/python3.11/site-packages/testinfra/modules/service.py:197: in is_enabled
    cmd = self.run_test("systemctl is-enabled %s", self.name)
venv/lib/python3.11/site-packages/testinfra/modules/base.py:40: in run_test
    return cls._host.run_test(*args, **kwargs)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <testinfra.host.Host paramiko://ubuntu24>, command = 'systemctl is-enabled %s', args = ('foo',), kwargs = {}

    def run_test(
        self, command: str, *args: str, **kwargs: Any
    ) -> testinfra.backend.base.CommandResult:
        """Run command and check it return an exit status of 0 or 1

        :raises: AssertionError
        """
>       return self.run_expect([0, 1], command, *args, **kwargs)
E       AssertionError: Unexpected exit code 4 for CommandResult(backend=<testinfra.backend.paramiko.ParamikoBackend object at 0x7f4b7768b490>, exit_status=4, command=b'systemctl is-enabled foo', _stdout=b'not-found\n', _stderr=b'')
E       assert 4 in [0, 1]
E        +  where 4 = CommandResult(backend=<testinfra.backend.paramiko.ParamikoBackend object at 0x7f4b7768b490>, exit_status=4, command=b'systemctl is-enabled foo', _stdout=b'not-found\n', _stderr=b'').rc

venv/lib/python3.11/site-packages/testinfra/host.py:120: AssertionError
====================================================================================== warnings summary ======================================================================================
service-test.py::test_faux_service[paramiko://ubuntu24]
  /home/jpfuntne/sto/issues/CL9-746/venv/lib/python3.11/site-packages/paramiko/client.py:889: UserWarning: Unknown ssh-ed25519 host key for 34.74.229.204: b'96b278a4e66eefe2941f43fa809646db'
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================================== short test summary info ===================================================================================
FAILED service-test.py::test_faux_service[paramiko://ubuntu24] - AssertionError: Unexpected exit code 4 for CommandResult(backend=<testinfra.backend.paramiko.ParamikoBackend object at 0x7f4b7768b490>, exit_status=4, command=b'systemctl is-enabled foo...
================================================================================ 1 failed, 1 warning in 1.46s ================================================================================
(venv) $ ssh ubuntu24 'systemctl is-enabled foo; echo $?'
not-found
4
(venv) $ ssh ubuntu24 cat /etc/os-release
PRETTY_NAME="Ubuntu 24.04.2 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.2 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo
(venv) $

Ubuntu 22

This is not a problem on Ubuntu 22 which has an exit code of 1 in the same scenario:

(venv) $ pytest --hosts ubuntu22 service-test.py
==================================================================================== test session starts =====================================================================================
platform linux -- Python 3.11.0rc1, pytest-8.3.5, pluggy-1.5.0
rootdir: /home/jpfuntne/sto/issues/CL9-746
plugins: testinfra-10.2.2
collected 1 item

service-test.py .                                                                                                                                                                      [100%]

====================================================================================== warnings summary ======================================================================================
service-test.py::test_faux_service[paramiko://ubuntu22]
  /home/jpfuntne/sto/issues/CL9-746/venv/lib/python3.11/site-packages/paramiko/client.py:889: UserWarning: Unknown ssh-ed25519 host key for 35.185.104.216: b'232b368a904a128f57b7601d1faeb172'
    warnings.warn(

-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
================================================================================ 1 passed, 1 warning in 1.48s ================================================================================
(venv) $ ssh ubuntu22 'systemctl is-enabled foo; echo $?'
1
Failed to get unit file state for foo.service: No such file or directory
(venv) $ ssh ubuntu22 cat /etc/os-release
PRETTY_NAME="Ubuntu 22.04.5 LTS"
NAME="Ubuntu"
VERSION_ID="22.04"
VERSION="22.04.5 LTS (Jammy Jellyfish)"
VERSION_CODENAME=jammy
ID=ubuntu
ID_LIKE=debian
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
UBUNTU_CODENAME=jammy
(venv) $

This is similar to #748 which I hoped would be fixed by #773 but it was not.

Python packages

(venv) $ pip list
Package          Version
---------------- -------
bcrypt           4.3.0
cffi             1.17.1
cryptography     44.0.2
iniconfig        2.1.0
packaging        24.2
paramiko         3.5.1
pip              22.0.2
pluggy           1.5.0
pycparser        2.22
PyNaCl           1.5.0
pytest           8.3.5
pytest-testinfra 10.2.2
setuptools       59.6.0
(venv) $

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions