-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelloworld_controller_test.go
132 lines (109 loc) · 4.47 KB
/
helloworld_controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright 2023.
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 controllers
import (
"context"
"fmt"
"os"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
examplesv1alpha1 "github.com/brusdev/hello-world-operator/api/v1alpha1"
)
var _ = Describe("HelloWorld controller", func() {
Context("HelloWorld controller test", func() {
const HelloWorldName = "test-helloworld"
ctx := context.Background()
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: HelloWorldName,
Namespace: HelloWorldName,
},
}
typeNamespaceName := types.NamespacedName{Name: HelloWorldName, Namespace: HelloWorldName}
BeforeEach(func() {
By("Creating the Namespace to perform the tests")
err := k8sClient.Create(ctx, namespace)
Expect(err).To(Not(HaveOccurred()))
By("Setting the Image ENV VAR which stores the Operand image")
err = os.Setenv("HELLOWORLD_IMAGE", "example.com/image:test")
Expect(err).To(Not(HaveOccurred()))
})
AfterEach(func() {
// TODO(user): Attention if you improve this code by adding other context test you MUST
// be aware of the current delete namespace limitations. More info: https://book.kubebuilder.io/reference/envtest.html#testing-considerations
By("Deleting the Namespace to perform the tests")
_ = k8sClient.Delete(ctx, namespace)
By("Removing the Image ENV VAR which stores the Operand image")
_ = os.Unsetenv("HELLOWORLD_IMAGE")
})
It("should successfully reconcile a custom resource for HelloWorld", func() {
By("Creating the custom resource for the Kind HelloWorld")
helloworld := &examplesv1alpha1.HelloWorld{}
err := k8sClient.Get(ctx, typeNamespaceName, helloworld)
if err != nil && errors.IsNotFound(err) {
// Let's mock our custom resource at the same way that we would
// apply on the cluster the manifest under config/samples
helloworld := &examplesv1alpha1.HelloWorld{
ObjectMeta: metav1.ObjectMeta{
Name: HelloWorldName,
Namespace: namespace.Name,
},
Spec: examplesv1alpha1.HelloWorldSpec{
Size: 1,
},
}
err = k8sClient.Create(ctx, helloworld)
Expect(err).To(Not(HaveOccurred()))
}
By("Checking if the custom resource was successfully created")
Eventually(func() error {
found := &examplesv1alpha1.HelloWorld{}
return k8sClient.Get(ctx, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())
By("Reconciling the custom resource created")
helloworldReconciler := &HelloWorldReconciler{
Client: k8sClient,
Scheme: k8sClient.Scheme(),
}
_, err = helloworldReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: typeNamespaceName,
})
Expect(err).To(Not(HaveOccurred()))
By("Checking if Deployment was successfully created in the reconciliation")
Eventually(func() error {
found := &appsv1.Deployment{}
return k8sClient.Get(ctx, typeNamespaceName, found)
}, time.Minute, time.Second).Should(Succeed())
By("Checking the latest Status Condition added to the HelloWorld instance")
Eventually(func() error {
if helloworld.Status.Conditions != nil && len(helloworld.Status.Conditions) != 0 {
latestStatusCondition := helloworld.Status.Conditions[len(helloworld.Status.Conditions)-1]
expectedLatestStatusCondition := metav1.Condition{Type: typeAvailableHelloWorld,
Status: metav1.ConditionTrue, Reason: "Reconciling",
Message: fmt.Sprintf("Deployment for custom resource (%s) with %d replicas created successfully", helloworld.Name, helloworld.Spec.Size)}
if latestStatusCondition != expectedLatestStatusCondition {
return fmt.Errorf("The latest status condition added to the helloworld instance is not as expected")
}
}
return nil
}, time.Minute, time.Second).Should(Succeed())
})
})
})