@@ -8,7 +8,10 @@ import (
8
8
"path"
9
9
"testing"
10
10
11
+ "github.com/ninech/nctl/api"
12
+ "github.com/ninech/nctl/internal/format"
11
13
"github.com/ninech/nctl/internal/test"
14
+ "github.com/stretchr/testify/require"
12
15
"k8s.io/client-go/tools/clientcmd"
13
16
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
14
17
)
@@ -19,7 +22,19 @@ func (f *fakeTokenGetter) GetTokenString(ctx context.Context, issuerURL, clientI
19
22
return test .FakeJWTToken , nil
20
23
}
21
24
25
+ func checkErrorRequire (t * testing.T , err error , expectError bool , expectedErrMsg string ) {
26
+ t .Helper ()
27
+ if expectError {
28
+ require .Error (t , err , "expected an error but got none" )
29
+ require .EqualError (t , err , expectedErrMsg , "unexpected error message" )
30
+ } else {
31
+ require .NoError (t , err , "expected no error but got one" )
32
+ }
33
+ }
34
+
22
35
func TestLoginCmd (t * testing.T ) {
36
+ ctx := format .WithForceInteractiveEnvironment (context .Background (), true )
37
+
23
38
// write our "existing" kubeconfig to a temp kubeconfig
24
39
kubeconfig , err := os .CreateTemp ("" , "*-kubeconfig.yaml" )
25
40
if err != nil {
@@ -39,7 +54,7 @@ func TestLoginCmd(t *testing.T) {
39
54
APIURL : "https://" + apiHost ,
40
55
IssuerURL : "https://auth.example.org" ,
41
56
}
42
- if err := cmd .Run (context . Background () , "" , tk ); err != nil {
57
+ if err := cmd .Run (ctx , "" , tk ); err != nil {
43
58
t .Fatal (err )
44
59
}
45
60
@@ -58,46 +73,83 @@ func TestLoginCmd(t *testing.T) {
58
73
}
59
74
60
75
func TestLoginStaticToken (t * testing.T ) {
61
- kubeconfig , err := os .CreateTemp ("" , "*-kubeconfig.yaml" )
62
- if err != nil {
63
- log .Fatal (err )
64
- }
65
- defer os .Remove (kubeconfig .Name ())
66
-
67
- os .Setenv (clientcmd .RecommendedConfigPathEnvVar , kubeconfig .Name ())
68
-
69
76
apiHost := "api.example.org"
70
- token := test .FakeJWTToken
71
77
72
- cmd := & LoginCmd {APIURL : "https://" + apiHost , APIToken : token , Organization : "test" }
73
- tk := & fakeTokenGetter {}
74
- if err := cmd .Run (context .Background (), "" , tk ); err != nil {
75
- t .Fatal (err )
76
- }
77
-
78
- // read out the kubeconfig again to test the contents
79
- b , err := io .ReadAll (kubeconfig )
80
- if err != nil {
81
- t .Fatal (err )
82
- }
83
-
84
- kc , err := clientcmd .Load (b )
85
- if err != nil {
86
- t .Fatal (err )
87
- }
88
-
89
- checkConfig (t , kc , 1 , "" )
90
-
91
- if token != kc .AuthInfos [apiHost ].Token {
92
- t .Fatalf ("expected token to be %s, got %s" , token , kc .AuthInfos [apiHost ].Token )
93
- }
94
-
95
- if kc .AuthInfos [apiHost ].Exec != nil {
96
- t .Fatalf ("expected execConfig to be empty, got %v" , kc .AuthInfos [apiHost ].Exec )
78
+ tests := []struct {
79
+ name string
80
+ ctx context.Context
81
+ cmd * LoginCmd
82
+ tk api.TokenGetter
83
+ wantToken string
84
+ wantErr bool
85
+ wantErrMessage string
86
+ }{
87
+ {
88
+ name : "interactive environment with token" ,
89
+ ctx : format .WithForceInteractiveEnvironment (context .Background (), true ),
90
+ cmd : & LoginCmd {APIURL : "https://" + apiHost , APIToken : test .FakeJWTToken , Organization : "test" },
91
+ tk : & fakeTokenGetter {},
92
+ wantToken : test .FakeJWTToken ,
93
+ },
94
+ {
95
+ name : "non-interactive environment with token" ,
96
+ ctx : context .Background (),
97
+ cmd : & LoginCmd {APIURL : "https://" + apiHost , APIToken : test .FakeJWTToken , Organization : "test" },
98
+ tk : & fakeTokenGetter {},
99
+ wantToken : test .FakeJWTToken ,
100
+ },
101
+ {
102
+ name : "non-interactive environment with empty token" ,
103
+ ctx : context .Background (),
104
+ cmd : & LoginCmd {APIURL : "https://" + apiHost , APIToken : "" , Organization : "test" },
105
+ wantErr : true ,
106
+ wantErrMessage : ErrNonInteractiveEnvironmentEptyToken ,
107
+ },
108
+ }
109
+ for _ , tt := range tests {
110
+ t .Run (tt .name , func (t * testing.T ) {
111
+ t .Parallel ()
112
+ kubeconfig , err := os .CreateTemp ("" , "*-kubeconfig.yaml" )
113
+ if err != nil {
114
+ log .Fatal (err )
115
+ }
116
+ defer os .Remove (kubeconfig .Name ())
117
+ os .Setenv (clientcmd .RecommendedConfigPathEnvVar , kubeconfig .Name ())
118
+
119
+ err = tt .cmd .Run (tt .ctx , "" , tt .tk )
120
+ checkErrorRequire (t , err , tt .wantErr , tt .wantErrMessage )
121
+
122
+ if tt .wantErr {
123
+ return
124
+ }
125
+
126
+ // read out the kubeconfig again to test the contents
127
+ b , err := io .ReadAll (kubeconfig )
128
+ if err != nil {
129
+ t .Fatal (err )
130
+ }
131
+
132
+ kc , err := clientcmd .Load (b )
133
+ if err != nil {
134
+ t .Fatal (err )
135
+ }
136
+
137
+ checkConfig (t , kc , 1 , "" )
138
+
139
+ if tt .wantToken != kc .AuthInfos [apiHost ].Token {
140
+ t .Fatalf ("expected token to be %s, got %s" , tt .wantToken , kc .AuthInfos [apiHost ].Token )
141
+ }
142
+
143
+ if kc .AuthInfos [apiHost ].Exec != nil {
144
+ t .Fatalf ("expected execConfig to be empty, got %v" , kc .AuthInfos [apiHost ].Exec )
145
+ }
146
+ })
97
147
}
98
148
}
99
149
100
150
func TestLoginCmdWithoutExistingKubeconfig (t * testing.T ) {
151
+ ctx := format .WithForceInteractiveEnvironment (context .Background (), true )
152
+
101
153
dir , err := os .MkdirTemp ("" , "nctl-test-*" )
102
154
if err != nil {
103
155
t .Fatal (err )
@@ -113,7 +165,7 @@ func TestLoginCmdWithoutExistingKubeconfig(t *testing.T) {
113
165
IssuerURL : "https://auth.example.org" ,
114
166
}
115
167
tk := & fakeTokenGetter {}
116
- if err := cmd .Run (context . Background () , "" , tk ); err != nil {
168
+ if err := cmd .Run (ctx , "" , tk ); err != nil {
117
169
t .Fatal (err )
118
170
}
119
171
0 commit comments