@@ -17,6 +17,8 @@ limitations under the License.
17
17
package conf
18
18
19
19
import (
20
+ "fmt"
21
+ "os"
20
22
"path/filepath"
21
23
"testing"
22
24
@@ -25,15 +27,93 @@ import (
25
27
26
28
var kubeconfig string
27
29
28
- func TestResolveKubeConfigFile (t * testing.T ) {
29
- home := homedir .HomeDir ()
30
+ func TestResolveKubeConfigFileFlag (t * testing.T ) {
30
31
filename := ResolveKubeConfigFile ()
31
-
32
- if filename != filepath .Join (home , "test" , ".kube" , "config" ) {
32
+ if filename != kubeconfigpath {
33
33
t .Errorf ("unexpected config path: %s" , filename )
34
34
}
35
35
}
36
36
37
+ func TestResolveKubeConfigFileEnv (t * testing.T ) {
38
+ clearKubeconfigFlags ()
39
+ defer setKubeconfigFlags ()
40
+
41
+ kubeConfigPath1 := filepath .Join (t .TempDir (), "config" )
42
+ if _ , err := os .Create (kubeConfigPath1 ); err != nil {
43
+ t .Errorf ("failed to create kubeconfig: %v" , err )
44
+ }
45
+
46
+ kubeConfigPath2 := filepath .Join (t .TempDir (), "config" )
47
+ if _ , err := os .Create (kubeConfigPath2 ); err != nil {
48
+ t .Errorf ("failed to create kubeconfig: %v" , err )
49
+ }
50
+
51
+ t .Run ("WithEnvEmpty" , func (t * testing.T ) {
52
+ t .Setenv ("KUBECONFIG" , "" )
53
+
54
+ filename := ResolveKubeConfigFile ()
55
+
56
+ // this will fallback to the true home directory.
57
+ if filename != filepath .Join (homedir .HomeDir (), ".kube" , "config" ) {
58
+ t .Errorf ("unexpected config path: %s" , filename )
59
+ }
60
+ })
61
+
62
+ t .Run ("WithEnvPath" , func (t * testing.T ) {
63
+ t .Setenv ("KUBECONFIG" , kubeConfigPath1 )
64
+
65
+ filename := ResolveKubeConfigFile ()
66
+
67
+ if filename != kubeConfigPath1 {
68
+ t .Errorf ("unexpected config path: %s" , filename )
69
+ }
70
+ })
71
+
72
+ t .Run ("WithEnvPathListAllExist" , func (t * testing.T ) {
73
+ t .Setenv ("KUBECONFIG" , fmt .Sprintf ("%s:%s" , kubeConfigPath1 , kubeConfigPath2 ))
74
+
75
+ filename := ResolveKubeConfigFile ()
76
+
77
+ // if all exist then it will take the first.
78
+ if filename != kubeConfigPath1 {
79
+ t .Errorf ("unexpected config path: %s" , filename )
80
+ }
81
+ })
82
+
83
+ t .Run ("WithEnvPathListFirstExists" , func (t * testing.T ) {
84
+ t .Setenv ("KUBECONFIG" , fmt .Sprintf ("%s:fake" , kubeConfigPath1 ))
85
+
86
+ filename := ResolveKubeConfigFile ()
87
+
88
+ // if first exists then it will take the first.
89
+ if filename != kubeConfigPath1 {
90
+ t .Errorf ("unexpected config path: %s" , filename )
91
+ }
92
+ })
93
+
94
+ t .Run ("WithEnvPathListLastExists" , func (t * testing.T ) {
95
+ t .Setenv ("KUBECONFIG" , fmt .Sprintf ("%s:fake" , kubeConfigPath1 ))
96
+
97
+ filename := ResolveKubeConfigFile ()
98
+
99
+ // if only last exists then it will take the last.
100
+ if filename != kubeConfigPath1 {
101
+ t .Errorf ("unexpected config path: %s" , filename )
102
+ }
103
+ })
104
+
105
+ t .Run ("WithEnvPathListNoneExist" , func (t * testing.T ) {
106
+ t .Setenv ("KUBECONFIG" , "fake-foo:fake-bar" )
107
+
108
+ filename := ResolveKubeConfigFile ()
109
+
110
+ // if none exist then it will take the last.
111
+ if filename != "fake-bar" {
112
+ t .Errorf ("unexpected config path: %s" , filename )
113
+ }
114
+ })
115
+ }
116
+
37
117
func TestNew (t * testing.T ) {
38
118
cfg , err := New (ResolveKubeConfigFile ())
39
119
if err != nil {
0 commit comments