Skip to content

Commit

Permalink
Add unit tests for ReplaceAllNamespaces()
Browse files Browse the repository at this point in the history
Signed-off-by: Joel Smith <[email protected]>
  • Loading branch information
joelsmith committed Sep 7, 2023
1 parent f0f1950 commit fed2be1
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/pr-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ jobs:
with:
fetch-depth: 1

- name: Run unit tests
run: make test

- name: Run functionality tests
run: make test-functionality

Expand Down
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ test-deployment: manifests generate fmt vet envtest ## Test OLM deployment.
kubectl create namespace olm --dry-run=client -o yaml | kubectl apply -f -
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -v -ginkgo.v -coverprofile cover.out -test.type deployment -ginkgo.focus "Deploying KedaController manifest"

test: manifests generate fmt vet envtest
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -v -ginkgo.v -coverprofile cover.out -test.type unit

##@ Build

build: generate fmt vet ## Build manager binary.
Expand Down
4 changes: 4 additions & 0 deletions controllers/keda/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func init() {
}

func TestAPIs(t *testing.T) {
// skip unit tests (or anything besides functionality or deployment)
if testType != "functionality" && testType != "deployment" {
return
}
RegisterFailHandler(Fail)

RunSpecs(t,
Expand Down
22 changes: 22 additions & 0 deletions controllers/keda/transform/transform_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package transform_test

import (
"flag"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var (
testType string
)

func TestTransform(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Transform Suite")
}

func init() {
flag.StringVar(&testType, "test.type", "", "type of test: functionality / deployment")
}
107 changes: 107 additions & 0 deletions controllers/keda/transform/transform_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
Copyright 2023 The KEDA Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transform_test

import (
"strings"

mf "github.com/manifestival/manifestival"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"

"github.com/kedacore/keda-olm-operator/controllers/keda/transform"
)

var _ = Describe("Transforming all resource namespaces", func() {
var _ = Describe("Changing namespace", func() {
Context("When transforming a ServiceAccount", func() {

yamlData := `---
apiVersion: v1
kind: ServiceAccount
metadata:
labels:
app.kubernetes.io/name: keda-operator
app.kubernetes.io/part-of: keda-operator
app.kubernetes.io/version: 2.10.1
name: keda-operator
namespace: keda
`
It("Should be able to change the object's metadata.namespace field", func() {
if testType != "unit" {
Skip("test.type isn't 'unit'")
}

manifest, err := mf.ManifestFrom(mf.Reader(strings.NewReader(yamlData)))
Expect(err).To(BeNil())

testNs := "default"
transforms := []mf.Transformer{transform.ReplaceAllNamespaces(testNs)}
newManifest, err := manifest.Transform(transforms...)
Expect(err).To(BeNil())

r := newManifest.Resources()
Expect(len(r)).To(Equal(1))
Expect(r[0].GetNamespace()).To(Equal(testNs))
})
})

Context("When transforming an APIService", func() {

yamlData := `---
apiVersion: apiregistration.k8s.io/v1
kind: APIService
metadata:
labels:
app.kubernetes.io/name: v1beta1.external.metrics.k8s.io
app.kubernetes.io/part-of: keda-operator
app.kubernetes.io/version: 2.10.1
name: v1beta1.external.metrics.k8s.io
spec:
group: external.metrics.k8s.io
groupPriorityMinimum: 100
service:
name: keda-metrics-apiserver
namespace: keda
version: v1beta1
versionPriority: 100
`
It("Should be able to change the namespace in the object's spec.service.namespace field", func() {
if testType != "unit" {
Skip("test.type isn't 'unit'")
}

manifest, err := mf.ManifestFrom(mf.Reader(strings.NewReader(yamlData)))
Expect(err).To(BeNil())

testNs := "default"
transforms := []mf.Transformer{transform.ReplaceAllNamespaces(testNs)}
newManifest, err := manifest.Transform(transforms...)
Expect(err).To(BeNil())

r := newManifest.Resources()
Expect(len(r)).To(Equal(1))
// get spec.service.namespace from the result
ns, found, err := unstructured.NestedString(r[0].UnstructuredContent(), "spec", "service", "namespace")
Expect(found).To(BeTrue())
Expect(err).To(BeNil())
Expect(ns).To(Equal(testNs))
})
})
})
})
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ require (
github.com/nxadm/tail v1.4.8 // indirect
github.com/olekukonko/tablewriter v0.0.4 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.15.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.42.0 // indirect
Expand Down

0 comments on commit fed2be1

Please sign in to comment.