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 664b3f2
Showing 1 changed file with 42 additions and 29 deletions.
71 changes: 42 additions & 29 deletions cmd/clusterctl/client/cluster/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestProxyGetConfig(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(tt.kubeconfigContents), 0o600)).To(Succeed())

proxy := NewProxy(Kubeconfig{Path: configFile, Context: tt.context})
conf, err := proxy.GetConfig()
Expand All @@ -89,32 +89,45 @@ 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) {

Check failure on line 101 in cmd/clusterctl/client/cluster/proxy_test.go

View workflow job for this annotation

GitHub Actions / lint

test helper function should start from t.Helper() (thelper)
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) {

Check failure on line 110 in cmd/clusterctl/client/cluster/proxy_test.go

View workflow job for this annotation

GitHub Actions / lint

test helper function should start from t.Helper() (thelper)
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")), 0o600)).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 All @@ -136,7 +149,7 @@ func TestKUBECONFIGEnvVar(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
defer os.RemoveAll(dir)
configFile := filepath.Join(dir, ".test-kubeconfig.yaml")
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0600)).To(Succeed())
g.Expect(os.WriteFile(configFile, []byte(kubeconfigContents), 0o600)).To(Succeed())

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

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

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

0 comments on commit 664b3f2

Please sign in to comment.