-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathcommon_test.go
247 lines (228 loc) · 6.6 KB
/
common_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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright 2020 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0
package statusreaders
import (
"context"
"fmt"
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
fakecr "sigs.k8s.io/cli-utils/pkg/kstatus/polling/clusterreader/fake"
fakesr "sigs.k8s.io/cli-utils/pkg/kstatus/polling/statusreaders/fake"
"sigs.k8s.io/cli-utils/pkg/kstatus/polling/testutil"
"sigs.k8s.io/cli-utils/pkg/object"
fakemapper "sigs.k8s.io/cli-utils/pkg/testutil"
)
var (
deploymentGVK = appsv1.SchemeGroupVersion.WithKind("Deployment")
deploymentGVR = appsv1.SchemeGroupVersion.WithResource("deployments")
replicaSetGVK = appsv1.SchemeGroupVersion.WithKind("ReplicaSet")
rsGVK = appsv1.SchemeGroupVersion.WithKind("ReplicaSet")
)
func TestLookupResource(t *testing.T) {
deploymentIdentifier := object.ObjMetadata{
GroupKind: deploymentGVK.GroupKind(),
Name: "Foo",
Namespace: "Bar",
}
testCases := map[string]struct {
identifier object.ObjMetadata
readerErr error
expectErr bool
expectedErrMessage string
}{
"unknown GVK": {
identifier: object.ObjMetadata{
GroupKind: schema.GroupKind{
Group: "custom.io",
Kind: "Custom",
},
Name: "Bar",
Namespace: "default",
},
expectErr: true,
expectedErrMessage: `no matches for kind "Custom" in group "custom.io"`,
},
"resource does not exist": {
identifier: deploymentIdentifier,
readerErr: errors.NewNotFound(deploymentGVR.GroupResource(), "Foo"),
expectErr: true,
expectedErrMessage: `deployments.apps "Foo" not found`,
},
"getting resource fails": {
identifier: deploymentIdentifier,
readerErr: errors.NewInternalError(fmt.Errorf("this is a test")),
expectErr: true,
expectedErrMessage: "Internal error occurred: this is a test",
},
"getting resource succeeds": {
identifier: deploymentIdentifier,
},
"context cancelled": {
identifier: deploymentIdentifier,
readerErr: context.Canceled,
expectErr: true,
expectedErrMessage: context.Canceled.Error(),
},
"rate would exceed context deadline": {
identifier: deploymentIdentifier,
readerErr: fmt.Errorf("client rate limiter Wait returned an error: %w", fmt.Errorf("rate: Wait(n=1) would exceed context deadline")),
expectErr: true,
expectedErrMessage: "client rate limiter Wait returned an error: rate: Wait(n=1) would exceed context deadline",
},
"rate would exceed context deadline wrapped error": {
identifier: deploymentIdentifier,
readerErr: fmt.Errorf("wrapped deeper: %w",
fmt.Errorf("client rate limiter Wait returned an error: %w", fmt.Errorf("rate: Wait(n=1) would exceed context deadline"))),
expectErr: true,
expectedErrMessage: "wrapped deeper: client rate limiter Wait returned an error: rate: Wait(n=1) would exceed context deadline",
},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
fakeReader := &fakecr.ClusterReader{
GetErr: tc.readerErr,
}
fakeMapper := fakemapper.NewFakeRESTMapper(deploymentGVK)
statusReader := &baseStatusReader{
mapper: fakeMapper,
}
u, err := statusReader.lookupResource(context.Background(), fakeReader, tc.identifier)
if tc.expectErr {
if err == nil {
t.Errorf("expected error, but didn't get one")
} else {
assert.EqualError(t, err, tc.expectedErrMessage)
}
return
}
require.NoError(t, err)
assert.Equal(t, deploymentGVK, u.GroupVersionKind())
})
}
}
func TestStatusForGeneratedResources(t *testing.T) {
testCases := map[string]struct {
manifest string
listObjects []unstructured.Unstructured
listErr error
gk schema.GroupKind
path []string
expectError bool
errMessage string
}{
"invalid selector": {
manifest: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: Foo
spec:
replicas: 1
`,
gk: appsv1.SchemeGroupVersion.WithKind("ReplicaSet").GroupKind(),
path: []string{"spec", "selector"},
expectError: true,
errMessage: "no selector found",
},
"Invalid GVK": {
manifest: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: Foo
spec:
replicas: 1
selector:
matchLabels:
app: nginx
`,
gk: schema.GroupKind{
Group: "custom.io",
Kind: "Custom",
},
path: []string{"spec", "selector"},
expectError: true,
errMessage: `no matches for kind "Custom" in group "custom.io"`,
},
"error listing replicasets": {
manifest: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: Foo
spec:
replicas: 1
selector:
matchLabels:
app: nginx
`,
listErr: fmt.Errorf("this is a test"),
gk: appsv1.SchemeGroupVersion.WithKind("ReplicaSet").GroupKind(),
path: []string{"spec", "selector"},
expectError: true,
errMessage: "this is a test",
},
"successfully lists and polling the generated resources": {
manifest: `
apiVersion: apps/v1
kind: Deployment
metadata:
name: Foo
spec:
replicas: 1
selector:
matchLabels:
app: nginx
`,
listObjects: []unstructured.Unstructured{
{
Object: map[string]interface{}{
"apiVersion": "apps/v1",
"kind": "ReplicaSet",
"metadata": map[string]interface{}{
"name": "Foo-12345",
"namespace": "default",
},
},
},
},
gk: appsv1.SchemeGroupVersion.WithKind("ReplicaSet").GroupKind(),
path: []string{"spec", "selector"},
expectError: false,
},
}
for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
fakeClusterReader := &fakecr.ClusterReader{
ListResources: &unstructured.UnstructuredList{
Items: tc.listObjects,
},
ListErr: tc.listErr,
}
fakeMapper := fakemapper.NewFakeRESTMapper(rsGVK)
fakeStatusReader := &fakesr.StatusReader{}
object := testutil.YamlToUnstructured(t, tc.manifest)
resourceStatuses, err := statusForGeneratedResources(context.Background(), fakeMapper, fakeClusterReader,
fakeStatusReader, object, tc.gk, tc.path...)
if tc.expectError {
if err == nil {
t.Errorf("expected an error, but didn't get one")
return
}
assert.EqualError(t, err, tc.errMessage)
return
}
if !tc.expectError && err != nil {
t.Errorf("did not expect an error, but got %v", err)
}
assert.Len(t, resourceStatuses, len(tc.listObjects))
assert.True(t, sort.IsSorted(resourceStatuses))
})
}
}