vuls: init at 0.27.0
[NixPkgs.git] / nixos / tests / k3s / auto-deploy.nix
blobc25503ac108749c510d9d86b38d72903b50a014d
1 # Tests whether container images are imported and auto deploying manifests work
2 import ../make-test-python.nix (
3   {
4     pkgs,
5     lib,
6     k3s,
7     ...
8   }:
9   let
10     pauseImageEnv = pkgs.buildEnv {
11       name = "k3s-pause-image-env";
12       paths = with pkgs; [
13         tini
14         (hiPrio coreutils)
15         busybox
16       ];
17     };
18     pauseImage = pkgs.dockerTools.buildImage {
19       name = "test.local/pause";
20       tag = "local";
21       copyToRoot = pauseImageEnv;
22       config.Entrypoint = [
23         "/bin/tini"
24         "--"
25         "/bin/sleep"
26         "inf"
27       ];
28     };
29     helloImage = pkgs.dockerTools.buildImage {
30       name = "test.local/hello";
31       tag = "local";
32       copyToRoot = pkgs.hello;
33       config.Entrypoint = [ "${pkgs.hello}/bin/hello" ];
34     };
35   in
36   {
37     name = "${k3s.name}-auto-deploy";
39     nodes.machine =
40       { pkgs, ... }:
41       {
42         environment.systemPackages = [ k3s ];
44         # k3s uses enough resources the default vm fails.
45         virtualisation.memorySize = 1536;
46         virtualisation.diskSize = 4096;
48         services.k3s.enable = true;
49         services.k3s.role = "server";
50         services.k3s.package = k3s;
51         # Slightly reduce resource usage
52         services.k3s.extraFlags = [
53           "--disable coredns"
54           "--disable local-storage"
55           "--disable metrics-server"
56           "--disable servicelb"
57           "--disable traefik"
58           "--pause-image test.local/pause:local"
59         ];
60         services.k3s.images = [
61           pauseImage
62           helloImage
63         ];
64         services.k3s.manifests = {
65           absent = {
66             enable = false;
67             content = {
68               apiVersion = "v1";
69               kind = "Namespace";
70               metadata.name = "absent";
71             };
72           };
74           present = {
75             target = "foo-namespace.yaml";
76             content = {
77               apiVersion = "v1";
78               kind = "Namespace";
79               metadata.name = "foo";
80             };
81           };
83           hello.content = {
84             apiVersion = "batch/v1";
85             kind = "Job";
86             metadata.name = "hello";
87             spec = {
88               template.spec = {
89                 containers = [
90                   {
91                     name = "hello";
92                     image = "test.local/hello:local";
93                   }
94                 ];
95                 restartPolicy = "OnFailure";
96               };
97             };
98           };
99         };
100       };
102     testScript = ''
103       start_all()
105       machine.wait_for_unit("k3s")
106       # check existence of the manifest files
107       machine.fail("ls /var/lib/rancher/k3s/server/manifests/absent.yaml")
108       machine.succeed("ls /var/lib/rancher/k3s/server/manifests/foo-namespace.yaml")
109       machine.succeed("ls /var/lib/rancher/k3s/server/manifests/hello.yaml")
111       # check if container images got imported
112       machine.wait_until_succeeds("crictl img | grep 'test\.local/pause'")
113       machine.wait_until_succeeds("crictl img | grep 'test\.local/hello'")
115       # check if resources of manifests got created
116       machine.wait_until_succeeds("kubectl get ns foo")
117       machine.wait_until_succeeds("kubectl wait --for=condition=complete job/hello")
118       machine.fail("kubectl get ns absent")
120       machine.shutdown()
121     '';
123     meta.maintainers = lib.teams.k3s.members;
124   }