From b6ebd3bac96cc9bb55c3ad337a38de49ddec875b Mon Sep 17 00:00:00 2001 From: Defelo Date: Sat, 12 Jul 2025 16:48:06 +0200 Subject: [PATCH] nixos/tests/chhoto-url: init --- nixos/tests/all-tests.nix | 1 + nixos/tests/chhoto-url.nix | 60 ++++++++++++++++++++++++++ pkgs/by-name/ch/chhoto-url/package.nix | 6 ++- 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 nixos/tests/chhoto-url.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 7aa2dbdbdaf5..dbfb793af023 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -311,6 +311,7 @@ in cfssl = runTestOn [ "aarch64-linux" "x86_64-linux" ] ./cfssl.nix; cgit = runTest ./cgit.nix; charliecloud = runTest ./charliecloud.nix; + chhoto-url = runTest ./chhoto-url.nix; chromadb = runTest ./chromadb.nix; chromium = (handleTestOn [ "aarch64-linux" "x86_64-linux" ] ./chromium.nix { }).stable or { }; chrony = runTestOn [ "aarch64-linux" "x86_64-linux" ] ./chrony.nix; diff --git a/nixos/tests/chhoto-url.nix b/nixos/tests/chhoto-url.nix new file mode 100644 index 000000000000..01572b3c3216 --- /dev/null +++ b/nixos/tests/chhoto-url.nix @@ -0,0 +1,60 @@ +{ config, lib, ... }: + +{ + name = "chhoto-url"; + meta.maintainers = with lib.maintainers; [ defelo ]; + + nodes.machine = { + services.chhoto-url = { + enable = true; + settings.port = 8000; + environmentFiles = [ + (builtins.toFile "chhoto-url.env" '' + api_key=api_key + password=password + '') + ]; + }; + }; + + interactive.nodes.machine = { + services.glitchtip.listenAddress = "0.0.0.0"; + networking.firewall.allowedTCPPorts = [ 8000 ]; + virtualisation.forwardPorts = [ + { + from = "host"; + host.port = 8000; + guest.port = 8000; + } + ]; + }; + + testScript = '' + import json + import re + + machine.wait_for_unit("chhoto-url.service") + machine.wait_for_open_port(8000) + + resp = json.loads(machine.succeed("curl localhost:8000/api/getconfig")) + assert resp["success"] is False + assert resp["reason"] == "No valid authentication was found" + + resp = json.loads(machine.succeed("curl -H 'X-API-Key: api_key' localhost:8000/api/getconfig")) + expected_version = "${config.nodes.machine.services.chhoto-url.package.version}" + assert resp["version"] == expected_version + + resp = json.loads(machine.succeed("curl -H 'X-API-Key: api_key' localhost:8000/api/new -d '{\"longlink\": \"https://nixos.org/\"}'")) + assert resp["success"] is True + assert (match := re.match(r"^http://localhost:8000/(.+)$", resp["shorturl"])) + slug = match[1] + + resp = machine.succeed(f"curl -i {resp["shorturl"]}") + assert (match := re.search(r"(?m)^location: (.+?)\r?$", resp)) + assert match[1] == "https://nixos.org/" + + resp = json.loads(machine.succeed(f"curl -H 'X-API-Key: api_key' localhost:8000/api/expand -d '{slug}'")) + assert resp["success"] is True + assert resp["hits"] == 1 + ''; +} diff --git a/pkgs/by-name/ch/chhoto-url/package.nix b/pkgs/by-name/ch/chhoto-url/package.nix index 73de105cf5cc..8622efa24fd0 100644 --- a/pkgs/by-name/ch/chhoto-url/package.nix +++ b/pkgs/by-name/ch/chhoto-url/package.nix @@ -2,6 +2,7 @@ lib, rustPlatform, fetchFromGitHub, + nixosTests, nix-update-script, }: @@ -30,7 +31,10 @@ rustPlatform.buildRustPackage (finalAttrs: { cp -r ${finalAttrs.src}/resources $out/share/chhoto-url/resources ''; - passthru.updateScript = nix-update-script { }; + passthru = { + tests = { inherit (nixosTests) chhoto-url; }; + updateScript = nix-update-script { }; + }; meta = { description = "Simple, blazingly fast, selfhosted URL shortener with no unnecessary features";