crossplane

Форк
0
521 строка · 12.0 Кб
1
/*
2
Copyright 2023 The Crossplane Authors.
3

4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
7

8
    http://www.apache.org/licenses/LICENSE-2.0
9

10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
15
*/
16

17
package config
18

19
import (
20
	"io"
21
	"testing"
22

23
	"github.com/google/go-cmp/cmp"
24

25
	"github.com/crossplane/crossplane-runtime/pkg/errors"
26
	"github.com/crossplane/crossplane-runtime/pkg/test"
27
)
28

29
func TestAddOrUpdateUpboundProfile(t *testing.T) {
30
	name := "cool-profile"
31
	profOne := Profile{
32
		ID:      "cool-user",
33
		Type:    UserProfileType,
34
		Account: "cool-org",
35
	}
36
	profTwo := Profile{
37
		ID:      "cool-user",
38
		Type:    UserProfileType,
39
		Account: "other-org",
40
	}
41

42
	cases := map[string]struct {
43
		reason string
44
		name   string
45
		cfg    *Config
46
		add    Profile
47
		want   *Config
48
		err    error
49
	}{
50
		"AddNewProfile": {
51
			reason: "Adding a new profile to an empty Config should not cause an error.",
52
			name:   name,
53
			cfg:    &Config{},
54
			add:    profOne,
55
			want: &Config{
56
				Upbound: Upbound{
57
					Profiles: map[string]Profile{name: profOne},
58
				},
59
			},
60
		},
61
		"UpdateExistingProfile": {
62
			reason: "Updating an existing profile in the Config should not cause an error.",
63
			name:   name,
64
			cfg: &Config{
65
				Upbound: Upbound{
66
					Profiles: map[string]Profile{name: profOne},
67
				},
68
			},
69
			add: profTwo,
70
			want: &Config{
71
				Upbound: Upbound{
72
					Profiles: map[string]Profile{name: profTwo},
73
				},
74
			},
75
		},
76
		"Invalid": {
77
			reason: "Adding an invalid profile should cause an error.",
78
			name:   name,
79
			cfg:    &Config{},
80
			add:    Profile{},
81
			want:   &Config{},
82
			err:    errors.New(errInvalidProfile),
83
		},
84
	}
85
	for name, tc := range cases {
86
		t.Run(name, func(t *testing.T) {
87
			err := tc.cfg.AddOrUpdateUpboundProfile(tc.name, tc.add)
88
			if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
89
				t.Errorf("\n%s\nAddOrUpdateUpboundProfile(...): -want error, +got error:\n%s", tc.reason, diff)
90
			}
91
			if diff := cmp.Diff(tc.want, tc.cfg); diff != "" {
92
				t.Errorf("\n%s\nAddOrUpdateUpboundProfile(...): -want, +got:\n%s", tc.reason, diff)
93
			}
94
		})
95
	}
96
}
97

98
func TestGetDefaultUpboundProfile(t *testing.T) {
99
	name := "cool-profile"
100
	profOne := Profile{
101
		ID:      "cool-user",
102
		Type:    UserProfileType,
103
		Account: "cool-org",
104
	}
105

106
	cases := map[string]struct {
107
		reason string
108
		name   string
109
		cfg    *Config
110
		want   Profile
111
		err    error
112
	}{
113
		"ErrorNoDefault": {
114
			reason: "If no default defined an error should be returned.",
115
			cfg:    &Config{},
116
			want:   Profile{},
117
			err:    errors.New(errNoDefaultSpecified),
118
		},
119
		"ErrorDefaultNotExist": {
120
			reason: "If defined default does not exist an error should be returned.",
121
			cfg: &Config{
122
				Upbound: Upbound{
123
					Default: "test",
124
				},
125
			},
126
			want: Profile{},
127
			err:  errors.New(errDefaultNotExist),
128
		},
129
		"Successful": {
130
			reason: "If defined default exists it should be returned.",
131
			name:   name,
132
			cfg: &Config{
133
				Upbound: Upbound{
134
					Default:  "cool-profile",
135
					Profiles: map[string]Profile{name: profOne},
136
				},
137
			},
138
			want: profOne,
139
		},
140
	}
141
	for name, tc := range cases {
142
		t.Run(name, func(t *testing.T) {
143
			name, prof, err := tc.cfg.GetDefaultUpboundProfile()
144
			if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
145
				t.Errorf("\n%s\nGetDefaultUpboundProfile(...): -want error, +got error:\n%s", tc.reason, diff)
146
			}
147
			if diff := cmp.Diff(tc.name, name); diff != "" {
148
				t.Errorf("\n%s\nGetDefaultUpboundProfile(...): -want, +got:\n%s", tc.reason, diff)
149
			}
150
			if diff := cmp.Diff(tc.want, prof); diff != "" {
151
				t.Errorf("\n%s\nGetDefaultUpboundProfile(...): -want, +got:\n%s", tc.reason, diff)
152
			}
153
		})
154
	}
