Skip to content

Commit

Permalink
sort out bats tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nieomylnieja committed Jan 11, 2024
1 parent 11a2fe4 commit 084b5c3
Show file tree
Hide file tree
Showing 16 changed files with 131 additions and 61 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ jobs:
GO111MODULE: on
steps:
- name: Check out code
uses: actions/checkout@v3
- uses: actions/setup-go@v4
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
Expand Down
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout Source
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
- name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Release Binaries
uses: goreleaser/goreleaser-action@v5
with:
version: latest
args: release --clean
env:
GITHUB_TOKEN: ${{secrets.GORELEASER_TOKEN}}
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
- uses: actions/setup-go@v4
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
cache: false
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/vulns.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Setup Golang
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version-file: go.mod
check-latest: true
Expand Down
11 changes: 11 additions & 0 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,17 @@ builds:
- amd64
binary: '{{ .ProjectName }}'

dockers:
- image_templates:
- 'nobl9/sloctl:latest'
- 'nobl9/sloctl:{{.Version }}'
dockerfile: Dockerfile
use: buildx
build_flag_templates:
- "--context=default buildx"
- "--build-arg=VERSION={{ .Version }}"
- "--build-arg=LDFLAGS='-s -w -X github.com/nobl9/sloctl/internal/sloctl.BuildVersion={{ .Version }} -X github.com/nobl9/sloctl/internal/sloctl.BuildGitBranch={{ .Branch }} -X github.com/nobl9/sloctl/internal/sloctl.BuildGitRevision={{ .ShortCommit }}'"

archives:
- format: binary
name_template: '{{ .ProjectName }}-{{ if eq .Os "darwin" }}macos{{ else }}{{ .Os }}{{ end }}-{{ .Version }}'
Expand Down
25 changes: 19 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
FROM curlimages/curl:latest AS builder
FROM golang:1.21-alpine3.18 AS builder

ARG VERSION
RUN curl -sL https://github.com/nobl9/sloctl/releases/download/$VERSION/sloctl-linux-${VERSION/v/} -o /tmp/sloctl
RUN chmod +x /tmp/sloctl

WORKDIR /app

COPY ./go.mod ./go.sum ./
COPY ./cmd/sloctl ./cmd/sloctl
COPY ./internal ./internal

ARG LDFLAGS

RUN CGO_ENABLED=0 go build \
-ldflags "${LDFLAGS}" \
-o /artifacts/sloctl \
"${PWD}/cmd/sloctl"

FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /tmp/sloctl /usr/bin/
ENTRYPOINT ["sloctl"]

COPY --from=builder /artifacts/sloctl ./sloctl

ENTRYPOINT ["sloctl"]
15 changes: 13 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
MAKEFLAGS += --silent --no-print-directory

BIN_DIR := ./bin
TEST_DIR := ./test
APP_NAME := sloctl
LDFLAGS += -s -w

Expand Down Expand Up @@ -38,11 +39,21 @@ endef
build:
go build -ldflags=$(LDFLAGS) -o $(BIN_DIR)/$(APP_NAME) .

.PHONY: test
.PHONY: test test/go/unit test/bats/%
## Run all unit tests.
test:
test/unit: test/go/unit test/bats/unit

## Run go unit tests.
test/go/unit:
$(call _print_step,Running go unit tests)
go test -race -cover ./...

