@@ -2,7 +2,6 @@ package mcp
22
33import (
44 "context"
5- "github.com/mark3labs/mcp-go/mcp"
65 corev1 "k8s.io/api/core/v1"
76 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
87 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -13,11 +12,9 @@ import (
1312
1413func TestPodsListInAllNamespaces (t * testing.T ) {
1514 testCase (t , func (c * mcpContext ) {
15+ c .withEnvTest ()
1616 createTestData (c .ctx , c .newKubernetesClient ())
17- configurationGet := mcp.CallToolRequest {}
18- configurationGet .Params .Name = "pods_list"
19- configurationGet .Params .Arguments = map [string ]interface {}{}
20- toolResult , err := c .mcpClient .CallTool (c .ctx , configurationGet )
17+ toolResult , err := c .callTool ("pods_list" , map [string ]interface {}{})
2118 t .Run ("pods_list returns pods list" , func (t * testing.T ) {
2219 if err != nil {
2320 t .Fatalf ("call tool failed %v" , err )
@@ -67,6 +64,68 @@ func TestPodsListInAllNamespaces(t *testing.T) {
6764 })
6865}
6966
67+ func TestPodsListInNamespace (t * testing.T ) {
68+ testCase (t , func (c * mcpContext ) {
69+ c .withEnvTest ()
70+ t .Run ("pods_list_in_namespace with nil namespace returns pods list" , func (t * testing.T ) {
71+ toolResult , _ := c .callTool ("pods_list_in_namespace" , map [string ]interface {}{})
72+ if toolResult .IsError != true {
73+ t .Fatalf ("call tool should fail" )
74+ return
75+ }
76+ if toolResult .Content [0 ].(map [string ]interface {})["text" ].(string ) != "failed to list pods in namespace, missing argument namespace" {
77+ t .Fatalf ("invalid error message, got %v" , toolResult .Content [0 ].(map [string ]interface {})["text" ].(string ))
78+ return
79+ }
80+ })
81+ createTestData (c .ctx , c .newKubernetesClient ())
82+ toolResult , err := c .callTool ("pods_list_in_namespace" , map [string ]interface {}{
83+ "namespace" : "ns-1" ,
84+ })
85+ t .Run ("pods_list_in_namespace returns pods list" , func (t * testing.T ) {
86+ if err != nil {
87+ t .Fatalf ("call tool failed %v" , err )
88+ return
89+ }
90+ if toolResult .IsError {
91+ t .Fatalf ("call tool failed" )
92+ return
93+ }
94+ })
95+ var decoded []unstructured.Unstructured
96+ err = yaml .Unmarshal ([]byte (toolResult .Content [0 ].(map [string ]interface {})["text" ].(string )), & decoded )
97+ t .Run ("pods_list_in_namespace has yaml content" , func (t * testing.T ) {
98+ if err != nil {
99+ t .Fatalf ("invalid tool result content %v" , err )
100+ return
101+ }
102+ })
103+ t .Run ("pods_list_in_namespace returns 1 items" , func (t * testing.T ) {
104+ if len (decoded ) != 1 {
105+ t .Fatalf ("invalid pods count, expected 1, got %v" , len (decoded ))
106+ return
107+ }
108+ })
109+ t .Run ("pods_list_in_namespace returns pod in ns-1" , func (t * testing.T ) {
110+ if decoded [0 ].GetName () != "a-pod-in-ns-1" {
111+ t .Fatalf ("invalid pod name, expected a-pod-in-ns-1, got %v" , decoded [0 ].GetName ())
112+ return
113+ }
114+ if decoded [0 ].GetNamespace () != "ns-1" {
115+ t .Fatalf ("invalid pod namespace, expected ns-1, got %v" , decoded [0 ].GetNamespace ())
116+ return
117+ }
118+ })
119+ t .Run ("pods_list_in_namespace omits managed fields" , func (t * testing.T ) {
120+ if decoded [0 ].GetManagedFields () != nil {
121+ t .Fatalf ("managed fields should be omitted, got %v" , decoded [0 ].GetManagedFields ())
122+ return
123+ }
124+ })
125+ })
126+
127+ }
128+
70129func createTestData (ctx context.Context , kc * kubernetes.Clientset ) {
71130 _ , _ = kc .CoreV1 ().Namespaces ().
72131 Create (ctx , & corev1.Namespace {ObjectMeta : metav1.ObjectMeta {Name : "ns-1" }}, metav1.CreateOptions {})
0 commit comments