155
}
156

157
func TestGetUpboundProfile(t *testing.T) {
158
	name := "cool-profile"
159
	profOne := Profile{
160
		ID:      "cool-user",
161
		Type:    UserProfileType,
162
		Account: "cool-org",
163
	}
164

165
	cases := map[string]struct {
166
		reason string
167
		name   string
168
		cfg    *Config
169
		want   Profile
170
		err    error
171
	}{
172
		"ErrorProfileNotExist": {
173
			reason: "If profile does not exist an error should be returned.",
174
			name:   name,
175
			cfg:    &Config{},
176
			want:   Profile{},
177
			err:    errors.Errorf(errProfileNotFoundFmt, "cool-profile"),
178
		},
179
		"Successful": {
180
			reason: "If profile exists it should be returned.",
181
			name:   "cool-profile",
182
			cfg: &Config{
183
				Upbound: Upbound{
184
					Profiles: map[string]Profile{name: profOne},
185
				},
186
			},
187
			want: profOne,
188
		},
189
	}
190
	for name, tc := range cases {
191
		t.Run(name, func(t *testing.T) {
192
			prof, err := tc.cfg.GetUpboundProfile(tc.name)
193
			if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
194
				t.Errorf("\n%s\nGetUpboundProfile(...): -want error, +got error:\n%s", tc.reason, diff)
195
			}
196
			if diff := cmp.Diff(tc.want, prof); diff != "" {
197
				t.Errorf("\n%s\nGetUpboundProfile(...): -want, +got:\n%s", tc.reason, diff)
198
			}
199
		})
200
	}
201
}
202

203
func TestSetDefaultUpboundProfile(t *testing.T) {
204
	name := "cool-user"
205
	profOne := Profile{
206
		Type:    UserProfileType,
207
		Account: "cool-org",
208
	}
209

210
	cases := map[string]struct {
211
		reason string
212
		name   string
213
		cfg    *Config
214
		err    error
215
	}{
216
		"ErrorProfileNotExist": {
217
			reason: "If profile does not exist an error should be returned.",
218
			name:   name,
219
			cfg:    &Config{},
220
			err:    errors.Errorf(errProfileNotFoundFmt, "cool-user"),
221
		},
222
		"Successful": {
223
			reason: "If profile exists it should be set as default.",
224
			name:   "cool-user",
225
			cfg: &Config{
226
				Upbound: Upbound{
227
					Profiles: map[string]Profile{name: profOne},
228
				},
229
			},
230
		},
231
	}
232
	for name, tc := range cases {
233
		t.Run(name, func(t *testing.T) {
234
			err := tc.cfg.SetDefaultUpboundProfile(tc.name)
235
			if diff := cmp.Diff(tc.err, err, test.EquateErrors()); diff != "" {
236
				t.Errorf("\n%s\nGetUpboundProfile(...): -want error, +got error:\n%s", tc.reason, diff)
237
			}
238
		})
239
	}
240
}
241