## Run all unit tests.
test/bats/%:
$(call _print_step,Running bats $(*F) tests)
docker build -t sloctl-bats -f $(TEST_DIR)/Dockerfile .
docker run --rm sloctl-bats --filter-tags $(*F) $(TEST_DIR)/*

.PHONY: check check/vet check/lint check/gosec check/spell check/trailing check/markdown check/format check/generate check/vulns
## Run all checks.
check: check/vet check/lint check/gosec check/spell check/trailing check/markdown check/format check/generate check/vulns
Expand Down
18 changes: 8 additions & 10 deletions test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
FROM golang:1.21-alpine3.18 AS builder

WORKDIR /src

COPY ./test/test_server.go ./
RUN go mod init test_server
FROM bats/bats:v1.10.0

RUN go build -o /artifacts/test_server .
WORKDIR /sloctl

FROM bats/bats:v1.10.0
COPY ./go.mod ./go.sum ./
COPY ./cmd/sloctl ./cmd/sloctl
COPY ./internal ./internal

RUN apk --no-cache --update add gettext
RUN apk --no-cache --update add \
gettext go jq git python3 py3-pip
RUN pip install yq

COPY ./test ./test
COPY --from=sloctl-test-bin ./sloctl /bin/sloctl
4 changes: 2 additions & 2 deletions test/apply-and-delete.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# setup is run before each test.
setup() {
load "test_helper/bats-assert/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-assert"

generate_inputs "$BATS_TEST_TMPDIR"
}
Expand Down
6 changes: 3 additions & 3 deletions test/config.bats
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ setup_file() {

# setup is run before each test.
setup() {
load "test_helper/bats-assert/load"
load "test_helper/bats-support/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-assert"
load_lib "bats-support"
}

run_sloctl_config() {
Expand Down
10 changes: 5 additions & 5 deletions test/delete-by-name.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# setup_file is run only once for the whole file.
setup_file() {
load "test_helper/bats-assert/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-assert"

generate_inputs "$BATS_FILE_TMPDIR"
run_sloctl apply -f "'$TEST_INPUTS/**'"
Expand All @@ -18,9 +18,9 @@ teardown_file() {

# setup is run before each test.
setup() {
load "test_helper/bats-support/load"
load "test_helper/bats-assert/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-support"
load_lib "bats-assert"
}

# -------------------------------------------------------------
Expand Down
10 changes: 5 additions & 5 deletions test/get.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# setup_file is run only once for the whole file.
setup_file() {
load "test_helper/bats-assert/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-assert"

generate_inputs "$BATS_FILE_TMPDIR"
run_sloctl apply -f "'$TEST_INPUTS/**'"
Expand All @@ -18,9 +18,9 @@ teardown_file() {

# setup is run before each test.
setup() {
load "test_helper/bats-support/load"
load "test_helper/bats-assert/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-support"
load_lib "bats-assert"
}

@test "alert methods" {
Expand Down
7 changes: 4 additions & 3 deletions test/goreleaser.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

# setup is run before each test.
setup_file() {
load "test_helper/bats-assert/load"
load "test_helper/sloctl-utils/load"
load "test_helper/load"
load_lib "bats-assert"

ensure_installed goreleaser

Expand All @@ -20,7 +20,8 @@ setup_file() {
}

setup() {
load "test_helper/bats-assert/load"
load "test_helper/load"
load_lib "bats-assert"
}

teardown_file() {
Expand Down
16 changes: 3 additions & 13 deletions test/setup_suite.bash
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
setup_suite() {
load "test_helper/sloctl-utils/load"
load "test_helper/load"

# General dependencies shared by all tests.
ensure_installed go jq yq git

# Version does need to be kept up to date,
# it is only here to assure we do not break `sloctl version` command.
PKG="github.com/nobl9/n9/pkg/version"
LD_FLAGS="$(
cat <<EOF
-X ${PKG}.BuildVersion=A.B.C.D
-X ${PKG}.BuildGitBranch=$(git rev-parse --short=8 HEAD)
-X ${PKG}.BuildGitRevision=$(git rev-parse --abbrev-ref HEAD)
EOF
)"
export SLOCTL_BIN="$BATS_SUITE_TMPDIR/sloctl"
go build \
-ldflags "$LD_FLAGS" \
-ldflags "-s -w" \
-o "$SLOCTL_BIN" \
"$BATS_TEST_DIRNAME/../main.go"
"$BATS_TEST_DIRNAME/../cmd/sloctl/main.go"

export TEST_SUITE_OUTPUTS="$BATS_TEST_DIRNAME/outputs"
export TEST_SUITE_INPUTS="$BATS_TEST_DIRNAME/inputs"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ assert_deleted() {
# - apply: assert that the output contains the expected object.
# - delete: assert that the output contains 'No resources found'.
_assert_objects_existence() {
load "test_helper/bats-support/load"
load_lib "bats-support"

assert [ -n "$2" ]
assert [ "$(yq -r 'type' <<<"$2")" = "array" ]
Expand Down Expand Up @@ -137,7 +137,7 @@ _assert_objects_existence() {
# them in parallel or a cleanup after the test fails for whatever reason.
# It works for both YAML and JSON files.
generate_inputs() {
load "test_helper/bats-support/load"
load_lib "bats-support"

directory="$1"
test_filename=$(basename "$BATS_TEST_FILENAME" .bats)
Expand Down Expand Up @@ -208,7 +208,7 @@ select_object() {
#
# If 'yq' is provided as one of the dependencies, ensure it is coming from https://github.com/kislyuk/yq.
ensure_installed() {
load "test_helper/bats-support/load"
load_lib "bats-support"

for dep in "$@"; do
if ! command -v "$dep" >/dev/null 2>&1; then
Expand All @@ -219,3 +219,17 @@ ensure_installed() {
fi
done
}

# load_lib
# ================
#
# Summary: Load a given bats library.
#
# Usage: load_lib <name>
#
# Options:
# <name> Name of the library to load.
load_lib() {
local name="$1"
load "/usr/lib/bats/${name}/load.bash"
}
8 changes: 5 additions & 3 deletions test/version.bats
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,22 @@

# setup is run before each test.
setup() {
load "test_helper/bats-assert/load"
load "test_helper/load"
load_lib "bats-support"
load_lib "bats-assert"
}

@test "sloctl version" {
TMP_DIR=$(mktemp -d 2>/dev/null || mktemp -d -t "sloctl-bats") # Works on Linux and OSX.
PKG="github.com/nobl9/n9/pkg/version"
PKG="github.com/nobl9/sloctl/internal"
LD_FLAGS="\
-X ${PKG}.BuildVersion=v0.0.1 \
-X ${PKG}.BuildGitBranch=PC-123-test \
-X ${PKG}.BuildGitRevision=e2602ddc"
go build \
-ldflags "$LD_FLAGS" \
-o "${TMP_DIR}/sloctl" \
"$BATS_TEST_DIRNAME/../main.go"
"$BATS_TEST_DIRNAME/../cmd/sloctl/main.go"

run "${TMP_DIR}/sloctl" version

Expand Down

0 comments on commit 084b5c3

Please sign in to comment.