Skip to content

Commit

Permalink
Unit test enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
halim-lee committed Jun 21, 2023
1 parent b365088 commit f47a7ae
Show file tree
Hide file tree
Showing 3 changed files with 691 additions and 104 deletions.
37 changes: 25 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ endif
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:crdVersions=v1,generateEmbeddedObjectMeta=true"

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.24

# Produce files under internal/deploy/kustomize/daily with runtime-component namespace
KUSTOMIZE_NAMESPACE = runtime-component
KUSTOMIZE_IMG = icr.io/appcafe/runtime-component-operator:daily
Expand Down Expand Up @@ -141,22 +144,31 @@ LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

## Tool Binaries
KUSTOMIZE ?= $(LOCALBIN)/kustomize
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
# find or download controller-gen
# download controller-gen if necessary
.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/[email protected]
ENVTEST ?= $(LOCALBIN)/setup-envtest

KUSTOMIZE ?= $(LOCALBIN)/kustomize
## Tool Versions
KUSTOMIZE_VERSION ?= 4.5.5
CONTROLLER_TOOLS_VERSION ?= 0.9.2

KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/kustomize/v${KUSTOMIZE_VERSION}/hack/install_kustomize.sh"
.PHONY: kustomize
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
$(KUSTOMIZE): $(LOCALBIN)
test -s $(LOCALBIN)/kustomize || curl -s $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s $(KUSTOMIZE_VERSION) $(LOCALBIN)

.PHONY: controller-gen
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
$(CONTROLLER_GEN): $(LOCALBIN)
test -s $(LOCALBIN)/controller-gen || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@v$(CONTROLLER_TOOLS_VERSION)

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: setup
setup: ## Ensure Operator SDK is installed.
./scripts/installers/install-operator-sdk.sh ${OPERATOR_SDK_RELEASE_VERSION}
Expand Down Expand Up @@ -219,12 +231,13 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
.PHONY: test
test: manifests generate fmt vet ## Run tests.
mkdir -p ${ENVTEST_ASSETS_DIR}
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
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out

.PHONY: test-cover
test-cover: test
go tool cover -html=cover.out

.PHONY: unit-test
unit-test: ## Run unit tests
Expand Down
21 changes: 11 additions & 10 deletions utils/reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package utils
import (
"context"
"fmt"
appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"
"reflect"
"strings"
"testing"

appstacksv1 "github.com/application-stacks/runtime-component-operator/api/v1"

"github.com/application-stacks/runtime-component-operator/common"

routev1 "github.com/openshift/api/route/v1"
Expand Down Expand Up @@ -166,7 +167,7 @@ func TestGetDiscoveryClient(t *testing.T) {
logger := zap.New()
logf.SetLogger(logger)

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand All @@ -186,7 +187,7 @@ func TestCreateOrUpdate(t *testing.T) {
logf.SetLogger(logger)
serviceAccount := &corev1.ServiceAccount{ObjectMeta: defaultMeta}

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand All @@ -207,7 +208,7 @@ func TestDeleteResources(t *testing.T) {
logger := zap.New()
logf.SetLogger(logger)

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand Down Expand Up @@ -260,7 +261,7 @@ func TestGetOpConfigMap(t *testing.T) {
},
}

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand All @@ -286,7 +287,7 @@ func TestManageError(t *testing.T) {
logf.SetLogger(logger)
err := fmt.Errorf("test-error")

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand All @@ -307,7 +308,7 @@ func TestManageSuccess(t *testing.T) {
logger := zap.New()
logf.SetLogger(logger)

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand All @@ -326,7 +327,7 @@ func TestIsGroupVersionSupported(t *testing.T) {
logger := zap.New()
logf.SetLogger(logger)

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
objs, s := []runtime.Object{runtimecomponent}, scheme.Scheme
s.AddKnownTypes(appstacksv1.GroupVersion, runtimecomponent)
cl := fakeclient.NewFakeClient(objs...)
Expand Down Expand Up @@ -363,7 +364,7 @@ func TestIsGroupVersionSupported(t *testing.T) {
func testGetSvcTLSValues(t *testing.T) {
// Configure the runtime component

runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
expose := true
runtimecomponent.Spec.Expose = &expose
runtimecomponent.Spec.Service = &appstacksv1.RuntimeComponentService{
Expand Down Expand Up @@ -404,7 +405,7 @@ func testGetSvcTLSValues(t *testing.T) {
// testGetRouteTLSValues test the function GetRouteTLSValues in reconciler.go.
func testGetRouteTLSValues(t *testing.T) {
// Configure the rumtime component
runtimecomponent := createRuntimeComponent(name, namespace, spec)
runtimecomponent := createRuntimeComponent(objMeta, spec)
terminationPolicy := routev1.TLSTerminationReencrypt
secretRefName := "my-app-route-tls"
runtimecomponent.Spec.Expose = &expose
Expand Down
Loading

0 comments on commit f47a7ae

Please sign in to comment.