Skip to content

Commit 29816d5

Browse files
authored
Merge pull request #539 from aryan9600/aryan9600/setup-testenv
Fix makefile envtest and controller-gen usage
2 parents 85bf785 + 2b8ede1 commit 29816d5

File tree

4 files changed

+52
-52
lines changed

4 files changed

+52
-52
lines changed

.github/workflows/e2e.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ jobs:
3131
image: kindest/node:v1.21.1@sha256:69860bda5563ac81e3c0057d654b5253219618a22ec3a346306239bba8cfa1a6
3232
- name: Setup Kustomize
3333
uses: fluxcd/pkg/actions/kustomize@main
34-
- name: Setup envtest
35-
uses: fluxcd/pkg/actions/envtest@main
36-
with:
37-
version: "1.19.2"
3834
- name: Setup Helm
3935
uses: fluxcd/pkg/actions/helm@main
4036
- name: Run tests

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
1616
bin/
17+
testbin/
1718
config/release/
1819

1920
# Exclude all libgit2 related files

Makefile

Lines changed: 44 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ LIBGIT2_VERSION ?= 1.1.1
2323

2424
# Other dependency versions
2525
ENVTEST_BIN_VERSION ?= 1.19.2
26-
KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use -i $(ENVTEST_BIN_VERSION) -p path)
2726

2827
# libgit2 related magical paths
2928
# These are used to determine if the target libgit2 version is already available on
@@ -67,6 +66,9 @@ ifdef HAS_OPENSSL
6766
MAKE_PKG_CONFIG_PATH := $(MAKE_PKG_CONFIG_PATH):$(HAS_OPENSSL)/lib/pkgconfig
6867
endif
6968

69+
# Architecture to use envtest with
70+
ENVTEST_ARCH ?= amd64
71+
7072
all: build
7173

7274
build: $(LIBGIT2) ## Build manager binary
@@ -79,15 +81,18 @@ else
7981
go build -o bin/manager main.go
8082
endif
8183

82-
test: $(LIBGIT2) test-api ## Run tests
84+
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
85+
test: $(LIBGIT2) install-envtest test-api ## Run tests
8386
ifeq ($(shell uname -s),Darwin)
8487
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
8588
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
8689
CGO_LDFLAGS="-Wl,-rpath,$(LIBGIT2_LIB_PATH)" \
90+
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
8791
go test ./... -coverprofile cover.out
8892
else
8993
LD_LIBRARY_PATH=$(LIBGIT2_LIB_PATH) \
9094
PKG_CONFIG_PATH=$(MAKE_PKG_CONFIG_PATH) \
95+
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
9196
go test ./... -coverprofile cover.out
9297
endif
9398

@@ -126,7 +131,7 @@ manifests: controller-gen ## Generate manifests, e.g. CRD, RBAC, etc.
126131
cd api; $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role paths="./..." output:crd:artifacts:config="../config/crd/bases"
127132

128133
api-docs: gen-crd-api-reference-docs ## Generate API reference documentation
129-
$(API_REF_GEN) -api-dir=./api/v1beta1 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/source.md
134+
$(GEN_CRD_API_REFERENCE_DOCS) -api-dir=./api/v1beta1 -config=./hack/api-docs/config.json -template-dir=./hack/api-docs/template -out-file=./docs/api/source.md
130135

131136
tidy: ## Run go mod tidy
132137
go mod tidy
@@ -162,50 +167,28 @@ docker-build: ## Build the Docker image
162167
docker-push: ## Push Docker image
163168
docker push $(IMG):$(TAG)
164169

