diff --git a/.github/workflows/pr-tests.yml b/.github/workflows/pr-tests.yml index 4fb475f9c..11a0130e8 100644 --- a/.github/workflows/pr-tests.yml +++ b/.github/workflows/pr-tests.yml @@ -16,6 +16,9 @@ jobs: with: fetch-depth: 1 + - name: Run unit tests + run: make test + - name: Run functionality tests run: make test-functionality diff --git a/Makefile b/Makefile index c903015ae..7dd393047 100644 --- a/Makefile +++ b/Makefile @@ -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. diff --git a/controllers/keda/suite_test.go b/controllers/keda/suite_test.go index 3c2ab16ca..4384af8e8 100644 --- a/controllers/keda/suite_test.go +++ b/controllers/keda/suite_test.go @@ -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, diff --git a/controllers/keda/transform/transform_suite_test.go b/controllers/keda/transform/transform_suite_test.go new file mode 100644 index 000000000..90dc795aa --- /dev/null +++ b/controllers/keda/transform/transform_suite_test.go @@ -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") +} diff --git a/controllers/keda/transform/transform_test.go b/controllers/keda/transform/transform_test.go new file mode 100644 index 000000000..f6379d9e1 --- /dev/null +++ b/controllers/keda/transform/transform_test.go @@ -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)) + }) + }) + }) +}) diff --git a/go.mod b/go.mod index db33aef0e..5a11f6815 100644 --- a/go.mod +++ b/go.mod @@ -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