@@ -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,95 @@ 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
+ // NOTE: not considered safe to run in parallel with other tests thats
39
+ // require the --kubeconfig and --kubecontext flags.
40
+ clearKubeconfigFlags ()
41
+ defer setKubeconfigFlags ()
42
+
43
+ kubeConfigPath1 := filepath .Join (t .TempDir (), "config" )
44
+ if _ , err := os .Create (kubeConfigPath1 ); err != nil {
45
+ t .Errorf ("failed to create kubeconfig: %v" , err )
46
+ }
47
+
48
+ kubeConfigPath2 := filepath .Join (t .TempDir (), "config" )
49
+ if _ , err := os .Create (kubeConfigPath2 ); err != nil {
50
+ t .Errorf ("failed to create kubeconfig: %v" , err )
51
+ }
52
+
53
+ t .Run ("WithEnvEmpty" , func (t * testing.T ) {
54
+ t .Setenv ("KUBECONFIG" , "" )
55
+
56
+ filename := ResolveKubeConfigFile ()
57
+
58
+ // this will fallback to the true home directory.
59
+ if filename != filepath .Join (homedir .HomeDir (), ".kube" , "config" ) {
60
+ t .Errorf ("unexpected config path: %s" , filename )
61
+ }
62
+ })
63
+
64
+ t .Run ("WithEnvPath" , func (t * testing.T ) {
65
+ t .Setenv ("KUBECONFIG" , kubeConfigPath1 )
66
+
67
+ filename := ResolveKubeConfigFile ()
68
+
69
+ if filename != kubeConfigPath1 {
70
+ t .Errorf ("unexpected config path: %s" , filename )
71
+ }
72
+ })
73
+
74
+ t .Run ("WithEnvPathListAllExist" , func (t * testing.T ) {
75
+ t .Setenv ("KUBECONFIG" , fmt .Sprintf ("%s:%s" , kubeConfigPath1 , kubeConfigPath2 ))
76
+
77
+ filename := ResolveKubeConfigFile ()
78
+
79
+ // if all exist then it will take the first.
80
+ if filename != kubeConfigPath1 {
81
+ t .Errorf ("unexpected config path: %s" , filename )
82
+ }
83
+ })
84
+
85
+ t .Run ("WithEnvPathListFirstExists" , func (t * testing.T ) {
86
+ t .Setenv ("KUBECONFIG" , fmt .Sprintf ("%s:fake" , kubeConfigPath1 ))
87
+
88
+ filename := ResolveKubeConfigFile ()
89
+
90
+ // if first exists then it will take the first.
91
+ if filename != kubeConfigPath1 {
92
+ t .Errorf ("unexpected config path: %s" , filename )
93
+ }
94
+ })
95
+
96
+ t .Run ("WithEnvPathListLastExists" , func (t * testing.T ) {
97
+ t .Setenv ("KUBECONFIG" , fmt .Sprintf ("%s:fake" , kubeConfigPath1 ))
98
+
99
+ filename := ResolveKubeConfigFile ()
100
+
101
+ // if only last exists then it will take the last.
102
+ if filename != kubeConfigPath1 {
103
+ t .Errorf ("unexpected config path: %s" , filename )
104
+ }
105
+ })
106
+
107
+ t .Run ("WithEnvPathListNoneExist" , func (t * testing.T ) {
108
+ t .Setenv ("KUBECONFIG" , "fake-foo:fake-bar" )
109
+
110
+ filename := ResolveKubeConfigFile ()
111
+
112
+ // if none exist then it will take the last.
113
+ if filename != "fake-bar" {
114
+ t .Errorf ("unexpected config path: %s" , filename )
115
+ }
116
+ })
117
+ }
118
+
37
119
func TestNew (t * testing.T ) {
38
120
cfg , err := New (ResolveKubeConfigFile ())
39
121
if err != nil {
0 commit comments