From af430c3219094c2dfa2dcb0ca3c07b305baf72af Mon Sep 17 00:00:00 2001 From: Guanran Wang Date: Sat, 10 Feb 2024 22:43:17 +0800 Subject: [PATCH] nixos/clash-meta: add tests --- nixos/tests/all-tests.nix | 1 + nixos/tests/clash-meta.nix | 44 ++++++++++++++++++++ pkgs/tools/networking/clash-meta/default.nix | 5 +++ 3 files changed, 50 insertions(+) create mode 100644 nixos/tests/clash-meta.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index c2114098fe0590a..ccc6943366e4505 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -191,6 +191,7 @@ in { cinnamon = handleTest ./cinnamon.nix {}; cinnamon-wayland = handleTest ./cinnamon-wayland.nix {}; cjdns = handleTest ./cjdns.nix {}; + clash-meta = handleTest ./clash-meta.nix {}; clickhouse = handleTest ./clickhouse.nix {}; cloud-init = handleTest ./cloud-init.nix {}; cloud-init-hostname = handleTest ./cloud-init-hostname.nix {}; diff --git a/nixos/tests/clash-meta.nix b/nixos/tests/clash-meta.nix new file mode 100644 index 000000000000000..2489bf8ca96f91b --- /dev/null +++ b/nixos/tests/clash-meta.nix @@ -0,0 +1,44 @@ +import ./make-test-python.nix ({ pkgs, ... }: { + name = "clash-meta"; + meta.maintainers = with pkgs.lib.maintainers; [ Guanran928 ]; + + nodes.machine = { + environment.systemPackages = [ pkgs.curl ]; + + services.nginx = { + enable = true; + statusPage = true; + }; + + services.clash-meta = { + enable = true; + configFile = pkgs.writeTextFile { + name = "config.yaml"; + text = '' + mixed-port: 7890 + external-controller: 127.0.0.1:9090 + authentication: + - "user:supersecret" + ''; + }; + }; + }; + + testScript = '' + # Wait until it starts + machine.wait_for_unit("nginx.service") + machine.wait_for_unit("clash-meta.service") + machine.wait_for_open_port(80) + machine.wait_for_open_port(7890) + machine.wait_for_open_port(9090) + + # Proxy + machine.succeed("curl --fail --max-time 10 --proxy http://user:supersecret@localhost:7890 http://localhost") + machine.succeed("curl --fail --max-time 10 --proxy socks5://user:supersecret@localhost:7890 http://localhost") + machine.fail("curl --fail --max-time 10 --proxy http://user:supervillain@localhost:7890 http://localhost") + machine.fail("curl --fail --max-time 10 --proxy socks5://user:supervillain@localhost:7890 http://localhost") + + # Web UI + machine.succeed("curl --fail http://localhost:9090") == '{"hello":"clash"}' + ''; +}) diff --git a/pkgs/tools/networking/clash-meta/default.nix b/pkgs/tools/networking/clash-meta/default.nix index 2ec32960d7fa5ff..7d4a0c7e338339e 100644 --- a/pkgs/tools/networking/clash-meta/default.nix +++ b/pkgs/tools/networking/clash-meta/default.nix @@ -1,6 +1,7 @@ { lib , fetchFromGitHub , buildGoModule +, nixosTests }: buildGoModule rec { pname = "clash-meta"; @@ -35,6 +36,10 @@ buildGoModule rec { mv $out/bin/clash $out/bin/clash-meta ''; + passthru.tests = { + nginx = nixosTests.clash-meta; + }; + meta = with lib; { description = "Another Clash Kernel"; homepage = "https://github.com/MetaCubeX/Clash.Meta";