From 02d3d1520d3fc15d0996f07e5d88885283bb39b9 Mon Sep 17 00:00:00 2001 From: Sergiu Ghitea <28300158+sergiught@users.noreply.github.com> Date: Thu, 27 Jun 2024 12:24:06 +0200 Subject: [PATCH] test: add commander testing infrastucture --- .github/workflows/main.yaml | 4 ++-- .gitignore | 1 + Makefile | 22 +++++++++++++++++++++- tests/scripts/run-test-suites.sh | 14 ++++++++++++++ tests/store-test-cases.yaml | 10 ++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/scripts/run-test-suites.sh create mode 100644 tests/store-test-cases.yaml diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index bb87af0..af4b108 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -68,7 +68,7 @@ jobs: - name: Build run: make build - - name: Run unit test + - name: Run tests run: make test shellcheck: @@ -282,4 +282,4 @@ jobs: --type slsaprovenance \ --certificate-oidc-issuer https://token.actions.githubusercontent.com \ --certificate-identity-regexp '^https://github.com/slsa-framework/slsa-github-generator/.github/workflows/generator_container_slsa3.yml@refs/tags/v[0-9]+.[0-9]+.[0-9]+$' \ - $IMAGE@$DIGEST \ No newline at end of file + $IMAGE@$DIGEST diff --git a/.gitignore b/.gitignore index b174599..a890128 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ *.test # Output of the go coverage tool, specifically when used with LiteIDE +coverage/ *.out # Dependency directories (remove the comment below to include it) diff --git a/Makefile b/Makefile index 9c75c65..38c4d0e 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,10 @@ $(GO_BIN)/mockgen: @echo "==> Installing mockgen within ${GO_BIN}" @go install go.uber.org/mock/mockgen@latest +$(GO_BIN)/commander: + @echo "==> Installing commander within ${GO_BIN}" + @go install github.com/commander-cli/commander/v2/cmd/commander@latest + $(MOCK_SRC_DIR): @echo "==> Cloning OpenFGA Go SDK within ${MOCK_SRC_DIR}" @git clone https://github.com/openfga/go-sdk ${MOCK_SRC_DIR} @@ -60,10 +64,18 @@ help: ## Show this help build: ## Build the CLI binary @echo "==> Building binary within ${BUILD_DIR}/${BINARY_NAME}" @go build -v -ldflags "$(GO_LINKER_FLAGS)" -o "${BUILD_DIR}/${BINARY_NAME}" "$(CURDIR)/cmd/fga/main.go" + +build-with-cover: ## Build the CLI binary for the native platform with coverage support + @echo "Building the cli binary" + @go build -cover -ldflags "$(GO_LINKER_FLAGS)" -o "${BUILD_DIR}/${BINARY_NAME}" "$(CURDIR)/cmd/fga/main.go" install: ## Install the CLI binary @$(MAKE) build BUILD_DIR="$(GO_BIN)" +install-with-cover: ## Install the CLI binary for the native platform with coverage support + @echo "Installing the CLI binary with coverage support" + @$(MAKE) build-with-cover BUILD_DIR="$(GO_BIN)" + run: $(GO_BIN)/CompileDaemon ## Watch for changes and recompile the CLI binary @echo "==> Watching for changes" @CompileDaemon -build='make install' -command='fga --version' @@ -73,7 +85,9 @@ clean: ## Clean project files @go clean @rm -f ${BUILD_DIR} -test: ## Run tests +test: test-unit test-integration ## Run all tests + +test-unit: ## Run unit tests go test -race \ -coverpkg=./... \ -coverprofile=coverageunit.tmp.out \ @@ -84,6 +98,12 @@ test: ## Run tests @cat coverageunit.tmp.out | grep -v "mocks" > coverageunit.out @rm coverageunit.tmp.out +test-integration: install-with-cover $(GO_BIN)/fga $(GO_BIN)/commander ## Run integration tests + @echo "==> Running integration tests" + @mkdir -p "coverage" + @PATH=$(GO_BIN):$$PATH GOCOVERDIR=coverage bash ./tests/scripts/run-test-suites.sh + @go tool covdata textfmt -i "coverage" -o "coverage-integration-tests.out" + lint: $(GO_BIN)/golangci-lint ## Lint Go source files @echo "==> Linting Go source files" @golangci-lint run -v --fix -c .golangci.yaml ./... diff --git a/tests/scripts/run-test-suites.sh b/tests/scripts/run-test-suites.sh new file mode 100644 index 0000000..16ba11e --- /dev/null +++ b/tests/scripts/run-test-suites.sh @@ -0,0 +1,14 @@ +#! /bin/bash -v + +docker run -p 8080:8080 -p 8081:8081 -p 3000:3000 -d --name openfga-cli-tests openfga/openfga run + +set +e + +commander test --dir ./tests + +exit_code=$? + +docker stop openfga-cli-tests +docker rm openfga-cli-tests + +exit $exit_code diff --git a/tests/store-test-cases.yaml b/tests/store-test-cases.yaml new file mode 100644 index 0000000..7eee7b9 --- /dev/null +++ b/tests/store-test-cases.yaml @@ -0,0 +1,10 @@ +config: + inherit-env: true + +tests: + 001 - it successfully uses the store command to create a new store: + command: fga store create --name "FGA Demo Store" + exit-code: 0 + stdout: + json: + store.name: "FGA Demo Store"