Skip to content

Commit c830b03

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

File tree

2 files changed

+256
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)