btrbk: add mainProgram
[NixPkgs.git] / pkgs / by-name / hi / hiawatha / test.nix
blob157d0ee79644d173c207efb46f552b102d530cdd
1 { lib
2 , stdenvNoCC
3 , hiawatha
4 , curl
5 , mbedtls
6 , enableTls
7 }:
9 stdenvNoCC.mkDerivation {
10   name = "hiawatha-test";
12   nativeBuildInputs = [
13     hiawatha
14     curl
15   ] ++ lib.optional enableTls mbedtls;
17   env = {
18     inherit enableTls;
19   };
21   buildCommand = ''
22     cp -r --no-preserve=mode ${hiawatha}/etc/hiawatha config
23     sed "1i set TEST_DIR = $(pwd)" $serverConfigPath > config/hiawatha.conf
25     mkdir www
26     echo "it works" > www/index.html
28     if [ -n "$enableTls" ]; then
29       echo "Generating self-signed certificate"
30       gen_key type=ec filename=server.key
31       cert_write selfsign=1 issuer_key=server.key output_file=server.crt
32       cat server.crt server.key > config/server.crt
33     fi
35     echo "Checking server configuration"
36     hiawatha -c ./config -k
38     echo "Starting server"
39     hiawatha -c ./config
41     testUrl() {
42       echo "Testing $1"
43       curl --verbose --insecure --fail "$1" | tee response
44       grep -q "it works" response
45     }
47     testUrl http://127.0.0.1:8000
48     if [ -n "$enableTls" ]; then
49       testUrl https://127.0.0.1:8443
50     fi
52     touch $out
53   '';
55   serverConfig = ''
56     # By default the server uses read-only directories like /var/lib and /etc
57     WorkDirectory = TEST_DIR
58     PIDfile = TEST_DIR/hiawatha.pid
59     SystemLogfile = TEST_DIR/system.log
60     GarbageLogfile = TEST_DIR/garbage.log
61     ExploitLogfile = TEST_DIR/exploit.log
62     AccessLogfile = TEST_DIR/access.log
63     ErrorLogfile = TEST_DIR/error.log
65     Binding {
66       Interface = 127.0.0.1
67       Port = 8000
68     }
70     ${lib.optionalString enableTls ''
71       Binding {
72         Interface = 127.0.0.1
73         Port = 8443
74         TLScertFile = TEST_DIR/config/server.crt
75       }
76     ''}
78     Hostname = 127.0.0.1
79     WebsiteRoot = TEST_DIR/www
80     StartFile = index.html
81   '';
83   passAsFile = [ "serverConfig" ];