From 5fad4242cc4f68c719ae925d200a36d94a670833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20Hamb=C3=BCchen?= Date: Sun, 3 Apr 2022 19:24:06 +0000 Subject: [PATCH] tusd: init at 2.8.0 --- nixos/tests/all-tests.nix | 1 + nixos/tests/tusd/default.nix | 50 +++++++++++++++++++++++++++++ nixos/tests/tusd/tus-curl-upload.sh | 50 +++++++++++++++++++++++++++++ pkgs/by-name/tu/tusd/package.nix | 38 ++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 nixos/tests/tusd/default.nix create mode 100755 nixos/tests/tusd/tus-curl-upload.sh create mode 100644 pkgs/by-name/tu/tusd/package.nix diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index a00ce39bdf4f..b9fc4c77a348 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -1389,6 +1389,7 @@ in tuptime = handleTest ./tuptime.nix { }; turbovnc-headless-server = handleTest ./turbovnc-headless-server.nix { }; turn-rs = handleTest ./turn-rs.nix { }; + tusd = runTest ./tusd/default.nix; tuxguitar = runTest ./tuxguitar.nix; twingate = runTest ./twingate.nix; typesense = handleTest ./typesense.nix { }; diff --git a/nixos/tests/tusd/default.nix b/nixos/tests/tusd/default.nix new file mode 100644 index 000000000000..3ef7544dd22f --- /dev/null +++ b/nixos/tests/tusd/default.nix @@ -0,0 +1,50 @@ +{ pkgs, lib, ... }: + +let + port = 1080; + + client = + { pkgs, ... }: + { + environment.systemPackages = [ pkgs.curl ]; + }; + + server = + { pkgs, ... }: + { + # tusd does not have a NixOS service yet. + systemd.services.tusd = { + wantedBy = [ "multi-user.target" ]; + + serviceConfig = { + ExecStart = ''${pkgs.tusd}/bin/tusd -port "${toString port}" -upload-dir=/data''; + }; + }; + networking.firewall.allowedTCPPorts = [ port ]; + }; +in +{ + name = "tusd"; + meta.maintainers = with lib.maintainers; [ + nh2 + kalbasit + ]; + + nodes = { + inherit server; + inherit client; + }; + + testScript = '' + server.wait_for_unit("tusd.service") + server.wait_for_open_port(${toString port}) + + # Create large file. + client.succeed("${pkgs.coreutils}/bin/truncate --size=100M file-100M.bin") + + # Upload it. + client.succeed("${./tus-curl-upload.sh} file-100M.bin http://server:${toString port}/files/") + + print("Upload succeeded") + ''; +} diff --git a/nixos/tests/tusd/tus-curl-upload.sh b/nixos/tests/tusd/tus-curl-upload.sh new file mode 100755 index 000000000000..a85d163cc25e --- /dev/null +++ b/nixos/tests/tusd/tus-curl-upload.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +# Adapted from: +# - https://github.com/tus/tus.io/issues/96 + +if [ ! -f "${1}" ]; then + echo -e "\n\033[1;31m✘\033[0m First argument needs to be an existing file.\n" + exit 1 +fi + +if [ -z "${2}" ]; then + echo -e "\n\033[1;31m✘\033[0m Second argument needs to be the TUS server's URL.\n" + exit 1 +fi + +file=${1} +TUS_URL=${2} +filename=$(basename "${file}" | base64) +filesize="$(wc -c <"${file}")" + +# Apparently 'Location: ..' is terminated by CRLF. grep and awk faithfully +# preserve the line ending, and the shell's $() substitution strips off the +# final LF leaving you with a string that just ends with a CR. +# +# When the CR is printed, the cursor moves to the beginning of the line and +# whatever gets printed next overwrites what was there. +# ... | tr -d '\015' +location=$(curl \ + --silent --show-error \ + -I \ + -X POST \ + -H "Tus-Resumable: 1.0.0" \ + -H "Content-Length: 0" \ + -H "Upload-Length: ${filesize}" \ + -H "Upload-Metadata: name ${filename}" \ + "${TUS_URL}" | grep 'Location:' | awk '{print $2}' | tr -d '\015') + +if [ -n "${location}" ]; then + curl \ + -X PATCH \ + -H "Tus-Resumable: 1.0.0" \ + -H "Upload-Offset: 0" \ + -H "Content-Length: ${filesize}" \ + -H "Content-Type: application/offset+octet-stream" \ + --data-binary "@${file}" \ + "${location}" -v +else + echo -e "\n\033[1;31m✘\033[0m File creation failed..\n" + exit 1 +fi diff --git a/pkgs/by-name/tu/tusd/package.nix b/pkgs/by-name/tu/tusd/package.nix new file mode 100644 index 000000000000..387352e4f553 --- /dev/null +++ b/pkgs/by-name/tu/tusd/package.nix @@ -0,0 +1,38 @@ +{ + lib, + buildGoModule, + fetchFromGitHub, + nixosTests, +}: + +buildGoModule rec { + pname = "tusd"; + version = "2.8.0"; + + src = fetchFromGitHub { + owner = "tus"; + repo = "tusd"; + tag = "v${version}"; + hash = "sha256-OzXBeLDjaJk4NVgsauR/NUATh7qHbuEfWNdhytZmd0A="; + }; + + vendorHash = "sha256-YununGyB72zE0tmqO3BREJeMTjCuy/1fhPHC5r8OLjg="; + + # Tests need the path to the binary: + # https://github.com/tus/tusd/blob/0e52ad650abed02ec961353bb0c3c8bc36650d2c/internal/e2e/e2e_test.go#L37 + preCheck = '' + export TUSD_BINARY=$PWD/../go/bin/tusd + ''; + + passthru.tests.tusd = nixosTests.tusd; + + meta = { + description = "Reference server implementation in Go of tus: the open protocol for resumable file uploads"; + license = lib.licenses.mit; + homepage = "https://tus.io/"; + maintainers = with lib.maintainers; [ + nh2 + kalbasit + ]; + }; +}