Skip to content

Commit c7dc6b6

Browse files
author
Israel Blancas
committed
Add unit tests
Signed-off-by: Israel Blancas <[email protected]>
1 parent 75d4c4d commit c7dc6b6

File tree

3 files changed

+438
-0
lines changed

3 files changed

+438
-0
lines changed

cmd/gather/cluster/cluster_test.go

+176
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cluster
16+
17+
import (
18+
"context"
19+
"testing"
20+
21+
"github.com/stretchr/testify/assert"
22+
"github.com/stretchr/testify/mock"
23+
appsv1 "k8s.io/api/apps/v1"
24+
"k8s.io/apimachinery/pkg/api/meta"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
"k8s.io/apimachinery/pkg/runtime"
27+
"k8s.io/apimachinery/pkg/runtime/schema"
28+
"k8s.io/apimachinery/pkg/types"
29+
"sigs.k8s.io/controller-runtime/pkg/client"
30+
31+
"github.com/open-telemetry/opentelemetry-operator/cmd/gather/config"
32+
)
33+
34+
// MockClient is a mock implementation of client.Client.
35+
type MockClient struct {
36+
mock.Mock
37+
}
38+
39+
func (m *MockClient) Get(ctx context.Context, key types.NamespacedName, obj client.Object, _ ...client.GetOption) error {
40+
args := m.Called(ctx, key, obj)
41+
return args.Error(0)
42+
}
43+
44+
func (m *MockClient) List(ctx context.Context, list client.ObjectList, opts ...client.ListOption) error {
45+
args := m.Called(ctx, list, opts)
46+
return args.Error(0)
47+
}
48+
49+
func (m *MockClient) Create(ctx context.Context, obj client.Object, opts ...client.CreateOption) error {
50+
args := m.Called(ctx, obj, opts)
51+
return args.Error(0)
52+
}
53+
54+
func (m *MockClient) Delete(ctx context.Context, obj client.Object, opts ...client.DeleteOption) error {
55+
args := m.Called(ctx, obj, opts)
56+
return args.Error(0)
57+
}
58+
59+
func (m *MockClient) Update(ctx context.Context, obj client.Object, opts ...client.UpdateOption) error {
60+
args := m.Called(ctx, obj, opts)
61+
return args.Error(0)
62+
}
63+
64+
func (m *MockClient) Patch(ctx context.Context, obj client.Object, patch client.Patch, opts ...client.PatchOption) error {
65+
args := m.Called(ctx, obj, patch, opts)
66+
return args.Error(0)
67+
}
68+
69+
func (m *MockClient) DeleteAllOf(ctx context.Context, obj client.Object, opts ...client.DeleteAllOfOption) error {
70+
args := m.Called(ctx, obj, opts)
71+
return args.Error(0)
72+
}
73+
74+
func (m *MockClient) Status() client.StatusWriter {
75+
args := m.Called()
76+
return args.Get(0).(client.StatusWriter)
77+
}
78+
79+
func (m *MockClient) Scheme() *runtime.Scheme {
80+
args := m.Called()
81+
return args.Get(0).(*runtime.Scheme)
82+
}
83+
84+
func (m *MockClient) RESTMapper() meta.RESTMapper {
85+
args := m.Called()
86+
return args.Get(0).(meta.RESTMapper)
87+
}
88+
89+
func (m *MockClient) GroupVersionKindFor(obj runtime.Object) (schema.GroupVersionKind, error) {
90+
return schema.GroupVersionKind{}, nil
91+
}
92+
93+
func (m *MockClient) IsObjectNamespaced(_ runtime.Object) (bool, error) {
94+
return true, nil
95+
}
96+
97+
func (m *MockClient) SubResource(string) client.SubResourceClient {
98+
return nil
99+
}
100+
101+
func TestGetOperatorNamespace(t *testing.T) {
102+
mockClient := new(MockClient)
103+
cfg := &config.Config{
104+
KubernetesClient: mockClient,
105+
}
106+
cluster := NewCluster(cfg)
107+
108+
// Test when OperatorNamespace is already set
109+
cfg.OperatorNamespace = "test-namespace"
110+
ns, err := cluster.getOperatorNamespace()
111+
assert.NoError(t, err)
112+
assert.Equal(t, "test-namespace", ns)
113+
114+
// Test when OperatorNamespace is not set
115+
cfg.OperatorNamespace = ""
116+
mockClient.On("List", mock.Anything, &appsv1.DeploymentList{}, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
117+
arg := args.Get(1).(*appsv1.DeploymentList)
118+
arg.Items = []appsv1.Deployment{
119+
{
120+
ObjectMeta: metav1.ObjectMeta{
121+
Namespace: "operator-namespace",
122+
},
123+
},
124+
}
125+
})
126+
127+
ns, err = cluster.getOperatorNamespace()
128+
assert.NoError(t, err)
129+
assert.Equal(t, "operator-namespace", ns)
130+
mockClient.AssertExpectations(t)
131+
}
132+
133+
func TestGetOperatorDeployment(t *testing.T) {
134+
135+
mockClient := new(MockClient)
136+
cfg := &config.Config{
137+
KubernetesClient: mockClient,
138+
}
139+
cluster := NewCluster(cfg)
140+
// Test successful case
141+
mockClient.On("List", mock.Anything, &appsv1.DeploymentList{}, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
142+
arg := args.Get(1).(*appsv1.DeploymentList)
143+
arg.Items = []appsv1.Deployment{
144+
{
145+
ObjectMeta: metav1.ObjectMeta{
146+
Name: "opentelemetry-operator",
147+
},
148+
},
149+
}
150+
})
151+
152+
deployment, err := cluster.getOperatorDeployment()
153+
assert.NoError(t, err)
154+
assert.Equal(t, "opentelemetry-operator", deployment.Name)
155+
156+
mockClient.AssertExpectations(t)
157+
}
158+
159+
func TestGetOperatorDeploymentNotFound(t *testing.T) {
160+
161+
mockClient := new(MockClient)
162+
cfg := &config.Config{
163+
KubernetesClient: mockClient,
164+
}
165+
cluster := NewCluster(cfg)
166+
167+
// Test when no operator is found
168+
mockClient.On("List", mock.Anything, &appsv1.DeploymentList{}, mock.Anything).Return(nil).Run(func(args mock.Arguments) {
169+
arg := args.Get(1).(*appsv1.DeploymentList)
170+
arg.Items = []appsv1.Deployment{}
171+
})
172+
173+
_, err := cluster.getOperatorDeployment()
174+
assert.Error(t, err)
175+
assert.Equal(t, "operator not found", err.Error())
176+
}

0 commit comments

Comments
 (0)