Skip to content

Commit abe9fcd

Browse files
committed
pkg/client/config: add tests
1 parent 16c93b0 commit abe9fcd

File tree

2 files changed

+240
-0
lines changed

2 files changed

+240
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
Copyright 2018 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package config
18+
19+
import (
20+
"testing"
21+
22+
. "github.com/onsi/ginkgo"
23+
. "github.com/onsi/gomega"
24+
25+
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
26+
logf "sigs.k8s.io/controller-runtime/pkg/log"
27+
"sigs.k8s.io/controller-runtime/pkg/log/zap"
28+
)
29+
30+
func TestConfig(t *testing.T) {
31+
RegisterFailHandler(Fail)
32+
RunSpecsWithDefaultAndCustomReporters(t, "Client Config Test Suite", []Reporter{printer.NewlineReporter{}})
33+
}
34+
35+
var _ = BeforeSuite(func(done Done) {
36+
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true))
37+
38+
close(done)
39+
}, 60)

pkg/client/config/config_test.go

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
package config
2+
3+
import (
4+
"io/ioutil"
5+
"os"
6+
"path/filepath"
7+
"strings"
8+
9+
. "github.com/onsi/ginkgo"
10+
. "github.com/onsi/gomega"
11+
"k8s.io/client-go/tools/clientcmd"
12+
)
13+
14+
type testCase struct {
15+
text string
16+
apiServerURL string
17+
context string
18+
kubeconfigFlag string
19+
kubeconfigEnv []string
20+
wantHost string
21+
}
22+
23+
var _ = Describe("Config", func() {
24+
25+
var dir string
26+
27+
kubeconfigFiles := map[string]string{
28+
"kubeconfig-flag": genKubeconfig("from-flag"),
29+
"kubeconfig-multi-context": genKubeconfig("ctx-1", "ctx-2"),
30+
"kubeconfig-env-1": genKubeconfig("from-env-1"),
31+
"kubeconfig-env-2": genKubeconfig("from-env-2"),
32+
".kubeconfig": genKubeconfig("from-home"),
33+
}
34+
35+
BeforeEach(func(done Done) {
36+
os.Unsetenv(clientcmd.RecommendedConfigPathEnvVar)
37+
kubeconfig = ""
38+
apiServerURL = ""
39+
40+
// create temporary directory for test case
41+
var err error
42+
dir, err = ioutil.TempDir("", "cr-test")
43+
Expect(err).NotTo(HaveOccurred())
44+
45+
// override $HOME/.kube/config
46+
clientcmd.RecommendedHomeFile = filepath.Join(dir, ".kubeconfig")
47+
48+
close(done)
49+
})
50+
51+
AfterEach(func(done Done) {
52+
err := os.RemoveAll(dir)
53+
Expect(err).NotTo(HaveOccurred())
54+
55+
close(done)
56+
})
57+
58+
Describe("GetConfigWithContext", func() {
59+
Context("when kubeconfig files don't exist", func() {
60+
It("should fail", func(done Done) {
61+
cfg, err := GetConfigWithContext("")
62+
Expect(cfg).To(BeNil())
63+
Expect(err).To(HaveOccurred())
64+
65+
close(done)
66+
})
67+
})
68+
69+
Context("when kubeconfig files exist", func() {
70+
BeforeEach(func(done Done) {
71+
err := createFiles(kubeconfigFiles, dir)
72+
Expect(err).NotTo(HaveOccurred())
73+
74+
close(done)
75+
})
76+
testCases := []testCase{
77+
{
78+
text: "should use the --kubeconfig flag",
79+
kubeconfigFlag: "kubeconfig-flag",
80+
wantHost: "from-flag",
81+
},
82+
{
83+
text: "should use the envvar",
84+
kubeconfigEnv: []string{"kubeconfig-multi-context"},
85+
wantHost: "ctx-1",
86+
},
87+
{
88+
text: "should use the recommended home file",
89+
wantHost: "from-home",
90+
},
91+
{
92+
text: "should prefer the flag over the envvar",
93+
kubeconfigFlag: "kubeconfig-flag",
94+
kubeconfigEnv: []string{"kubeconfig-multi-context"},
95+
wantHost: "from-flag",
96+
},
97+
{
98+
text: "should prefer the envar over the recommended home file",
99+
kubeconfigEnv: []string{"kubeconfig-multi-context"},
100+
wantHost: "ctx-1",
101+
},
102+
{
103+
text: "should allow overriding the API server URL",
104+
apiServerURL: "override",
105+
kubeconfigEnv: []string{"kubeconfig-multi-context"},
106+
wantHost: "override",
107+
},
108+
{
109+
text: "should allow overriding the context",
110+
context: "ctx-2",
111+
kubeconfigEnv: []string{"kubeconfig-multi-context"},
112+
wantHost: "ctx-2",
113+
},
114+
{
115+
text: "should support a multi-value envvar",
116+
context: "from-env-2",
117+
kubeconfigEnv: []string{"kubeconfig-env-1", "kubeconfig-env-2"},
118+
wantHost: "from-env-2",
119+
},
120+
}
121+
122+
for _, testCase := range testCases {
123+
tc := testCase
124+
It(tc.text, func(done Done) {
125+
// set global and environment configs
126+
setConfigs(tc, dir)
127+
128+
// run the test
129+
cfg, err := GetConfigWithContext(tc.context)
130+
Expect(err).NotTo(HaveOccurred())
131+
Expect(cfg.Host).To(Equal(tc.wantHost))
132+
133+
close(done)
134+
})
135+
}
136+
})
137+
})
138+
})
139+
140+
func setConfigs(tc testCase, dir string) {
141+
// Set API Server URL
142+
apiServerURL = tc.apiServerURL
143+
144+
// Set kubeconfig flag value
145+
if len(tc.kubeconfigFlag) > 0 {
146+
kubeconfig = filepath.Join(dir, tc.kubeconfigFlag)
147+
}
148+
149+
// Set KUBECONFIG env value
150+
if len(tc.kubeconfigEnv) > 0 {
151+
kubeconfigEnvPaths := []string{}
152+
for _, k := range tc.kubeconfigEnv {
153+
kubeconfigEnvPaths = append(kubeconfigEnvPaths, filepath.Join(dir, k))
154+
}
155+
os.Setenv(clientcmd.RecommendedConfigPathEnvVar, strings.Join(kubeconfigEnvPaths, ":"))
156+
}
157+
}
158+
159+
func createFiles(files map[string]string, dir string) error {
160+
for path, data := range files {
161+
if err := ioutil.WriteFile(filepath.Join(dir, path), []byte(data), 0644); err != nil {
162+
return err
163+
}
164+
}
165+
return nil
166+
}
167+
168+
func genKubeconfig(contexts ...string) string {
169+
var sb strings.Builder
170+
sb.WriteString(`---
171+
apiVersion: v1
172+
kind: Config
173+
clusters:
174+
`)
175+
for _, ctx := range contexts {
176+
sb.WriteString(`- cluster:
177+
server: ` + ctx + `
178+
name: ` + ctx + `
179+
`)
180+
}
181+
sb.WriteString("contexts:\n")
182+
for _, ctx := range contexts {
183+
sb.WriteString(`- context:
184+
cluster: ` + ctx + `
185+
user: ` + ctx + `
186+
name: ` + ctx + `
187+
`)
188+
}
189+
190+
sb.WriteString("users:\n")
191+
for _, ctx := range contexts {
192+
sb.WriteString(`- name: ` + ctx + `
193+
`)
194+
}
195+
sb.WriteString("preferences: {}\n")
196+
if len(contexts) > 0 {
197+
sb.WriteString("current-context: " + contexts[0] + "\n")
198+
}
199+
200+
return sb.String()
201+
}

0 commit comments

Comments
 (0)