Skip to content

Commit

Permalink
add namespace controller tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zreigz committed Oct 21, 2024
1 parent 4ac5bb8 commit 0cdbcaa
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/controller/namespaces/reconciler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package namespaces_test

import (
"context"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
console "github.com/pluralsh/console/go/client"
"github.com/pluralsh/deployment-operator/pkg/controller/namespaces"
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
"github.com/stretchr/testify/mock"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Reconciler", Ordered, func() {
Context("When reconciling a resource", func() {
const (
namespaceId = "1"
namespaceName = "test"
)
clusterNamespace := &console.ManagedNamespaceFragment{
ID: namespaceId,
Name: namespaceName,
}
ctx := context.Background()

It("should create Namespace", func() {
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetNamespace", mock.Anything).Return(clusterNamespace, nil)

reconciler := namespaces.NewNamespaceReconciler(fakeConsoleClient, kClient, time.Minute)
_, err := reconciler.Reconcile(ctx, namespaceId)
Expect(err).NotTo(HaveOccurred())

existing := &v1.Namespace{}
Expect(kClient.Get(ctx, types.NamespacedName{Name: namespaceName}, existing)).NotTo(HaveOccurred())
})

It("should Update Namespace", func() {
clusterNamespace.Labels = map[string]interface{}{
"a": "b",
}

fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
fakeConsoleClient.On("GetNamespace", mock.Anything).Return(clusterNamespace, nil)

reconciler := namespaces.NewNamespaceReconciler(fakeConsoleClient, kClient, time.Minute)
_, err := reconciler.Reconcile(ctx, namespaceId)
Expect(err).NotTo(HaveOccurred())

existing := &v1.Namespace{}
Expect(kClient.Get(ctx, types.NamespacedName{Name: namespaceName}, existing)).NotTo(HaveOccurred())
Expect(existing.Labels).To(Not(BeNil()))
Expect(existing.Labels["a"]).To(Equal("b"))
})

})
})
65 changes: 65 additions & 0 deletions pkg/controller/namespaces/suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2024.
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 namespaces_test

import (
"fmt"
"path/filepath"
"runtime"
"testing"

. "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"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
)

// These tests use Ginkgo (BDD-style Go testing framework). Refer to
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
var testEnv *envtest.Environment
var kClient client.Client

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

var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))

By("bootstrapping test environment")
testEnv = &envtest.Environment{
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", fmt.Sprintf("1.28.3-%s-%s", runtime.GOOS, runtime.GOARCH)),
}

cfg, err := testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())

kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(kClient).NotTo(BeNil())
})

var _ = AfterSuite(func() {
By("tearing down the test environment")
err := testEnv.Stop()
Expect(err).NotTo(HaveOccurred())
})

0 comments on commit 0cdbcaa

Please sign in to comment.