Skip to content

Commit f7ab542

Browse files
committed
tests
1 parent 41d1f48 commit f7ab542

File tree

10 files changed

+1401
-233
lines changed

10 files changed

+1401
-233
lines changed

.golangci.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ linters:
4747
values:
4848
const:
4949
COMPANY: Cisco Systems, Inc. and/or its affiliates
50-
ADOBE_LINE_DEFAULT: ''
50+
ADOBE_COMPANY: Adobe. All rights reserved.
5151
regexp:
5252
COPYRIGHT: (Copyright|Copyright ©)
5353
YEAR: 20[0-9]{2}
54+
CISCO_LINE: 'Copyright(?: ©)? 20[0-9]{2} Cisco Systems, Inc\. and/or its affiliates'
5455
ADOBE_LINE: 'Copyright(?: ©)? 20[0-9]{2} Adobe\. All rights reserved\.'
5556
template: |-
56-
{{ COPYRIGHT }} {{ YEAR }} {{ COMPANY }}{{ if .ADOBE_LINE }}
57-
{{ ADOBE_LINE }}{{ end }}
57+
{{ if .CISCO_LINE }}{{ CISCO_LINE }}{{ end }}{{ if and .CISCO_LINE .ADOBE_LINE }}
58+
{{ end }}{{ if .ADOBE_LINE }}{{ ADOBE_LINE }}{{ end }}
5859
5960
Licensed under the Apache License, Version 2.0 (the "License");
6061
you may not use this file except in compliance with the License.

pkg/k8sutil/resource_test.go

Lines changed: 278 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,278 @@
1+
// Copyright © 2019 Cisco Systems, Inc. and/or its affiliates
2+
// Copyright 2025 Adobe. All rights reserved.
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+
package k8sutil
17+
18+
import (
19+
"context"
20+
"testing"
21+
22+
"github.com/go-logr/logr"
23+
"github.com/stretchr/testify/assert"
24+
"github.com/stretchr/testify/mock"
25+
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/api/errors"
27+
"k8s.io/apimachinery/pkg/api/meta"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
29+
"k8s.io/apimachinery/pkg/runtime"
30+
"k8s.io/apimachinery/pkg/runtime/schema"
31+
"k8s.io/apimachinery/pkg/types"
32+
"sigs.k8s.io/controller-runtime/pkg/client"
33+
34+
"github.com/banzaicloud/koperator/api/v1beta1"
35+
)
36+
37+
// MockClient is a mock implementation of client.Client
38+
type MockClient struct {
39+
mock.Mock
40+
}
41+
42+
func (m *MockClient) Get(ctx context.Context, key types.NamespacedName, obj client.Object, opts ...client.GetOption) error {
43+
args := m.Called(ctx, key, obj, opts)
44+
return args.Error(0)
45+
}
46+
47+
func (m *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
48+
args := m.Called(ctx, list, opts)
49+
return args.Error(0)
50+
}
51+
52+
func (m *MockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
53+
args := m.Called(ctx, obj, opts)
54+
return args.Error(0)
55+
}
56+
57+
func (m *MockClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
58+
args := m.Called(ctx, obj, opts)
59+
return args.Error(0)
60+
}
61+
62+
func (m *MockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
63+
args := m.Called(ctx, obj, opts)
64+
return args.Error(0)
65+
}
66+
67+
func (m *MockClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
68+
args := m.Called(ctx, obj, patch, opts)
69+
return args.Error(0)
70+
}
71+
72+
func (m *MockClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
73+
args := m.Called(ctx, obj, opts)
74+
return args.Error(0)
75+
}
76+
77+
func (m *MockClient) Status() client.StatusWriter {
78+
args := m.Called()
79+
return args.Get(0).(client.StatusWriter)
80+
}
81+
82+
func (m *MockClient) Scheme() *runtime.Scheme {
83+
args := m.Called()
84+
return args.Get(0).(*runtime.Scheme)
85+
}
86+
87+
func (m *MockClient) RESTMapper() meta.RESTMapper {
88+
args := m.Called()
89+
return args.Get(0).(meta.RESTMapper)
90+
}
91+
92+
func (m *MockClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
93+
args := m.Called(obj)
94+
return args.Get(0).(schema.GroupVersionKind), args.Error(1)
95+
}
96+
97+
func (m *MockClient) IsObjectNamespaced(obj runtime.Object) (bool, error) {
98+
args := m.Called(obj)
99+
return args.Bool(0), args.Error(1)
100+
}
101+
102+
func (m *MockClient) Apply(ctx context.Context, obj runtime.ApplyConfiguration, opts ...client.ApplyOption) error {
103+
args := m.Called(ctx, obj, opts)
104+
return args.Error(0)
105+
}
106+
107+
func (m *MockClient) SubResource(subResource string) client.SubResourceClient {
108+
args := m.Called(subResource)
109+
return args.Get(0).(client.SubResourceClient)
110+
}
111+
112+
func TestReconcile_CreateResource(t *testing.T) {
113+
mockClient := &MockClient{}
114+
cluster := &v1beta1.KafkaCluster{
115+
ObjectMeta: metav1.ObjectMeta{
116+
Name: "test-cluster",
117+
Namespace: "test-namespace",
118+
},
119+
}
120+
121+
// Create a test ConfigMap
122+
desired := &corev1.ConfigMap{
123+
ObjectMeta: metav1.ObjectMeta{
124+
Name: "test-configmap",
125+
Namespace: "test-namespace",
126+
},
127+
Data: map[string]string{
128+
"key": "value",
129+
},
130+
}
131+
132+
// Mock Get to return NotFound error (resource doesn't exist)
133+
mockClient.On("Get", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(errors.NewNotFound(schema.GroupResource{}, "test-configmap"))
134+
// Mock Create to succeed
135+
mockClient.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(nil)
136+
137+
err := Reconcile(logr.Discard(), mockClient, desired, cluster)
138+
assert.NoError(t, err)
139+
140+
// Verify that Create was called
141+
mockClient.AssertCalled(t, "Create", mock.Anything, mock.Anything, mock.Anything)
142+
}
143+
144+
func TestReconcile_UpdateResource(t *testing.T) {
145+
mockClient := &MockClient{}
146+
cluster := &v1beta1.KafkaCluster{
147+
ObjectMeta: metav1.ObjectMeta{
148+
Name: "test-cluster",
149+
Namespace: "test-namespace",
150+
},
151+
}
152+
153+
// Create a test ConfigMap
154+
desired := &corev1.ConfigMap{
155+
ObjectMeta: metav1.ObjectMeta{
156+
Name: "test-configmap",
157+
Namespace: "test-namespace",
158+
},
159+
Data: map[string]string{
160+
"key": "value",
161+
},
162+
}
163+
164+
// Mock Get to return existing resource
165+
existing := desired.DeepCopy()
166+
existing.Data["existing-key"] = "existing-value"
167+
mockClient.On("Get", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
168+
obj := args.Get(2).(client.Object)
169+
// Set the existing data
170+
if cm, ok := obj.(*corev1.ConfigMap); ok {
171+
cm.Data = map[string]string{
172+
"existing-key": "existing-value",
173+
}
174+
}
175+
})
176+
// Mock Update to succeed
177+
mockClient.On("Update", mock.Anything, mock.Anything, mock.Anything).Return(nil)
178+
179+
err := Reconcile(logr.Discard(), mockClient, desired, cluster)
180+
assert.NoError(t, err)
181+
}
182+
183+
func TestReconcile_ErrorHandling(t *testing.T) {
184+
mockClient := &MockClient{}
185+
cluster := &v1beta1.KafkaCluster{
186+
ObjectMeta: metav1.ObjectMeta{
187+
Name: "test-cluster",
188+
Namespace: "test-namespace",
189+
},
190+
}
191+
192+
// Create a test ConfigMap
193+
desired := &corev1.ConfigMap{
194+
ObjectMeta: metav1.ObjectMeta{
195+
Name: "test-configmap",
196+
Namespace: "test-namespace",
197+
},
198+
Data: map[string]string{
199+
"key": "value",
200+
},
201+
}
202+
203+
// Mock Get to return an error
204+
mockClient.On("Get", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(assert.AnError)
205+
206+
err := Reconcile(logr.Discard(), mockClient, desired, cluster)
207+
assert.Error(t, err)
208+
}
209+
210+
func TestReconcile_CreateErrorHandling(t *testing.T) {
211+
mockClient := &MockClient{}
212+
cluster := &v1beta1.KafkaCluster{
213+
ObjectMeta: metav1.ObjectMeta{
214+
Name: "test-cluster",
215+
Namespace: "test-namespace",
216+
},
217+
}
218+
219+
// Create a test ConfigMap
220+
desired := &corev1.ConfigMap{
221+
ObjectMeta: metav1.ObjectMeta{
222+
Name: "test-configmap",
223+
Namespace: "test-namespace",
224+
},
225+
Data: map[string]string{
226+
"key": "value",
227+
},
228+
}
229+
230+
// Mock Get to return NotFound error (resource doesn't exist)
231+
mockClient.On("Get", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(assert.AnError)
232+
// Mock Create to return an error
233+
mockClient.On("Create", mock.Anything, mock.Anything, mock.Anything).Return(assert.AnError)
234+
235+
err := Reconcile(logr.Discard(), mockClient, desired, cluster)
236+
assert.Error(t, err)
237+
}
238+
239+
func TestReconcile_UpdateErrorHandling(t *testing.T) {
240+
mockClient := &MockClient{}
241+
cluster := &v1beta1.KafkaCluster{
242+
ObjectMeta: metav1.ObjectMeta{
243+
Name: "test-cluster",
244+
Namespace: "test-namespace",
245+
},
246+
}
247+
248+
// Create a test ConfigMap
249+
desired := &corev1.ConfigMap{
250+
ObjectMeta: metav1.ObjectMeta{
251+
Name: "test-configmap",
252+
Namespace: "test-namespace",
253+
},
254+
Data: map[string]string{
255+
"key": "value",
256+
},
257+
}
258+
259+
// Mock Get to return a different existing resource (so update is needed)
260+
existing := &corev1.ConfigMap{
261+
ObjectMeta: metav1.ObjectMeta{
262+
Name: "test-configmap",
263+
Namespace: "test-namespace",
264+
},
265+
Data: map[string]string{
266+
"key": "different-value",
267+
},
268+
}
269+
mockClient.On("Get", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
270+
obj := args.Get(2).(*corev1.ConfigMap)
271+
*obj = *existing
272+
})
273+
// Mock Update to return an error
274+
mockClient.On("Update", mock.Anything, mock.Anything, mock.Anything).Return(assert.AnError)
275+
276+
err := Reconcile(logr.Discard(), mockClient, desired, cluster)
277+
assert.Error(t, err)
278+
}

0 commit comments

Comments
 (0)