242
func TestGetUpboundProfiles(t *testing.T) {
243
	nameOne := "cool-user"
244
	profOne := Profile{
245
		Type:    UserProfileType,
246
		Account: "cool-org",
247
	}
248
	nameTwo := "cool-user2"
249
	profTwo := Profile{
250
		Type:    UserProfileType,
251
		Account: "cool-org2",
252
	}
253

254
	type args struct {
255
		cfg *Config
256
	}
257
	type want struct {
258
		err      error
259
		profiles map[string]Profile
260
	}
261

262
	cases := map[string]struct {
263
		reason string
264
		args   args
265
		want   want
266
	}{
267
		"ErrorNoProfilesExist": {
268
			reason: "If no profiles exist an error should be returned.",
269
			args: args{
270
				cfg: &Config{},
271
			},
272
			want: want{
273
				err: errors.New(errNoProfilesFound),
274
			},
275
		},
276
		"Successful": {
277
			reason: "If profile exists it should be set as default.",
278
			args: args{
279
				cfg: &Config{
280
					Upbound: Upbound{
281
						Profiles: map[string]Profile{
282
							nameOne: profOne,
283
							nameTwo: profTwo,
284
						},
285
					},
286
				},
287
			},
288
			want: want{
289
				profiles: map[string]Profile{
290
					nameOne: profOne,
291
					nameTwo: profTwo,
292
				},
293
			},
294
		},
295
	}
296
	for name, tc := range cases {
297
		t.Run(name, func(t *testing.T) {
298
			profiles, err := tc.args.cfg.GetUpboundProfiles()
299

300
			if diff := cmp.Diff(tc.want.profiles, profiles); diff != "" {
301
				t.Errorf("\n%s\nGetUpboundProfiles(...): -want, +got:\n%s", tc.reason, diff)
302
			}
303
			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
304
				t.Errorf("\n%s\nGetUpboundProfiles(...): -want error, +got error:\n%s", tc.reason, diff)
305
			}
306
		})
307
	}
308
}
309

310
func TestGetBaseConfig(t *testing.T) {
311
	nameOne := "cool-user"
312
	profOne := Profile{
313
		Type:    UserProfileType,
314
		Account: "cool-org",
315
		BaseConfig: map[string]string{
316
			"key": "value",
317
		},
318
	}
319
	nameTwo := "cool-user2"
320
	profTwo := Profile{
321
		Type:    UserProfileType,
322
		Account: "cool-org2",
323
	}
324

325
	type args struct {
326
		profile string
327
		cfg     *Config
328
	}
329
	type want struct {
330
		err  error
331
		base map[string]string
332
	}
333

334
	cases := map[string]struct {
335
		reason string
336
		args   args
337
		want   want
338
	}{
339
		"ErrorNoProfilesExist": {
340
			reason: "If no profiles exist an error should be returned.",
341
			args: args{
342
				profile: nameTwo,
343
				cfg:     &Config{},
344
			},
345
			want: want{
346
				err: errors.Errorf(errProfileNotFoundFmt, nameTwo),
347
			},
348
		},
349
		"Successful": {
350
			reason: "If profile exists, its base config should be returned.",
351
			args: args{
352
				profile: nameOne,
353
				cfg: &Config{
354
					Upbound: Upbound{
355
						Profiles: map[string]Profile{
356
							nameOne: profOne,
357
							nameTwo: profTwo,
358
						},
359
					},
360
				},
361
			},
362
			want: want{
363
				base: profOne.BaseConfig,
364
			},
365
		},
366
	}
367
	for name, tc := range cases {
368
		t.Run(name, func(t *testing.T) {
369
			base, err := tc.args.cfg.GetBaseConfig(tc.args.profile)
370

371
			if diff := cmp.Diff(tc.want.base, base); diff != "" {
372
				t.Errorf("\n%s\nGetBaseConfig(...): -want, +got:\n%s", tc.reason, diff)
373
			}
374
			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
375
				t.Errorf("\n%s\nGetBaseConfig(...): -want error, +got error:\n%s", tc.reason, diff)
376
			}
377
		})
378
	}
379
}
380

