-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcommon_utils_test.go
211 lines (186 loc) · 5.63 KB
/
common_utils_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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package controllers
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
addonsv1alpha1 "github.com/openshift/addon-operator/api/v1alpha1"
"github.com/openshift/addon-operator/internal/testutil"
)
func TestHasEqualControllerReference(t *testing.T) {
require.True(t, HasSameController(
testutil.NewTestNamespace(),
testutil.NewTestNamespace(),
))
require.False(t, HasSameController(
testutil.NewTestNamespace(),
testutil.NewTestExistingNamespace(),
))
require.False(t, HasSameController(
testutil.NewTestNamespace(),
testutil.NewTestNamespaceWithoutOwner(),
))
}
func TestAddCommonLabels(t *testing.T) {
addon := &addonsv1alpha1.Addon{
ObjectMeta: v1.ObjectMeta{
Name: "test",
},
}
obj := &unstructured.Unstructured{} // some arbitrary object
AddCommonLabels(obj, addon)
labels := obj.GetLabels()
if labels[CommonInstanceLabel] != addon.Name {
t.Error("commonInstanceLabel was not set to addon name")
}
if labels[CommonManagedByLabel] != CommonManagedByValue {
t.Error("commonManagedByLabel was not set to operator name")
}
if labels[CommonCacheLabel] != CommonCacheValue {
t.Error("commonCacheLabel was not set to operator name")
}
}
func TestCommonLabelsAsLabelSelector(t *testing.T) {
addonWithCorrectName := &addonsv1alpha1.Addon{
ObjectMeta: v1.ObjectMeta{
Name: "test",
},
}
selector := CommonLabelsAsLabelSelector(addonWithCorrectName)
if selector.Empty() {
t.Fatal("selector is empty but should filter on common labels")
}
}
// The TestAddCommonAnnotations function tests adding common annotations from an Addon
// object to a metav1.Object object
func TestAddCommonAnnotations(t *testing.T) {
type args struct {
obj metav1.Object
addon *addonsv1alpha1.Addon
}
tests := []struct {
name string
args args
}{
{
name: "EmptyObject",
args: args{
obj: &metav1.ObjectMeta{},
addon: &addonsv1alpha1.Addon{},
},
},
{
name: "ObjectWithExistingAnnotations",
args: args{
obj: &metav1.ObjectMeta{
Annotations: map[string]string{
"existing.annotation": "existingValue",
},
},
addon: &addonsv1alpha1.Addon{},
},
},
{
name: "ObjectWithCommonAnnotations",
args: args{
obj: &metav1.ObjectMeta{},
addon: &addonsv1alpha1.Addon{
Spec: addonsv1alpha1.AddonSpec{
CommonAnnotations: map[string]string{
"test.annotation1": "lpsre",
"test.annotation2": "mtsre",
},
},
},
},
},
{
name: "ObjectWithExistingAndCommonAnnotations",
args: args{
obj: &metav1.ObjectMeta{
Annotations: map[string]string{
"existing.annotation": "existingValue",
},
},
addon: &addonsv1alpha1.Addon{
Spec: addonsv1alpha1.AddonSpec{
CommonAnnotations: map[string]string{
"test.annotation1": "lpsre",
"test.annotation2": "mtsre",
},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Copy the original annotations to compare them later
originalAnnotations := make(map[string]string)
for k, v := range tt.args.obj.GetAnnotations() {
originalAnnotations[k] = v
}
AddCommonAnnotations(tt.args.obj, tt.args.addon)
// Check if the common annotations have been added
for k, v := range tt.args.addon.Spec.CommonAnnotations {
assert.Equal(t, v, tt.args.obj.GetAnnotations()[k], "Expected common annotation %s to have value %s", k, v)
}
// Check if the original annotations are still present
for k, v := range originalAnnotations {
assert.Equal(t, v, tt.args.obj.GetAnnotations()[k], "Expected original annotation %s to have value %s", k, v)
}
// Check if any additional annotations were added apart from the common ones
for k, v := range tt.args.obj.GetAnnotations() {
_, commonAnnotationExists := tt.args.addon.Spec.CommonAnnotations[k]
_, originalAnnotationExists := originalAnnotations[k]
require.Truef(t, commonAnnotationExists || originalAnnotationExists, "Unexpected annotation %s with value %s has been added", k, v)
}
})
}
}
// TestCurrentNamespace tests the CurrentNamespace function to ensure it
// behaves correctly under different scenarios.
func TestCurrentNamespace(t *testing.T) {
tests := []struct {
name string
wantNamespace string
wantErr bool
}{
{
name: "Running in-cluster",
wantNamespace: "test-namespace",
wantErr: false,
},
{
name: "Running outside cluster with ADDON_OPERATOR_NAMESPACE environment variable set",
wantNamespace: "test-namespace",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
switch tt.name {
case "Running in-cluster":
// Set the ADDON_OPERATOR_NAMESPACE environment variable
os.Setenv("ADDON_OPERATOR_NAMESPACE", tt.wantNamespace)
defer os.Unsetenv("ADDON_OPERATOR_NAMESPACE")
case "Running outside cluster with ADDON_OPERATOR_NAMESPACE environment variable set":
// Set the ADDON_OPERATOR_NAMESPACE environment variable
os.Setenv("ADDON_OPERATOR_NAMESPACE", tt.wantNamespace)
defer os.Unsetenv("ADDON_OPERATOR_NAMESPACE")
}
// Call the CurrentNamespace function and compare the result with the expected values
gotNamespace, err := CurrentNamespace()
if (err != nil) != tt.wantErr {
t.Errorf("CurrentNamespace() error = %v, wantErr %v", err, tt.wantErr)
return
}
if gotNamespace != tt.wantNamespace {
t.Errorf("CurrentNamespace() = %v, want %v", gotNamespace, tt.wantNamespace)
}
})
}
}