From 4b80387ac9d5b8d51afa0e784c16598cc700ba48 Mon Sep 17 00:00:00 2001 From: Simon Gerber Date: Fri, 20 Dec 2024 10:08:15 +0100 Subject: [PATCH] Add testing framework and a implement tests for `syn-teams.libsonnet` --- .github/workflows/test.yml | 37 +++++++++++ Makefile | 45 ++++++++++++++ Makefile.vars.mk | 15 +++++ tests/golden/syn-teams.yml | 61 ++++++++++++++++++ tests/run-instance.sh | 15 +++++ tests/syn-teams.jsonnet | 124 +++++++++++++++++++++++++++++++++++++ tests/syn-teams.yaml | 43 +++++++++++++ 7 files changed, 340 insertions(+) create mode 100644 .github/workflows/test.yml create mode 100644 Makefile create mode 100644 Makefile.vars.mk create mode 100644 tests/golden/syn-teams.yml create mode 100644 tests/run-instance.sh create mode 100644 tests/syn-teams.jsonnet create mode 100644 tests/syn-teams.yaml diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..9453ce0 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,37 @@ +--- +name: Lint & Test + +"on": + pull_request: {} + +jobs: + jsonnetfmt: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - run: | + make jsonnetfmt_check + + test_libraries_discover_cases: + name: Discover golden test cases + runs-on: ubuntu-latest + outputs: + instances: ${{ steps.instances.outputs.instances }} + steps: + - uses: actions/checkout@v4 + - name: Find test cases + id: instances + run: | + echo "instances=$(make -s list_test_instances)" >> "$GITHUB_OUTPUT" + + test_libraries: + needs: test_libraries_discover_cases + strategy: + matrix: + instance: ${{ fromJSON(needs.test_libraries_discover_cases.outputs.instances) }} + runs-on: ubuntu-latest + name: 'Golden test: ${{ matrix.instance }}' + steps: + - uses: actions/checkout@v4 + - run: | + make golden-diff -e instance=${{ matrix.instance }} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..6b6c046 --- /dev/null +++ b/Makefile @@ -0,0 +1,45 @@ +MAKEFLAGS += --warn-undefined-variables +SHELL := bash +.SHELLFLAGS := -eu -o pipefail -c + +include Makefile.vars.mk + +.PHONY: jsonnetfmt_check jsonnetfmt +jsonnetfmt_check: JSONNET_ENTRYPOINT=jsonnetfmt +jsonnetfmt_check: + $(JSONNET_DOCKER) --test --pad-arrays -- *.libsonnet + +jsonnetfmt: JSONNET_ENTRYPOINT=jsonnetfmt +jsonnetfmt: + $(JSONNET_DOCKER) --in-place --pad-arrays -- *.libsonnet + +tests/lib/commodore-real.libjsonnet: + @mkdir -p "tests/lib" + curl -fsSLo "tests/lib/commodore-real.libjsonnet" https://raw.githubusercontent.com/projectsyn/commodore/refs/heads/master/commodore/lib/commodore.libjsonnet + +.PHONY: gen-golden +gen-golden: tests/lib/commodore-real.libjsonnet + $(JSONNET_DOCKER) tests/run-instance.sh $(instance) > tests/golden/$(instance).yml + +.PHONY: golden-diff +golden-diff: tests/lib/commodore-real.libjsonnet + @mkdir -p /tmp/golden + $(JSONNET_DOCKER) tests/run-instance.sh $(instance) > /tmp/golden/$(instance).yml + @git diff --exit-code --minimal --no-index -- tests/golden/$(instance).yml /tmp/golden/$(instance).yml + +.PHONY: golden-diff-all +golden-diff-all: recursive_target=golden-diff +golden-diff-all: $(test_instances) + +.PHONY: gen-golden-all +gen-golden-all: recursive_target=gen-golden +gen-golden-all: $(test_instances) + +.PHONY: $(test_instances) +$(test_instances): + $(MAKE) $(recursive_target) -e instance=$(basename $(@F)) + +.PHONY: list_test_instances +list_test_instances: JSONNET_ENTRYPOINT=jsonnet +list_test_instances: + $(JSONNET_DOCKER) -J . -J tests --ext-str instances="$(basename $(notdir $(test_instances)))" -e 'std.split(std.extVar("instances"), " ")' | jq -c diff --git a/Makefile.vars.mk b/Makefile.vars.mk new file mode 100644 index 0000000..13b418e --- /dev/null +++ b/Makefile.vars.mk @@ -0,0 +1,15 @@ +ifneq "$(shell which docker 2>/dev/null)" "" + DOCKER_CMD ?= $(shell which docker) + DOCKER_USERNS ?= "" +else + DOCKER_CMD ?= podman + DOCKER_USERNS ?= keep-id +endif +DOCKER_ARGS ?= run --rm -u "$$(id -u):$$(id -g)" --userns=$(DOCKER_USERNS) -w /work -e HOME="/work" + +JSONNET_IMAGE ?= docker.io/bitnami/jsonnet:latest +JSONNET_ENTRYPOINT ?= bash +JSONNET_DOCKER ?= $(DOCKER_CMD) $(DOCKER_ARGS) -v "$${PWD}:/work" --entrypoint=$(JSONNET_ENTRYPOINT) $(JSONNET_IMAGE) + +test_instances=$(shell find tests/ -maxdepth 1 -name '*.jsonnet') +instance=syn-teams diff --git a/tests/golden/syn-teams.yml b/tests/golden/syn-teams.yml new file mode 100644 index 0000000..9837abe --- /dev/null +++ b/tests/golden/syn-teams.yml @@ -0,0 +1,61 @@ +{ + "all_teams": [ + "fragrant-flower", + "solitary-wood", + "sparkling-sound" + ], + "appKeys": { + "foo": [ + "foo" + ], + "foo-bar": [ + "foo_bar" + ], + "foo-bar as bar": [ + "bar", + "foo_bar" + ], + "foo-bar as bar-qux": [ + "bar_qux", + "foo_bar" + ] + }, + "appKeysRaw": { + "foo": [ + "foo" + ], + "foo-bar": [ + "foo-bar" + ], + "foo-bar as bar": [ + "bar", + "foo-bar" + ], + "foo-bar as bar-qux": [ + "bar-qux", + "foo-bar" + ] + }, + "appsForTeam": { + "fragrant-flower": [ + "red-flower" + ], + "solitary-wood": [ + "small-sun" + ], + "sparkling-sound": [ + "cool-breeze", + "fragrant-smoke" + ] + }, + "teamForApp": { + "cool-breeze": "sparkling-sound", + "fragrant-smoke": "sparkling-sound", + "red-flower": "fragrant-flower", + "small-sun": "solitary-wood" + }, + "teams": [ + "fragrant-flower", + "solitary-wood" + ] +} diff --git a/tests/run-instance.sh b/tests/run-instance.sh new file mode 100644 index 0000000..2515499 --- /dev/null +++ b/tests/run-instance.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -e -u -x + +testdir=$(dirname "$0") +instance="${testdir}/${1}" + +cat > "${testdir}/lib/commodore.libjsonnet" <