From 1247e7eae4f480e0b0edc7abbc66c55c0e5bc33f Mon Sep 17 00:00:00 2001 From: Malte Poll <1780588+malt3@users.noreply.github.com> Date: Tue, 27 Feb 2024 13:55:09 +0100 Subject: [PATCH] nix: compile go test binaries for e2e tests in nix --- .github/workflows/e2e_openssl.yml | 2 +- packages/by-name/buildGoTest/package.nix | 79 ++++++++++++++++++++++++ packages/by-name/nunki/package.nix | 17 +++++ 3 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 packages/by-name/buildGoTest/package.nix diff --git a/.github/workflows/e2e_openssl.yml b/.github/workflows/e2e_openssl.yml index 64eccd7c09..9d0a83e398 100644 --- a/.github/workflows/e2e_openssl.yml +++ b/.github/workflows/e2e_openssl.yml @@ -64,7 +64,7 @@ jobs: cat ./workspace/just.perf | tee -a "${GITHUB_STEP_SUMMARY}" - name: E2E Test run: | - env K8S_NAMESPACE=$(cat ./workspace/just.namespace) go test -v -count=1 -tags e2e ./e2e/openssl + env K8S_NAMESPACE=$(cat ./workspace/just.namespace) nix shell .#nunki.e2e --command openssl.test - name: Undeploy if: always() && inputs.skip-undeploy != 'true' run: | diff --git a/packages/by-name/buildGoTest/package.nix b/packages/by-name/buildGoTest/package.nix new file mode 100644 index 0000000000..96e685429d --- /dev/null +++ b/packages/by-name/buildGoTest/package.nix @@ -0,0 +1,79 @@ +{ buildGoModule }: +args': +let + args = args' // { "doCheck" = false; }; +in +buildGoModule ({ + # copy of buildGoModule.buildPhase with the following changes: + # - use `go test -c -o $GOPATH/bin/` instead of `go install` to build the binary of a test package + # original: + # https://github.com/NixOS/nixpkgs/blob/c44815411ae47dd8bbbb92d60c3a83abff28a9f3/pkgs/build-support/go/module.nix#L188-L266 + buildPhase = '' + runHook preBuild + + exclude='\(/_\|examples\|Godeps\|testdata' + if [[ -n "$excludedPackages" ]]; then + IFS=' ' read -r -a excludedArr <<<$excludedPackages + printf -v excludedAlternates '%s\\|' "''${excludedArr[@]}" + excludedAlternates=''${excludedAlternates%\\|} # drop final \| added by printf + exclude+='\|'"$excludedAlternates" + fi + exclude+='\)' + + buildGoDir() { + local cmd="$1" dir="$2" + + . $TMPDIR/buildFlagsArray + + declare -a flags + flags+=($buildFlags "''${buildFlagsArray[@]}") + flags+=(''${tags:+-tags=''${tags// /,}}) + flags+=(''${ldflags:+-ldflags="$ldflags"}) + flags+=("-p" "$NIX_BUILD_CORES") + + if [ "$cmd" = "test" ]; then + flags+=(-vet=off) + flags+=($checkFlags) + fi + + local OUT + if ! OUT="$(go $cmd -c -o $GOPATH/bin/ "''${flags[@]}" $dir 2>&1)"; then + if ! echo "$OUT" | grep -qE '(no( buildable| non-test)?|build constraints exclude all) Go (source )?files'; then + echo "$OUT" >&2 + return 1 + fi + fi + if [ -n "$OUT" ]; then + echo "$OUT" >&2 + fi + return 0 + } + + getGoDirs() { + local type; + type="$1" + if [ -n "$subPackages" ]; then + echo "$subPackages" | sed "s,\(^\| \),\1./,g" + else + find . -type f -name \*$type.go -exec dirname {} \; | grep -v "/vendor/" | sort --unique | grep -v "$exclude" + fi + } + + if (( "''${NIX_DEBUG:-0}" >= 1 )); then + buildFlagsArray+=(-x) + fi + + if [ ''${#buildFlagsArray[@]} -ne 0 ]; then + declare -p buildFlagsArray > $TMPDIR/buildFlagsArray + else + touch $TMPDIR/buildFlagsArray + fi + if [ -z "$enableParallelBuilding" ]; then + export NIX_BUILD_CORES=1 + fi + for pkg in $(getGoDirs ""); do + echo "Building subPackage $pkg" + buildGoDir test "$pkg" + done + ''; +} // args) diff --git a/packages/by-name/nunki/package.nix b/packages/by-name/nunki/package.nix index 8cdd505ee1..b34948900e 100644 --- a/packages/by-name/nunki/package.nix +++ b/packages/by-name/nunki/package.nix @@ -1,8 +1,22 @@ { lib , buildGoModule +, buildGoTest , genpolicy-msft , genpolicy ? genpolicy-msft +, nunki }: +let + e2e = buildGoTest rec { + inherit (nunki) version src proxyVendor vendorHash prePatch CGO_ENABLED; + pname = "${nunki.pname}-e2e"; + + tags = [ "e2e" ]; + + ldflags = [ "-w" ]; + + subPackages = [ "e2e/openssl" ]; + }; +in buildGoModule rec { pname = "nunki"; @@ -68,5 +82,8 @@ buildGoModule rec { # rename the cli binary to nunki mv "$cli/bin/cli" "$cli/bin/nunki" ''; + + passthru.e2e = e2e; + meta.mainProgram = "nunki"; }