summaryrefslogtreecommitdiffstats
path: root/nixos/tests/sabnzbd-module.nix
blob: 4398c540183246285d4e458ff0aaaa7668aebfa6 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
{ lib, ... }:
let
  common-config =
    { pkgs, ... }:
    {
      services.sabnzbd = {
        enable = true;
        secretFiles = [
          (pkgs.writeText "secret-a.toml" ''
            [servers]
            [[example.com]]
            required=1
            priority=2
          '')
          (pkgs.writeText "secret-b.toml" ''
            [servers]
            [[example.com]]
            enable=1
            priority=5
          '')
        ];
        settings = {
          misc = {
            html_login = false;
            inet_exposure = "api (full)";
            api_key = "123456";
          };
          servers."example.com" = {
            enable = false;
            required = false;
            optional = true;
            displayname = "example.com";
            host = "example.com";
            name = "example.com";
          };
        };
      };

      environment.systemPackages = [
        pkgs.jq
        (pkgs.writeScriptBin "do_test" ''
          set -euxo pipefail

          misc_url="http://127.0.0.1:8080/api?mode=get_config&section=misc&output=json&apikey=123456"
          servers_url="http://127.0.0.1:8080/api?mode=get_config&section=servers&output=json&apikey=123456"

          [[ $(curl $misc_url | jq .config.misc.inet_exposure) == 3 ]]
          [[ $(curl $misc_url | jq .config.misc.html_login) == "false" ]]

          [[ $(curl $servers_url | jq .config.servers[0].enable) == 1 ]]
          [[ $(curl $servers_url | jq .config.servers[0].required) == 1 ]]
          [[ $(curl $servers_url | jq .config.servers[0].optional) == 1 ]]
          [[ $(curl $servers_url | jq .config.servers[0].priority) == 5 ]]
        '')
      ];

      # unrar is unfree
      nixpkgs.config.allowUnfreePackages = [ "unrar" ];
    };
in
{
  name = "sabnzbd";
  meta.maintainers = with lib.maintainers; [ jojosch ];

  node.pkgsReadOnly = false;

  nodes.machine =
    { ... }:
    {
      imports = [ common-config ];
    };
  nodes.with_writeable_config =
    { ... }:
    {
      imports = [ common-config ];
      config.services.sabnzbd.allowConfigWrite = true;
    };
  nodes.with_raw_config_file =
    { pkgs, ... }:
    {
      imports = [ common-config ];
      config = {
        services.sabnzbd = {
          configFile = builtins.toFile "config.ini" ''
            [misc]
            inet_exposure = 2
            html_login = 0
            api_key = abcdef
            log_dir = /var/lib/sabnzbd
            admin_dir = /var/lib/sabnzbd
          '';
        };

        environment.systemPackages = [
          pkgs.jq
          (pkgs.writeScriptBin "do_test_2" ''
            set -euxo pipefail

            misc_url="http://127.0.0.1:8080/api?mode=get_config&section=misc&output=json&apikey=abcdef"

            [[ $(curl $misc_url | jq .config.misc.inet_exposure) == 2 ]]
            [[ $(curl $misc_url | jq .config.misc.html_login) == "false" ]]
          '')
        ];
      };
    };

  nodes.with_secret_values =
    { pkgs, ... }:
    {
      config = {
        services.sabnzbd = {
          enable = true;
          settings = {
            misc.api_key = "@api_key@";
            misc.nzb_key = "@nzb_key@";
          };
          secretValues = {
            # Just for testing; don't use world readable files from the Nix
            # store in production!
            "@api_key@" = builtins.toFile "api_key" "dummyapikey";
            "@nzb_key@" = builtins.toFile "nzb_key" "dummynzbkey";
          };
        };
      };
    };

  testScript = ''
    def wait_for_up(m):
      m.wait_for_unit("sabnzbd.service")
      m.wait_until_succeeds("curl --fail -L http://localhost:8080")

    wait_for_up(machine)
    wait_for_up(with_writeable_config)
    wait_for_up(with_raw_config_file)
    wait_for_up(with_secret_values)

    machine.succeed("do_test")
    with_writeable_config.succeed("do_test")
    with_raw_config_file.succeed("do_test_2")
    with_secret_values.succeed("grep dummyapikey /var/lib/sabnzbd/sabnzbd.ini")
    with_secret_values.succeed("grep dummynzbkey /var/lib/sabnzbd/sabnzbd.ini")
  '';
}