1 package preference
2
3 import (
4 "context"
5 "fmt"
6 "os"
7 "strconv"
8 "testing"
9 "time"
10
11 "github.com/google/go-cmp/cmp"
12
13 "github.com/redhat-developer/odo/pkg/config"
14 envcontext "github.com/redhat-developer/odo/pkg/config/context"
15
16 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
17 )
18
19 func TestNew(t *testing.T) {
20
21 tempConfigFile, err := os.CreateTemp("", "odoconfig")
22 if err != nil {
23 t.Fatal(err)
24 }
25 defer tempConfigFile.Close()
26 tempConfigFileName := tempConfigFile.Name()
27
28 tests := []struct {
29 name string
30 output *preferenceInfo
31 success bool
32 }{
33 {
34 name: "Test filename is being set",
35 output: &preferenceInfo{
36 Filename: tempConfigFile.Name(),
37 Preference: Preference{
38 TypeMeta: metav1.TypeMeta{
39 Kind: preferenceKind,
40 APIVersion: preferenceAPIVersion,
41 },
42 OdoSettings: odoSettings{
43 RegistryList: &[]Registry{
44 {
45 Name: DefaultDevfileRegistryName,
46 URL: DefaultDevfileRegistryURL,
47 Secure: false,
48 },
49 },
50 },
51 },
52 },
53 success: true,
54 },
55 }
56
57 for _, test := range tests {
58 t.Run(test.name, func(t *testing.T) {
59 ctx := context.Background()
60 ctx = envcontext.WithEnvConfig(ctx, config.Configuration{
61 Globalodoconfig: &tempConfigFileName,
62 })
63 cfi, err := newPreferenceInfo(ctx)
64 switch test.success {
65 case true:
66 if err != nil {
67 t.Errorf("expected test to pass, but it failed with error: %v", err)
68 }
69 case false:
70 if err == nil {
71 t.Errorf("expected test to fail, but it passed!")
72 }
73 }
74 if diff := cmp.Diff(test.output, cfi); diff != "" {
75 t.Errorf("newPreferenceInfo() mismatch (-want +got):\n%s", diff)
76 }
77 })
78 }
79 }
80
81 func TestGetPushTimeout(t *testing.T) {
82
83 nonzeroValue := 5 * time.Second
84
85 tests := []struct {
86 name string
87 existingConfig Preference
88 want time.Duration
89 }{
90 {
91 name: "Validating default value from test case",
92 existingConfig: Preference{},
93 want: 240,
94 },
95 {
96 name: "Validating value 5 from configuration",
97 existingConfig: Preference{
98 OdoSettings: odoSettings{
99 PushTimeout: &nonzeroValue,
100 },
101 },
102 want: 5,
103 },
104 }
105 for _, tt := range tests {
106 t.Run(tt.name, func(t *testing.T) {
107 ctx := context.Background()
108 ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
109 cfg, err := newPreferenceInfo(ctx)
110 if err != nil {
111 t.Error(err)
112 }
113 cfg.Preference = tt.existingConfig
114
115 output := cfg.GetPushTimeout()
116 if output != (tt.want * time.Second) {
117 t.Errorf("GetPushTimeout returned unexpected value\ngot: %d \nexpected: %d\n", output, tt.want)
118 }
119 })
120 }
121 }
122
123 func TestGetTimeout(t *testing.T) {
124 zeroValue := 0 * time.Second
125 nonzeroValue := 5 * time.Second
126 tests := []struct {
127 name string
128 existingConfig Preference
129 want time.Duration
130 }{
131 {
132 name: "validating value 1 from config in default case",
133 existingConfig: Preference{},
134 want: 1,
135 },
136
137 {
138 name: "validating value 0 from config",
139 existingConfig: Preference{
140 OdoSettings: odoSettings{
141 Timeout: &zeroValue,
142 },
143 },
144 want: 0,
145 },
146
147 {
148 name: "validating value 5 from config",
149 existingConfig: Preference{
150 OdoSettings: odoSettings{
151 Timeout: &nonzeroValue,
152 },
153 },
154 want: 5,
155 },
156 }
157 for _, tt := range tests {
158 t.Run(tt.name, func(t *testing.T) {
159 ctx := context.Background()
160 ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
161 cfg, err := newPreferenceInfo(ctx)
162 if err != nil {
163 t.Error(err)
164 }
165 cfg.Preference = tt.existingConfig
166
167 output := cfg.GetTimeout()
168 if output != (tt.want * time.Second) {
169 t.Errorf("GetTimeout returned unexpected value\ngot: %d \nexpected: %d\n", output, tt.want)
170 }
171 })
172 }
173 }
174
175 func TestSetConfiguration(t *testing.T) {
176 trueValue := true
177 falseValue := false
178 minValue := minimumDurationValue
179
180 tests := []struct {
181 name string
182 parameter string
183 value string
184 existingConfig Preference
185 wantErr bool
186 want interface{}
187 }{
188
189 {
190 name: fmt.Sprintf("%s set nil to true", UpdateNotificationSetting),
191 parameter: UpdateNotificationSetting,
192 value: "true",
193 existingConfig: Preference{},
194 want: true,
195 wantErr: false,
196 },
197 {
198 name: fmt.Sprintf("%s set true to false", UpdateNotificationSetting),
199 parameter: UpdateNotificationSetting,
200 value: "false",
201 existingConfig: Preference{
202 OdoSettings: odoSettings{
203 UpdateNotification: &trueValue,
204 },
205 },
206 want: false,
207 wantErr: false,
208 },
209 {
210 name: fmt.Sprintf("%s set false to true", UpdateNotificationSetting),
211 parameter: UpdateNotificationSetting,
212 value: "true",
213 existingConfig: Preference{
214 OdoSettings: odoSettings{
215 UpdateNotification: &falseValue,
216 },
217 },
218 want: true,
219 wantErr: false,
220 },
221
222 {
223 name: fmt.Sprintf("%s invalid value", UpdateNotificationSetting),
224 parameter: UpdateNotificationSetting,
225 value: "invalid_value",
226 existingConfig: Preference{},
227 wantErr: true,
228 },
229
230 {
231 name: fmt.Sprintf("%s set to 5 from 0", TimeoutSetting),
232 parameter: TimeoutSetting,
233 value: "5s",
234 existingConfig: Preference{
235 OdoSettings: odoSettings{
236 Timeout: &minValue,
237 },
238 },
239 want: 5 * time.Second,
240 wantErr: false,
241 },
242 {
243 name: fmt.Sprintf("%s set to 300", TimeoutSetting),
244 parameter: TimeoutSetting,
245 value: "5m",
246 existingConfig: Preference{},
247 want: 5 * time.Second,
248 wantErr: false,
249 },
250 {
251 name: fmt.Sprintf("%s set to 0", TimeoutSetting),
252 parameter: TimeoutSetting,
253 value: "0s",
254 existingConfig: Preference{},
255 wantErr: true,
256 },
257 {
258 name: fmt.Sprintf("%s invalid value", TimeoutSetting),
259 parameter: TimeoutSetting,
260 value: "this",
261 existingConfig: Preference{},
262 wantErr: true,
263 },
264 {
265 name: fmt.Sprintf("%s set to 300 with mixed case in parameter name", TimeoutSetting),
266 parameter: "TimeOut",
267 value: "5m",
268 existingConfig: Preference{},
269 want: 5 * time.Minute,
270 wantErr: false,
271 },
272
273 {
274 name: "invalid parameter",
275 parameter: "invalid_parameter",
276 existingConfig: Preference{},
277 wantErr: true,
278 },
279 {
280 name: fmt.Sprintf("%s set to 0", TimeoutSetting),
281 parameter: TimeoutSetting,
282 value: "0s",
283 existingConfig: Preference{},
284 wantErr: true,
285 },
286 {
287 name: fmt.Sprintf("%s invalid value", TimeoutSetting),
288 parameter: TimeoutSetting,
289 value: "invalid",
290 existingConfig: Preference{},
291 wantErr: true,
292 },
293 {
294 name: fmt.Sprintf("%s negative value", TimeoutSetting),
295 parameter: TimeoutSetting,
296 value: "-5s",
297 existingConfig: Preference{},
298 wantErr: true,
299 },
300 {
301 name: fmt.Sprintf("%s set to 99 with mixed case in parameter name", TimeoutSetting),
302 parameter: "PushTimeout",
303 value: "99s",
304 existingConfig: Preference{},
305 want: 99 * time.Second,
306 wantErr: false,
307 },
308 {
309 name: "set RegistryCacheTime to 1 minutes",
310 parameter: "RegistryCacheTime",
311 value: "1m",
312 existingConfig: Preference{},
313 want: 1 * time.Minute,
314 wantErr: false,
315 },
316 {
317 name: "set RegistryCacheTime to non int value",
318 parameter: "RegistryCacheTime",
319 value: "a",
320 existingConfig: Preference{},
321 wantErr: true,
322 },
323 {
324 name: fmt.Sprintf("set %s to non bool value", ConsentTelemetrySetting),
325 parameter: ConsentTelemetrySetting,
326 value: "123",
327 existingConfig: Preference{},
328 wantErr: true,
329 },
330 {
331 name: fmt.Sprintf("set %s from nil to true", ConsentTelemetrySetting),
332 parameter: ConsentTelemetrySetting,
333 value: "true",
334 existingConfig: Preference{},
335 wantErr: false,
336 want: true,
337 },
338 {
339 name: fmt.Sprintf("set %s from true to false", ConsentTelemetrySetting),
340 parameter: ConsentTelemetrySetting,
341 value: "false",
342 existingConfig: Preference{
343 OdoSettings: odoSettings{
344 ConsentTelemetry: &trueValue,
345 },
346 },
347 wantErr: false,
348 want: false,
349 },
350 }
351 for _, tt := range tests {
352 t.Run(tt.name, func(t *testing.T) {
353 ctx := context.Background()
354 ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
355 cfg, err := newPreferenceInfo(ctx)
356 if err != nil {
357 t.Error(err)
358 }
359 cfg.Preference = tt.existingConfig
360
361 err = cfg.SetConfiguration(tt.parameter, tt.value)
362
363 if !tt.wantErr && err == nil {
364
365
366 switch tt.parameter {
367 case "updatenotification":
368 if *cfg.OdoSettings.UpdateNotification != tt.want {
369 t.Errorf("unexpected value after execution of SetConfiguration\ngot: %t \nexpected: %t\n", *cfg.OdoSettings.UpdateNotification, tt.want)
370 }
371 case "timeout":
372 if *cfg.OdoSettings.Timeout != tt.want {
373 t.Errorf("unexpected value after execution of SetConfiguration\ngot: %v \nexpected: %d\n", cfg.OdoSettings.Timeout, tt.want)
374 }
375 case "registrycachetime":
376 if *cfg.OdoSettings.RegistryCacheTime != tt.want {
377 t.Errorf("unexpected value after execution of SetConfiguration\ngot: %v \nexpected: %d\n", *cfg.OdoSettings.RegistryCacheTime, tt.want)
378 }
379 }
380 } else if tt.wantErr && err != nil {
381
382 switch tt.parameter {
383 case "updatenotification":
384 case "timeout":
385 typedval, e := strconv.Atoi(tt.value)
386
387 if !(typedval < 0 || e != nil) {
388 t.Error(e)
389 }
390 }
391 } else {
392 t.Error(err)
393 }
394
395 })
396 }
397 }
398
399 func TestConsentTelemetry(t *testing.T) {
400 trueValue := true
401 falseValue := false
402
403 tests := []struct {
404 name string
405 existingConfig Preference
406 want bool
407 }{
408 {
409 name: fmt.Sprintf("%s nil", ConsentTelemetrySetting),
410 existingConfig: Preference{},
411 want: false,
412 },
413 {
414 name: fmt.Sprintf("%s true", ConsentTelemetrySetting),
415 existingConfig: Preference{
416 OdoSettings: odoSettings{
417 ConsentTelemetry: &trueValue,
418 },
419 },
420 want: true,
421 },
422 {
423 name: fmt.Sprintf("%s false", ConsentTelemetrySetting),
424 existingConfig: Preference{
425 OdoSettings: odoSettings{
426 ConsentTelemetry: &falseValue,
427 },
428 },
429 want: false,
430 },
431 }
432
433 for _, tt := range tests {
434 t.Run(tt.name, func(t *testing.T) {
435 cfg := preferenceInfo{
436 Preference: tt.existingConfig,
437 }
438 output := cfg.GetConsentTelemetry()
439
440 if output != tt.want {
441 t.Errorf("ConsentTelemetry returned unexpected value\ngot: %t \nexpected: %t\n", output, tt.want)
442 }
443
444 })
445 }
446 }
447
448 func TestGetupdateNotification(t *testing.T) {
449 trueValue := true
450 falseValue := false
451
452 tests := []struct {
453 name string
454 existingConfig Preference
455 want bool
456 }{
457 {
458 name: fmt.Sprintf("%s nil", UpdateNotificationSetting),
459 existingConfig: Preference{},
460 want: true,
461 },
462 {
463 name: fmt.Sprintf("%s true", UpdateNotificationSetting),
464 existingConfig: Preference{
465 OdoSettings: odoSettings{
466 UpdateNotification: &trueValue,
467 },
468 },
469 want: true,
470 },
471 {
472 name: fmt.Sprintf("%s false", UpdateNotificationSetting),
473 existingConfig: Preference{
474 OdoSettings: odoSettings{
475 UpdateNotification: &falseValue,
476 },
477 },
478 want: false,
479 },
480 }
481
482 for _, tt := range tests {
483 t.Run(tt.name, func(t *testing.T) {
484 cfg := preferenceInfo{
485 Preference: tt.existingConfig,
486 }
487 output := cfg.GetUpdateNotification()
488
489 if output != tt.want {
490 t.Errorf("GetUpdateNotification returned unexpected value\ngot: %t \nexpected: %t\n", output, tt.want)
491 }
492
493 })
494 }
495 }
496
497 func TestIsSupportedParameter(t *testing.T) {
498 tests := []struct {
499 testName string
500 param string
501 expectedLower string
502 expected bool
503 }{
504 {
505 testName: "existing, lower case",
506 param: "timeout",
507 expectedLower: "timeout",
508 expected: true,
509 },
510 {
511 testName: "existing, from description",
512 param: "Timeout",
513 expectedLower: "timeout",
514 expected: true,
515 },
516 {
517 testName: "existing, mixed case",
518 param: "TimeOut",
519 expectedLower: "timeout",
520 expected: true,
521 },
522 {
523 testName: "empty",
524 param: "",
525 expected: false,
526 },
527 {
528 testName: "unexisting",
529 param: "foo",
530 expected: false,
531 },
532 }
533
534 for _, tt := range tests {
535 t.Log("Running test: ", tt.testName)
536 t.Run(tt.testName, func(t *testing.T) {
537 actual, ok := asSupportedParameter(tt.param)
538 if tt.expected != ok && tt.expectedLower != actual {
539 t.Fail()
540 }
541 })
542 }
543 }
544
545 func TestPreferenceIsntCreatedWhenOdoIsUsed(t *testing.T) {
546
547 ctx := context.Background()
548 ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
549 filename, err := getPreferenceFile(ctx)
550 if err != nil {
551 t.Error(err)
552 }
553 os.RemoveAll(filename)
554
555 conf, err := newPreferenceInfo(ctx)
556 if err != nil {
557 t.Errorf("error while creating global preference %v", err)
558 }
559 if _, err = os.Stat(conf.Filename); !os.IsNotExist(err) {
560 t.Errorf("preference file shouldn't exist yet")
561 }
562 }
563
564 func TestMetaTypePopulatedInPreference(t *testing.T) {
565 ctx := context.Background()
566 ctx = envcontext.WithEnvConfig(ctx, config.Configuration{})
567 pi, err := newPreferenceInfo(ctx)
568
569 if err != nil {
570 t.Error(err)
571 }
572 if pi.APIVersion != preferenceAPIVersion || pi.Kind != preferenceKind {
573 t.Error("the api version and kind in preference are incorrect")
574 }
575 }
576
577 func TestHandleWithoutRegistryExist(t *testing.T) {
578 tests := []struct {
579 name string
580 registryList []Registry
581 operation string
582 registryName string
583 registryURL string
584 want []Registry
585 }{
586 {
587 name: "Add registry",
588 registryList: []Registry{},
589 operation: "add",
590 registryName: "testName",
591 registryURL: "testURL",
592 want: []Registry{
593 {
594 Name: "testName",
595 URL: "testURL",
596 },
597 },
598 },
599 {
600 name: "Delete registry",
601 registryList: []Registry{},
602 operation: "remove",
603 registryName: "testName",
604 registryURL: "testURL",
605 want: nil,
606 },
607 }
608
609 for _, tt := range tests {
610 t.Run(tt.name, func(t *testing.T) {
611 got, err := handleWithoutRegistryExist(tt.registryList, tt.operation, tt.registryName, tt.registryURL, false)
612 if err != nil {
613 t.Logf("Error message is %v", err)
614 }
615
616 if diff := cmp.Diff(tt.want, got); diff != "" {
617 t.Errorf("handleWithoutRegistryExist() mismatch (-want +got):\n%s", diff)
618 }
619 })
620 }
621 }
622
623 func TestHandleWithRegistryExist(t *testing.T) {
624 tests := []struct {
625 name string
626 index int
627 registryList []Registry
628 operation string
629 registryName string
630 forceFlag bool
631 want []Registry
632 }{
633 {
634 name: "Add registry",
635 index: 0,
636 registryList: []Registry{
637 {
638 Name: "testName",
639 URL: "testURL",
640 },
641 },
642 operation: "add",
643 registryName: "testName",
644 forceFlag: false,
645 want: nil,
646 },
647 {
648 name: "Delete registry",
649 index: 0,
650 registryList: []Registry{
651 {
652 Name: "testName",
653 URL: "testURL",
654 },
655 },
656 operation: "remove",
657 registryName: "testName",
658 forceFlag: true,
659 want: []Registry{},
660 },
661 }
662
663 for _, tt := range tests {
664 got, err := handleWithRegistryExist(tt.index, tt.registryList, tt.operation, tt.registryName, tt.forceFlag)
665 if err != nil {
666 t.Logf("Error message is %v", err)
667 }
668
669 if diff := cmp.Diff(tt.want, got); diff != "" {
670 t.Errorf("handleWithRegistryExist() mismatch (-want +got):\n%s", diff)
671 }
672 }
673 }
674
675 func TestGetConsentTelemetry(t *testing.T) {
676 trueValue := true
677 falseValue := false
678
679 tests := []struct {
680 name string
681 existingConfig Preference
682 want bool
683 }{{
684 name: fmt.Sprintf("%s nil", ConsentTelemetrySetting),
685 existingConfig: Preference{},
686 want: false,
687 },
688 {
689 name: fmt.Sprintf("%s true", ConsentTelemetrySetting),
690 existingConfig: Preference{
691 OdoSettings: odoSettings{
692 ConsentTelemetry: &trueValue,
693 },
694 },
695 want: true,
696 },
697 {
698 name: fmt.Sprintf("%s false", ConsentTelemetrySetting),
699 existingConfig: Preference{
700 OdoSettings: odoSettings{
701 ConsentTelemetry: &falseValue,
702 },
703 },
704 want: false,
705 },
706 }
707
708 for _, tt := range tests {
709 t.Run(tt.name, func(t *testing.T) {
710 cfg := preferenceInfo{
711 Preference: tt.existingConfig,
712 }
713 output := cfg.GetConsentTelemetry()
714
715 if output != tt.want {
716 t.Errorf("GetConsentTelemetry returned unexpected value\ngot: %t \nexpected: %t\n", output, tt.want)
717 }
718
719 })
720 }
721 }
722
View as plain text