@@ -24,6 +24,7 @@ import (
2424
2525 . "github.com/onsi/ginkgo"
2626 . "github.com/onsi/gomega"
27+ "k8s.io/client-go/rest"
2728 "k8s.io/client-go/tools/clientcmd"
2829)
2930
@@ -42,14 +43,6 @@ var _ = Describe("Config", func() {
4243
4344 origRecommendedHomeFile := clientcmd .RecommendedHomeFile
4445
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-
5346 BeforeEach (func () {
5447 // create temporary directory for test case
5548 var err error
@@ -71,6 +64,21 @@ var _ = Describe("Config", func() {
7164 })
7265
7366 Describe ("GetConfigWithContext" , func () {
67+ defineTests := func (testCases []testCase ) {
68+ for _ , testCase := range testCases {
69+ tc := testCase
70+ It (tc .text , func () {
71+ // set global and environment configs
72+ setConfigs (tc , dir )
73+
74+ // run the test
75+ cfg , err := GetConfigWithContext (tc .context )
76+ Expect (err ).NotTo (HaveOccurred ())
77+ Expect (cfg .Host ).To (Equal (tc .wantHost ))
78+ })
79+ }
80+ }
81+
7482 Context ("when kubeconfig files don't exist" , func () {
7583 It ("should fail" , func () {
7684 cfg , err := GetConfigWithContext ("" )
@@ -79,7 +87,44 @@ var _ = Describe("Config", func() {
7987 })
8088 })
8189
82- Context ("when kubeconfig files exist" , func () {
90+ Context ("when in-cluster" , func () {
91+ kubeconfigFiles := map [string ]string {
92+ "kubeconfig-multi-context" : genKubeconfig ("from-multi-env-1" , "from-multi-env-2" ),
93+ ".kubeconfig" : genKubeconfig ("from-home" ),
94+ }
95+ BeforeEach (func () {
96+ err := createFiles (kubeconfigFiles , dir )
97+ Expect (err ).NotTo (HaveOccurred ())
98+
99+ // override in-cluster config loader
100+ loadInClusterConfig = func () (* rest.Config , error ) {
101+ return & rest.Config {Host : "from-in-cluster" }, nil
102+ }
103+ })
104+ AfterEach (func () { loadInClusterConfig = rest .InClusterConfig })
105+
106+ testCases := []testCase {
107+ {
108+ text : "should prefer the envvar over the in-cluster config" ,
109+ kubeconfigEnv : []string {"kubeconfig-multi-context" },
110+ wantHost : "from-multi-env-1" ,
111+ },
112+ {
113+ text : "should prefer in-cluster over the recommended home file" ,
114+ wantHost : "from-in-cluster" ,
115+ },
116+ }
117+ defineTests (testCases )
118+ })
119+
120+ Context ("when outside the cluster" , func () {
121+ kubeconfigFiles := map [string ]string {
122+ "kubeconfig-flag" : genKubeconfig ("from-flag" ),
123+ "kubeconfig-multi-context" : genKubeconfig ("from-multi-env-1" , "from-multi-env-2" ),
124+ "kubeconfig-env-1" : genKubeconfig ("from-env-1" ),
125+ "kubeconfig-env-2" : genKubeconfig ("from-env-2" ),
126+ ".kubeconfig" : genKubeconfig ("from-home" ),
127+ }
83128 BeforeEach (func () {
84129 err := createFiles (kubeconfigFiles , dir )
85130 Expect (err ).NotTo (HaveOccurred ())
@@ -93,7 +138,7 @@ var _ = Describe("Config", func() {
93138 {
94139 text : "should use the envvar" ,
95140 kubeconfigEnv : []string {"kubeconfig-multi-context" },
96- wantHost : "ctx -1" ,
141+ wantHost : "from-multi-env -1" ,
97142 },
98143 {
99144 text : "should use the recommended home file" ,
@@ -106,9 +151,9 @@ var _ = Describe("Config", func() {
106151 wantHost : "from-flag" ,
107152 },
108153 {
109- text : "should prefer the envar over the recommended home file" ,
154+ text : "should prefer the envvar over the recommended home file" ,
110155 kubeconfigEnv : []string {"kubeconfig-multi-context" },
111- wantHost : "ctx -1" ,
156+ wantHost : "from-multi-env -1" ,
112157 },
113158 {
114159 text : "should allow overriding the API server URL" ,
@@ -118,9 +163,9 @@ var _ = Describe("Config", func() {
118163 },
119164 {
120165 text : "should allow overriding the context" ,
121- context : "ctx -2" ,
166+ context : "from-multi-env -2" ,
122167 kubeconfigEnv : []string {"kubeconfig-multi-context" },
123- wantHost : "ctx -2" ,
168+ wantHost : "from-multi-env -2" ,
124169 },
125170 {
126171 text : "should support a multi-value envvar" ,
@@ -129,19 +174,7 @@ var _ = Describe("Config", func() {
129174 wantHost : "from-env-2" ,
130175 },
131176 }
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- }
177+ defineTests (testCases )
145178 })
146179 })
147180})
0 commit comments