From 77de1bb168d7c53914cd5b0b19cc3f3082c7de4b Mon Sep 17 00:00:00 2001 From: Pierre Penninckx Date: Mon, 4 Mar 2024 20:36:47 -0800 Subject: [PATCH] refactor vm tests to have common test script (#202) --- test/vm/audiobookshelf.nix | 124 ++++++++++-------------------- test/vm/grocy.nix | 92 ++++++++++------------- test/vm/jellyfin.nix | 149 +++++++++++-------------------------- 3 files changed, 123 insertions(+), 242 deletions(-) diff --git a/test/vm/audiobookshelf.nix b/test/vm/audiobookshelf.nix index 101bb73..7d035fc 100644 --- a/test/vm/audiobookshelf.nix +++ b/test/vm/audiobookshelf.nix @@ -1,4 +1,42 @@ { pkgs, lib, ... }: +let + # TODO: Test login + commonTestScript = { nodes, ... }: + let + hasSSL = !(isNull nodes.server.shb.audiobookshelf.ssl); + fqdn = if hasSSL then "https://a.example.com" else "http://a.example.com"; + in + '' + import json + import os + import pathlib + + start_all() + server.wait_for_unit("audiobookshelf.service") + server.wait_for_unit("nginx.service") + server.wait_for_open_port(${builtins.toString nodes.server.shb.audiobookshelf.webPort}) + + if ${if hasSSL then "True" else "False"}: + server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") + client.succeed("rm -r /etc/ssl/certs") + client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") + + def curl(target, format, endpoint, succeed=True): + return json.loads(target.succeed( + "curl --fail-with-body --silent --show-error --output /dev/null --location" + + " --connect-to a.example.com:443:server:443" + + " --connect-to a.example.com:80:server:80" + + f" --write-out '{format}'" + + " " + endpoint + )) + + with subtest("access"): + response = curl(client, """{"code":%{response_code}}""", "${fqdn}") + + if response['code'] != 200: + raise Exception(f"Code is {response['code']}") + ''; +in { basic = pkgs.nixosTest { name = "audiobookshelf-basic"; @@ -25,29 +63,7 @@ nodes.client = {}; - # TODO: Test login - testScript = { nodes, ... }: '' - import json - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to a.example.com:443:server:443" - + " --connect-to a.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("audiobookshelf.service") - server.wait_for_unit("nginx.service") - server.wait_for_open_port(${builtins.toString nodes.server.shb.audiobookshelf.webPort}) - - response = curl(client, """{"code":%{response_code}}""", "http://a.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; cert = pkgs.nixosTest { @@ -96,35 +112,7 @@ nodes.client = {}; - # TODO: Test login - testScript = { nodes, ... }: '' - import json - import os - import pathlib - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to a.example.com:443:server:443" - + " --connect-to a.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("audiobookshelf.service") - server.wait_for_unit("nginx.service") - server.wait_for_open_port(${builtins.toString nodes.server.shb.audiobookshelf.webPort}) - - server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") - client.succeed("rm -r /etc/ssl/certs") - client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") - - response = curl(client, """{"code":%{response_code}}""", "https://a.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; sso = pkgs.nixosTest { @@ -208,36 +196,6 @@ nodes.client = {}; - # TODO: Test login with ldap user - testScript = { nodes, ... }: '' - import json - import os - import pathlib - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to a.example.com:443:server:443" - + " --connect-to a.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("audiobookshelf.service") - server.wait_for_unit("nginx.service") - server.wait_for_unit("lldap.service") - server.wait_for_unit("authelia-auth.example.com.service") - server.wait_for_open_port(${builtins.toString nodes.server.shb.audiobookshelf.webPort}) - - server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") - client.succeed("rm -r /etc/ssl/certs") - client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") - - response = curl(client, """{"code":%{response_code}}""", "https://a.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; } diff --git a/test/vm/grocy.nix b/test/vm/grocy.nix index 08fe0ae..5e33eb9 100644 --- a/test/vm/grocy.nix +++ b/test/vm/grocy.nix @@ -1,4 +1,42 @@ { pkgs, lib, ... }: +let + # TODO: Test login + commonTestScript = { nodes, ... }: + let + hasSSL = !(isNull nodes.server.shb.grocy.ssl); + fqdn = if hasSSL then "https://g.example.com" else "http://g.example.com"; + in + '' + import json + import os + import pathlib + + start_all() + server.wait_for_unit("phpfpm-grocy.service") + server.wait_for_unit("nginx.service") + server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.grocy.socket}") + + if ${if hasSSL then "True" else "False"}: + server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") + client.succeed("rm -r /etc/ssl/certs") + client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") + + def curl(target, format, endpoint, succeed=True): + return json.loads(target.succeed( + "curl --fail-with-body --silent --show-error --output /dev/null --location" + + " --connect-to g.example.com:443:server:443" + + " --connect-to g.example.com:80:server:80" + + f" --write-out '{format}'" + + " " + endpoint + )) + + with subtest("access"): + response = curl(client, """{"code":%{response_code}}""", "${fqdn}") + + if response['code'] != 200: + raise Exception(f"Code is {response['code']}") + ''; +in { basic = pkgs.nixosTest { name = "grocy-basic"; @@ -24,29 +62,7 @@ nodes.client = {}; - # TODO: Test login - testScript = { nodes, ... }: '' - import json - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to g.example.com:443:server:443" - + " --connect-to g.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("phpfpm-grocy.service") - server.wait_for_unit("nginx.service") - server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.grocy.socket}") - - response = curl(client, """{"code":%{response_code}}""", "http://g.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; cert = pkgs.nixosTest { @@ -95,34 +111,6 @@ nodes.client = {}; - # TODO: Test login - testScript = { nodes, ... }: '' - import json - import os - import pathlib - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to g.example.com:443:server:443" - + " --connect-to g.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("phpfpm-grocy.service") - server.wait_for_unit("nginx.service") - server.wait_for_open_unix_socket("${nodes.server.services.phpfpm.pools.grocy.socket}") - - server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") - client.succeed("rm -r /etc/ssl/certs") - client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") - - response = curl(client, """{"code":%{response_code}}""", "https://g.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; } diff --git a/test/vm/jellyfin.nix b/test/vm/jellyfin.nix index 0cdef24..bfc6454 100644 --- a/test/vm/jellyfin.nix +++ b/test/vm/jellyfin.nix @@ -1,4 +1,42 @@ { pkgs, lib, ... }: +let + # TODO: Test login + commonTestScript = { nodes, ... }: + let + hasSSL = !(isNull nodes.server.shb.jellyfin.ssl); + fqdn = if hasSSL then "https://j.example.com" else "http://j.example.com"; + in + '' + import json + import os + import pathlib + + start_all() + server.wait_for_unit("jellyfin.service") + server.wait_for_unit("nginx.service") + server.wait_for_open_port(8096) + + if ${if hasSSL then "True" else "False"}: + server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") + client.succeed("rm -r /etc/ssl/certs") + client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") + + def curl(target, format, endpoint, succeed=True): + return json.loads(target.succeed( + "curl --fail-with-body --silent --show-error --output /dev/null --location" + + " --connect-to j.example.com:443:server:443" + + " --connect-to j.example.com:80:server:80" + + f" --write-out '{format}'" + + " " + endpoint + )) + + with subtest("access"): + response = curl(client, """{"code":%{response_code}}""", "${fqdn}") + + if response['code'] != 200: + raise Exception(f"Code is {response['code']}") + ''; +in { basic = pkgs.nixosTest { name = "jellyfin-basic"; @@ -25,29 +63,7 @@ nodes.client = {}; - # TODO: Test login - testScript = { nodes, ... }: '' - import json - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to j.example.com:443:server:443" - + " --connect-to j.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("jellyfin.service") - server.wait_for_unit("nginx.service") - server.wait_for_open_port(8096) - - response = curl(client, """{"code":%{response_code}}""", "http://j.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; ldap = pkgs.nixosTest { @@ -95,30 +111,7 @@ nodes.client = {}; - # TODO: Test login with ldap user - testScript = { nodes, ... }: '' - import json - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to j.example.com:443:server:443" - + " --connect-to j.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("jellyfin.service") - server.wait_for_unit("nginx.service") - server.wait_for_unit("lldap.service") - server.wait_for_open_port(8096) - - response = curl(client, """{"code":%{response_code}}""", "http://j.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; cert = pkgs.nixosTest { @@ -168,35 +161,7 @@ nodes.client = {}; - # TODO: Test login - testScript = { nodes, ... }: '' - import json - import os - import pathlib - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to j.example.com:443:server:443" - + " --connect-to j.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("jellyfin.service") - server.wait_for_unit("nginx.service") - server.wait_for_open_port(8096) - - server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") - client.succeed("rm -r /etc/ssl/certs") - client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") - - response = curl(client, """{"code":%{response_code}}""", "https://j.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; sso = pkgs.nixosTest { @@ -291,36 +256,6 @@ nodes.client = {}; - # TODO: Test login with ldap user - testScript = { nodes, ... }: '' - import json - import os - import pathlib - - def curl(target, format, endpoint): - return json.loads(target.succeed( - "curl --fail-with-body --silent --show-error --output /dev/null --location" - + " --connect-to j.example.com:443:server:443" - + " --connect-to j.example.com:80:server:80" - + f" --write-out '{format}'" - + " " + endpoint - )) - - start_all() - server.wait_for_unit("jellyfin.service") - server.wait_for_unit("nginx.service") - server.wait_for_unit("lldap.service") - server.wait_for_unit("authelia-auth.example.com.service") - server.wait_for_open_port(8096) - - server.copy_from_vm("/etc/ssl/certs/ca-certificates.crt") - client.succeed("rm -r /etc/ssl/certs") - client.copy_from_host(str(pathlib.Path(os.environ.get("out", os.getcwd())) / "ca-certificates.crt"), "/etc/ssl/certs/ca-certificates.crt") - - response = curl(client, """{"code":%{response_code}}""", "https://j.example.com") - - if response['code'] != 200: - raise Exception(f"Code is {response['code']}") - ''; + testScript = commonTestScript; }; }