165-
controller-gen: ## Find or download controller-gen
166-
ifeq (, $(shell which controller-gen))
167-
@{ \
168-
set -e; \
169-
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d); \
170-
cd $$CONTROLLER_GEN_TMP_DIR; \
171-
go mod init tmp; \
172-
go get sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VERSION); \
173-
rm -rf $$CONTROLLER_GEN_TMP_DIR; \
174-
}
175-
CONTROLLER_GEN=$(GOBIN)/controller-gen
176-
else
177-
CONTROLLER_GEN=$(shell which controller-gen)
178-
endif
179-
180-
gen-crd-api-reference-docs: ## Find or download gen-crd-api-reference-docs
181-
ifeq (, $(shell which gen-crd-api-reference-docs))
182-
@{ \
183-
set -e; \
184-
API_REF_GEN_TMP_DIR=$$(mktemp -d); \
185-
cd $$API_REF_GEN_TMP_DIR; \
186-
go mod init tmp; \
187-
go get github.com/ahmetb/gen-crd-api-reference-docs@$(GEN_API_REF_DOCS_VERSION); \
188-
rm -rf $$API_REF_GEN_TMP_DIR; \
189-
}
190-
API_REF_GEN=$(GOBIN)/gen-crd-api-reference-docs
191-
else
192-
API_REF_GEN=$(shell which gen-crd-api-reference-docs)
193-
endif
194-
195-
setup-envtest: ## Find or download setup-envtest
196-
ifeq (, $(shell which setup-envtest))
197-
@{ \
198-
set -e; \
199-
SETUP_ENVTEST_TMP_DIR=$$(mktemp -d); \
200-
cd $$SETUP_ENVTEST_TMP_DIR; \
201-
go mod init tmp; \
202-
go get sigs.k8s.io/controller-runtime/tools/setup-envtest@latest; \
203-
rm -rf $$SETUP_ENVTEST_TMP_DIR; \
204-
}
205-
SETUP_ENVTEST=$(GOBIN)/setup-envtest
206-
else
207-
SETUP_ENVTEST=$(shell which setup-envtest)
208-
endif
170+
# Find or download controller-gen
171+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
172+
.PHONY: controller-gen
173+
controller-gen: ## Download controller-gen locally if necessary.
174+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
175+
176+
# Find or download gen-crd-api-reference-docs
177+
GEN_CRD_API_REFERENCE_DOCS = $(shell pwd)/bin/gen-crd-api-reference-docs
178+
.PHONY: gen-crd-api-reference-docs
179+
gen-crd-api-reference-docs: ## Download gen-crd-api-reference-docs locally if necessary
180+
$(call go-install-tool,$(GEN_CRD_API_REFERENCE_DOCS),github.com/ahmetb/[email protected])
181+
182+
ENVTEST = $(shell pwd)/bin/setup-envtest
183+
.PHONY: envtest
184+
setup-envtest: ## Download setup-envtest locally if necessary.
185+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
186+
187+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
188+
ENVTEST_KUBERNETES_VERSION?=latest
189+
install-envtest: setup-envtest ## Download envtest binaries locally.
190+
mkdir -p ${ENVTEST_ASSETS_DIR}
191+
$(ENVTEST) use $(ENVTEST_KUBERNETES_VERSION) --arch=$(ENVTEST_ARCH) --bin-dir=$(ENVTEST_ASSETS_DIR)
209192

210193
libgit2: $(LIBGIT2) ## Detect or download libgit2 library
211194

@@ -237,3 +220,17 @@ ifneq (, $(shell git status --porcelain --untracked-files=no))
237220
exit 1; \
238221
}
239222
endif
223+
224+
# go-install-tool will 'go install' any package $2 and install it to $1.
225+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
226+
define go-install-tool
227+
@[ -f $(1) ] || { \
228+
set -e ;\
229+
TMP_DIR=$$(mktemp -d) ;\
230+
cd $$TMP_DIR ;\
231+
go mod init tmp ;\
232+
echo "Downloading $(2)" ;\
233+
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
234+
rm -rf $$TMP_DIR ;\
235+
}
236+
endef

controllers/suite_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
2021
"math/rand"
2122
"net/http"
2223
"os"
@@ -52,6 +53,8 @@ var storage *Storage
5253
var examplePublicKey []byte
5354
var examplePrivateKey []byte
5455
var exampleCA []byte
56+
var ctx context.Context
57+
var cancel context.CancelFunc
5558

5659
func TestAPIs(t *testing.T) {
5760
RegisterFailHandler(Fail)
@@ -65,6 +68,7 @@ var _ = BeforeSuite(func(done Done) {
6568
logf.SetLogger(
6669
zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)),
6770
)
71+
ctx, cancel = context.WithCancel(context.TODO())
6872

6973
By("bootstrapping test environment")
7074
t := true
@@ -141,7 +145,8 @@ var _ = BeforeSuite(func(done Done) {
141145
Expect(err).ToNot(HaveOccurred(), "failed to setup HelmChartReconciler")
142146

143147
go func() {
144-
err = k8sManager.Start(ctrl.SetupSignalHandler())
148+
defer GinkgoRecover()
149+
err = k8sManager.Start(ctx)
145150
Expect(err).ToNot(HaveOccurred())
146151
}()
147152

@@ -152,6 +157,7 @@ var _ = BeforeSuite(func(done Done) {
152157
}, 60)
153158

154159
var _ = AfterSuite(func() {
160+
cancel()
155161
By("tearing down the test environment")
156162
if storage != nil {
157163
err := os.RemoveAll(storage.BasePath)

0 commit comments

Comments
 (0)