Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce unit-testing through the Ginkgo BDD framework #68

Merged
merged 12 commits into from
Sep 7, 2022
Merged
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -13,6 +13,8 @@ testbin/*

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
*.profile
profile.json

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

93 changes: 63 additions & 30 deletions Makefile
Original file line number Diff line number Diff line change
@@ -129,8 +129,9 @@ vet: ## Run go vet against code.
go vet ./...

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

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

.PHONY: kustomize
KUSTOMIZE = $(LOCALBIN)/kustomize
kustomize: $(KUSTOMIZE)## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
@ echo "📥 Downloading kustomize"
GOBIN=$(LOCALBIN) go install sigs.k8s.io/kustomize/kustomize/$(KUSTOMIZE_MAJOR)@$(KUSTOMIZE_VERSION)
@ echo "✅ Done"

.PHONY: helm
HELM := $(LOCALBIN)/helm
HELM_URL := https://get.helm.sh/helm-$(HELM_VERSION)-$(OS)-$(ARCH).tar.gz
helm: $(HELM)
$(HELM): $(LOCALBIN)
@ echo "📥 Downloading helm"
curl -sSL "$(HELM_URL)" | tar xzf - -C $(LOCALBIN) --strip-components=1 --wildcards '*/helm'
@ echo "✅ Done"


.PHONY: golangci-lint
GOLANGCILINT := $(LOCALBIN)/golangci-lint
GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
golangci-lint: $(GOLANGCILINT) ## Download golangci-lint
$(GOLANGCILINT): $(LOCALBIN)
@ echo "📥 Downloading helm"
curl -sSfL $(GOLANGCI_URL) | sh -s -- -b $(LOCALBIN) $(GOLANGCI_VERSION)
@ echo "✅ Done"

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


##@ Download Utilities

# download-tool will curl any file $2 and install it to $1.
define download-tool
@[ -f $(1) ] || { \
set -e ;\
echo "Downloading $(2)" ;\
echo "📥 Downloading $(2)" ;\
curl -sSLo "$(1)" "$(2)" ;\
chmod a+x "$(1)" ;\
echo "✅ Done" ;\
}
endef

# install-go-tool will download any $2 URL and install to $1
define install-go-tool
@[ -f $(1) ] || { \
set -e ;\
echo "📥 Downloading $(2)" ;\
GOBIN=$(1) go install $(2) ;\
echo "✅ Done" ;\
}
endef

# install-go-tool will download any $2 URL and install to $1
define install-go-tool-mod
@[ -f $(1) ] || { \
set -e ;\
echo "📥 Downloading $(2)" ;\
GOBIN=$(1) go install -mod=mod $(2) ;\
echo "✅ Done" ;\
}
endef



.PHONY: kuttl
KUTTL := $(PROJECT_DIR)/bin/kuttl
KUTTL := $(LOCALBIN)/kuttl
KUTTL_URL := https://github.com/kudobuilder/kuttl/releases/download/v$(KUTTL_VERSION)/kubectl-kuttl_$(KUTTL_VERSION)_linux_x86_64
kuttl: ## Download kuttl
$(call download-tool,$(KUTTL),$(KUTTL_URL))

.PHONY: ginkgo
GINKGO := $(LOCALBIN)/ginkgo
GINKGO_URL := github.com/onsi/ginkgo/v2/ginkgo
ginkgo: $(GINKGO) ## Install ginkgo
$(GINKGO): $(LOCALBIN)
$(call install-go-tool-mod,$(LOCALBIN),$(GINKGO_URL))


.PHONY: kustomize
KUSTOMIZE = $(LOCALBIN)/kustomize
KUSTOMIZE_URL := sigs.k8s.io/kustomize/kustomize/$(KUSTOMIZE_MAJOR)@$(KUSTOMIZE_VERSION)
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
$(call install-go-tool,$(LOCALBIN),$(KUSTOMIZE_URL))

.PHONY: helm
HELM := $(LOCALBIN)/helm
HELM_URL := https://get.helm.sh/helm-$(HELM_VERSION)-$(OS)-$(ARCH).tar.gz
helm: $(HELM) ## Install helm
$(HELM): $(LOCALBIN)
$(call download-tool,$(HELM),$(HELM_URL))


