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

test-driver: support testing user units #32845

Merged
merged 1 commit into from Jan 4, 2018

Conversation

Ma27
Copy link
Member

@Ma27 Ma27 commented Dec 19, 2017

Motivation for this change

It is quite complicated to test services using the test-driver when
declaring user services with systemd.user.services such as many
X11-based services like xautolock.service.

This change adds an optional $user parameter to each systemd-related
function in the test-driver and runs systemctl --user commands using
su -l $user -c ... and sets the XDG_RUNTIME_DIR variable
accordingly and a new function named systemctl which is able to run a
systemd command with or without a specified user.

The change can be confirmed with a simple VM declaration like this:

import ./nixos/tests/make-test.nix ({ pkgs, lib }:

with lib;

{
  name = "systemd-user-test";

  nodes.machine = {
    imports = [ ./nixos/tests/common/user-account.nix ];

    services.xserver.enable = true;
    services.xserver.displayManager.auto.enable = true;
    services.xserver.displayManager.auto.user = "bob";
    services.xserver.xautolock.enable = true;
  };

  testScript = ''
    $machine->start;
    $machine->waitForX;

    $machine->waitForUnit("xautolock.service", "bob");
    $machine->stopJob("xautolock.service", "bob");
    $machine->startJob("xautolock.service", "bob");
    $machine->systemctl("list-jobs --no-pager", "bob");
  '';
})
Things done
  • Tested using sandboxing (nix.useSandbox on NixOS, or option build-use-sandbox in nix.conf on non-NixOS)
  • Built on platform(s)
    • NixOS
    • macOS
    • other Linux distributions
  • Tested via one or more NixOS test(s) if existing and applicable for the change (look inside nixos/tests)
  • Tested compilation of all pkgs that depend on this change using nix-shell -p nox --run "nox-review wip"
  • Tested execution of all binary files (usually in ./result/bin/)
  • Fits CONTRIBUTING.md.

@Ma27 Ma27 force-pushed the test-driver/allow-user-units branch from 77d8c9d to 0392283 Compare December 19, 2017 14:39
@Ma27
Copy link
Member Author

Ma27 commented Dec 19, 2017

To avoid any breaks I just checked some tests: the one I described above, the rabbitmq test and the statsd test (all of them use the waitForUnit API)

$cmd = "XDG_RUNTIME_DIR=/run/user/`id -u` systemctl --user";
}

my ($status, $lines) = $self->execute("su -l $user -c '$cmd $q'");
Copy link
Member

@Mic92 Mic92 Jan 2, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we run into shell quoting issues, since a second shell is spawned?

@Ma27 Ma27 force-pushed the test-driver/allow-user-units branch from 116e892 to 90de1af Compare January 2, 2018 19:03
@Ma27
Copy link
Member Author

Ma27 commented Jan 2, 2018

@Mic92 you're right. I changed the systemctl sub to replace all single quotes that might harm the execution with \'

It is quite complicated to test services using the test-driver when
declaring user services with `systemd.user.services` such as many
X11-based services like `xautolock.service`.

This change adds an optional `$user` parameter to each systemd-related
function in the test-driver and runs `systemctl --user` commands using
`su -l $user -c ...` and sets the `XDG_RUNTIME_DIR` variable
accordingly and a new function named `systemctl` which is able to run a
systemd command with or without a specified user.

The change can be confirmed with a simple VM declaration like this:

```
import ./nixos/tests/make-test.nix ({ pkgs, lib }:

with lib;

{
  name = "systemd-user-test";

  nodes.machine = {
    imports = [ ./nixos/tests/common/user-account.nix ];

    services.xserver.enable = true;
    services.xserver.displayManager.auto.enable = true;
    services.xserver.displayManager.auto.user = "bob";
    services.xserver.xautolock.enable = true;
  };

  testScript = ''
    $machine->start;
    $machine->waitForX;

    $machine->waitForUnit("xautolock.service", "bob");
    $machine->stopJob("xautolock.service", "bob");
    $machine->startJob("xautolock.service", "bob");
    $machine->systemctl("list-jobs --no-pager", "bob");
    $machine->systemctl("show 'xautolock.service' --no-pager", "bob");
  '';
})
```
@Ma27 Ma27 force-pushed the test-driver/allow-user-units branch from 90de1af to e538e00 Compare January 2, 2018 19:15
@Mic92 Mic92 merged commit 814b385 into NixOS:master Jan 4, 2018
@Ma27 Ma27 deleted the test-driver/allow-user-units branch January 4, 2018 08:43
@c0bw3b c0bw3b added the 6.topic: testing Tooling for automated testing of packages and modules label Apr 28, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
6.topic: testing Tooling for automated testing of packages and modules 10.rebuild-darwin: 0 10.rebuild-linux: 0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants