@@ -17,9 +17,227 @@ limitations under the License.
1717package controller
1818
1919import (
20+ "context"
21+ "fmt"
22+ "time"
23+
2024 . "github.com/onsi/ginkgo/v2"
25+ . "github.com/onsi/gomega"
26+ enterpriseApi "github.com/splunk/splunk-operator/api/v4"
27+ "github.com/splunk/splunk-operator/internal/controller/testutils"
28+ corev1 "k8s.io/api/core/v1"
29+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
30+ "k8s.io/apimachinery/pkg/types"
31+ "k8s.io/client-go/kubernetes/scheme"
32+ "sigs.k8s.io/controller-runtime/pkg/client"
33+ "sigs.k8s.io/controller-runtime/pkg/client/fake"
34+ "sigs.k8s.io/controller-runtime/pkg/reconcile"
2135)
2236
2337var _ = Describe ("IngestorCluster Controller" , func () {
38+ BeforeEach (func () {
39+ time .Sleep (2 * time .Second )
40+ })
41+
42+ AfterEach (func () {
43+
44+ })
45+
46+ Context ("IngestorCluster Management" , func () {
47+
48+ It ("Get IngestorCluster custom resource should fail" , func () {
49+ namespace := "ns-splunk-ing-1"
50+ ApplyIngestorCluster = func (ctx context.Context , client client.Client , instance * enterpriseApi.IngestorCluster ) (reconcile.Result , error ) {
51+ return reconcile.Result {}, nil
52+ }
53+ nsSpecs := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace }}
54+
55+ Expect (k8sClient .Create (context .Background (), nsSpecs )).Should (Succeed ())
56+
57+ _ , err := GetIngestorCluster ("test" , nsSpecs .Name )
58+ Expect (err .Error ()).Should (Equal ("ingestorclusters.enterprise.splunk.com \" test\" not found" ))
59+
60+ Expect (k8sClient .Delete (context .Background (), nsSpecs )).Should (Succeed ())
61+ })
62+
63+ It ("Create IngestorCluster custom resource with annotations should pause" , func () {
64+ namespace := "ns-splunk-ing-2"
65+ annotations := make (map [string ]string )
66+ annotations [enterpriseApi .IngestorClusterPausedAnnotation ] = ""
67+ ApplyIngestorCluster = func (ctx context.Context , client client.Client , instance * enterpriseApi.IngestorCluster ) (reconcile.Result , error ) {
68+ return reconcile.Result {}, nil
69+ }
70+ nsSpecs := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace }}
71+
72+ Expect (k8sClient .Create (context .Background (), nsSpecs )).Should (Succeed ())
73+
74+ CreateIngestorCluster ("test" , nsSpecs .Name , annotations , enterpriseApi .PhaseReady )
75+ icSpec , _ := GetIngestorCluster ("test" , nsSpecs .Name )
76+ annotations = map [string ]string {}
77+ icSpec .Annotations = annotations
78+ icSpec .Status .Phase = "Ready"
79+ UpdateIngestorCluster (icSpec , enterpriseApi .PhaseReady )
80+ DeleteIngestorCluster ("test" , nsSpecs .Name )
81+ Expect (k8sClient .Delete (context .Background (), nsSpecs )).Should (Succeed ())
82+ })
83+
84+ It ("Create IngestorCluster custom resource should succeeded" , func () {
85+ namespace := "ns-splunk-ing-3"
86+ ApplyIngestorCluster = func (ctx context.Context , client client.Client , instance * enterpriseApi.IngestorCluster ) (reconcile.Result , error ) {
87+ return reconcile.Result {}, nil
88+ }
89+ nsSpecs := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace }}
90+
91+ Expect (k8sClient .Create (context .Background (), nsSpecs )).Should (Succeed ())
92+
93+ annotations := make (map [string ]string )
94+ CreateIngestorCluster ("test" , nsSpecs .Name , annotations , enterpriseApi .PhaseReady )
95+ DeleteIngestorCluster ("test" , nsSpecs .Name )
96+ Expect (k8sClient .Delete (context .Background (), nsSpecs )).Should (Succeed ())
97+ })
98+
99+ It ("Cover Unused methods" , func () {
100+ namespace := "ns-splunk-ing-4"
101+ ApplyIngestorCluster = func (ctx context.Context , client client.Client , instance * enterpriseApi.IngestorCluster ) (reconcile.Result , error ) {
102+ return reconcile.Result {}, nil
103+ }
104+ nsSpecs := & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : namespace }}
105+
106+ Expect (k8sClient .Create (context .Background (), nsSpecs )).Should (Succeed ())
107+
108+ ctx := context .TODO ()
109+ builder := fake .NewClientBuilder ()
110+ c := builder .Build ()
111+ instance := IngestorClusterReconciler {
112+ Client : c ,
113+ Scheme : scheme .Scheme ,
114+ }
115+ request := reconcile.Request {
116+ NamespacedName : types.NamespacedName {
117+ Name : "test" ,
118+ Namespace : namespace ,
119+ },
120+ }
121+ _ , err := instance .Reconcile (ctx , request )
122+ Expect (err ).ToNot (HaveOccurred ())
123+
124+ icSpec := testutils .NewIngestorCluster ("test" , namespace , "image" )
125+ Expect (c .Create (ctx , icSpec )).Should (Succeed ())
126+
127+ annotations := make (map [string ]string )
128+ annotations [enterpriseApi .IngestorClusterPausedAnnotation ] = ""
129+ icSpec .Annotations = annotations
130+ Expect (c .Update (ctx , icSpec )).Should (Succeed ())
131+
132+ _ , err = instance .Reconcile (ctx , request )
133+ Expect (err ).ToNot (HaveOccurred ())
24134
135+ annotations = map [string ]string {}
136+ icSpec .Annotations = annotations
137+ Expect (c .Update (ctx , icSpec )).Should (Succeed ())
138+
139+ _ , err = instance .Reconcile (ctx , request )
140+ Expect (err ).ToNot (HaveOccurred ())
141+
142+ icSpec .DeletionTimestamp = & metav1.Time {}
143+ _ , err = instance .Reconcile (ctx , request )
144+ Expect (err ).ToNot (HaveOccurred ())
145+ })
146+
147+ })
25148})
149+
150+ func GetIngestorCluster (name string , namespace string ) (* enterpriseApi.IngestorCluster , error ) {
151+ By ("Expecting IngestorCluster custom resource to be created successfully" )
152+
153+ key := types.NamespacedName {
154+ Name : name ,
155+ Namespace : namespace ,
156+ }
157+ ic := & enterpriseApi.IngestorCluster {}
158+
159+ err := k8sClient .Get (context .Background (), key , ic )
160+ if err != nil {
161+ return nil , err
162+ }
163+
164+ return ic , err
165+ }
166+
167+ func CreateIngestorCluster (name string , namespace string , annotations map [string ]string , status enterpriseApi.Phase ) * enterpriseApi.IngestorCluster {
168+ By ("Expecting IngestorCluster custom resource to be created successfully" )
169+
170+ key := types.NamespacedName {
171+ Name : name ,
172+ Namespace : namespace ,
173+ }
174+ ingSpec := & enterpriseApi.IngestorCluster {
175+ ObjectMeta : metav1.ObjectMeta {
176+ Name : name ,
177+ Namespace : namespace ,
178+ Annotations : annotations ,
179+ },
180+ Spec : enterpriseApi.IngestorClusterSpec {},
181+ }
182+
183+ ingSpec = testutils .NewIngestorCluster (name , namespace , "image" )
184+ Expect (k8sClient .Create (context .Background (), ingSpec )).Should (Succeed ())
185+ time .Sleep (2 * time .Second )
186+ ic := & enterpriseApi.IngestorCluster {}
187+ Eventually (func () bool {
188+ _ = k8sClient .Get (context .Background (), key , ic )
189+ if status != "" {
190+ fmt .Printf ("status is set to %v" , status )
191+ ic .Status .Phase = status
192+ Expect (k8sClient .Status ().Update (context .Background (), ic )).Should (Succeed ())
193+ time .Sleep (2 * time .Second )
194+ }
195+ return true
196+ }, timeout , interval ).Should (BeTrue ())
197+
198+ return ic
199+ }
200+
201+ func UpdateIngestorCluster (instance * enterpriseApi.IngestorCluster , status enterpriseApi.Phase ) * enterpriseApi.IngestorCluster {
202+ By ("Expecting IngestorCluster custom resource to be created successfully" )
203+
204+ key := types.NamespacedName {
205+ Name : instance .Name ,
206+ Namespace : instance .Namespace ,
207+ }
208+
209+ icSpec := testutils .NewIngestorCluster (instance .Name , instance .Namespace , "image" )
210+ icSpec .ResourceVersion = instance .ResourceVersion
211+ Expect (k8sClient .Update (context .Background (), icSpec )).Should (Succeed ())
212+ time .Sleep (2 * time .Second )
213+
214+ ic := & enterpriseApi.IngestorCluster {}
215+ Eventually (func () bool {
216+ _ = k8sClient .Get (context .Background (), key , ic )
217+ if status != "" {
218+ fmt .Printf ("status is set to %v" , status )
219+ ic .Status .Phase = status
220+ Expect (k8sClient .Status ().Update (context .Background (), ic )).Should (Succeed ())
221+ time .Sleep (2 * time .Second )
222+ }
223+ return true
224+ }, timeout , interval ).Should (BeTrue ())
225+
226+ return ic
227+ }
228+
229+ func DeleteIngestorCluster (name string , namespace string ) {
230+ By ("Expecting IngestorCluster Deleted successfully" )
231+
232+ key := types.NamespacedName {
233+ Name : name ,
234+ Namespace : namespace ,
235+ }
236+
237+ Eventually (func () error {
238+ ic := & enterpriseApi.IngestorCluster {}
239+ _ = k8sClient .Get (context .Background (), key , ic )
240+ err := k8sClient .Delete (context .Background (), ic )
241+ return err
242+ }, timeout , interval ).Should (Succeed ())
243+ }
0 commit comments