.PHONY: golangci-lint
GOLANGCILINT := $(LOCALBIN)/golangci-lint
GOLANGCI_URL := https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh
golangci-lint: $(GOLANGCILINT) ## Download golangci-lint
$(GOLANGCILINT): $(LOCALBIN)
@ echo "📥 Downloading helm"
curl -sSfL $(GOLANGCI_URL) | sh -s -- -b $(LOCALBIN) $(GOLANGCI_VERSION)
@ echo "✅ Done"
2 changes: 1 addition & 1 deletion controllers/ipfs_controller.go
Original file line number Diff line number Diff line change
@@ -143,7 +143,7 @@ func (r *IpfsReconciler) createTrackedObjects(

mutsa := r.serviceAccount(instance, &sa)
mutsvc, svcName := r.serviceCluster(instance, &svc)
mutCmScripts, cmScriptName := r.configMapScripts(ctx, instance, &cmScripts)
mutCmScripts, cmScriptName := r.ConfigMapScripts(ctx, instance, &cmScripts)
mutCmConfig, cmConfigName := r.configMapConfig(instance, &cmConfig, peerID.String())
mutSecConfig, secConfigName := r.secretConfig(instance, &secConfig, []byte(clusterSecret), []byte(privateString))
mutSts := r.statefulSet(instance, &sts, svcName, secConfigName, cmConfigName, cmScriptName)
13 changes: 11 additions & 2 deletions controllers/scripts.go
Original file line number Diff line number Diff line change
@@ -19,9 +19,18 @@ import (
ctrllog "sigs.k8s.io/controller-runtime/pkg/log"
)

// configMapScripts Returns a mutate function which loads the given configMap with scripts that
const (
// ScriptConfigureIPFS Defines the script run by the IPFS containers
// in order to initialize their state.
ScriptConfigureIPFS = "configure-ipfs.sh"
// ScriptIPFSClusterEntryPoint Defines a shell script used as the entrypoint
// for the IPFS Cluster container.
ScriptIPFSClusterEntryPoint = "entrypoint.sh"
)

// ConfigMapScripts Returns a mutate function which loads the given configMap with scripts that
// customize the startup of the IPFS containers depending on the values from the given IPFS cluster resource.
func (r *IpfsReconciler) configMapScripts(
func (r *IpfsReconciler) ConfigMapScripts(
ctx context.Context,
m *clusterv1alpha1.Ipfs,
cm *corev1.ConfigMap,
65 changes: 65 additions & 0 deletions controllers/scripts_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package controllers_test

import (
"context"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"

"github.com/redhat-et/ipfs-operator/api/v1alpha1"
"github.com/redhat-et/ipfs-operator/controllers"
)

var _ = Describe("IPFS Reconciler", func() {
var ipfsReconciler *controllers.IpfsReconciler
var ipfs *v1alpha1.Ipfs
var configMap *v1.ConfigMap
var ctx context.Context

BeforeEach(func() {
ctx = context.TODO()
ipfsReconciler = &controllers.IpfsReconciler{
Scheme: k8sClient.Scheme(),
Client: k8sClient,
}
configMap = &v1.ConfigMap{}
ipfs = &v1alpha1.Ipfs{}
})

When("ConfigMapScripts are edited", func() {
const myName = "my-fav-ipfs-node"
BeforeEach(func() {
// uses the name
ipfs.Name = myName
})

It("populates the ConfigMap", func() {
// configMap is empty
Expect(len(configMap.Data)).To(Equal(0))
fn, _ := ipfsReconciler.ConfigMapScripts(ctx, ipfs, configMap)
// should not have errored
Expect(fn()).NotTo(HaveOccurred())
// the configmap should be populated with the following scripts
Expect(len(configMap.Data)).To(Equal(2))

expectedKeys := []string{
controllers.ScriptConfigureIPFS,
controllers.ScriptIPFSClusterEntryPoint,
}
for _, key := range expectedKeys {
data, ok := configMap.Data[key]
Expect(ok).To(BeTrue())
Expect(data).NotTo(BeEmpty())
}
})

It("contains the IPFS resource name", func() {
fn, name := ipfsReconciler.ConfigMapScripts(ctx, ipfs, configMap)
Expect(fn).NotTo(BeNil())
Expect(fn()).NotTo(HaveOccurred())
Expect(name).To(ContainSubstring(myName))
Expect(name).To(Equal("ipfs-cluster-scripts-" + myName))
})
})
})
12 changes: 4 additions & 8 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
@@ -14,18 +14,17 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers
package controllers_test

import (
"path/filepath"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/client-go/kubernetes/scheme"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

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

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
RunSpecs(t, "Controller Suite")
}

var _ = BeforeSuite(func() {
@@ -68,8 +65,7 @@ var _ = BeforeSuite(func() {
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())

}, 60)
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
78 changes: 42 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
@@ -9,19 +9,27 @@ require (
github.com/libp2p/go-libp2p-core v0.16.1
github.com/libp2p/go-libp2p-relay-daemon v0.1.1-0.20220720133550-bd5627c90f06
github.com/multiformats/go-multiaddr v0.5.0
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.17.0
k8s.io/apimachinery v0.23.5
k8s.io/client-go v0.23.0
sigs.k8s.io/controller-runtime v0.11.0
github.com/onsi/ginkgo/v2 v2.1.4
github.com/onsi/gomega v1.20.0
k8s.io/apimachinery v0.25.0
k8s.io/client-go v0.25.0
sigs.k8s.io/controller-runtime v0.12.3
)

require (
cloud.google.com/go/compute v1.6.1 // indirect
cloud.google.com/go/compute v1.7.0 // indirect
github.com/btcsuite/btcd v0.22.1 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.1.3 // indirect
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.1 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.0.1 // indirect
github.com/emicklei/go-restful/v3 v3.9.0 // indirect
github.com/go-openapi/jsonpointer v0.19.5 // indirect
github.com/go-openapi/jsonreference v0.20.0 // indirect
github.com/go-openapi/swag v0.22.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang-jwt/jwt/v4 v4.2.0 // indirect
github.com/google/gnostic v0.6.9 // indirect
github.com/google/pprof v0.0.0-20210720184732-4bb14d4b1be1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-block-format v0.0.3 // indirect
@@ -43,13 +51,15 @@ require (
github.com/ipld/go-codec-dagpb v1.4.0 // indirect
github.com/ipld/go-ipld-prime v0.17.0 // indirect
github.com/jbenet/goprocess v0.1.4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/klauspost/cpuid/v2 v2.0.12 // indirect
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
github.com/libp2p/go-cidranger v1.1.0 // indirect
github.com/libp2p/go-libp2p-asn-util v0.2.0 // indirect
github.com/libp2p/go-libp2p-resource-manager v0.3.0 // indirect
github.com/libp2p/go-msgio v0.2.0 // indirect
github.com/libp2p/go-openssl v0.0.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/minio/sha256-simd v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
@@ -60,76 +70,72 @@ require (
github.com/multiformats/go-multicodec v0.5.0 // indirect
github.com/multiformats/go-multihash v0.2.0 // indirect
github.com/multiformats/go-varint v0.0.6 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
github.com/polydawn/refmt v0.0.0-20201211092308-30ac6d18308e // indirect
github.com/spacemonkeygo/spacelog v0.0.0-20180420211403-2296661a0572 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/stretchr/testify v1.7.2 // indirect
github.com/whyrusleeping/cbor-gen v0.0.0-20210219115102-f37d292932f2 // indirect
golang.org/x/xerrors v0.0.0-20220517211312-f3a8303e98df // indirect
google.golang.org/grpc v1.46.2 // indirect
golang.org/x/tools v0.1.12 // indirect
golang.org/x/xerrors v0.0.0-20220609144429-65e65417b02f // indirect
lukechampine.com/blake3 v1.1.7 // indirect
)

require (
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest v0.11.18 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.13 // indirect
github.com/Azure/go-autorest/autorest v0.11.27 // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.20 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
github.com/form3tech-oss/jwt-go v3.2.3+incompatible // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.3
github.com/go-logr/zapr v1.2.0 // indirect
github.com/go-logr/zapr v1.2.3 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/go-cmp v0.5.8 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/gnostic v0.5.5 // indirect
github.com/imdario/mergo v0.3.12 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.2-0.20181231171920-c182affec369 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/nxadm/tail v1.4.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.12.1 // indirect
github.com/prometheus/client_golang v1.13.0 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
github.com/prometheus/common v0.33.0 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
go.uber.org/zap v1.21.0 // indirect
golang.org/x/crypto v0.0.0-20220525230936-793ad666bf5e // indirect
golang.org/x/net v0.0.0-20220520000938-2e3eb7b945c2 // indirect
golang.org/x/oauth2 v0.0.0-20220411215720-9780585627b5 // indirect
golang.org/x/sys v0.0.0-20220808155132-1c4a2a72c664 // indirect
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211 // indirect
golang.org/x/net v0.0.0-20220826154423-83b083e8dc8b // indirect
golang.org/x/oauth2 v0.0.0-20220822191816-0ebed06d0094 // indirect
golang.org/x/sys v0.0.0-20220825204002-c680a09ffe64 // indirect
golang.org/x/term v0.0.0-20220722155259-a9ba230a4035 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20210723032227-1f47c861a9ac // indirect
golang.org/x/time v0.0.0-20220722155302-e5dcc9cfc0b9 // indirect
gomodules.xyz/jsonpatch/v2 v2.2.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.28.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v2 v2.4.0
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.23.5
k8s.io/apiextensions-apiserver v0.23.0 // indirect
k8s.io/component-base v0.23.0 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20211116205334-6203023598ed // indirect
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
k8s.io/api v0.25.0
k8s.io/apiextensions-apiserver v0.25.0 // indirect
k8s.io/component-base v0.25.0 // indirect
k8s.io/klog/v2 v2.70.1 // indirect
k8s.io/kube-openapi v0.0.0-20220803164354-a70c9af30aea // indirect
k8s.io/utils v0.0.0-20220823124924-e9cbc92d1a73 // indirect
sigs.k8s.io/json v0.0.0-20220713155537-f223a00ba0e2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
344 changes: 123 additions & 221 deletions go.sum

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build tools
// +build tools

package main

// this file exists so that we can store the dependencies from the
// ginkgo/v2 binary within our go.mod file
import (
_ "github.com/onsi/ginkgo/v2/ginkgo"
)