@@ -24,6 +24,7 @@ import (
24
24
25
25
. "github.com/onsi/ginkgo"
26
26
. "github.com/onsi/gomega"
27
+ "k8s.io/client-go/rest"
27
28
"k8s.io/client-go/tools/clientcmd"
28
29
)
29
30
@@ -42,14 +43,6 @@ var _ = Describe("Config", func() {
42
43
43
44
origRecommendedHomeFile := clientcmd .RecommendedHomeFile
44
45
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
46
BeforeEach (func () {
54
47
// create temporary directory for test case
55
48
var err error
@@ -71,6 +64,21 @@ var _ = Describe("Config", func() {
71
64
})
72
65
73
66
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
+
74
82
Context ("when kubeconfig files don't exist" , func () {
75
83
It ("should fail" , func () {
76
84
cfg , err := GetConfigWithContext ("" )
@@ -79,7 +87,44 @@ var _ = Describe("Config", func() {
79
87
})
80
88
})
81
89
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
+ }
83
128
BeforeEach (func () {
84
129
err := createFiles (kubeconfigFiles , dir )
85
130
Expect (err ).NotTo (HaveOccurred ())
@@ -93,7 +138,7 @@ var _ = Describe("Config", func() {
93
138
{
94
139
text : "should use the envvar" ,
95
140
kubeconfigEnv : []string {"kubeconfig-multi-context" },
96
- wantHost : "ctx -1" ,
141
+ wantHost : "from-multi-env -1" ,
97
142
},
98
143
{
99
144
text : "should use the recommended home file" ,
@@ -106,9 +151,9 @@ var _ = Describe("Config", func() {
106
151
wantHost : "from-flag" ,
107
152
},
108
153
{
109
- text : "should prefer the envar over the recommended home file" ,
154
+ text : "should prefer the envvar over the recommended home file" ,
110
155
kubeconfigEnv : []string {"kubeconfig-multi-context" },
111
- wantHost : "ctx -1" ,
156
+ wantHost : "from-multi-env -1" ,
112
157
},
113
158
{
114
159
text : "should allow overriding the API server URL" ,
@@ -118,9 +163,9 @@ var _ = Describe("Config", func() {
118
163
},
119
164
{
120
165
text : "should allow overriding the context" ,
121
- context : "ctx -2" ,
166
+ context : "from-multi-env -2" ,
122
167
kubeconfigEnv : []string {"kubeconfig-multi-context" },
123
- wantHost : "ctx -2" ,
168
+ wantHost : "from-multi-env -2" ,
124
169
},
125
170
{
126
171
text : "should support a multi-value envvar" ,
@@ -129,19 +174,7 @@ var _ = Describe("Config", func() {
129
174
wantHost : "from-env-2" ,
130
175
},
131
176
}
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 )
145
178
})
146
179
})
147
180
})
0 commit comments