Skip to content

Commit

Permalink
🌱 clusterctl: Refactor proxy configuration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipovetsky committed Sep 6, 2024
1 parent 841092c commit 142cbb2
Showing 1 changed file with 40 additions and 25 deletions.
65 changes: 40 additions & 25 deletions cmd/clusterctl/client/cluster/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,32 +89,47 @@ func TestProxyGetConfig(t *testing.T) {
}
})

t.Run("configure timeout", func(t *testing.T) {
g := NewWithT(t)
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())

proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectProxyTimeout(23*time.Second))
conf, err := proxy.GetConfig()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(conf.Timeout.String()).To(Equal("23s"))
})

t.Run("configure warning handler", func(t *testing.T) {
g := NewWithT(t)
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0o600)).To(Succeed())
t.Run("configure option", func(t *testing.T) {
tests := []struct {
name string
option ProxyOption
optionTester func(t *testing.T, r *rest.Config, e error)
}{
{
name: "timeout",
option: InjectProxyTimeout(23 * time.Second),
optionTester: func(t *testing.T, r *rest.Config, e error) {
t.Helper()
g := NewWithT(t)
g.Expect(e).ToNot(HaveOccurred())
g.Expect(r.Timeout.String()).To(Equal("23s"))
},
},
{
name: "warning handler",
option: InjectWarningHandler(rest.NoWarnings{}),
optionTester: func(t *testing.T, r *rest.Config, e error) {
t.Helper()
g := NewWithT(t)
g.Expect(e).ToNot(HaveOccurred())
g.Expect(r.WarningHandler).To(Equal(rest.NoWarnings{}))
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
g := NewWithT(t)
dir, err := os.MkdirTemp("", "clusterctl")
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(kubeconfig("management", "default")), 0600)).To(Succeed())

proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, InjectWarningHandler(rest.NoWarnings{}))
conf, err := proxy.GetConfig()
g.Expect(err).ToNot(HaveOccurred())
g.Expect(conf.WarningHandler).To(Equal(rest.NoWarnings{}))
proxy := NewProxy(Kubeconfig{Path: configFile, Context: "management"}, tt.option)
conf, err := proxy.GetConfig()
tt.optionTester(t, conf, err)
})
}
})
}

Expand Down

0 comments on commit 142cbb2

Please sign in to comment.