summaryrefslogtreecommitdiffstats
path: root/nixos/tests/labgrid.nix
blob: 1b405dc0c51c8547dc8167119e37494d92ff5987 (plain)
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
{ pkgs, ... }:
{
  name = "Labgrid";
  meta.maintainers = with pkgs.lib.maintainers; [
    aiyion
    emantor
  ];

  nodes.coordinator =
    { pkgs, ... }:
    {
      services.labgrid.coordinator.enable = true;
      services.labgrid.coordinator.openFirewall = true;
    };

  nodes.client =
    { pkgs, ... }:
    {
      environment.variables = {
        LG_COORDINATOR = "coordinator:20408";
      };
      environment.systemPackages = [ pkgs.python3Packages.labgrid ];
    };

  testScript =
    { nodes, ... }:
    #python
    ''
      def assert_contains(haystack, needle):
          if needle not in haystack:
              print("The haystack that will cause the following exception is:")
              print("---")
              print(haystack)
              print("---")
              raise Exception(f"Expected string '{needle}' was not found")

      with subtest("Wait for coordinator startup"):
          coordinator.start()
          coordinator.wait_for_unit("labgrid-coordinator.service")
          coordinator.wait_for_open_port(20408)

      with subtest("Connect from client"):
          client.start()
          out = client.succeed("labgrid-client resources")

      with subtest("Create place"):
          client.succeed("labgrid-client -p testplace create")
          out = client.succeed("labgrid-client places")
          assert_contains(out, "testplace")
          # Give the coordinator enough time to persist place creation
          coordinator.wait_until_succeeds("grep -q testplace /var/lib/labgrid-coordinator/places.yaml")

      with subtest("Test coordinator persistence"):
          coordinator.shutdown()
          coordinator.start()
          coordinator.wait_for_unit("labgrid-coordinator.service")
          coordinator.wait_for_open_port(20408)
          out = client.succeed("labgrid-client places")
          assert_contains(out, "testplace")

      with subtest("Check systemd hardening does not degrade unnoticed"):
          exact_threshold = 11
          out = coordinator.fail(f"systemd-analyze security labgrid-coordinator.service --threshold={exact_threshold-1}")
          out = coordinator.succeed(f"systemd-analyze security labgrid-coordinator.service --threshold={exact_threshold}")
    '';
}