@@ -2,52 +2,69 @@ package mcp
22
33import (
44 "context"
5- "encoding/json"
65 "github.com/mark3labs/mcp-go/mcp"
76 corev1 "k8s.io/api/core/v1"
87 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
98 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
109 "k8s.io/client-go/kubernetes"
10+ "sigs.k8s.io/yaml"
1111 "testing"
1212)
1313
1414func TestPodsListInAllNamespaces (t * testing.T ) {
15- t . Run ( "pods_list" , testCase (func ( t * testing. T , c * mcpContext ) {
15+ testCase (t , func ( c * mcpContext ) {
1616 createTestData (c .ctx , c .newKubernetesClient ())
1717 configurationGet := mcp.CallToolRequest {}
1818 configurationGet .Params .Name = "pods_list"
1919 configurationGet .Params .Arguments = map [string ]interface {}{}
2020 toolResult , err := c .mcpClient .CallTool (c .ctx , configurationGet )
21- if err != nil {
22- t .Fatalf ("call tool failed %v" , err )
23- return
24- }
21+ t .Run ("pods_list returns pods list" , func (t * testing.T ) {
22+ if err != nil {
23+ t .Fatalf ("call tool failed %v" , err )
24+ return
25+ }
26+ })
2527 var decoded []unstructured.Unstructured
26- if json .Unmarshal ([]byte (toolResult .Content [0 ].(map [string ]interface {})["text" ].(string )), & decoded ) != nil {
27- t .Fatalf ("invalid tool result content %v" , err )
28- return
29- }
30- if len (decoded ) != 2 {
31- t .Fatalf ("invalid pods count, expected 2, got %v" , len (decoded ))
32- return
33- }
34- if decoded [0 ].GetName () != "a-pod-in-ns-1" {
35- t .Fatalf ("invalid pod name, expected a-pod-in-ns-1, got %v" , decoded [0 ].GetName ())
36- return
37- }
38- if decoded [0 ].GetNamespace () != "ns-1" {
39- t .Fatalf ("invalid pod namespace, expected ns-1, got %v" , decoded [0 ].GetNamespace ())
40- return
41- }
42- if decoded [1 ].GetName () != "a-pod-in-ns-2" {
43- t .Fatalf ("invalid pod name, expected a-pod-in-ns-2, got %v" , decoded [1 ].GetName ())
44- return
45- }
46- if decoded [1 ].GetNamespace () != "ns-2" {
47- t .Fatalf ("invalid pod namespace, expected ns-2, got %v" , decoded [1 ].GetNamespace ())
48- return
49- }
50- }))
28+ err = yaml .Unmarshal ([]byte (toolResult .Content [0 ].(map [string ]interface {})["text" ].(string )), & decoded )
29+ t .Run ("pods_list has yaml content" , func (t * testing.T ) {
30+ if err != nil {
31+ t .Fatalf ("invalid tool result content %v" , err )
32+ return
33+ }
34+ })
35+ t .Run ("pods_list returns 2 items" , func (t * testing.T ) {
36+ if len (decoded ) != 2 {
37+ t .Fatalf ("invalid pods count, expected 2, got %v" , len (decoded ))
38+ return
39+ }
40+ })
41+ t .Run ("pods_list returns pod in ns-1" , func (t * testing.T ) {
42+ if decoded [0 ].GetName () != "a-pod-in-ns-1" {
43+ t .Fatalf ("invalid pod name, expected a-pod-in-ns-1, got %v" , decoded [0 ].GetName ())
44+ return
45+ }
46+ if decoded [0 ].GetNamespace () != "ns-1" {
47+ t .Fatalf ("invalid pod namespace, expected ns-1, got %v" , decoded [0 ].GetNamespace ())
48+ return
49+ }
50+ })
51+ t .Run ("pods_list returns pod in ns-2" , func (t * testing.T ) {
52+ if decoded [1 ].GetName () != "a-pod-in-ns-2" {
53+ t .Fatalf ("invalid pod name, expected a-pod-in-ns-2, got %v" , decoded [1 ].GetName ())
54+ return
55+ }
56+ if decoded [1 ].GetNamespace () != "ns-2" {
57+ t .Fatalf ("invalid pod namespace, expected ns-2, got %v" , decoded [1 ].GetNamespace ())
58+ return
59+ }
60+ })
61+ t .Run ("pods_list omits managed fields" , func (t * testing.T ) {
62+ if decoded [0 ].GetManagedFields () != nil {
63+ t .Fatalf ("managed fields should be omitted, got %v" , decoded [0 ].GetManagedFields ())
64+ return
65+ }
66+ })
67+ })
5168}
5269
5370func createTestData (ctx context.Context , kc * kubernetes.Clientset ) {
0 commit comments