summary refs log tree commit diff stats
path: root/scripts/lint_missing_tests.sh
blob: dffb86e190c8d7c4a2db0cfc6557f7c595ebf478 (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
#!/usr/bin/env sh

# Shell library {{{
die() {
    echo "$1" >&2
    exit 1
}
warning() {
    eprintln "\033[1;91m==> WARNING:\033[0m" "\033[1;93m$*\033[0m"
}
eprintln() {
    >&2 println "$@"
}
println() {
    printf "$*\n"
}
# }}}

test_template() {
    test_name="$(basename "$1")"
    cat <<EOF
{pkgs}:
pkgs.testers.runNixOSTest {
  name = "$test_name";

  nodes.server = { pkgs, ... }: {
    networking = {
      firewall = {
        allowedTCPPorts = [ 80 ];
      };
    };
    services.nginx = {
      enable = true;
      virtualHosts."server" = {};
    };
  };

  nodes.client = { pkgs, ... }: {
    environment.systemPackages = with pkgs; [
      curl
    ];
  };

  testScript = ''
    server.wait_for_unit("default.target")
    client.wait_for_unit("default.target")
    client.succeed("curl http://server/ | grep -o \"Welcome to nginx!\"")
  '';
}
EOF
}

cd "$(dirname "$0")/.." || die "Bug: The root dir does not exist"

fd default.nix ./modules | while IFS= read -r dir; do
    mkdir --parents "tests/$(dirname "$dir")"

    test_file="$(realpath --relative-to="$(pwd)" "tests/$(dirname "$dir")/test.nix")"
    dir="$(realpath --relative-to="$(pwd)" "$dir")"

    if [ "$1" = generate ]; then
        test_template "$dir" >"$test_file"
    else
        warning "'$dir' is missing it's test (at '$test_file')!"
    fi
done