Skip to content

Commit 664b3f2

Browse files
committed
🌱 clusterctl: Refactor proxy configuration tests
1 parent 841092c commit 664b3f2

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

cmd/clusterctl/client/cluster/proxy_test.go

+42-29
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func TestProxyGetConfig(t *testing.T) {
6868
g.Expect(err).ToNot(HaveOccurred())
6969
defer os.RemoveAll(dir)
7070
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
71-
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
71+
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0o600)).To(Succeed())
7272

7373
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.context})
7474
conf, err := proxy.GetConfig()
@@ -89,32 +89,45 @@ func TestProxyGetConfig(t *testing.T) {
8989
}
9090
})
9191

92-
t.Run("configure timeout", func(t *testing.T) {
93-
g := NewWithT(t)
94-
dir, err := os.MkdirTemp("", "clusterctl")
95-
g.Expect(err).ToNot(HaveOccurred())
96-
defer os.RemoveAll(dir)
97-
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
98-
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
99-
100-
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
101-
conf, err := proxy.GetConfig()
102-
g.Expect(err).ToNot(HaveOccurred())
103-
g.Expect(conf.Timeout.String()).To(Equal("23s"))
104-
})
105-
106-
t.Run("configure warning handler", func(t *testing.T) {
107-
g := NewWithT(t)
108-
dir, err := os.MkdirTemp("", "clusterctl")
109-
g.Expect(err).ToNot(HaveOccurred())
110-
defer os.RemoveAll(dir)
111-
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
112-
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0o600)).To(Succeed())
92+
t.Run("configure option", func(t *testing.T) {
93+
tests := []struct {
94+
name string
95+
option ProxyOption
96+
optionTester func(t *testing.T, r *rest.Config, e error)
97+
}{
98+
{
99+
name: "timeout",
100+
option: InjectProxyTimeout(23 * time.Second),
101+
optionTester: func(t *testing.T, r *rest.Config, e error) {
102+
g := NewWithT(t)
103+
g.Expect(e).ToNot(HaveOccurred())
104+
g.Expect(r.Timeout.String()).To(Equal("23s"))
105+
},
106+
},
107+
{
108+
name: "warning handler",
109+
option: InjectWarningHandler(rest.NoWarnings{}),
110+
optionTester: func(t *testing.T, r *rest.Config, e error) {
111+
g := NewWithT(t)
112+
g.Expect(e).ToNot(HaveOccurred())
113+
g.Expect(r.WarningHandler).To(Equal(rest.NoWarnings{}))
114+
},
115+
},
116+
}
117+
for _, tt := range tests {
118+
t.Run(tt.name, func(t *testing.T) {
119+
g := NewWithT(t)
120+
dir, err := os.MkdirTemp("", "clusterctl")
121+
g.Expect(err).ToNot(HaveOccurred())
122+
defer os.RemoveAll(dir)
123+
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
124+
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0o600)).To(Succeed())
113125

114-
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectWarningHandler(rest.NoWarnings{}))
115-
conf, err := proxy.GetConfig()
116-
g.Expect(err).ToNot(HaveOccurred())
117-
g.Expect(conf.WarningHandler).To(Equal(rest.NoWarnings{}))
126+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, tt.option)
127+
conf, err := proxy.GetConfig()
128+
tt.optionTester(t, conf, err)
129+
})
130+
}
118131
})
119132
}
120133

@@ -136,7 +149,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
136149
g.Expect(err).ToNot(HaveOccurred())
137150
defer os.RemoveAll(dir)
138151
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
139-
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
152+
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0o600)).To(Succeed())
140153

141154
proxy := NewProxy(
142155
// dont't give an explicit path but rather define the file in the
@@ -164,7 +177,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
164177
g.Expect(err).ToNot(HaveOccurred())
165178
defer os.RemoveAll(dir)
166179
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
167-
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
180+
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0o600)).To(Succeed())
168181

169182
proxy := NewProxy(
170183
// dont't give an explicit path but rather define the file in the
@@ -241,7 +254,7 @@ func TestProxyCurrentNamespace(t *testing.T) {
241254
g.Expect(err).ToNot(HaveOccurred())
242255
defer os.RemoveAll(dir)
243256
configFile = filepath.Join(dir, ".test-kubeconfig.yaml")
244-
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
257+
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0o600)).To(Succeed())
245258
}
246259

247260
proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.kubeconfigContext})

0 commit comments

Comments
 (0)