Skip to content

Commit 70bfc5c

Browse files
committed
CSPL-3551 Adding tests
1 parent 7134350 commit 70bfc5c

File tree

12 files changed

+267
-15
lines changed

12 files changed

+267
-15
lines changed

internal/controller/ingestorcluster_controller_test.go

Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,227 @@ limitations under the License.
1717
package controller
1818

1919
import (
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

2337
var _ = 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-ic-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-ic-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+
}

internal/controller/testutils/new.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,40 @@ func NewStandalone(name, ns, image string) *enterpriseApi.Standalone {
4545
return ad
4646
}
4747

48+
// NewIngestorCluster returns new IngestorCluster instance with its config hash
49+
func NewIngestorCluster(name, ns, image string) *enterpriseApi.IngestorCluster {
50+
c := &enterpriseApi.Spec{
51+
ImagePullPolicy: string(pullPolicy),
52+
}
53+
54+
cs := &enterpriseApi.CommonSplunkSpec{
55+
Mock: true,
56+
Spec: *c,
57+
Volumes: []corev1.Volume{},
58+
MonitoringConsoleRef: corev1.ObjectReference{
59+
Name: "mcName",
60+
},
61+
}
62+
63+
ic := &enterpriseApi.IngestorCluster{
64+
TypeMeta: metav1.TypeMeta{
65+
APIVersion: "enterprise.splunk.com/v4",
66+
Kind: "IngestorCluster",
67+
},
68+
ObjectMeta: metav1.ObjectMeta{
69+
Name: name,
70+
Namespace: ns,
71+
Finalizers: []string{"enterprise.splunk.com/delete-pvc"},
72+
},
73+
}
74+
75+
ic.Spec = enterpriseApi.IngestorClusterSpec{
76+
CommonSplunkSpec: *cs,
77+
}
78+
79+
return ic
80+
}
81+
4882
// NewSearchHeadCluster returns new serach head cluster instance with its config hash
4983
func NewSearchHeadCluster(name, ns, image string) *enterpriseApi.SearchHeadCluster {
5084

pkg/config/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func GetWatchNamespaces() []string {
4848

4949
// ManagerOptionsWithNamespaces returns an updated Options with namespaces information.
5050
func ManagerOptionsWithNamespaces(logger logr.Logger, opt ctrl.Options) ctrl.Options {
51-
opts := cache.Options{}
51+
opts := cache.Options{}
5252
namespaces := GetWatchNamespaces()
5353
switch {
5454
case len(namespaces) == 1:

pkg/splunk/enterprise/configuration_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,10 +1430,10 @@ func TestAddStorageVolumes(t *testing.T) {
14301430
// test if adminManagedPV logic works
14311431

14321432
labels = map[string]string{
1433-
"app.kubernetes.io/component": "indexer",
1434-
"app.kubernetes.io/instance": "splunk-CM-cluster-manager",
1433+
"app.kubernetes.io/component": "indexer",
1434+
"app.kubernetes.io/instance": "splunk-CM-cluster-manager",
14351435
"app.kubernetes.io/managed-by": "splunk-operator",
1436-
"app.kubernetes.io/name": "cluster-manager",
1436+
"app.kubernetes.io/name": "cluster-manager",
14371437
}
14381438

14391439
// adjust CR annotations
@@ -1466,10 +1466,10 @@ func TestAddStorageVolumes(t *testing.T) {
14661466
APIVersion: "apps/v1",
14671467
},
14681468
ObjectMeta: metav1.ObjectMeta{
1469-
Name: "test-statefulset",
1470-
Namespace: cr.GetNamespace(),
1469+
Name: "test-statefulset",
1470+
Namespace: cr.GetNamespace(),
14711471
Annotations: cr.GetAnnotations(),
1472-
Labels: cr.GetLabels(),
1472+
Labels: cr.GetLabels(),
14731473
},
14741474
Spec: appsv1.StatefulSetSpec{
14751475
Replicas: &replicas,

test/secret/manager_secret_m4_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import (
1717
"context"
1818
"fmt"
1919

20-
"github.com/onsi/ginkgo/v2/types"
2120
. "github.com/onsi/ginkgo/v2"
21+
"github.com/onsi/ginkgo/v2/types"
2222
. "github.com/onsi/gomega"
2323

2424
enterpriseApi "github.com/splunk/splunk-operator/api/v4"

test/secret/manager_secret_s1_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
2121

22-
"github.com/onsi/ginkgo/v2/types"
2322
. "github.com/onsi/ginkgo/v2"
23+
"github.com/onsi/ginkgo/v2/types"
2424
. "github.com/onsi/gomega"
2525

2626
corev1 "k8s.io/api/core/v1"

test/secret/secret_c3_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
2121

22-
"github.com/onsi/ginkgo/v2/types"
2322
. "github.com/onsi/ginkgo/v2"
23+
"github.com/onsi/ginkgo/v2/types"
2424
. "github.com/onsi/gomega"
2525

2626
"github.com/splunk/splunk-operator/test/testenv"

test/secret/secret_m4_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
2121

22-
"github.com/onsi/ginkgo/v2/types"
2322
. "github.com/onsi/ginkgo/v2"
23+
"github.com/onsi/ginkgo/v2/types"
2424
. "github.com/onsi/gomega"
2525

2626
"github.com/splunk/splunk-operator/test/testenv"

test/secret/secret_s1_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919

2020
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
2121

22-
"github.com/onsi/ginkgo/v2/types"
2322
. "github.com/onsi/ginkgo/v2"
23+
"github.com/onsi/ginkgo/v2/types"
2424
. "github.com/onsi/gomega"
2525

2626
corev1 "k8s.io/api/core/v1"

test/smartstore/manager_smartstore_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
enterpriseApi "github.com/splunk/splunk-operator/api/v4"
99

10-
"github.com/onsi/ginkgo/v2/types"
1110
. "github.com/onsi/ginkgo/v2"
11+
"github.com/onsi/ginkgo/v2/types"
1212
. "github.com/onsi/gomega"
1313

1414
"github.com/splunk/splunk-operator/test/testenv"

0 commit comments

Comments
 (0)