@@ -30,9 +30,9 @@ func TestPodsExec(t *testing.T) {
30
30
_ , _ = w .Write ([]byte (err .Error ()))
31
31
return
32
32
}
33
- defer ctx . conn .Close ()
34
- _ , _ = io .WriteString (ctx .stdoutStream , strings .Join (req .URL .Query ()["command" ], " " ))
35
- _ , _ = io .WriteString (ctx .stdoutStream , "\n total 0 \n " )
33
+ defer func ( conn io. Closer ) { _ = conn .Close () }( ctx . conn )
34
+ _ , _ = io .WriteString (ctx .stdoutStream , "command:" + strings .Join (req .URL .Query ()["command" ], " " )+ " \n " )
35
+ _ , _ = io .WriteString (ctx .stdoutStream , "container:" + strings . Join ( req . URL . Query ()[ "container" ], " " ) + " \n " )
36
36
}))
37
37
mockServer .Handle (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
38
38
if req .URL .Path != "/api/v1/namespaces/default/pods/pod-to-exec" {
@@ -46,20 +46,55 @@ func TestPodsExec(t *testing.T) {
46
46
Spec : v1.PodSpec {Containers : []v1.Container {{Name : "container-to-exec" }}},
47
47
})
48
48
}))
49
- toolResult , err := c .callTool ("pods_exec" , map [string ]interface {}{
49
+ podsExecNilNamespace , err := c .callTool ("pods_exec" , map [string ]interface {}{
50
+ "name" : "pod-to-exec" ,
51
+ "command" : []interface {}{"ls" , "-l" },
52
+ })
53
+ t .Run ("pods_exec with name and nil namespace returns command output" , func (t * testing.T ) {
54
+ if err != nil {
55
+ t .Fatalf ("call tool failed %v" , err )
56
+ }
57
+ if podsExecNilNamespace .IsError {
58
+ t .Fatalf ("call tool failed" )
59
+ }
60
+ if ! strings .Contains (podsExecNilNamespace .Content [0 ].(mcp.TextContent ).Text , "command:ls -l\n " ) {
61
+ t .Errorf ("unexpected result %v" , podsExecNilNamespace .Content [0 ].(mcp.TextContent ).Text )
62
+ }
63
+ })
64
+ podsExecInNamespace , err := c .callTool ("pods_exec" , map [string ]interface {}{
65
+ "namespace" : "default" ,
66
+ "name" : "pod-to-exec" ,
67
+ "command" : []interface {}{"ls" , "-l" },
68
+ })
69
+ t .Run ("pods_exec with name and namespace returns command output" , func (t * testing.T ) {
70
+ if err != nil {
71
+ t .Fatalf ("call tool failed %v" , err )
72
+ }
73
+ if podsExecInNamespace .IsError {
74
+ t .Fatalf ("call tool failed" )
75
+ }
76
+ if ! strings .Contains (podsExecNilNamespace .Content [0 ].(mcp.TextContent ).Text , "command:ls -l\n " ) {
77
+ t .Errorf ("unexpected result %v" , podsExecInNamespace .Content [0 ].(mcp.TextContent ).Text )
78
+ }
79
+ })
80
+ podsExecInNamespaceAndContainer , err := c .callTool ("pods_exec" , map [string ]interface {}{
50
81
"namespace" : "default" ,
51
82
"name" : "pod-to-exec" ,
52
83
"command" : []interface {}{"ls" , "-l" },
84
+ "container" : "a-specific-container" ,
53
85
})
54
- t .Run ("pods_exec returns command output" , func (t * testing.T ) {
86
+ t .Run ("pods_exec with name, namespace, and container returns command output" , func (t * testing.T ) {
55
87
if err != nil {
56
88
t .Fatalf ("call tool failed %v" , err )
57
89
}
58
- if toolResult .IsError {
90
+ if podsExecInNamespaceAndContainer .IsError {
59
91
t .Fatalf ("call tool failed" )
60
92
}
61
- if toolResult .Content [0 ].(mcp.TextContent ).Text != "ls -l\n total 0\n " {
62
- t .Errorf ("unexpected result %v" , toolResult .Content [0 ].(mcp.TextContent ).Text )
93
+ if ! strings .Contains (podsExecInNamespaceAndContainer .Content [0 ].(mcp.TextContent ).Text , "command:ls -l\n " ) {
94
+ t .Errorf ("unexpected result %v" , podsExecInNamespaceAndContainer .Content [0 ].(mcp.TextContent ).Text )
95
+ }
96
+ if ! strings .Contains (podsExecInNamespaceAndContainer .Content [0 ].(mcp.TextContent ).Text , "container:a-specific-container\n " ) {
97
+ t .Errorf ("expected container name not found %v" , podsExecInNamespaceAndContainer .Content [0 ].(mcp.TextContent ).Text )
63
98
}
64
99
})
65
100
0 commit comments