@@ -10,6 +10,8 @@ import (
10
10
"strings"
11
11
"testing"
12
12
13
+ "github.com/stretchr/testify/assert"
14
+ "github.com/stretchr/testify/require"
13
15
"k8s.io/cli-runtime/pkg/genericiooptions"
14
16
)
15
17
@@ -48,7 +50,7 @@ func TestConfig(t *testing.T) {
48
50
t .Run ("defaults to none" , func (t * testing.T ) {
49
51
ioStreams , out := testStream ()
50
52
rootCmd := NewMCPServer (ioStreams )
51
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" })
53
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" })
52
54
expectedConfig := `" - Config: "`
53
55
if err := rootCmd .Execute (); ! strings .Contains (out .String (), expectedConfig ) {
54
56
t .Fatalf ("Expected config to be %s, got %s %v" , expectedConfig , out .String (), err )
@@ -59,7 +61,7 @@ func TestConfig(t *testing.T) {
59
61
rootCmd := NewMCPServer (ioStreams )
60
62
_ , file , _ , _ := runtime .Caller (0 )
61
63
emptyConfigPath := filepath .Join (filepath .Dir (file ), "testdata" , "empty-config.toml" )
62
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--config" , emptyConfigPath })
64
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" , "--config" , emptyConfigPath })
63
65
_ = rootCmd .Execute ()
64
66
expected := `(?m)\" - Config\:[^\"]+empty-config\.toml\"`
65
67
if m , err := regexp .MatchString (expected , out .String ()); ! m || err != nil {
@@ -69,7 +71,7 @@ func TestConfig(t *testing.T) {
69
71
t .Run ("invalid path throws error" , func (t * testing.T ) {
70
72
ioStreams , _ := testStream ()
71
73
rootCmd := NewMCPServer (ioStreams )
72
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--config" , "invalid-path-to-config.toml" })
74
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" , "--config" , "invalid-path-to-config.toml" })
73
75
err := rootCmd .Execute ()
74
76
if err == nil {
75
77
t .Fatal ("Expected error for invalid config path, got nil" )
@@ -142,15 +144,15 @@ func TestToolsets(t *testing.T) {
142
144
t .Run ("default" , func (t * testing.T ) {
143
145
ioStreams , out := testStream ()
144
146
rootCmd := NewMCPServer (ioStreams )
145
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" })
147
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" })
146
148
if err := rootCmd .Execute (); ! strings .Contains (out .String (), "- Toolsets: core, config, helm" ) {
147
149
t .Fatalf ("Expected toolsets 'full', got %s %v" , out , err )
148
150
}
149
151
})
150
152
t .Run ("set with --toolsets" , func (t * testing.T ) {
151
153
ioStreams , out := testStream ()
152
154
rootCmd := NewMCPServer (ioStreams )
153
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--toolsets" , "helm,config" })
155
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" , "--toolsets" , "helm,config" })
154
156
_ = rootCmd .Execute ()
155
157
expected := `(?m)\" - Toolsets\: helm, config\"`
156
158
if m , err := regexp .MatchString (expected , out .String ()); ! m || err != nil {
@@ -172,15 +174,15 @@ func TestListOutput(t *testing.T) {
172
174
t .Run ("defaults to table" , func (t * testing.T ) {
173
175
ioStreams , out := testStream ()
174
176
rootCmd := NewMCPServer (ioStreams )
175
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" })
177
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" })
176
178
if err := rootCmd .Execute (); ! strings .Contains (out .String (), "- ListOutput: table" ) {
177
179
t .Fatalf ("Expected list-output 'table', got %s %v" , out , err )
178
180
}
179
181
})
180
182
t .Run ("set with --list-output" , func (t * testing.T ) {
181
183
ioStreams , out := testStream ()
182
184
rootCmd := NewMCPServer (ioStreams )
183
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--list-output" , "yaml" })
185
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" , "--list-output" , "yaml" })
184
186
_ = rootCmd .Execute ()
185
187
expected := `(?m)\" - ListOutput\: yaml\"`
186
188
if m , err := regexp .MatchString (expected , out .String ()); ! m || err != nil {
@@ -193,15 +195,15 @@ func TestReadOnly(t *testing.T) {
193
195
t .Run ("defaults to false" , func (t * testing.T ) {
194
196
ioStreams , out := testStream ()
195
197
rootCmd := NewMCPServer (ioStreams )
196
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" })
198
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" })
197
199
if err := rootCmd .Execute (); ! strings .Contains (out .String (), " - Read-only mode: false" ) {
198
200
t .Fatalf ("Expected read-only mode false, got %s %v" , out , err )
199
201
}
200
202
})
201
203
t .Run ("set with --read-only" , func (t * testing.T ) {
202
204
ioStreams , out := testStream ()
203
205
rootCmd := NewMCPServer (ioStreams )
204
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--read-only" })
206
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" , "--read-only" })
205
207
_ = rootCmd .Execute ()
206
208
expected := `(?m)\" - Read-only mode\: true\"`
207
209
if m , err := regexp .MatchString (expected , out .String ()); ! m || err != nil {
@@ -214,15 +216,15 @@ func TestDisableDestructive(t *testing.T) {
214
216
t .Run ("defaults to false" , func (t * testing.T ) {
215
217
ioStreams , out := testStream ()
216
218
rootCmd := NewMCPServer (ioStreams )
217
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" })
219
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" })
218
220
if err := rootCmd .Execute (); ! strings .Contains (out .String (), " - Disable destructive tools: false" ) {
219
221
t .Fatalf ("Expected disable destructive false, got %s %v" , out , err )
220
222
}
221
223
})
222
224
t .Run ("set with --disable-destructive" , func (t * testing.T ) {
223
225
ioStreams , out := testStream ()
224
226
rootCmd := NewMCPServer (ioStreams )
225
- rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--disable-destructive" })
227
+ rootCmd .SetArgs ([]string {"--version" , "--port=1337" , "-- log-level=1" , "--disable-destructive" })
226
228
_ = rootCmd .Execute ()
227
229
expected := `(?m)\" - Disable destructive tools\: true\"`
228
230
if m , err := regexp .MatchString (expected , out .String ()); ! m || err != nil {
@@ -255,3 +257,22 @@ func TestAuthorizationURL(t *testing.T) {
255
257
}
256
258
})
257
259
}
260
+
261
+ func TestStdioLogging (t * testing.T ) {
262
+ t .Run ("stdio disables klog" , func (t * testing.T ) {
263
+ ioStreams , out := testStream ()
264
+ rootCmd := NewMCPServer (ioStreams )
265
+ rootCmd .SetArgs ([]string {"--version" , "--log-level=1" })
266
+ err := rootCmd .Execute ()
267
+ require .NoErrorf (t , err , "Expected no error executing command, got %v" , err )
268
+ assert .Equalf (t , "0.0.0\n " , out .String (), "Expected only version output, got %s" , out .String ())
269
+ })
270
+ t .Run ("http mode enables klog" , func (t * testing.T ) {
271
+ ioStreams , out := testStream ()
272
+ rootCmd := NewMCPServer (ioStreams )
273
+ rootCmd .SetArgs ([]string {"--version" , "--log-level=1" , "--port=1337" })
274
+ err := rootCmd .Execute ()
275
+ require .NoErrorf (t , err , "Expected no error executing command, got %v" , err )
276
+ assert .Containsf (t , out .String (), "Starting kubernetes-mcp-server" , "Expected klog output, got %s" , out .String ())
277
+ })
278
+ }
0 commit comments