381
func TestAddToBaseConfig(t *testing.T) {
382
	nameOne := "cool-user"
383
	profOne := Profile{
384
		Type:    UserProfileType,
385
		Account: "cool-org",
386
	}
387
	nameTwo := "cool-user2"
388
	profTwo := Profile{
389
		Type:    UserProfileType,
390
		Account: "cool-org2",
391
	}
392

393
	type args struct {
394
		profile string
395
		key     string
396
		value   string
397
		cfg     *Config
398
	}
399
	type want struct {
400
		err  error
401
		base map[string]string
402
	}
403

404
	cases := map[string]struct {
405
		reason string
406
		args   args
407
		want   want
408
	}{
409
		"ErrorNoProfilesExist": {
410
			reason: "If no profiles exist an error should be returned.",
411
			args: args{
412
				profile: nameTwo,
413
				cfg:     &Config{},
414
			},
415
			want: want{
416
				err: errors.Errorf(errProfileNotFoundFmt, nameTwo),
417
			},
418
		},
419
		"Successful": {
420
			reason: "If profile exists, we should add the k,v pair to the base config.",
421
			args: args{
422
				profile: nameOne,
423
				key:     "k",
424
				value:   "v",
425
				cfg: &Config{
426
					Upbound: Upbound{
427
						Profiles: map[string]Profile{
428
							nameOne: profOne,
429
							nameTwo: profTwo,
430
						},
431
					},
432
				},
433
			},
434
			want: want{
435
				base: map[string]string{
436
					"k": "v",
437
				},
438
			},
439
		},
440
	}
441
	for name, tc := range cases {
442
		t.Run(name, func(t *testing.T) {
443
			err := tc.args.cfg.AddToBaseConfig(tc.args.profile, tc.args.key, tc.args.value)
444
			base, _ := tc.args.cfg.GetBaseConfig(tc.args.profile)
445

446
			if diff := cmp.Diff(tc.want.base, base); diff != "" {
447
				t.Errorf("\n%s\nAddToBaseConfig(...): -want, +got:\n%s", tc.reason, diff)
448
			}
449
			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
450
				t.Errorf("\n%s\nAddToBaseConfig(...): -want error, +got error:\n%s", tc.reason, diff)
451
			}
452
		})
453
	}
454
}
455

456
func TestBaseToJSON(t *testing.T) {
457
	dneName := "does not exist"
458
	exists := "exists"
459

460
	type args struct {
461
		profile string
462
		cfg     *Config
463
	}
464
	type want struct {
465
		err  error
466
		base string
467
	}
468

469
	cases := map[string]struct {
470
		reason string
471
		args   args
472
		want   want
473
	}{
474
		"ErrorNoProfilesExist": {
475
			reason: "If no profiles exist an error should be returned.",
476
			args: args{
477
				profile: dneName,
478
				cfg:     &Config{},
479
			},
480
			want: want{
481
				err: errors.Errorf(errProfileNotFoundFmt, dneName),
482
			},
483
		},
484
		"Successful": {
485
			reason: "If profile exists, we should add the k,v pair to the base config.",
486
			args: args{
487
				profile: exists,
488
				cfg: &Config{
489
					Upbound: Upbound{
490
						Profiles: map[string]Profile{
491
							exists: {
492
								Type:    UserProfileType,
493
								Account: "account",
494
								BaseConfig: map[string]string{
495
									"k": "v",
496
								},
497
							},
498
						},
499
					},
500
				},
501
			},
502
			want: want{
503
				base: "{\"k\":\"v\"}\n",
504
			},
505
		},
506
	}
507
	for name, tc := range cases {
508
		t.Run(name, func(t *testing.T) {
509
			r, err := tc.args.cfg.BaseToJSON(tc.args.profile)
510
			if r != nil {
511
				base, _ := io.ReadAll(r)
512
				if diff := cmp.Diff(tc.want.base, string(base)); diff != "" {
513
					t.Errorf("\n%s\nBaseToJSON(...): -want, +got:\n%s", tc.reason, diff)
514
				}
515
			}
516
			if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
517
				t.Errorf("\n%s\nBaseToJSON(...): -want error, +got error:\n%s", tc.reason, diff)
518
			}
519
		})
520
	}
521
}
522

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.