Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use service module for rootless systemd #647

Open
xoxys opened this issue Apr 10, 2022 · 3 comments
Open

Use service module for rootless systemd #647

xoxys opened this issue Apr 10, 2022 · 3 comments

Comments

@xoxys
Copy link

xoxys commented Apr 10, 2022

Hi, is there a way to use the service module with systemd services running on a non-privileged user? Just using sudo doesn't seems to work:

    with host.sudo(user="renovate"):
        service = host.service("renovate.timer")
        assert service.is_enabled
cls = <class 'abc.SystemdService'>
args = ('find -L /etc/rc?.d/ -name %s', 'S??renovate.timer'), kwargs = {}

    @classmethod
    def check_output(cls, *args, **kwargs):
>       return cls._host.check_output(*args, **kwargs)
E       AssertionError: Unexpected exit code 1 for CommandResult(command=b'sudo -u renovate /bin/sh -c \'find -L /etc/rc?.d/ -name \'"\'"\'S??renovate.timer\'"\'"\'\'', exit_status=1, stdout=None, stderr=b'find: Failed to restore initial working directory: /root: Permission denied\n')
E       assert 1 == 0
E        +  where 1 = CommandResult(command=b'sudo -u renovate /bin/sh -c \'find -L /etc/rc?.d/ -name \'"\'"\'S??renovate.timer\'"\'"\'\'', exit_status=1, stdout=None, stderr=b'find: Failed to restore initial working directory: /root: Permission denied\n').rc
@coofercat
Copy link

Not sure what's going on here, but you're looking for a systemd thing (ie. a timer), but the code looks like it's using the SysV code to do a 'find'. The systemd is_enabled method (https://github.com/pytest-dev/pytest-testinfra/blob/main/testinfra/modules/service.py#L152) uses systemctl which should work fine with sudo.

@xoxys
Copy link
Author

xoxys commented Apr 3, 2023

Yeah, that's weird, but I have no idea why the SysV code is used.

@CarstenGrohmann
Copy link
Contributor

The SysV code is a fallback:

@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
# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=760616
return super().is_enabled

I get a similar error when I try to check a non-existing service as user root:

with host.sudo():
    srv = ansiblehost.service("nonexisting.service")
    assert srv.is_enabled
    assert srv.is_running
cls = <class 'testinfra.modules.base.SystemdService'>, args = ('find -L /etc/rc?.d/ -name %s', 'S??nonexisting.service'), kwargs = {}

    @classmethod
    def check_output(cls, *args, **kwargs):
>       return cls._host.check_output(*args, **kwargs)
E       AssertionError: Unexpected exit code 1 for CommandResult(command=b"find -L /etc/rc?.d/ -name 'S??nonexisting.service'", exit_status=1, stdout=None, stderr=b'find: \xe2\x80\x98/etc/rc?.d/\xe2\x80\x99: No such file or directory\n')
E       assert 1 == 0
E        +  where 1 = CommandResult(command=b"find -L /etc/rc?.d/ -name 'S??nonexisting.service'", exit_status=1, stdout=None, stderr=b'find: \xe2\x80\x98/etc/rc?.d/\xe2\x80\x99: No such file or directory\n').rc

Since I use "root", I do not get the permission error that was mentioned initially

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants