Skip to content

Commit 9a99bd0

Browse files
authored
Merge pull request #68 from RobotSail/RobotSail/issue22
Introduce unit-testing through the Ginkgo BDD framework
2 parents 4175d3c + 10a9b6d commit 9a99bd0

File tree

9 files changed

+321
-298
lines changed

9 files changed

+321
-298
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ testbin/*
1515

1616
# Output of the go coverage tool, specifically when used with LiteIDE
1717
*.out
18+
*.profile
19+
profile.json
1820

1921
# Kubernetes Generated files - skip generated files, except for vendored files
2022

Makefile

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ vet: ## Run go vet against code.
129129
go vet ./...
130130

131131
.PHONY: test
132-
test: lint manifests generate fmt vet lint helm-lint envtest ## Run tests.
133-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
132+
GINKGO_ARGS ?= --progress --fail-on-pending --keep-going --cover --coverprofile=cover.profile --race --trace --json-report=report.json --timeout=3m
133+
test: lint manifests generate fmt vet lint envtest ginkgo ## Run tests.
134+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" $(GINKGO) run $(GINKGO_ARGS) ./...
134135

135136
.PHONY: test-e2e
136137
test-e2e: kuttl ## Run e2e tests. Requires cluster w/ Scribe already installed
@@ -194,32 +195,6 @@ $(CONTROLLER_GEN): $(LOCALBIN)
194195
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
195196
@ echo "✅ Done"
196197

197-
.PHONY: kustomize
198-
KUSTOMIZE = $(LOCALBIN)/kustomize
199-
kustomize: $(KUSTOMIZE)## Download kustomize locally if necessary.
200-
$(KUSTOMIZE): $(LOCALBIN)
201-
@ echo "📥 Downloading kustomize"
202-
GOBIN=$(LOCALBIN) go install sigs.k8s.io/kustomize/kustomize/$(KUSTOMIZE_MAJOR)@$(KUSTOMIZE_VERSION)
203-
@ echo "✅ Done"
204-
205-
.PHONY: helm
206-
HELM := $(LOCALBIN)/helm
207-
HELM_URL := https://get.helm.sh/helm-$(HELM_VERSION)-$(OS)-$(ARCH).tar.gz
208-
helm: $(HELM)
209-
$(HELM): $(LOCALBIN)
210-
@ echo "📥 Downloading helm"
211-
curl -sSL "$(HELM_URL)" | tar xzf - -C $(LOCALBIN) --strip-components=1 --wildcards '*/helm'
212-
@ echo "✅ Done"
213-
214-
215-
.PHONY: golangci-lint
216-
GOLANGCILINT := $(LOCALBIN)/golangci-lint
217-
GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
218-
golangci-lint: $(GOLANGCILINT) ## Download golangci-lint
219-
$(GOLANGCILINT): $(LOCALBIN)
220-
@ echo "📥 Downloading helm"
221-
curl -sSfL $(GOLANGCI_URL) | sh -s -- -b $(LOCALBIN) $(GOLANGCI_VERSION)
222-
@ echo "✅ Done"
223198

224199
.PHONY: envtest
225200
ENVTEST = $(LOCALBIN)/setup-envtest
@@ -297,18 +272,76 @@ catalog-build: opm ## Build a catalog image.
297272
catalog-push: ## Push a catalog image.
298273
$(MAKE) docker-push IMG=$(CATALOG_IMG)
299274

275+
276+
##@ Download Utilities
277+
300278
# download-tool will curl any file $2 and install it to $1.
301279
define download-tool
302280
@[ -f $(1) ] || { \
303281
set -e ;\
304-
echo "Downloading $(2)" ;\
282+
echo "📥 Downloading $(2)" ;\
305283
curl -sSLo "$(1)" "$(2)" ;\
306284
chmod a+x "$(1)" ;\
285+
echo "✅ Done" ;\
286+
}
287+
endef
288+
289+
# install-go-tool will download any $2 URL and install to $1
290+
define install-go-tool
291+
@[ -f $(1) ] || { \
292+
set -e ;\
293+
echo "📥 Downloading $(2)" ;\
294+
GOBIN=$(1) go install $(2) ;\
295+
echo "✅ Done" ;\
307296
}
308297
endef
309298

