Skip to content

Commit d455cde

Browse files
victorvarzaVictor Varza
andauthored
Improve testing (#8)
* improve testing * replace os.exist with log.fatalf where is possible * fix Makefile indentation * revert: replace os.exist with log.fatalf where is possible Co-authored-by: Victor Varza <[email protected]>
1 parent 0d9f21e commit d455cde

File tree

29 files changed

+756
-258
lines changed

29 files changed

+756
-258
lines changed

.env.sample

Lines changed: 0 additions & 14 deletions
This file was deleted.

.github/workflows/tests.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ jobs:
1717
uses: actions/setup-go@v2
1818
with:
1919
go-version: '${{ env.golang-version }}'
20+
- name: Run code format check
21+
run: make format
2022
- name: Run unit tests
2123
run: make test
2224
- name: Run e2e tests

Makefile

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ CC_PKGS = $(shell go list ./pkg/cc/...)
3333
CC_PKGS += $(shell go list ./cmd/cc/...)
3434

3535
.PHONY: all
36-
all: format generate build test
36+
all: format generate build test test-e2e
3737

3838
.PHONY: clean
3939
clean:
@@ -85,19 +85,25 @@ update-go-deps:
8585
##############
8686

8787
.PHONY: format
88-
format: go-fmt jsonnet-fmt check-license shellcheck
88+
format: go-fmt go-vet go-lint go-sec check-license
8989

9090
.PHONY: go-fmt
9191
go-fmt:
92-
gofmt -s -w .
92+
@echo 'Formatting go code...'
93+
@gofmt -s -w .
94+
@echo 'Not formatiing issues found in go codebase!'
95+
9396

9497
.PHONY: check-license
9598
check-license:
9699
./hack/check_license.sh
97100

98-
.PHONY: lint
99-
lint: golangci-lint
100-
$(GOLANGCI_LINT) run
101+
102+
.PHONY: go-lint
103+
go-lint: golangci-lint
104+
@echo 'Linting go code...'
105+
$(GOLANGCI_LINT) run -v --timeout 5m
106+
@echo 'Not linting issues found in go codebase!'
101107

102108
.PHONY: lint-fix
103109
lint-fix: golangci-lint
@@ -110,6 +116,18 @@ golangci-lint:
110116
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) v1.41.1 ;\
111117
}
112118

119+
GOSEC = $(shell pwd)/bin/gosec
120+
go-sec: ## Inspects source code for security problems
121+
@if ! command -v gosec >/dev/null 2>&1 ; then $(call go-get-tool,$(GOSEC),github.com/securego/gosec/v2/cmd/gosec); fi
122+
@echo 'Checking source code for security problems...'
123+
$(GOSEC) ./pkg/...
124+
@echo 'No security problems found in go codebase!'
125+
126+
go-vet: ## Identifying subtle source code issues
127+
@echo 'Vetting go code...'
128+
@go vet $(shell pwd)/pkg/api/...
129+
@echo 'Not issues found in go codebase!'
130+
113131

114132
###########
115133
# Testing #
@@ -121,23 +139,21 @@ ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
121139
test: test-api test-cc
122140

123141
.PHONY: test-api
124-
test-api: source-env
125-
source $(shell pwd)/.env.sample; go test -race $(TEST_RUN_ARGS) -short $(API_PKGS) -count=1 -v
142+
test-api:
143+
go test -race $(TEST_RUN_ARGS) -short $(API_PKGS) -count=1 -v
126144

127145
.PHONY: test-cc
128-
test-cc: source-env
146+
test-cc:
129147
mkdir -p ${ENVTEST_ASSETS_DIR}
130148
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.2/hack/setup-envtest.sh
131149
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test -race $(TEST_RUN_ARGS) -short $(CC_PKGS) -count=1 -v
132150

133151
.PHONY: test-e2e
134-
test-e2e: source-env
152+
test-e2e:
135153
$(shell pwd)/local/setup.sh
136-
@. local/.env.local && go test github.com/adobe/cluster-registry/test/e2e
154+
@. local/.env.local && go test -race github.com/adobe/cluster-registry/test/e2e -count=1 -v
137155
$(shell pwd)/local/cleanup.sh
138156

139-
source-env:
140-
source $(shell pwd)/.env.sample
141157

142158

143159
###############
@@ -163,15 +179,15 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
163179

164180
# go-get-tool will 'go get' any package $2 and install it to $1.
165181
define go-get-tool
166-
@[ -f $(1) ] || { \
167-
set -e ;\
168-
TMP_DIR=$$(mktemp -d) ;\
169-
cd $$TMP_DIR ;\
170-
go mod init tmp ;\
171-
echo "Downloading $(2)" ;\
172-
GOBIN=$(shell pwd)/bin go get $(2) ;\
173-
rm -rf $$TMP_DIR ;\
174-
}
182+
@[ -f $(1) ] || { \
183+
set -e; \
184+
TMP_DIR=$$(mktemp -d); \
185+
cd $$TMP_DIR; \
186+
go mod init tmp; \
187+
echo "Downloading $(2)"; \
188+
GOBIN=$(shell pwd)/bin go get $(2); \
189+
rm -rf $$TMP_DIR; \
190+
}
175191
endef
176192

177193
# -------------

cmd/api/api.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/adobe/cluster-registry/pkg/api/monitoring"
2020
"github.com/adobe/cluster-registry/pkg/api/sqs"
2121
"github.com/adobe/cluster-registry/pkg/api/utils"
22+
"github.com/labstack/gommon/log"
2223
echoSwagger "github.com/swaggo/echo-swagger"
2324
)
2425

@@ -39,7 +40,11 @@ import (
3940
// @name Authorization
4041
func main() {
4142

42-
appConfig := utils.LoadApiConfig()
43+
appConfig, err := utils.LoadApiConfig()
44+
if err != nil {
45+
log.Fatalf("Cannot load the api configuration: '%v'", err.Error())
46+
}
47+
4348
a := api.NewRouter()
4449

4550
a.GET("/api/swagger/*", echoSwagger.WrapHandler)

cmd/cc/client.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,13 @@ func main() {
120120
os.Exit(1)
121121
}
122122

123-
appConfig := utils.LoadClientConfig()
123+
appConfig, err := utils.LoadClientConfig()
124+
125+
if err != nil {
126+
setupLog.Error(err, "failed to load client configuration")
127+
os.Exit(1)
128+
}
129+
124130
sqsProducer := sqs.NewProducer(appConfig, m)
125131

126132
if err = (&controllers.ClusterReconciler{

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,16 @@ require (
2424
github.com/onsi/ginkgo v1.16.5
2525
github.com/onsi/gomega v1.17.0
2626
github.com/prometheus/client_golang v1.11.0
27+
github.com/securego/gosec/v2 v2.9.6 // indirect
2728
github.com/stretchr/testify v1.7.0
2829
github.com/swaggo/echo-swagger v1.0.0
2930
github.com/swaggo/swag v1.7.0
30-
golang.org/x/crypto v0.0.0-20211202192323-5770296d904e // indirect
3131
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616 // indirect
3232
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2
3333
golang.org/x/tools v0.1.8 // indirect
3434
gopkg.in/go-playground/validator.v9 v9.28.0
3535
gopkg.in/square/go-jose.v2 v2.5.1
36+
gopkg.in/yaml.v2 v2.4.0 // indirect
3637
k8s.io/api v0.21.2
3738
k8s.io/apimachinery v0.21.2
3839
k8s.io/client-go v0.21.2

0 commit comments

Comments
 (0)