about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/isolate.nix
blob: 327231be1cd4a0c6dc7f93d1e8d75700fa28e3c3 (plain) (blame)
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
import ./make-test-python.nix ({ lib, ... }:
{
  name = "isolate";
  meta.maintainers = with lib.maintainers; [ virchau13 ];

  nodes.machine =
    { ... }:
    {
      security.isolate = {
        enable = true;
      };
    };

  testScript = ''
    bash_path = machine.succeed('realpath $(which bash)').strip()
    sleep_path = machine.succeed('realpath $(which sleep)').strip()
    def sleep_test(walltime, sleeptime):
        return f'isolate --no-default-dirs --wall-time {walltime} ' + \
            f'--dir=/box={box_path} --dir=/nix=/nix --run -- ' + \
            f"{bash_path} -c 'exec -a sleep {sleep_path} {sleeptime}'"

    def sleep_test_cg(walltime, sleeptime):
        return f'isolate --cg --no-default-dirs --wall-time {walltime} ' + \
            f'--dir=/box={box_path} --dir=/nix=/nix --processes=2 --run -- ' + \
            f"{bash_path} -c '( exec -a sleep {sleep_path} {sleeptime} )'"

    with subtest("without cgroups"):
        box_path = machine.succeed('isolate --init').strip()
        machine.succeed(sleep_test(1, 0.5))
        machine.fail(sleep_test(0.5, 1))
        machine.succeed('isolate --cleanup')
    with subtest("with cgroups"):
        box_path = machine.succeed('isolate --cg --init').strip()
        machine.succeed(sleep_test_cg(1, 0.5))
        machine.fail(sleep_test_cg(0.5, 1))
        machine.succeed('isolate --cg --cleanup')
  '';
})