299+
# install-go-tool will download any $2 URL and install to $1
300+
define install-go-tool-mod
301+
@[ -f $(1) ] || { \
302+
set -e ;\
303+
echo "📥 Downloading $(2)" ;\
304+
GOBIN=$(1) go install -mod=mod $(2) ;\
305+
echo "✅ Done" ;\
306+
}
307+
endef
308+
309+
310+
310311
.PHONY: kuttl
311-
KUTTL := $(PROJECT_DIR)/bin/kuttl
312+
KUTTL := $(LOCALBIN)/kuttl
312313
KUTTL_URL := https://github.com/kudobuilder/kuttl/releases/download/v$(KUTTL_VERSION)/kubectl-kuttl_$(KUTTL_VERSION)_linux_x86_64
313314
kuttl: ## Download kuttl
314315
$(call download-tool,$(KUTTL),$(KUTTL_URL))
316+
317+
.PHONY: ginkgo
318+
GINKGO := $(LOCALBIN)/ginkgo
319+
GINKGO_URL := github.com/onsi/ginkgo/v2/ginkgo
320+
ginkgo: $(GINKGO) ## Install ginkgo
321+
$(GINKGO): $(LOCALBIN)
322+
$(call install-go-tool-mod,$(LOCALBIN),$(GINKGO_URL))
323+
324+
325+
.PHONY: kustomize
326+
KUSTOMIZE = $(LOCALBIN)/kustomize
327+
KUSTOMIZE_URL := sigs.k8s.io/kustomize/kustomize/$(KUSTOMIZE_MAJOR)@$(KUSTOMIZE_VERSION)
328+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
329+
$(KUSTOMIZE): $(LOCALBIN)
330+
$(call install-go-tool,$(LOCALBIN),$(KUSTOMIZE_URL))
331+
332+
.PHONY: helm
333+
HELM := $(LOCALBIN)/helm
334+
HELM_URL := https://get.helm.sh/helm-$(HELM_VERSION)-$(OS)-$(ARCH).tar.gz
335+
helm: $(HELM) ## Install helm
336+
$(HELM): $(LOCALBIN)
337+
$(call download-tool,$(HELM),$(HELM_URL))
338+
339+
340+
.PHONY: golangci-lint
341+
GOLANGCILINT := $(LOCALBIN)/golangci-lint
342+
GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
343+
golangci-lint: $(GOLANGCILINT) ## Download golangci-lint
344+
$(GOLANGCILINT): $(LOCALBIN)
345+
@ echo "📥 Downloading helm"
346+
curl -sSfL $(GOLANGCI_URL) | sh -s -- -b $(LOCALBIN) $(GOLANGCI_VERSION)
347+
@ echo "✅ Done"

