-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add envtest setup and controller tests (#10)
Adding the envtest setup, that can be triggered by a Make target and allows unit tests for the controllers. Adding a test provider objects TestInfrastructureProviderClusterStackReleaseTemplate to make unit tests indepedent of any specific provider. Adding mocking setup to generate mocks for the interfaces which can be used in unit testing. Adding mocks for Github client and for the kube client. Adding utils package for testing that contains utility functions to work with conditions and checks whether certain objects have a specific condition and how its properties look like. Adding local cluster stacks that have been downloaded from Github SovereignCloudStack/cluster-stacks repository to make unit tests independent of another repository and make it possible to run them locally. Adding unit tests to CI. Signed-off-by: janiskemper <[email protected]>
- Loading branch information
1 parent
4431f3e
commit 168960c
Showing
305 changed files
with
71,111 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
name: Test Code | ||
# yamllint disable rule:line-length | ||
on: # yamllint disable-line rule:truthy | ||
workflow_dispatch: | ||
pull_request: | ||
types: [opened, synchronize, reopened, ready_for_review] | ||
branches: | ||
- main | ||
- "releases/**" | ||
paths: | ||
- "**.go" | ||
- "**go.mod" | ||
- "**go.sum" | ||
- ".github/workflows/**" | ||
- "Makefile" | ||
- "images/builder/**" | ||
- "images/**" | ||
- "test/releases/**" | ||
push: | ||
branches: | ||
- main | ||
concurrency: | ||
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.event.after }} | ||
cancel-in-progress: true | ||
jobs: | ||
test: | ||
name: Test Code | ||
if: github.event_name != 'pull_request' || !github.event.pull_request.draft | ||
runs-on: ubuntu-latest | ||
timeout-minutes: 10 | ||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@c85c95e3d7251135ab7dc9ce3241c5835cc595a9 # v3.5.3 | ||
- name: Coverage result name | ||
id: name | ||
run: | | ||
if [ ${{ github.event.pull_request }} ]; then | ||
NAME=pr-${{ github.event.pull_request.number }} | ||
else | ||
NAME=${{ github.sha }} | ||
fi | ||
echo name=${NAME} >> $GITHUB_OUTPUT | ||
- name: Setup Go | ||
uses: ./.github/actions/setup-go | ||
|
||
- name: Install dependencies | ||
run: make gotestsum go-cover-treemap setup-envtest helm | ||
|
||
- name: Install go modules for test | ||
shell: bash | ||
run: | | ||
GO111MODULE=on GOPROXY=https://proxy.golang.org go mod download | ||
- name: Running unit tests | ||
env: | ||
GO111MODULE: "on" | ||
run: make test-unit |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -139,6 +139,48 @@ $(HELM): | |
curl -sSL https://get.helm.sh/helm-v3.12.2-linux-amd64.tar.gz | tar xz -C $(TOOLS_BIN_DIR) --strip-components=1 linux-amd64/helm | ||
chmod a+rx $(HELM) | ||
|
||
MOCKERY := $(abspath $(TOOLS_BIN_DIR)/mockery) | ||
mockery: $(MOCKERY) ## Download and extract mockery binary from github releases page | ||
$(MOCKERY): | ||
curl -sSL https://github.com/vektra/mockery/releases/download/v2.32.4/mockery_2.32.4_Linux_x86_64.tar.gz | tar xz -C $(TOOLS_BIN_DIR) | ||
|
||
|
||
|
||
mock_dir := $(shell dirname $$(grep -r -w -l -E "type Client interface" --exclude='Makefile' --exclude-dir=vendor)) | ||
|
||
.PHONY: mock | ||
mock: $(MOCKERY) | ||
@for path in $(mock_dir); do \ | ||
echo "Running mockery for $$path"; \ | ||
$(MOCKERY) --all --recursive --dir=$$path --output=$$path/mocks; \ | ||
done | ||
|
||
.PHONY: mock-clean | ||
mock-clean: | ||
@echo "Erasing the directory test/$(mock_test_dir)/mocks" | ||
@rm -rf $(mock_test_dir)/mocks | ||
@for path in $(mock_dir); do \ | ||
echo "Erasing this directory $$path/mocks"; \ | ||
rm -rf $$path/mocks; \ | ||
done | ||
|
||
go-binsize-treemap := $(abspath $(TOOLS_BIN_DIR)/go-binsize-treemap) | ||
go-binsize-treemap: $(go-binsize-treemap) # Build go-binsize-treemap from tools folder. | ||
$(go-binsize-treemap): | ||
go install github.com/nikolaydubina/[email protected] | ||
|
||
go-cover-treemap := $(abspath $(TOOLS_BIN_DIR)/go-cover-treemap) | ||
go-cover-treemap: $(go-cover-treemap) # Build go-cover-treemap from tools folder. | ||
$(go-cover-treemap): | ||
go install github.com/nikolaydubina/[email protected] | ||
|
||
|
||
GOTESTSUM := $(abspath $(TOOLS_BIN_DIR)/gotestsum) | ||
gotestsum: $(GOTESTSUM) # Build gotestsum from tools folder. | ||
$(GOTESTSUM): | ||
go install gotest.tools/[email protected] | ||
|
||
|
||
all-tools: $(KIND) $(KUBECTL) $(CLUSTERCTL) $(CTLPTL) $(SETUP_ENVTEST) $(ENVSUBST) $(KUSTOMIZE) $(CONTROLLER_GEN) | ||
echo 'done' | ||
|
||
|
@@ -277,6 +319,11 @@ $(WORKER_CLUSTER_KUBECONFIG): | |
|
||
KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env --bin-dir $(abspath $(TOOLS_BIN_DIR)) -p path $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION)) | ||
|
||
.PHONY: test-unit | ||
test-unit: $(SETUP_ENVTEST) $(GOTESTSUM) $(HELM) ## Run unit | ||
@mkdir -p $(shell pwd)/.coverage | ||
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" $(GOTESTSUM) --junitfile=.coverage/junit.xml --format testname -- -mod=vendor \ | ||
-covermode=atomic -coverprofile=.coverage/cover.out -p=4 ./internal/controller/... | ||
|
||
##@ Verify | ||
########## | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.