summaryrefslogtreecommitdiffstats
path: root/nixos/tests/evcc.nix
blob: 24afd1671c8e5cb971621d9288f69129b5f75938 (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
{ pkgs, lib, ... }:

let
  port = "1234";
in
{
  name = "evcc";
  meta.maintainers = with lib.maintainers; [ hexa ];

  containers = {
    machine = {
      services.evcc = {
        enable = true;
        # This is NOT a safe way to deal with secrets in production
        environmentFile = pkgs.writeText "evcc-secrets" ''
          PORT=${toString port}
        '';
        settings = {
          network = {
            schema = "http";
            host = "localhost";
            port = "$PORT";
          };

          log = "info";

          site = {
            title = "NixOS Test";
            meters = {
              grid = "grid";
              pv = "pv";
            };
          };

          meters = [
            {
              type = "custom";
              name = "grid";
              power = {
                source = "script";
                cmd = "/bin/sh -c 'echo -4500'";
              };
            }
            {
              type = "custom";
              name = "pv";
              power = {
                source = "script";
                cmd = "/bin/sh -c 'echo 7500'";
              };
            }
          ];

          chargers = [
            {
              name = "dummy-charger";
              type = "custom";
              status = {
                source = "script";
                cmd = "/bin/sh -c 'echo A'";
              };
              enabled = {
                source = "script";
                cmd = "/bin/sh -c 'echo false'";
              };
              enable = {
                source = "script";
                cmd = "/bin/sh -c 'echo true'";
              };
              maxcurrent = {
                source = "script";
                cmd = "/bin/sh -c 'echo 7200'";
              };
            }
          ];

          loadpoints = [
            {
              title = "Dummy";
              charger = "dummy-charger";
            }
          ];
        };
      };
    };
  };

  testScript = ''
    start_all()

    machine.wait_for_unit("evcc.service")
    machine.wait_for_open_port(${port})

    with subtest("Check package version propagates into frontend"):
        machine.fail(
            "curl --fail http://localhost:${port} | grep '0.0.1-alpha'"
        )
        machine.succeed(
            "curl --fail http://localhost:${port} | grep '${pkgs.evcc.version}'"
        )

    with subtest("Check journal for errors"):
        _, output = machine.execute("journalctl -o cat -u evcc.service")
        assert "FATAL" not in output

    with subtest("Check systemd hardening"):
        _, output = machine.execute("systemd-analyze security evcc.service | grep -v '✓'")
        machine.log(output)
  '';
}