Skip to content

Commit 142cbb2

Browse files
committed
🌱 clusterctl: Refactor proxy configuration tests
1 parent 841092c commit 142cbb2

File tree

1 file changed

+40
-25
lines changed

1 file changed

+40
-25
lines changed

cmd/clusterctl/client/cluster/proxy_test.go

+40-25
Original file line numberDiff line numberDiff line change
@@ -89,32 +89,47 @@ 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+
t.Helper()
103+
g := NewWithT(t)
104+
g.Expect(e).ToNot(HaveOccurred())
105+
g.Expect(r.Timeout.String()).To(Equal("23s"))
106+
},
107+
},
108+
{
109+
name: "warning handler",
110+
option: InjectWarningHandler(rest.NoWarnings{}),
111+
optionTester: func(t *testing.T, r *rest.Config, e error) {
112+
t.Helper()
113+
g := NewWithT(t)
114+
g.Expect(e).ToNot(HaveOccurred())
115+
g.Expect(r.WarningHandler).To(Equal(rest.NoWarnings{}))
116+
},
117+
},
118+
}
119+
for _, tt := range tests {
120+
t.Run(tt.name, func(t *testing.T) {
121+
g := NewWithT(t)
122+
dir, err := os.MkdirTemp("", "clusterctl")
123+
g.Expect(err).ToNot(HaveOccurred())
124+
defer os.RemoveAll(dir)
125+
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
126+
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())
113127

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{}))
128+
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, tt.option)
129+
conf, err := proxy.GetConfig()
130+
tt.optionTester(t, conf, err)
131+
})
132+
}
118133
})
119134
}
120135

0 commit comments

Comments
 (0)