controllers/ipfs_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ func (r *IpfsReconciler) createTrackedObjects(
143143

144144
mutsa := r.serviceAccount(instance, &sa)
145145
mutsvc, svcName := r.serviceCluster(instance, &svc)
146-
mutCmScripts, cmScriptName := r.configMapScripts(ctx, instance, &cmScripts)
146+
mutCmScripts, cmScriptName := r.ConfigMapScripts(ctx, instance, &cmScripts)
147147
mutCmConfig, cmConfigName := r.configMapConfig(instance, &cmConfig, peerID.String())
148148
mutSecConfig, secConfigName := r.secretConfig(instance, &secConfig, []byte(clusterSecret), []byte(privateString))
149149
mutSts := r.statefulSet(instance, &sts, svcName, secConfigName, cmConfigName, cmScriptName)

controllers/scripts.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,18 @@ import (
1919
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
2020
)
2121

22-
// configMapScripts Returns a mutate function which loads the given configMap with scripts that
22+
const (
23+
// ScriptConfigureIPFS Defines the script run by the IPFS containers
24+
// in order to initialize their state.
25+
ScriptConfigureIPFS = "configure-ipfs.sh"
26+
// ScriptIPFSClusterEntryPoint Defines a shell script used as the entrypoint
27+
// for the IPFS Cluster container.
28+
ScriptIPFSClusterEntryPoint = "entrypoint.sh"
29+
)
30+
31+
// ConfigMapScripts Returns a mutate function which loads the given configMap with scripts that
2332
// customize the startup of the IPFS containers depending on the values from the given IPFS cluster resource.
24-
func (r *IpfsReconciler) configMapScripts(
33+
func (r *IpfsReconciler) ConfigMapScripts(
2534
ctx context.Context,
2635
m *clusterv1alpha1.Ipfs,
2736
cm *corev1.ConfigMap,

controllers/scripts_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package controllers_test
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
. "github.com/onsi/gomega"
8+
v1 "k8s.io/api/core/v1"
9+
10+
"github.com/redhat-et/ipfs-operator/api/v1alpha1"
11+
"github.com/redhat-et/ipfs-operator/controllers"
12+
)
13+
14+
var _ = Describe("IPFS Reconciler", func() {
15+
var ipfsReconciler *controllers.IpfsReconciler
16+
var ipfs *v1alpha1.Ipfs
17+
var configMap *v1.ConfigMap
18+
var ctx context.Context
19+
20+
BeforeEach(func() {
21+
ctx = context.TODO()
22+
ipfsReconciler = &controllers.IpfsReconciler{
23+
Scheme: k8sClient.Scheme(),
24+
Client: k8sClient,
25+
}
26+
configMap = &v1.ConfigMap{}
27+
ipfs = &v1alpha1.Ipfs{}
28+
})
29+
30+
When("ConfigMapScripts are edited", func() {
31+
const myName = "my-fav-ipfs-node"
32+
BeforeEach(func() {
33+
// uses the name
34+
ipfs.Name = myName
35+
})
36+
37+
It("populates the ConfigMap", func() {
38+
// configMap is empty
39+
Expect(len(configMap.Data)).To(Equal(0))
40+
fn, _ := ipfsReconciler.ConfigMapScripts(ctx, ipfs, configMap)
41+
// should not have errored
42+
Expect(fn()).NotTo(HaveOccurred())
43+
// the configmap should be populated with the following scripts
44+
Expect(len(configMap.Data)).To(Equal(2))
45+
46+
expectedKeys := []string{
47+
controllers.ScriptConfigureIPFS,
48+
controllers.ScriptIPFSClusterEntryPoint,
49+
}
50+
for _, key := range expectedKeys {
51+
data, ok := configMap.Data[key]
52+
Expect(ok).To(BeTrue())
53+
Expect(data).NotTo(BeEmpty())
54+
}
55+
})
56+
57+
It("contains the IPFS resource name", func() {
58+
fn, name := ipfsReconciler.ConfigMapScripts(ctx, ipfs, configMap)
59+
Expect(fn).NotTo(BeNil())
60+
Expect(fn()).NotTo(HaveOccurred())
61+
Expect(name).To(ContainSubstring(myName))
62+
Expect(name).To(Equal("ipfs-cluster-scripts-" + myName))
63+
})
64+
})
65+
})

controllers/suite_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17-
package controllers
17+
package controllers_test
1818

1919
import (
2020
"path/filepath"
2121
"testing"
2222

23-
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/ginkgo/v2"
2424
. "github.com/onsi/gomega"
2525
"k8s.io/client-go/kubernetes/scheme"
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727
"sigs.k8s.io/controller-runtime/pkg/envtest"
28-
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
2928
logf "sigs.k8s.io/controller-runtime/pkg/log"
3029
"sigs.k8s.io/controller-runtime/pkg/log/zap"
3130

@@ -42,9 +41,7 @@ var testEnv *envtest.Environment
4241
func TestAPIs(t *testing.T) {
4342
RegisterFailHandler(Fail)
4443

45-
RunSpecsWithDefaultAndCustomReporters(t,
46-
"Controller Suite",
47-
[]Reporter{printer.NewlineReporter{}})
44+
RunSpecs(t, "Controller Suite")
4845
}
4946

5047
var _ = BeforeSuite(func() {
@@ -68,8 +65,7 @@ var _ = BeforeSuite(func() {
6865
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
6966
Expect(err).NotTo(HaveOccurred())
7067
Expect(k8sClient).NotTo(BeNil())
71-
72-
}, 60)
68+
})
7369

7470
var _ = AfterSuite(func() {
7571
By("tearing down the test environment")

0 commit comments

Comments
 (0)