about summary refs log tree commit diff
path: root/nixos/tests/mattermost.nix
blob: e11201f05357da7ac7032f2a3c60656067df70b6 (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
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
import ./make-test-python.nix ({ pkgs, lib, ... }:
let
  host = "smoke.test";
  port = "8065";
  url = "http://${host}:${port}";
  siteName = "NixOS Smoke Tests, Inc.";

  makeMattermost = mattermostConfig:
    { config, ... }: {
      environment.systemPackages = [
        pkgs.mattermost
        pkgs.curl
        pkgs.jq
      ];
      networking.hosts = {
        "127.0.0.1" = [ host ];
      };
      services.mattermost = lib.recursiveUpdate {
        enable = true;
        inherit siteName;
        listenAddress = "0.0.0.0:${port}";
        siteUrl = url;
        extraConfig = {
          SupportSettings.AboutLink = "https://nixos.org";
        };
      } mattermostConfig;
    };
in
{
  name = "mattermost";

  nodes = {
    mutable = makeMattermost {
      mutableConfig = true;
      extraConfig.SupportSettings.HelpLink = "https://search.nixos.org";
    };
    mostlyMutable = makeMattermost {
      mutableConfig = true;
      preferNixConfig = true;
      plugins = let
        mattermostDemoPlugin = pkgs.fetchurl {
          url = "https://github.com/mattermost/mattermost-plugin-demo/releases/download/v0.9.0/com.mattermost.demo-plugin-0.9.0.tar.gz";
          sha256 = "1h4qi34gcxcx63z8wiqcf2aaywmvv8lys5g8gvsk13kkqhlmag25";
        };
      in [
        mattermostDemoPlugin
      ];
    };
    immutable = makeMattermost {
      mutableConfig = false;
      extraConfig.SupportSettings.HelpLink = "https://search.nixos.org";
    };
    environmentFile = makeMattermost {
      mutableConfig = false;
      extraConfig.SupportSettings.AboutLink = "https://example.org";
      environmentFile = pkgs.writeText "mattermost-env" ''
        MM_SUPPORTSETTINGS_ABOUTLINK=https://nixos.org
      '';
    };
  };

  testScript = let
    expectConfig = jqExpression: pkgs.writeShellScript "expect-config" ''
      set -euo pipefail
      echo "Expecting config to match: "${lib.escapeShellArg jqExpression} >&2
      curl ${lib.escapeShellArg url} >/dev/null
      config="$(curl ${lib.escapeShellArg "${url}/api/v4/config/client?format=old"})"
      echo "Config: $(echo "$config" | ${pkgs.jq}/bin/jq)" >&2
      [[ "$(echo "$config" | ${pkgs.jq}/bin/jq -r ${lib.escapeShellArg ".SiteName == $siteName and .Version == ($mattermostName / $sep)[-1] and (${jqExpression})"} --arg siteName ${lib.escapeShellArg siteName} --arg mattermostName ${lib.escapeShellArg pkgs.mattermost.name} --arg sep '-')" = "true" ]]
    '';

    setConfig = jqExpression: pkgs.writeShellScript "set-config" ''
      set -euo pipefail
      mattermostConfig=/var/lib/mattermost/config/config.json
      newConfig="$(${pkgs.jq}/bin/jq -r ${lib.escapeShellArg jqExpression} $mattermostConfig)"
      rm -f $mattermostConfig
      echo "$newConfig" > "$mattermostConfig"
    '';

  in
  ''
    start_all()

    ## Mutable node tests ##
    mutable.wait_for_unit("mattermost.service")
    mutable.wait_for_open_port(8065)

    # Get the initial config
    mutable.succeed("${expectConfig ''.AboutLink == "https://nixos.org" and .HelpLink == "https://search.nixos.org"''}")

    # Edit the config
    mutable.succeed("${setConfig ''.SupportSettings.AboutLink = "https://mattermost.com"''}")
    mutable.succeed("${setConfig ''.SupportSettings.HelpLink = "https://nixos.org/nixos/manual"''}")
    mutable.systemctl("restart mattermost.service")
    mutable.wait_for_open_port(8065)

    # AboutLink and HelpLink should be changed
    mutable.succeed("${expectConfig ''.AboutLink == "https://mattermost.com" and .HelpLink == "https://nixos.org/nixos/manual"''}")

    ## Mostly mutable node tests ##
    mostlyMutable.wait_for_unit("mattermost.service")
    mostlyMutable.wait_for_open_port(8065)

    # Get the initial config
    mostlyMutable.succeed("${expectConfig ''.AboutLink == "https://nixos.org"''}")

    # Edit the config
    mostlyMutable.succeed("${setConfig ''.SupportSettings.AboutLink = "https://mattermost.com"''}")
    mostlyMutable.succeed("${setConfig ''.SupportSettings.HelpLink = "https://nixos.org/nixos/manual"''}")
    mostlyMutable.systemctl("restart mattermost.service")
    mostlyMutable.wait_for_open_port(8065)

    # AboutLink should be overridden by NixOS configuration; HelpLink should be what we set above
    mostlyMutable.succeed("${expectConfig ''.AboutLink == "https://nixos.org" and .HelpLink == "https://nixos.org/nixos/manual"''}")

    ## Immutable node tests ##
    immutable.wait_for_unit("mattermost.service")
    immutable.wait_for_open_port(8065)

    # Get the initial config
    immutable.succeed("${expectConfig ''.AboutLink == "https://nixos.org" and .HelpLink == "https://search.nixos.org"''}")

    # Edit the config
    immutable.succeed("${setConfig ''.SupportSettings.AboutLink = "https://mattermost.com"''}")
    immutable.succeed("${setConfig ''.SupportSettings.HelpLink = "https://nixos.org/nixos/manual"''}")
    immutable.systemctl("restart mattermost.service")
    immutable.wait_for_open_port(8065)

    # Our edits should be ignored on restart
    immutable.succeed("${expectConfig ''.AboutLink == "https://nixos.org" and .HelpLink == "https://search.nixos.org"''}")


    ## Environment File node tests ##
    environmentFile.wait_for_unit("mattermost.service")
    environmentFile.wait_for_open_port(8065)

    # Settings in the environment file should override settings set otherwise
    environmentFile.succeed("${expectConfig ''.AboutLink == "https://nixos.org"''}")
  '';
})