1 package segment
2
3 import (
4 "context"
5 "os"
6 "testing"
7
8 "k8s.io/utils/pointer"
9
10 "github.com/redhat-developer/odo/pkg/config"
11 envcontext "github.com/redhat-developer/odo/pkg/config/context"
12 "github.com/redhat-developer/odo/pkg/preference"
13 scontext "github.com/redhat-developer/odo/pkg/segment/context"
14 )
15
16 func TestGetRegistryOptions(t *testing.T) {
17 tempConfigFile, err := os.CreateTemp("", "odoconfig")
18 if err != nil {
19 t.Fatal(err)
20 }
21 err = tempConfigFile.Close()
22 if err != nil {
23 t.Fatal(err)
24 }
25 defer os.Remove(tempConfigFile.Name())
26
27 t.Setenv(preference.GlobalConfigEnvName, tempConfigFile.Name())
28
29 type want struct {
30 localeUserEmpty bool
31 skipTLSVerify bool
32 caller string
33 }
34 tests := []struct {
35 testName string
36 consent bool
37 telemetryFile bool
38 caller string
39 cfg preference.Client
40 want want
41 }{
42 {
43 testName: "Registry options with telemetry consent and telemetry file",
44 consent: true,
45 telemetryFile: true,
46 want: want{
47 localeUserEmpty: true,
48 skipTLSVerify: false,
49 caller: "odo",
50 },
51 },
52 {
53 testName: "Registry options with telemetry consent and no telemetry file",
54 consent: true,
55 telemetryFile: false,
56 want: want{
57 localeUserEmpty: false,
58 skipTLSVerify: false,
59 caller: "odo",
60 },
61 },
62
63 {
64 testName: "Registry options without telemetry consent and telemetry file",
65 consent: false,
66 telemetryFile: true,
67 want: want{
68 localeUserEmpty: true,
69 skipTLSVerify: false,
70 caller: "odo",
71 },
72 },
73 {
74 testName: "Registry options without telemetry consent and no telemetry file",
75 consent: false,
76 telemetryFile: false,
77 want: want{
78 localeUserEmpty: true,
79 skipTLSVerify: false,
80 caller: "odo",
81 },
82 },
83 {
84 testName: "Registry options without telemetry consent and no telemetry file, with caller",
85 consent: false,
86 telemetryFile: false,
87 caller: "vscode",
88 want: want{
89 localeUserEmpty: true,
90 skipTLSVerify: false,
91 caller: "odo-vscode",
92 },
93 },
94 }
95
96 for _, tt := range tests {
97 t.Run(tt.testName, func(t *testing.T) {
98 ctx := scontext.NewContext(context.Background())
99 var envConfig config.Configuration
100 if tt.telemetryFile {
101 envConfig.OdoDebugTelemetryFile = pointer.String("/a/telemetry/file")
102 }
103 if tt.caller != "" {
104 envConfig.TelemetryCaller = tt.caller
105 }
106 ctx = envcontext.WithEnvConfig(ctx, envConfig)
107 scontext.SetTelemetryStatus(ctx, tt.consent)
108
109 ro := GetRegistryOptions(ctx)
110
111 if len(ro.Telemetry.Locale) == 0 != tt.want.localeUserEmpty || len(ro.Telemetry.User) == 0 != tt.want.localeUserEmpty {
112 t.Errorf("Locale %q and User %q emptiness should be %v when telemetry enabled is %v and telemetry file is %v", ro.Telemetry.Locale, ro.Telemetry.User, tt.want.localeUserEmpty, tt.consent, tt.telemetryFile)
113 }
114
115 if ro.SkipTLSVerify != tt.want.skipTLSVerify {
116 t.Errorf("SkipTLSVerify should be set to %v by default", tt.want.skipTLSVerify)
117 }
118
119 if ro.Telemetry.Client != tt.want.caller {
120 t.Errorf("caller should be %q but is %q", tt.want.caller, ro.Telemetry.Client)
121 }
122 })
123 }
124 }
125
View as plain text