Skip to content

Commit c8d90fe

Browse files
committed
pkg/client/config: add tests
1 parent ca33b65 commit c8d90fe

File tree

2 files changed

+249
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)