about summary refs log tree commit diff
path: root/nixpkgs/nixos/tests/octoprint.nix
blob: 15a2d677d4cf8bb9fc81216ab502ec7081676484 (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
import ./make-test-python.nix ({ pkgs, lib, ... }:

let
  apikey = "testapikey";
in
{
  name = "octoprint";
  meta.maintainers = with lib.maintainers; [ gador ];

  nodes.machine = { pkgs, ... }: {
    environment.systemPackages = with pkgs; [ jq ];
    services.octoprint = {
      enable = true;
      extraConfig = {
        server = {
          firstRun = false;
        };
        api = {
          enabled = true;
          key = apikey;
        };
        plugins = {
          # these need internet access and pollute the output with connection failed errors
          _disabled = [ "softwareupdate" "announcements" "pluginmanager" ];
        };
      };
    };
  };

  testScript = ''
    import json

    @polling_condition
    def octoprint_running():
        machine.succeed("pgrep octoprint")

    with subtest("Wait for octoprint service to start"):
        machine.wait_for_unit("octoprint.service")
        machine.wait_until_succeeds("pgrep octoprint")

    with subtest("Wait for final boot"):
        # this appears whe octoprint is almost finished starting
        machine.wait_for_file("/var/lib/octoprint/uploads")

    # octoprint takes some time to start. This makes sure we'll retry just in case it takes longer
    # retry-all-errors in necessary, since octoprint will report a 404 error when not yet ready
    curl_cmd = "curl --retry-all-errors --connect-timeout 5 --max-time 10 --retry 5 --retry-delay 0 \
                --retry-max-time 40 -X GET --header 'X-API-Key: ${apikey}' "

    # used to fail early, in case octoprint first starts and then crashes
    with octoprint_running: # type: ignore[union-attr]
        with subtest("Check for web interface"):
            machine.wait_until_succeeds("curl -s localhost:5000")

        with subtest("Check API"):
            version = json.loads(machine.succeed(curl_cmd + "localhost:5000/api/version"))
            server = json.loads(machine.succeed(curl_cmd + "localhost:5000/api/server"))
            assert version["server"] == str("${pkgs.octoprint.version}")
            assert server["safemode"] == None
  '';
})