-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
servers_test.go
334 lines (301 loc) · 9.04 KB
/
servers_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
/*
Copyright 2015 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 services
import (
"fmt"
"testing"
"time"
"github.com/google/go-cmp/cmp"
"github.com/gravitational/trace"
"github.com/stretchr/testify/require"
apidefaults "github.com/gravitational/teleport/api/defaults"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/defaults"
)
// TestServersCompare tests comparing two servers
func TestServersCompare(t *testing.T) {
t.Parallel()
t.Run("compare servers", func(t *testing.T) {
node := &types.ServerV2{
Kind: types.KindNode,
Version: types.V2,
Metadata: types.Metadata{
Name: "node1",
Namespace: apidefaults.Namespace,
Labels: map[string]string{"a": "b"},
},
Spec: types.ServerSpecV2{
Addr: "localhost:3022",
CmdLabels: map[string]types.CommandLabelV2{"a": {Period: types.Duration(time.Minute), Command: []string{"ls", "-l"}}},
Version: "4.0.0",
},
}
node.SetExpiry(time.Date(2018, 1, 2, 3, 4, 5, 6, time.UTC))
// Server is equal to itself
require.Equal(t, CompareServers(node, node), Equal)
// Only timestamps are different
node2 := *node
node2.SetExpiry(time.Date(2018, 1, 2, 3, 4, 5, 8, time.UTC))
require.Equal(t, CompareServers(node, &node2), OnlyTimestampsDifferent)
// Labels are different
node2 = *node
node2.Metadata.Labels = map[string]string{"a": "d"}
require.Equal(t, CompareServers(node, &node2), Different)
// Command labels are different
node2 = *node
node2.Spec.CmdLabels = map[string]types.CommandLabelV2{"a": {Period: types.Duration(time.Minute), Command: []string{"ls", "-lR"}}}
require.Equal(t, CompareServers(node, &node2), Different)
// Address has changed
node2 = *node
node2.Spec.Addr = "localhost:3033"
require.Equal(t, CompareServers(node, &node2), Different)
// Proxy addr has changed
node2 = *node
node2.Spec.PublicAddrs = []string{"localhost:3033"}
require.Equal(t, CompareServers(node, &node2), Different)
// Hostname has changed
node2 = *node
node2.Spec.Hostname = "luna2"
require.Equal(t, CompareServers(node, &node2), Different)
// TeleportVersion has changed
node2 = *node
node2.Spec.Version = "5.0.0"
require.Equal(t, CompareServers(node, &node2), Different)
// Rotation has changed
node2 = *node
node2.Spec.Rotation = types.Rotation{
State: types.RotationStateInProgress,
Phase: types.RotationPhaseUpdateClients,
CurrentID: "1",
Started: time.Date(2018, 3, 4, 5, 6, 7, 8, time.UTC),
GracePeriod: types.Duration(3 * time.Hour),
LastRotated: time.Date(2017, 2, 3, 4, 5, 6, 7, time.UTC),
Schedule: types.RotationSchedule{
UpdateClients: time.Date(2018, 3, 4, 5, 6, 7, 8, time.UTC),
UpdateServers: time.Date(2018, 3, 4, 7, 6, 7, 8, time.UTC),
Standby: time.Date(2018, 3, 4, 5, 6, 13, 8, time.UTC),
},
}
require.Equal(t, CompareServers(node, &node2), Different)
})
t.Run("compare DatabaseServices", func(t *testing.T) {
service := &types.DatabaseServiceV1{
ResourceHeader: types.ResourceHeader{
Kind: types.KindDatabaseService,
Metadata: types.Metadata{
Name: "dbServiceT01",
},
},
Spec: types.DatabaseServiceSpecV1{
ResourceMatchers: []*types.DatabaseResourceMatcher{
{Labels: &types.Labels{"env": []string{"stg"}}},
},
},
}
service.SetExpiry(time.Date(2018, 1, 2, 3, 4, 5, 6, time.UTC))
// DatabaseService is equal to itself
require.Equal(t, CompareServers(service, service), Equal)
// Only timestamps are different
service2 := *service
service2.SetExpiry(time.Date(2018, 1, 2, 3, 4, 5, 8, time.UTC))
require.Equal(t, CompareServers(service, &service2), OnlyTimestampsDifferent)
// Resource Matcher has changed
service2 = *service
service2.Spec.ResourceMatchers = []*types.DatabaseResourceMatcher{
{Labels: &types.Labels{"env": []string{"stg", "qa"}}},
}
require.Equal(t, CompareServers(service, &service2), Different)
})
}
// TestGuessProxyHostAndVersion checks that the GuessProxyHostAndVersion
// correctly guesses the public address of the proxy (Teleport Cluster).
func TestGuessProxyHostAndVersion(t *testing.T) {
t.Parallel()
// No proxies passed in.
host, version, err := GuessProxyHostAndVersion(nil)
require.Equal(t, host, "")
require.Equal(t, version, "")
require.True(t, trace.IsNotFound(err))
// No proxies have public address set.
proxyA := types.ServerV2{}
proxyA.Spec.Hostname = "test-A"
proxyA.Spec.Version = "test-A"
host, version, err = GuessProxyHostAndVersion([]types.Server{&proxyA})
require.Equal(t, host, fmt.Sprintf("%v:%v", proxyA.Spec.Hostname, defaults.HTTPListenPort))
require.Equal(t, version, proxyA.Spec.Version)
require.NoError(t, err)
// At least one proxy has proxy address set.
proxyB := types.ServerV2{}
proxyB.Spec.PublicAddrs = []string{"test-B"}
proxyB.Spec.Version = "test-B"
host, version, err = GuessProxyHostAndVersion([]types.Server{&proxyA, &proxyB})
require.Equal(t, host, proxyB.Spec.PublicAddrs[0])
require.Equal(t, version, proxyB.Spec.Version)
require.NoError(t, err)
}
func TestUnmarshalServerKubernetes(t *testing.T) {
t.Parallel()
// Regression test for
// https://github.com/gravitational/teleport/issues/4862
//
// Verifies unmarshaling succeeds, when provided a 4.4 server JSON
// definition.
tests := []struct {
desc string
in string
want *types.ServerV2
}{
{
desc: "4.4 kubernetes_clusters field",
in: `{
"version": "v2",
"kind": "kube_service",
"metadata": {
"name": "foo"
},
"spec": {
"kubernetes_clusters": ["a", "b", "c"]
}
}`,
want: &types.ServerV2{
Version: types.V2,
Kind: types.KindKubeService,
Metadata: types.Metadata{
Name: "foo",
Namespace: apidefaults.Namespace,
},
},
},
{
desc: "5.0 kubernetes_clusters field",
in: `{
"version": "v2",
"kind": "kube_service",
"metadata": {
"name": "foo"
},
"spec": {
"kube_clusters": [{"name": "a"}, {"name": "b"}, {"name": "c"}]
}
}`,
want: &types.ServerV2{
Version: types.V2,
Kind: types.KindKubeService,
Metadata: types.Metadata{
Name: "foo",
Namespace: apidefaults.Namespace,
},
Spec: types.ServerSpecV2{
KubernetesClusters: []*types.KubernetesCluster{
{Name: "a"},
{Name: "b"},
{Name: "c"},
},
},
},
},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
got, err := UnmarshalServer([]byte(tt.in), types.KindKubeService)
require.NoError(t, err)
require.Empty(t, cmp.Diff(got, tt.want))
})
}
}
// TestOnlyTimestampsDifferent tests that OnlyTimestampsDifferent is returned
// after checking that whether KubernetesClusters and Apps are different.
func TestOnlyTimestampsDifferent(t *testing.T) {
t.Parallel()
now := time.Now()
later := now.Add(time.Minute)
tests := []struct {
desc string
a types.Resource
b types.Resource
expect int
}{
{
desc: "Kube cluster change returns Different",
a: &types.ServerV2{
Spec: types.ServerSpecV2{KubernetesClusters: []*types.KubernetesCluster{}},
Metadata: types.Metadata{
Expires: &now,
},
},
b: &types.ServerV2{
Spec: types.ServerSpecV2{KubernetesClusters: []*types.KubernetesCluster{{
Name: "test-cluster",
}}},
Metadata: types.Metadata{
Expires: &later,
},
},
expect: Different,
},
{
desc: "No kube cluster change returns OnlyTimestampsDifferent",
a: &types.ServerV2{
Spec: types.ServerSpecV2{KubernetesClusters: []*types.KubernetesCluster{}},
Metadata: types.Metadata{
Expires: &now,
},
},
b: &types.ServerV2{
Spec: types.ServerSpecV2{KubernetesClusters: []*types.KubernetesCluster{}},
Metadata: types.Metadata{
Expires: &later,
},
},
expect: OnlyTimestampsDifferent,
},
{
desc: "Apps change returns Different",
a: &types.ServerV2{
Spec: types.ServerSpecV2{Apps: []*types.App{}},
Metadata: types.Metadata{
Expires: &now,
},
},
b: &types.ServerV2{
Spec: types.ServerSpecV2{Apps: []*types.App{{
Name: "test-app",
}}},
Metadata: types.Metadata{
Expires: &later,
},
},
expect: Different,
},
{
desc: "No apps change returns OnlyTimestampsDifferent",
a: &types.ServerV2{
Spec: types.ServerSpecV2{Apps: []*types.App{}},
Metadata: types.Metadata{
Expires: &now,
},
},
b: &types.ServerV2{
Spec: types.ServerSpecV2{Apps: []*types.App{}},
Metadata: types.Metadata{
Expires: &later,
},
},
expect: OnlyTimestampsDifferent,
},
}
for _, tc := range tests {
got := CompareServers(tc.a, tc.b)
require.Equal(t, tc.expect, got, tc.desc)
}
}