Skip to content

Commit e618ab8

Browse files
authored
test: add controler's unit tests (#304)
* add controler's unit tests * add namespace controller tests * gofmt
1 parent 4c20742 commit e618ab8

5 files changed

Lines changed: 284 additions & 10 deletions

File tree

pkg/common/health_test.go

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package common_test
22

33
import (
4+
"os"
5+
"path/filepath"
46
"time"
57

68
. "github.com/onsi/ginkgo/v2"
79
. "github.com/onsi/gomega"
810
deploymentsv1alpha1 "github.com/pluralsh/deployment-operator/api/v1alpha1"
911
"github.com/pluralsh/deployment-operator/pkg/common"
1012
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11-
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
12-
"k8s.io/apimachinery/pkg/runtime"
1313
)
1414

1515
var _ = Describe("Health Test", Ordered, func() {
@@ -21,9 +21,9 @@ var _ = Describe("Health Test", Ordered, func() {
2121
}
2222

2323
It("should get default status from CRD without condition block", func() {
24-
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(customResource)
24+
obj, err := common.ToUnstructured(customResource)
2525
Expect(err).NotTo(HaveOccurred())
26-
status, err := common.GetResourceHealth(&unstructured.Unstructured{Object: obj})
26+
status, err := common.GetResourceHealth(obj)
2727
Expect(err).NotTo(HaveOccurred())
2828
Expect(status).To(Not(BeNil()))
2929
Expect(*status).To(Equal(common.HealthStatus{
@@ -39,9 +39,9 @@ var _ = Describe("Health Test", Ordered, func() {
3939
},
4040
},
4141
}
42-
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(customResource)
42+
obj, err := common.ToUnstructured(customResource)
4343
Expect(err).NotTo(HaveOccurred())
44-
status, err := common.GetResourceHealth(&unstructured.Unstructured{Object: obj})
44+
status, err := common.GetResourceHealth(obj)
4545
Expect(err).NotTo(HaveOccurred())
4646
Expect(status).To(Not(BeNil()))
4747
Expect(*status).To(Equal(common.HealthStatus{
@@ -58,9 +58,9 @@ var _ = Describe("Health Test", Ordered, func() {
5858
},
5959
},
6060
}
61-
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(customResource)
61+
obj, err := common.ToUnstructured(customResource)
6262
Expect(err).NotTo(HaveOccurred())
63-
status, err := common.GetResourceHealth(&unstructured.Unstructured{Object: obj})
63+
status, err := common.GetResourceHealth(obj)
6464
Expect(err).NotTo(HaveOccurred())
6565
Expect(status).To(Not(BeNil()))
6666
Expect(*status).To(Equal(common.HealthStatus{
@@ -72,9 +72,9 @@ var _ = Describe("Health Test", Ordered, func() {
7272
customResource.DeletionTimestamp = &metav1.Time{
7373
Time: time.Now(),
7474
}
75-
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(customResource)
75+
obj, err := common.ToUnstructured(customResource)
7676
Expect(err).NotTo(HaveOccurred())
77-
status, err := common.GetResourceHealth(&unstructured.Unstructured{Object: obj})
77+
status, err := common.GetResourceHealth(obj)
7878
Expect(err).NotTo(HaveOccurred())
7979
Expect(status).To(Not(BeNil()))
8080
Expect(*status).To(Equal(common.HealthStatus{
@@ -83,5 +83,21 @@ var _ = Describe("Health Test", Ordered, func() {
8383
}))
8484
})
8585

86+
It("should get status from Lua script", func() {
87+
customResource.DeletionTimestamp = nil
88+
obj, err := common.ToUnstructured(customResource)
89+
Expect(err).NotTo(HaveOccurred())
90+
scriptPath := filepath.Join("..", "..", "test", "lua", "test.lua")
91+
script, err := os.ReadFile(scriptPath)
92+
Expect(err).NotTo(HaveOccurred())
93+
common.GetLuaScript().SetValue(string(script))
94+
status, err := common.GetResourceHealth(obj)
95+
Expect(err).NotTo(HaveOccurred())
96+
Expect(status).To(Not(BeNil()))
97+
Expect(*status).To(Equal(common.HealthStatus{
98+
Status: common.HealthStatusProgressing,
99+
}))
100+
})
101+
86102
})
87103
})
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package namespaces_test
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
console "github.com/pluralsh/console/go/client"
10+
"github.com/pluralsh/deployment-operator/pkg/controller/namespaces"
11+
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
12+
"github.com/stretchr/testify/mock"
13+
v1 "k8s.io/api/core/v1"
14+
"k8s.io/apimachinery/pkg/types"
15+
)
16+
17+
var _ = Describe("Reconciler", Ordered, func() {
18+
Context("When reconciling a resource", func() {
19+
const (
20+
namespaceId = "1"
21+
namespaceName = "test"
22+
)
23+
clusterNamespace := &console.ManagedNamespaceFragment{
24+
ID: namespaceId,
25+
Name: namespaceName,
26+
}
27+
ctx := context.Background()
28+
29+
It("should create Namespace", func() {
30+
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
31+
fakeConsoleClient.On("GetNamespace", mock.Anything).Return(clusterNamespace, nil)
32+
33+
reconciler := namespaces.NewNamespaceReconciler(fakeConsoleClient, kClient, time.Minute)
34+
_, err := reconciler.Reconcile(ctx, namespaceId)
35+
Expect(err).NotTo(HaveOccurred())
36+
37+
existing := &v1.Namespace{}
38+
Expect(kClient.Get(ctx, types.NamespacedName{Name: namespaceName}, existing)).NotTo(HaveOccurred())
39+
})
40+
41+
It("should Update Namespace", func() {
42+
clusterNamespace.Labels = map[string]interface{}{
43+
"a": "b",
44+
}
45+
46+
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
47+
fakeConsoleClient.On("GetNamespace", mock.Anything).Return(clusterNamespace, nil)
48+
49+
reconciler := namespaces.NewNamespaceReconciler(fakeConsoleClient, kClient, time.Minute)
50+
_, err := reconciler.Reconcile(ctx, namespaceId)
51+
Expect(err).NotTo(HaveOccurred())
52+
53+
existing := &v1.Namespace{}
54+
Expect(kClient.Get(ctx, types.NamespacedName{Name: namespaceName}, existing)).NotTo(HaveOccurred())
55+
Expect(existing.Labels).To(Not(BeNil()))
56+
Expect(existing.Labels["a"]).To(Equal("b"))
57+
})
58+
59+
})
60+
})
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package namespaces_test
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
"runtime"
23+
"testing"
24+
25+
. "github.com/onsi/ginkgo/v2"
26+
. "github.com/onsi/gomega"
27+
"k8s.io/client-go/kubernetes/scheme"
28+
"sigs.k8s.io/controller-runtime/pkg/client"
29+
"sigs.k8s.io/controller-runtime/pkg/envtest"
30+
logf "sigs.k8s.io/controller-runtime/pkg/log"
31+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
32+
)
33+
34+
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
35+
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
36+
var testEnv *envtest.Environment
37+
var kClient client.Client
38+
39+
func TestStacks(t *testing.T) {
40+
RegisterFailHandler(Fail)
41+
RunSpecs(t, "Stack Suite")
42+
}
43+
44+
var _ = BeforeSuite(func() {
45+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
46+
47+
By("bootstrapping test environment")
48+
testEnv = &envtest.Environment{
49+
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", fmt.Sprintf("1.28.3-%s-%s", runtime.GOOS, runtime.GOARCH)),
50+
}
51+
52+
cfg, err := testEnv.Start()
53+
Expect(err).NotTo(HaveOccurred())
54+
Expect(cfg).NotTo(BeNil())
55+
56+
kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
57+
Expect(err).NotTo(HaveOccurred())
58+
Expect(kClient).NotTo(BeNil())
59+
})
60+
61+
var _ = AfterSuite(func() {
62+
By("tearing down the test environment")
63+
err := testEnv.Stop()
64+
Expect(err).NotTo(HaveOccurred())
65+
})
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package restore_test
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
. "github.com/onsi/ginkgo/v2"
8+
. "github.com/onsi/gomega"
9+
console "github.com/pluralsh/console/go/client"
10+
"github.com/pluralsh/deployment-operator/pkg/controller/restore"
11+
"github.com/pluralsh/deployment-operator/pkg/test/mocks"
12+
"github.com/stretchr/testify/mock"
13+
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
14+
"k8s.io/apimachinery/pkg/types"
15+
)
16+
17+
var _ = Describe("Reconciler", Ordered, func() {
18+
Context("When reconciling a resource", func() {
19+
const (
20+
namespace = "default"
21+
restoreId = "1"
22+
backupName = "test"
23+
)
24+
clusterRestore := &console.ClusterRestoreFragment{
25+
ID: restoreId,
26+
Status: console.RestoreStatusPending,
27+
Backup: &console.ClusterBackupFragment{
28+
Name: backupName,
29+
},
30+
}
31+
ctx := context.Background()
32+
33+
It("should create Restore object", func() {
34+
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
35+
fakeConsoleClient.On("GetClusterRestore", mock.Anything).Return(clusterRestore, nil)
36+
37+
reconciler := restore.NewRestoreReconciler(fakeConsoleClient, kClient, time.Minute, namespace)
38+
_, err := reconciler.Reconcile(ctx, restoreId)
39+
Expect(err).NotTo(HaveOccurred())
40+
41+
veleroRestore := &velerov1.Restore{}
42+
Expect(kClient.Get(ctx, types.NamespacedName{Name: restoreId, Namespace: namespace}, veleroRestore)).NotTo(HaveOccurred())
43+
})
44+
45+
It("should Update Cluster Restore", func() {
46+
47+
veleroRestore := &velerov1.Restore{}
48+
Expect(kClient.Get(ctx, types.NamespacedName{Name: restoreId, Namespace: namespace}, veleroRestore)).NotTo(HaveOccurred())
49+
50+
fakeConsoleClient := mocks.NewClientMock(mocks.TestingT)
51+
fakeConsoleClient.On("GetClusterRestore", mock.Anything).Return(clusterRestore, nil)
52+
fakeConsoleClient.On("UpdateClusterRestore", mock.AnythingOfType("string"), mock.Anything).Return(nil, nil)
53+
54+
reconciler := restore.NewRestoreReconciler(fakeConsoleClient, kClient, time.Minute, namespace)
55+
_, err := reconciler.Reconcile(ctx, restoreId)
56+
Expect(err).NotTo(HaveOccurred())
57+
Expect(kClient.Delete(ctx, veleroRestore)).To(Succeed())
58+
})
59+
60+
})
61+
})
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package restore_test
18+
19+
import (
20+
"fmt"
21+
"path/filepath"
22+
"runtime"
23+
"testing"
24+
25+
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
26+
27+
. "github.com/onsi/ginkgo/v2"
28+
. "github.com/onsi/gomega"
29+
"k8s.io/client-go/kubernetes/scheme"
30+
"sigs.k8s.io/controller-runtime/pkg/client"
31+
"sigs.k8s.io/controller-runtime/pkg/envtest"
32+
logf "sigs.k8s.io/controller-runtime/pkg/log"
33+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
34+
)
35+
36+
// These tests use Ginkgo (BDD-style Go testing framework). Refer to
37+
// http://onsi.github.io/ginkgo/ to learn more about Ginkgo.
38+
var testEnv *envtest.Environment
39+
var kClient client.Client
40+
41+
func TestStacks(t *testing.T) {
42+
RegisterFailHandler(Fail)
43+
RunSpecs(t, "Stack Suite")
44+
}
45+
46+
var _ = BeforeSuite(func() {
47+
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
48+
49+
By("bootstrapping test environment")
50+
testEnv = &envtest.Environment{
51+
CRDDirectoryPaths: []string{filepath.Join("..", "..", "..", "test", "crd")},
52+
ErrorIfCRDPathMissing: true,
53+
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s", fmt.Sprintf("1.28.3-%s-%s", runtime.GOOS, runtime.GOARCH)),
54+
}
55+
56+
cfg, err := testEnv.Start()
57+
Expect(err).NotTo(HaveOccurred())
58+
Expect(cfg).NotTo(BeNil())
59+
60+
err = velerov1.AddToScheme(scheme.Scheme)
61+
Expect(err).NotTo(HaveOccurred())
62+
63+
kClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
64+
Expect(err).NotTo(HaveOccurred())
65+
Expect(kClient).NotTo(BeNil())
66+
})
67+
68+
var _ = AfterSuite(func() {
69+
By("tearing down the test environment")
70+
err := testEnv.Stop()
71+
Expect(err).NotTo(HaveOccurred())
72+
})

0 commit comments

Comments
 (0)