-
Notifications
You must be signed in to change notification settings - Fork 320
Expand file tree
/
Copy pathupgrade_test.go
More file actions
183 lines (164 loc) · 4.48 KB
/
upgrade_test.go
File metadata and controls
183 lines (164 loc) · 4.48 KB
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
package cmd
import (
"os"
"path/filepath"
"testing"
)
func TestIsRemoteAccessAllowed(t *testing.T) {
cases := []struct {
name string
cmd diffCmd
expected bool
}{
{
name: "no flags",
cmd: diffCmd{
dryRunMode: "none",
},
expected: true,
},
{
name: "legacy explicit dry-run=true flag",
cmd: diffCmd{
dryRunMode: "true",
},
expected: false,
},
{
name: "legacy explicit dry-run=false flag",
cmd: diffCmd{
dryRunMode: "false",
},
expected: true,
},
{
name: "legacy empty dry-run flag",
cmd: diffCmd{
dryRunMode: dryRunNoOptDefVal,
},
expected: false,
},
{
name: "server-side dry-run flag",
cmd: diffCmd{
dryRunMode: "server",
},
expected: true,
},
{
name: "client-side dry-run flag",
cmd: diffCmd{
dryRunMode: "client",
},
expected: false,
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
actual := tc.cmd.clusterAccessAllowed()
if actual != tc.expected {
t.Errorf("Expected %v, got %v", tc.expected, actual)
}
})
}
}
func TestPrepareEnvSettings_MultiFileKubeconfig(t *testing.T) {
original := os.Getenv("KUBECONFIG")
defer os.Setenv("KUBECONFIG", original)
cases := []struct {
name string
kubeconfig string
kubeContext string
wantKubeConfig string
wantKubeContext string
}{
{
name: "single file kubeconfig is preserved",
kubeconfig: "/path/to/config",
kubeContext: "",
wantKubeConfig: "/path/to/config",
wantKubeContext: "",
},
{
name: "multi-file kubeconfig is cleared",
kubeconfig: "/path/to/file1" + string(filepath.ListSeparator) + "/path/to/file2",
kubeContext: "",
wantKubeConfig: "",
wantKubeContext: "",
},
{
name: "multi-file kubeconfig with three files is cleared",
kubeconfig: "/a" + string(filepath.ListSeparator) + "/b" + string(filepath.ListSeparator) + "/c",
kubeContext: "",
wantKubeConfig: "",
wantKubeContext: "",
},
{
name: "empty kubeconfig is preserved",
kubeconfig: "",
kubeContext: "",
wantKubeConfig: "",
wantKubeContext: "",
},
{
name: "kube-context override is applied",
kubeconfig: "/path/to/config",
kubeContext: "my-context",
wantKubeConfig: "/path/to/config",
wantKubeContext: "my-context",
},
{
name: "multi-file kubeconfig with kube-context override",
kubeconfig: "/path/to/file1" + string(filepath.ListSeparator) + "/path/to/file2",
kubeContext: "my-context",
wantKubeConfig: "",
wantKubeContext: "my-context",
},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
os.Setenv("KUBECONFIG", tc.kubeconfig)
env := prepareEnvSettings(tc.kubeContext)
if env.KubeConfig != tc.wantKubeConfig {
t.Errorf("KubeConfig: got %q, want %q", env.KubeConfig, tc.wantKubeConfig)
}
if env.KubeContext != tc.wantKubeContext {
t.Errorf("KubeContext: got %q, want %q", env.KubeContext, tc.wantKubeContext)
}
})
}
}
func TestPrepareEnvSettings_ConfigFlagsPointToCorrectFields(t *testing.T) {
original := os.Getenv("KUBECONFIG")
defer os.Setenv("KUBECONFIG", original)
t.Run("config flags reflect kube-context override", func(t *testing.T) {
os.Setenv("KUBECONFIG", "/some/config")
env := prepareEnvSettings("my-override-context")
if env.KubeContext != "my-override-context" {
t.Errorf("env.KubeContext = %q, want %q", env.KubeContext, "my-override-context")
}
})
t.Run("multi-file kubeconfig does not set ExplicitPath", func(t *testing.T) {
multiPath := "/tmp/file1" + string(filepath.ListSeparator) + "/tmp/file2"
os.Setenv("KUBECONFIG", multiPath)
env := prepareEnvSettings("")
if env.KubeConfig != "" {
t.Errorf("env.KubeConfig = %q, want empty string for multi-file KUBECONFIG", env.KubeConfig)
}
getter := env.RESTClientGetter()
rawConfig := getter.ToRawKubeConfigLoader()
loadingRules := rawConfig.ConfigAccess()
if loadingRules != nil {
if explicitPath := loadingRules.GetExplicitFile(); explicitPath != "" {
t.Errorf("ExplicitPath = %q, want empty string for multi-file KUBECONFIG", explicitPath)
}
}
})
t.Run("single file kubeconfig preserves ExplicitPath", func(t *testing.T) {
os.Setenv("KUBECONFIG", "/tmp/single-config")
env := prepareEnvSettings("")
if env.KubeConfig != "/tmp/single-config" {
t.Errorf("env.KubeConfig = %q, want %q", env.KubeConfig, "/tmp/single-config")
}
})
}