Skip to content

Commit

Permalink
Add tests for workloads & fix ignoring metadata.managedFields (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinbunney authored Dec 3, 2024
1 parent 4449e71 commit b36092b
Show file tree
Hide file tree
Showing 32 changed files with 197 additions and 151 deletions.
54 changes: 44 additions & 10 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ on:
pull_request:
branches: [master]

permissions:
contents: read
pull-requests: read

env:
GO_VERSION: 1.23

jobs:

build:
Expand All @@ -14,10 +21,10 @@ jobs:
GO111MODULE: on
steps:
- uses: actions/checkout@v4
- name: Set up GO 1.23.x
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: ${{env.GO_VERSION}}
check-latest: true

- run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
Expand All @@ -32,17 +39,23 @@ jobs:
GO111MODULE: on
steps:
- uses: actions/checkout@v4
- name: Set up GO 1.23.x
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: ${{env.GO_VERSION}}
check-latest: true

- run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Setup Environment
run: make ci-build-setup
- name: Run unit tests
run: make test
- name: Archive code coverage report
uses: actions/upload-artifact@v4
if: always()
with:
name: test-code-coverage-report
path: build/test-coverage.html

testacc:
strategy:
Expand All @@ -60,10 +73,10 @@ jobs:
GO111MODULE: on
steps:
- uses: actions/checkout@v4
- name: Set up GO 1.23.x
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: ${{env.GO_VERSION}}
check-latest: true

- run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
Expand All @@ -77,17 +90,23 @@ jobs:
run: make testacc
- name: Stop k3s
run: make k3s-stop
- name: Archive code coverage report
uses: actions/upload-artifact@v4
if: always()
with:
name: testacc-${{matrix.k3s_version}}-code-coverage-report
path: build/testacc-coverage.html

fmtcheck:
runs-on: ubuntu-latest
env:
GO111MODULE: on
steps:
- uses: actions/checkout@v4
- name: Set up GO 1.23.x
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: ${{env.GO_VERSION}}
check-latest: true

- run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
Expand All @@ -102,14 +121,29 @@ jobs:
GO111MODULE: on
steps:
- uses: actions/checkout@v4
- name: Set up GO 1.23.x
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: ${{env.GO_VERSION}}
check-latest: true

- run: echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Setup Environment
run: make ci-build-setup
- name: Run vet
run: make vet

golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: ${{env.GO_VERSION}}
check-latest: true
- name: golangci-lint
uses: golangci/golangci-lint-action@v6
with:
version: v1.60
7 changes: 5 additions & 2 deletions .github/workflows/tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ on:
permissions:
contents: write

env:
GO_VERSION: 1.23

jobs:
goreleaser:
runs-on: ubuntu-latest
Expand All @@ -16,10 +19,10 @@ jobs:
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up GO 1.23.x
- name: Set up GO ${{env.GO_VERSION}}.x
uses: actions/setup-go@v5
with:
go-version: '1.23'
go-version: ${{env.GO_VERSION}}
check-latest: true

- name: Import GPG key
Expand Down
35 changes: 7 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
*.dll
*.exe
.DS_Store
.vscode
example.tf

build/
dist
scripts/kubeconfig.yaml

terraform.tfplan
terraform.tfstate
bin/
modules-dev/
/pkg/
website/.vagrant
website/.bundle
website/build
website/node_modules
.vagrant/

*.backup
./*.tfstate
/*.tfstate
.terraform/
*.log
*.bak
Expand All @@ -23,18 +17,3 @@ website/node_modules
.idea
*.iml
*.test
*.iml
dist

website/vendor

# Test exclusions
!command/test-fixtures/**/*.tfstate
!command/test-fixtures/**/.terraform/
**/terraform-provider-kubectl
k3s

node_modules/
docusaurus/website/build/
coverage.txt
scripts/kubeconfig.yaml
30 changes: 30 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
run:
timeout: 5m
output:
formats:
- format: line-number
path: stderr
- format: checkstyle
path: ./build/lint-report-checkstyle.xml
- format: html
path: ./build/lint-report.html

linters:
enable:
# default linters
#- errcheck
#- gosimple
#- govet
#- ineffassign
#- staticcheck
#- unused

# additional enabled
#- bodyclose
#- goconst
#- gosec
#- misspell
- prealloc

disable-all: true

44 changes: 27 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
SHELL := /usr/bin/env bash
CURRENT_DIR=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
TEST?=$$(go list ./... |grep -v 'vendor')
PKG_NAME=kubernetes
export GO111MODULE=on

export TESTARGS=-race -coverprofile=coverage.txt -covermode=atomic
export KUBECONFIG=$(CURRENT_DIR)/scripts/kubeconfig.yaml

default: build
Expand All @@ -14,13 +12,21 @@ build:
dist:
goreleaser build --single-target --skip validate --clean

test:
go test -i $(TEST) || exit 1
echo $(TEST) | \
xargs -t -n4 go test $(TESTARGS) -timeout=30s -parallel=4
test-setup:
@go install gotest.tools/gotestsum@latest
@go install github.com/boumenot/gocover-cobertura@latest
rm -rf ./build
mkdir -p ./build

test: test-setup
gotestsum --format testname --hide-summary=skipped -- -coverprofile=build/test-coverage.out -covermode=atomic -timeout=30s -count=1 -race ./... || exit 1
go tool cover -html ./build/test-coverage.out -o ./build/test-coverage.html
gocover-cobertura < ./build/test-coverage.out > ./build/test-coverage.xml

testacc:
TF_ACC=1 go test ./kubernetes -v $(TESTARGS) -timeout 120m -count=1
TF_ACC=1 gotestsum --format testname -- -coverprofile=build/testacc-coverage.out -covermode=atomic -timeout=600s -count=1 ./kubernetes || exit 1
go tool cover -html ./build/testacc-coverage.out -o ./build/testacc-coverage.html
gocover-cobertura < ./build/testacc-coverage.out > ./build/testacc-coverage.xml

k3s-start:
@bash scripts/start-k3s.sh
Expand All @@ -31,11 +37,13 @@ k3s-stop:
publish:
goreleaser release --clean

lint: test-setup
golangci-lint run ./...

vet:
@echo "go vet ."
@go vet $$(go list ./... | grep -v vendor/) ; if [ $$? -eq 1 ]; then \
echo ""; \
echo "Vet found suspicious constructs. Please check the reported constructs"; \
@go vet ./... ; if [ $$? -eq 1 ]; then \
echo "[!] Vet found suspicious constructs. Please check the reported constructs"; \
echo "and fix them if necessary before submitting the code for review."; \
exit 1; \
fi
Expand All @@ -48,12 +56,14 @@ fmt:
gofmt -s -w .

fmtcheck:
@sh -c "'$(CURDIR)/scripts/gofmtcheck.sh'"

errcheck:
@sh -c "'$(CURDIR)/scripts/errcheck.sh'"
@if [[ -n `gofmt -l .` ]]; then \
echo "[!] Found unformatted files. Run formatting with \`make fmt\`"; \
exit 1; \
else \
echo "All files are formatted"; \
fi

ci-build-setup:
ci-build-setup: test-setup
sudo rm -f /usr/local/bin/docker-compose
curl -L https://github.com/docker/compose/releases/download/v2.30.3/docker-compose-`uname -s`-`uname -m` > docker-compose
chmod +x docker-compose
Expand All @@ -64,4 +74,4 @@ ci-build-setup:
sudo mv kubectl /usr/local/bin/
bash scripts/gogetcookie.sh

.PHONY: build dist test testacc k3s-start k3s-stop publish vet fmt fmtcheck errcheck ci-build-setup
.PHONY: build dist test-setup test testacc k3s-start k3s-stop publish lint vet fmt fmtcheck ci-build-setup
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,11 @@ In order to test the provider, you can simply run `make test`.
$ make test
```

The provider uses k3s to run integration tests. These tests look for any `*.tf` files in the `_examples` folder and run an `plan`, `apply`, `refresh` and `plan` loop over each file.
The provider uses k3s to run integration tests. These tests look for any `*.tf` files in the `test/e2e` folder and run an `plan`, `apply`, `refresh` and `plan` loop over each file.

Inside each file the string `name-here` is replaced with a unique name during test execution. This is a simple string replace before the TF is applied to ensure that tests don't fail due to naming clashes.

Each scenario can be placed in a folder, to help others navigate and use the examples, and added to the [README.MD](./_examples/README.MD).
Each scenario can be placed in a folder, to help others navigate and use the examples, and added to the [README.MD](./test/e2e/README.MD).

> Note: The test infrastructure doesn't support multi-file TF configurations so ensure your test scenario is in a single file.
Expand Down
13 changes: 0 additions & 13 deletions _examples/README.MD

This file was deleted.

27 changes: 0 additions & 27 deletions codecov.yml

This file was deleted.

2 changes: 1 addition & 1 deletion kubernetes/data_source_kubectl_filename_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func dataSourceKubectlFilenameListRead(ctx context.Context, d *schema.ResourceDa
}
sort.Strings(items)
var elemhash string
var basenames []string
basenames := make([]string, 0, len(items))
for i, s := range items {
elemhash += strconv.Itoa(i) + s
basenames = append(basenames, filepath.Base(s))
Expand Down
2 changes: 1 addition & 1 deletion kubernetes/data_source_kubectl_filename_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func TestAccKubectlDataSourceFilenameList_basic(t *testing.T) {
path := "../_examples/crds"
path := "../test/e2e/crds"
resource.Test(t, resource.TestCase{
PreCheck: func() {},
Providers: testAccProviders,
Expand Down
Loading

0 comments on commit b36092b

Please sign in to comment.