Skip to content

Commit 5d7d0a3

Browse files
authored
Merge pull request #1183 from Nordix/lentzi90/enable-controller-tests
🏃 Enable controller tests
2 parents 1c3cb90 + 7dad064 commit 5d7d0a3

File tree

3 files changed

+105
-6
lines changed

3 files changed

+105
-6
lines changed

Makefile

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ E2E_DATA_DIR ?= $(REPO_ROOT)/test/e2e/data
3939
E2E_CONF_PATH ?= $(E2E_DATA_DIR)/e2e_conf.yaml
4040
KUBETEST_CONF_PATH ?= $(abspath $(E2E_DATA_DIR)/kubetest/conformance.yaml)
4141
KUBETEST_FAST_CONF_PATH ?= $(abspath $(E2E_DATA_DIR)/kubetest/conformance-fast.yaml)
42+
GO_INSTALL := ./scripts/go_install.sh
4243

4344
# Binaries.
4445
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
@@ -53,6 +54,17 @@ KUSTOMIZE := $(TOOLS_BIN_DIR)/kustomize
5354
MOCKGEN := $(TOOLS_BIN_DIR)/mockgen
5455
RELEASE_NOTES := $(TOOLS_BIN_DIR)/release-notes
5556

57+
# Setup-envtest
58+
SETUP_ENVTEST_VER := v0.0.0-20211110210527-619e6b92dab9
59+
SETUP_ENVTEST_BIN := setup-envtest
60+
SETUP_ENVTEST := $(abspath $(TOOLS_BIN_DIR)/$(SETUP_ENVTEST_BIN)-$(SETUP_ENVTEST_VER))
61+
SETUP_ENVTEST_PKG := sigs.k8s.io/controller-runtime/tools/setup-envtest
62+
63+
# Kubebuilder
64+
export KUBEBUILDER_ENVTEST_KUBERNETES_VERSION ?= 1.23.3
65+
export KUBEBUILDER_CONTROLPLANE_START_TIMEOUT ?= 60s
66+
export KUBEBUILDER_CONTROLPLANE_STOP_TIMEOUT ?= 60s
67+
5668
PATH := $(abspath $(TOOLS_BIN_DIR)):$(PATH)
5769
export PATH
5870
export DOCKER_CLI_EXPERIMENTAL=enabled
@@ -116,9 +128,15 @@ E2E_ARGS ?=
116128
$(ARTIFACTS):
117129
mkdir -p $@
118130

131+
ifeq ($(shell go env GOOS),darwin) # Use the darwin/amd64 binary until an arm64 version is available
132+
KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path --arch amd64 $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION))
133+
else
134+
KUBEBUILDER_ASSETS ?= $(shell $(SETUP_ENVTEST) use --use-env -p path $(KUBEBUILDER_ENVTEST_KUBERNETES_VERSION))
135+
endif
136+
119137
.PHONY: test
120-
test: ## Run tests
121-
go test -v ./...
138+
test: $(SETUP_ENVTEST) ## Run tests
139+
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test -v ./... $(TEST_ARGS)
122140

123141
# Can be run manually, e.g. via:
124142
# export OPENSTACK_CLOUD_YAML_FILE="$(pwd)/clouds.yaml"
@@ -167,6 +185,12 @@ managers:
167185
manager-openstack-infrastructure: ## Build manager binary.
168186
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "${LDFLAGS} -extldflags '-static'" -o $(BIN_DIR)/manager .
169187

188+
$(SETUP_ENVTEST): # Build setup-envtest from tools folder.
189+
GOBIN=$(abspath $(TOOLS_BIN_DIR)) $(GO_INSTALL) $(SETUP_ENVTEST_PKG) $(SETUP_ENVTEST_BIN) $(SETUP_ENVTEST_VER)
190+
191+
.PHONY: $(SETUP_ENVTEST_BIN)
192+
$(SETUP_ENVTEST_BIN): $(SETUP_ENVTEST) ## Build a local copy of setup-envtest.
193+
170194
## --------------------------------------
171195
## Linting
172196
## --------------------------------------

controllers/suite_test.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"context"
2021
"path/filepath"
2122
"testing"
2223

2324
. "github.com/onsi/ginkgo"
2425
. "github.com/onsi/gomega"
26+
corev1 "k8s.io/api/core/v1"
27+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28+
"k8s.io/apimachinery/pkg/types"
2529
"k8s.io/client-go/kubernetes/scheme"
2630
"k8s.io/client-go/rest"
31+
"sigs.k8s.io/cluster-api/test/framework"
2732
"sigs.k8s.io/controller-runtime/pkg/client"
2833
"sigs.k8s.io/controller-runtime/pkg/envtest"
2934
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
@@ -41,10 +46,6 @@ var (
4146
)
4247

4348
func TestAPIs(t *testing.T) {
44-
// TODO(sbueringer) controller don't work yet because kubebuilder is not installed correctly
45-
// and therefore no etcd is available inside the path
46-
t.Skip()
47-
4849
RegisterFailHandler(Fail)
4950

5051
RunSpecsWithDefaultAndCustomReporters(t,
@@ -83,3 +84,32 @@ var _ = AfterSuite(func() {
8384
err := testEnv.Stop()
8485
Expect(err).ToNot(HaveOccurred())
8586
})
87+
88+
var _ = Describe("EnvTest sanity check", func() {
89+
ctx := context.TODO()
90+
It("should be able to create a namespace", func() {
91+
testNamespace := "capo-test"
92+
namespacedName := types.NamespacedName{
93+
Name: testNamespace,
94+
}
95+
namespaceInput := framework.CreateNamespaceInput{
96+
Creator: k8sClient,
97+
Name: testNamespace,
98+
}
99+
100+
// Create the namespace
101+
namespace := framework.CreateNamespace(ctx, namespaceInput)
102+
// Check the result
103+
namespaceResult := &corev1.Namespace{}
104+
err := k8sClient.Get(ctx, namespacedName, namespaceResult)
105+
Expect(err).To(BeNil())
106+
Expect(namespaceResult).To(Equal(namespace))
107+
108+
// Clean up
109+
foregroundDeletePropagation := metav1.DeletePropagationForeground
110+
err = k8sClient.Delete(ctx, namespace, &client.DeleteOptions{PropagationPolicy: &foregroundDeletePropagation})
111+
Expect(err).To(BeNil())
112+
// Note: Since the controller-manager is not part of envtest the namespace
113+
// will actually stay in "Terminating" state and never be completely gone.
114+
})
115+
})

scripts/go_install.sh

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
# Copyright 2021 The Kubernetes Authors.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
set -o errexit
17+
set -o nounset
18+
set -o pipefail
19+
20+
if [ -z "${1}" ]; then
21+
echo "must provide module as first parameter"
22+
exit 1
23+
fi
24+
25+
if [ -z "${2}" ]; then
26+
echo "must provide binary name as second parameter"
27+
exit 1
28+
fi
29+
30+
if [ -z "${3}" ]; then
31+
echo "must provide version as third parameter"
32+
exit 1
33+
fi
34+
35+
if [ -z "${GOBIN}" ]; then
36+
echo "GOBIN is not set. Must set GOBIN to install the bin in a specified directory."
37+
exit 1
38+
fi
39+
40+
rm "${GOBIN}/${2}"* || true
41+
42+
# install the golang module specified as the first argument
43+
go install -tags tools "${1}@${3}"
44+
mv "${GOBIN}/${2}" "${GOBIN}/${2}-${3}"
45+
ln -sf "${GOBIN}/${2}-${3}" "${GOBIN}/${2}"

0 commit comments

Comments
 (0)