-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
resource_test.go
411 lines (379 loc) · 9.68 KB
/
resource_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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/**
* Copyright 2022 Gravitational, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package types
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestMatchSearch(t *testing.T) {
t.Parallel()
cases := []struct {
name string
expectMatch require.BoolAssertionFunc
fieldVals []string
searchVals []string
customFn func(v string) bool
}{
{
name: "no match",
expectMatch: require.False,
fieldVals: []string{"foo", "bar", "baz"},
searchVals: []string{"cat"},
customFn: func(v string) bool {
return false
},
},
{
name: "no match for partial match",
expectMatch: require.False,
fieldVals: []string{"foo"},
searchVals: []string{"foo", "dog"},
},
{
name: "no match for partial custom match",
expectMatch: require.False,
fieldVals: []string{"foo", "bar", "baz"},
searchVals: []string{"foo", "bee", "rat"},
customFn: func(v string) bool {
return v == "bee"
},
},
{
name: "no match for search phrase",
expectMatch: require.False,
fieldVals: []string{"foo", "dog", "dog foo", "foodog"},
searchVals: []string{"foo dog"},
},
{
name: "match",
expectMatch: require.True,
fieldVals: []string{"foo", "bar", "baz"},
searchVals: []string{"baz"},
},
{
name: "match with nil search values",
expectMatch: require.True,
},
{
name: "match with repeat search vals",
expectMatch: require.True,
fieldVals: []string{"foo", "bar", "baz"},
searchVals: []string{"foo", "foo", "baz"},
},
{
name: "match for a list of search vals contained within one field value",
expectMatch: require.True,
fieldVals: []string{"foo barbaz"},
searchVals: []string{"baz", "foo", "bar"},
},
{
name: "match with mix of single vals and phrases",
expectMatch: require.True,
fieldVals: []string{"foo baz", "bar"},
searchVals: []string{"baz", "foo", "foo baz", "bar"},
},
{
name: "match ignore case",
expectMatch: require.True,
fieldVals: []string{"FOO barBaz"},
searchVals: []string{"baZ", "foo", "BaR"},
},
{
name: "match with custom match",
expectMatch: require.True,
fieldVals: []string{"foo", "bar", "baz"},
searchVals: []string{"foo", "bar", "tunnel"},
customFn: func(v string) bool {
return v == "tunnel"
},
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
matched := MatchSearch(tc.fieldVals, tc.searchVals, tc.customFn)
tc.expectMatch(t, matched)
})
}
}
func TestMatchSearch_ResourceSpecific(t *testing.T) {
t.Parallel()
labels := map[string]string{"env": "prod", "os": "mac"}
cases := []struct {
name string
// searchNotDefined refers to resources where the searcheable field values are not defined.
searchNotDefined bool
matchingSearchVals []string
newResource func(*testing.T) ResourceWithLabels
}{
{
name: "node",
matchingSearchVals: []string{"foo", "bar", "prod", "os"},
newResource: func(t *testing.T) ResourceWithLabels {
server, err := NewServerWithLabels("_", KindNode, ServerSpecV2{
Hostname: "foo",
Addr: "bar",
}, labels)
require.NoError(t, err)
return server
},
},
{
name: "node using tunnel",
matchingSearchVals: []string{"tunnel"},
newResource: func(t *testing.T) ResourceWithLabels {
server, err := NewServer("_", KindNode, ServerSpecV2{
UseTunnel: true,
})
require.NoError(t, err)
return server
},
},
{
name: "windows desktop",
matchingSearchVals: []string{"foo", "bar", "env", "prod", "os"},
newResource: func(t *testing.T) ResourceWithLabels {
desktop, err := NewWindowsDesktopV3("foo", labels, WindowsDesktopSpecV3{
Addr: "bar",
})
require.NoError(t, err)
return desktop
},
},
{
name: "application",
matchingSearchVals: []string{"foo", "bar", "baz", "mac"},
newResource: func(t *testing.T) ResourceWithLabels {
app, err := NewAppV3(Metadata{
Name: "foo",
Description: "bar",
Labels: labels,
}, AppSpecV3{
PublicAddr: "baz",
URI: "_",
})
require.NoError(t, err)
return app
},
},
{
name: "kube cluster",
matchingSearchVals: []string{"foo", "prod", "env"},
newResource: func(t *testing.T) ResourceWithLabels {
kc, err := NewKubernetesClusterV3FromLegacyCluster("_", &KubernetesCluster{
Name: "foo",
StaticLabels: labels,
})
require.NoError(t, err)
return kc
},
},
{
name: "database",
matchingSearchVals: []string{"foo", "bar", "baz", "prod", DatabaseTypeRedshift},
newResource: func(t *testing.T) ResourceWithLabels {
db, err := NewDatabaseV3(Metadata{
Name: "foo",
Description: "bar",
Labels: labels,
}, DatabaseSpecV3{
Protocol: "baz",
URI: "_",
AWS: AWS{
Redshift: Redshift{
ClusterID: "_",
},
},
})
require.NoError(t, err)
return db
},
},
{
name: "database with gcp keywords",
matchingSearchVals: []string{"cloud", "cloud sql"},
newResource: func(t *testing.T) ResourceWithLabels {
db, err := NewDatabaseV3(Metadata{
Name: "foo",
Labels: labels,
}, DatabaseSpecV3{
Protocol: "_",
URI: "_",
GCP: GCPCloudSQL{
ProjectID: "_",
InstanceID: "_",
},
})
require.NoError(t, err)
return db
},
},
{
name: "app server",
searchNotDefined: true,
newResource: func(t *testing.T) ResourceWithLabels {
appServer, err := NewAppServerV3(Metadata{
Name: "foo",
}, AppServerSpecV3{
HostID: "_",
App: &AppV3{Metadata: Metadata{Name: "_"}, Spec: AppSpecV3{URI: "_"}},
})
require.NoError(t, err)
return appServer
},
},
{
name: "db server",
searchNotDefined: true,
newResource: func(t *testing.T) ResourceWithLabels {
dbServer, err := NewDatabaseServerV3(Metadata{
Name: "foo",
}, DatabaseServerSpecV3{
HostID: "_",
Hostname: "_",
})
require.NoError(t, err)
return dbServer
},
},
{
name: "kube server",
searchNotDefined: true,
newResource: func(t *testing.T) ResourceWithLabels {
kubeServer, err := NewKubernetesServerV3(
Metadata{
Name: "foo",
}, KubernetesServerSpecV3{
HostID: "_",
Hostname: "_",
Cluster: &KubernetesClusterV3{
Metadata: Metadata{
Name: "_",
},
},
})
require.NoError(t, err)
return kubeServer
},
},
{
name: "desktop service",
searchNotDefined: true,
newResource: func(t *testing.T) ResourceWithLabels {
desktopService, err := NewWindowsDesktopServiceV3(Metadata{
Name: "foo",
}, WindowsDesktopServiceSpecV3{
Addr: "_",
TeleportVersion: "_",
})
require.NoError(t, err)
return desktopService
},
},
}
for _, tc := range cases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
resource := tc.newResource(t)
// Nil search values, should always return true
match := resource.MatchSearch(nil)
require.True(t, match)
switch {
case tc.searchNotDefined:
// Trying to search something in resources without search field values defined
// should always return false.
match := resource.MatchSearch([]string{"_"})
require.False(t, match)
default:
// Test no match.
match := resource.MatchSearch([]string{"foo", "llama"})
require.False(t, match)
// Test match.
match = resource.MatchSearch(tc.matchingSearchVals)
require.True(t, match)
}
})
}
}
func TestResourcesWithLabels_ToMap(t *testing.T) {
mkServerHost := func(name string, hostname string) ResourceWithLabels {
server, err := NewServerWithLabels(name, KindNode, ServerSpecV2{
Hostname: hostname + ".example.com",
Addr: name + ".example.com",
}, nil)
require.NoError(t, err)
return server
}
mkServer := func(name string) ResourceWithLabels {
return mkServerHost(name, name)
}
tests := []struct {
name string
r ResourcesWithLabels
want ResourcesWithLabelsMap
}{
{
name: "empty",
r: nil,
want: map[string]ResourceWithLabels{},
},
{
name: "simple list",
r: []ResourceWithLabels{mkServer("a"), mkServer("b"), mkServer("c")},
want: map[string]ResourceWithLabels{
"a": mkServer("a"),
"b": mkServer("b"),
"c": mkServer("c"),
},
},
{
name: "first duplicate wins",
r: []ResourceWithLabels{mkServerHost("a", "a1"), mkServerHost("a", "a2"), mkServerHost("a", "a3")},
want: map[string]ResourceWithLabels{
"a": mkServerHost("a", "a1"),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
require.Equal(t, tt.r.ToMap(), tt.want)
})
}
}
func TestValidLabelKey(t *testing.T) {
for _, tc := range []struct {
label string
valid bool
}{
{
label: "1x/Y*_-",
valid: true,
},
{
label: "x:y",
valid: true,
},
{
label: "x\\y",
valid: false,
},
} {
isValid := IsValidLabelKey(tc.label)
require.Equal(t, tc.valid, isValid)
}
}