forked from argoproj/argo-cd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller_test.go
197 lines (174 loc) · 5.07 KB
/
controller_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
package controller
import (
"context"
"testing"
"time"
"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/sirupsen/logrus/hooks/test"
"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/dynamic/fake"
k8sfake "k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/cache"
)
func TestIsAppSyncStatusRefreshed(t *testing.T) {
logger, _ := test.NewNullLogger()
logEntry := logger.WithField("", "")
tests := []struct {
name string
app *unstructured.Unstructured
expectedValue bool
}{
{
name: "No OperationState",
app: &unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{},
},
},
expectedValue: true,
},
{
name: "No FinishedAt, Completed Phase",
app: &unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
"operationState": map[string]interface{}{
"phase": "Succeeded",
},
},
},
},
expectedValue: false,
},
{
name: "FinishedAt After ReconciledAt & ObservedAt",
app: &unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
"operationState": map[string]interface{}{
"finishedAt": "2021-01-01T01:05:00Z",
"phase": "Succeeded",
},
"reconciledAt": "2021-01-01T01:02:00Z",
"observedAt": "2021-01-01T01:04:00Z",
},
},
},
expectedValue: false,
},
{
name: "FinishedAt Before ReconciledAt & ObservedAt",
app: &unstructured.Unstructured{
Object: map[string]interface{}{
"status": map[string]interface{}{
"operationState": map[string]interface{}{
"finishedAt": "2021-01-01T01:02:00Z",
"phase": "Succeeded",
},
"reconciledAt": "2021-01-01T01:04:00Z",
"observedAt": "2021-01-01T01:06:00Z",
},
},
},
expectedValue: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
actualValue := isAppSyncStatusRefreshed(test.app, logEntry)
assert.Equal(t, test.expectedValue, actualValue)
})
}
}
func TestGetAppProj_invalidProjectNestedString(t *testing.T) {
app := &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{},
},
}
informer := cache.NewSharedIndexInformer(nil, nil, 0, nil)
proj := getAppProj(app, informer)
assert.Nil(t, proj)
}
func TestInit(t *testing.T) {
scheme := runtime.NewScheme()
err := v1alpha1.SchemeBuilder.AddToScheme(scheme)
if err != nil {
t.Fatalf("Error registering the resource: %v", err)
}
dynamicClient := fake.NewSimpleDynamicClient(scheme)
k8sClient := k8sfake.NewSimpleClientset()
appLabelSelector := "app=test"
selfServiceNotificationEnabledFlags := []bool{false, true}
for _, selfServiceNotificationEnabled := range selfServiceNotificationEnabledFlags {
nc := NewController(
k8sClient,
dynamicClient,
nil,
"default",
[]string{},
appLabelSelector,
nil,
"my-secret",
"my-configmap",
selfServiceNotificationEnabled,
)
assert.NotNil(t, nc)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = nc.Init(ctx)
assert.NoError(t, err)
}
}
func TestInitTimeout(t *testing.T) {
scheme := runtime.NewScheme()
err := v1alpha1.SchemeBuilder.AddToScheme(scheme)
if err != nil {
t.Fatalf("Error registering the resource: %v", err)
}
dynamicClient := fake.NewSimpleDynamicClient(scheme)
k8sClient := k8sfake.NewSimpleClientset()
appLabelSelector := "app=test"
nc := NewController(
k8sClient,
dynamicClient,
nil,
"default",
[]string{},
appLabelSelector,
nil,
"my-secret",
"my-configmap",
false,
)
assert.NotNil(t, nc)
// Use a short timeout to simulate a timeout during cache synchronization
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Millisecond)
defer cancel()
err = nc.Init(ctx)
// Expect an error & add assertion for the error message
assert.Error(t, err)
assert.Equal(t, "Timed out waiting for caches to sync", err.Error())
}
func TestCheckAppNotInAdditionalNamespaces(t *testing.T) {
app := &unstructured.Unstructured{
Object: map[string]interface{}{
"spec": map[string]interface{}{},
},
}
namespace := "argocd"
var applicationNamespaces []string
applicationNamespaces = append(applicationNamespaces, "namespace1")
applicationNamespaces = append(applicationNamespaces, "namespace2")
// app is in same namespace as controller's namespace
app.SetNamespace(namespace)
assert.False(t, checkAppNotInAdditionalNamespaces(app, namespace, applicationNamespaces))
// app is not in the namespace as controller's namespace, but it is in one of the applicationNamespaces
app.SetNamespace("namespace2")
assert.False(t, checkAppNotInAdditionalNamespaces(app, "", applicationNamespaces))
// app is not in the namespace as controller's namespace, and it is not in any of the applicationNamespaces
app.SetNamespace("namespace3")
assert.True(t, checkAppNotInAdditionalNamespaces(app, "", applicationNamespaces))
}