summary refs log tree commit diff
path: root/nixos/tests/lxd-image.nix
blob: 096b9d9aba9066a9618f4f8222a75d112b48eec0 (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
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# This test ensures that the nixOS lxd images builds and functions properly
# It has been extracted from `lxd.nix` to seperate failures of just the image and the lxd software

import ./make-test-python.nix ({ pkgs, ...} : let
  release = import ../release.nix {
    /* configuration = {
      environment.systemPackages = with pkgs; [ stdenv ]; # inject stdenv so rebuild test works
    }; */
  };

  metadata = release.lxdMeta.${pkgs.system};
  image = release.lxdImage.${pkgs.system};

  lxd-config = pkgs.writeText "config.yaml" ''
    storage_pools:
      - name: default
        driver: dir
        config:
          source: /var/lxd-pool

    networks:
      - name: lxdbr0
        type: bridge
        config:
          ipv4.address: auto
          ipv6.address: none

    profiles:
      - name: default
        devices:
          eth0:
            name: eth0
            network: lxdbr0
            type: nic
          root:
            path: /
            pool: default
            type: disk
  '';
in {
  name = "lxd-image";

  meta = with pkgs.lib.maintainers; {
    maintainers = [ mkg20001 ];
  };

  machine = { lib, ... }: {
    virtualisation = {
      # disk full otherwise
      diskSize = 2048;

      lxc.lxcfs.enable = true;
      lxd.enable = true;
    };
  };

  testScript = ''
    machine.wait_for_unit("sockets.target")
    machine.wait_for_unit("lxd.service")
    machine.wait_for_file("/var/lib/lxd/unix.socket")

    # It takes additional second for lxd to settle
    machine.sleep(1)

    # lxd expects the pool's directory to already exist
    machine.succeed("mkdir /var/lxd-pool")

    machine.succeed(
        "cat ${lxd-config} | lxd init --preseed"
    )

    # TODO: test custom built container aswell

    with subtest("importing container works"):
        machine.succeed("lxc image import ${metadata}/*/*.tar.xz ${image}/*/*.tar.xz --alias nixos")

    with subtest("launching container works"):
        machine.succeed("lxc launch nixos machine -c security.nesting=true")
        # make sure machine boots up properly
        machine.sleep(5)

    with subtest("container shell works"):
        machine.succeed("echo true | lxc exec machine /run/current-system/sw/bin/bash -")
        machine.succeed("lxc exec machine /run/current-system/sw/bin/true")

    # with subtest("rebuilding works"):
    #     machine.succeed("lxc exec machine /run/current-system/sw/bin/nixos-rebuild switch")
  '';
})