|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/mark3labs/mcp-go/mcp" |
| 5 | + "net/http" |
| 6 | + "regexp" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestPodsTop(t *testing.T) { |
| 11 | + testCase(t, func(c *mcpContext) { |
| 12 | + mockServer := NewMockServer() |
| 13 | + defer mockServer.Close() |
| 14 | + c.withKubeConfig(mockServer.config) |
| 15 | + metricsApiAvailable := false |
| 16 | + mockServer.Handle(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 17 | + println("Request received:", req.Method, req.URL.Path) // TODO: REMOVE LINE |
| 18 | + w.Header().Set("Content-Type", "application/json") |
| 19 | + // Request Performed by DiscoveryClient to Kube API (Get API Groups legacy -core-) |
| 20 | + if req.URL.Path == "/api" { |
| 21 | + if !metricsApiAvailable { |
| 22 | + |
| 23 | + _, _ = w.Write([]byte(`{"kind":"APIVersions","versions":[],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0"}]}`)) |
| 24 | + } else { |
| 25 | + _, _ = w.Write([]byte(`{"kind":"APIVersions","versions":["metrics.k8s.io/v1beta1"],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0"}]}`)) |
| 26 | + } |
| 27 | + return |
| 28 | + } |
| 29 | + // Request Performed by DiscoveryClient to Kube API (Get API Groups) |
| 30 | + if req.URL.Path == "/apis" { |
| 31 | + _, _ = w.Write([]byte(`{"kind":"APIGroupList","apiVersion":"v1","groups":[]}`)) |
| 32 | + return |
| 33 | + } |
| 34 | + // Request Performed by DiscoveryClient to Kube API (Get API Resources) |
| 35 | + if metricsApiAvailable && req.URL.Path == "/apis/metrics.k8s.io/v1beta1" { |
| 36 | + _, _ = w.Write([]byte(`{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"metrics.k8s.io/v1beta1","resources":[{"name":"pods","singularName":"","namespaced":true,"kind":"PodMetrics","verbs":["get","list"]}]}`)) |
| 37 | + return |
| 38 | + } |
| 39 | + // Pod Metrics from all namespaces |
| 40 | + if metricsApiAvailable && req.URL.Path == "/apis/metrics.k8s.io/v1beta1/pods" { |
| 41 | + if req.URL.Query().Get("labelSelector") == "app=pod-ns-5-42" { |
| 42 | + _, _ = w.Write([]byte(`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` + |
| 43 | + `{"metadata":{"name":"pod-ns-5-42","namespace":"ns-5"},"containers":[{"name":"container-1","usage":{"cpu":"42m","memory":"42Mi"}}]}` + |
| 44 | + `]}`)) |
| 45 | + } else { |
| 46 | + _, _ = w.Write([]byte(`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` + |
| 47 | + `{"metadata":{"name":"pod-1","namespace":"default"},"containers":[{"name":"container-1","usage":{"cpu":"100m","memory":"200Mi"}},{"name":"container-2","usage":{"cpu":"200m","memory":"300Mi"}}]},` + |
| 48 | + `{"metadata":{"name":"pod-2","namespace":"ns-1"},"containers":[{"name":"container-1-ns-1","usage":{"cpu":"300m","memory":"400Mi"}}]}` + |
| 49 | + `]}`)) |
| 50 | + |
| 51 | + } |
| 52 | + return |
| 53 | + } |
| 54 | + // Pod Metrics from configured namespace |
| 55 | + if metricsApiAvailable && req.URL.Path == "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods" { |
| 56 | + _, _ = w.Write([]byte(`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` + |
| 57 | + `{"metadata":{"name":"pod-1","namespace":"default"},"containers":[{"name":"container-1","usage":{"cpu":"10m","memory":"20Mi"}},{"name":"container-2","usage":{"cpu":"30m","memory":"40Mi"}}]}` + |
| 58 | + `]}`)) |
| 59 | + return |
| 60 | + } |
| 61 | + // Pod Metrics from ns-5 namespace |
| 62 | + if metricsApiAvailable && req.URL.Path == "/apis/metrics.k8s.io/v1beta1/namespaces/ns-5/pods" { |
| 63 | + _, _ = w.Write([]byte(`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` + |
| 64 | + `{"metadata":{"name":"pod-ns-5-1","namespace":"ns-5"},"containers":[{"name":"container-1","usage":{"cpu":"10m","memory":"20Mi"}}]}` + |
| 65 | + `]}`)) |
| 66 | + return |
| 67 | + } |
| 68 | + // Pod Metrics from ns-5 namespace with pod-ns-5-5 pod name |
| 69 | + if metricsApiAvailable && req.URL.Path == "/apis/metrics.k8s.io/v1beta1/namespaces/ns-5/pods/pod-ns-5-5" { |
| 70 | + _, _ = w.Write([]byte(`{"kind":"PodMetrics","apiVersion":"metrics.k8s.io/v1beta1",` + |
| 71 | + `"metadata":{"name":"pod-ns-5-5","namespace":"ns-5"},` + |
| 72 | + `"containers":[{"name":"container-1","usage":{"cpu":"13m","memory":"37Mi"}}]` + |
| 73 | + `}`)) |
| 74 | + } |
| 75 | + })) |
| 76 | + podsTopMetricsApiUnavailable, err := c.callTool("pods_top", map[string]interface{}{}) |
| 77 | + t.Run("pods_top with metrics API not available", func(t *testing.T) { |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("call tool failed %v", err) |
| 80 | + } |
| 81 | + if !podsTopMetricsApiUnavailable.IsError { |
| 82 | + t.Errorf("call tool should have returned an error") |
| 83 | + } |
| 84 | + if podsTopMetricsApiUnavailable.Content[0].(mcp.TextContent).Text != "failed to get pods top: metrics API is not available" { |
| 85 | + t.Errorf("call tool returned unexpected content: %s", podsTopMetricsApiUnavailable.Content[0].(mcp.TextContent).Text) |
| 86 | + } |
| 87 | + }) |
| 88 | + // Enable metrics API addon |
| 89 | + metricsApiAvailable = true |
| 90 | + d, _ := c.mcpServer.k.ToDiscoveryClient() |
| 91 | + d.Invalidate() // Force discovery client to refresh |
| 92 | + podsTopDefaults, err := c.callTool("pods_top", map[string]interface{}{}) |
| 93 | + t.Run("pods_top defaults returns pod metrics from all namespaces", func(t *testing.T) { |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("call tool failed %v", err) |
| 96 | + } |
| 97 | + textContent := podsTopDefaults.Content[0].(mcp.TextContent).Text |
| 98 | + if podsTopDefaults.IsError { |
| 99 | + t.Fatalf("call tool failed %s", textContent) |
| 100 | + } |
| 101 | + expectedHeaders := regexp.MustCompile("(?m)^\\s*NAMESPACE\\s+POD\\s+NAME\\s+CPU\\(cores\\)\\s+MEMORY\\(bytes\\)\\s*$") |
| 102 | + if !expectedHeaders.MatchString(textContent) { |
| 103 | + t.Errorf("Expected headers '%s' not found in output:\n%s", expectedHeaders.String(), textContent) |
| 104 | + } |
| 105 | + expectedRows := []string{ |
| 106 | + "default\\s+pod-1\\s+container-1\\s+100m\\s+200Mi", |
| 107 | + "default\\s+pod-1\\s+container-2\\s+200m\\s+300Mi", |
| 108 | + "ns-1\\s+pod-2\\s+container-1-ns-1\\s+300m\\s+400Mi", |
| 109 | + } |
| 110 | + for _, row := range expectedRows { |
| 111 | + if !regexp.MustCompile(row).MatchString(textContent) { |
| 112 | + t.Errorf("Expected row '%s' not found in output:\n%s", row, textContent) |
| 113 | + } |
| 114 | + } |
| 115 | + expectedTotal := regexp.MustCompile("(?m)^\\s+600m\\s+900Mi\\s*$") |
| 116 | + if !expectedTotal.MatchString(textContent) { |
| 117 | + t.Errorf("Expected total row '%s' not found in output:\n%s", expectedTotal.String(), textContent) |
| 118 | + } |
| 119 | + }) |
| 120 | + podsTopConfiguredNamespace, err := c.callTool("pods_top", map[string]interface{}{ |
| 121 | + "all_namespaces": false, |
| 122 | + }) |
| 123 | + t.Run("pods_top[allNamespaces=false] returns pod metrics from configured namespace", func(t *testing.T) { |
| 124 | + if err != nil { |
| 125 | + t.Fatalf("call tool failed %v", err) |
| 126 | + } |
| 127 | + textContent := podsTopConfiguredNamespace.Content[0].(mcp.TextContent).Text |
| 128 | + expectedRows := []string{ |
| 129 | + "default\\s+pod-1\\s+container-1\\s+10m\\s+20Mi", |
| 130 | + "default\\s+pod-1\\s+container-2\\s+30m\\s+40Mi", |
| 131 | + } |
| 132 | + for _, row := range expectedRows { |
| 133 | + if !regexp.MustCompile(row).MatchString(textContent) { |
| 134 | + t.Errorf("Expected row '%s' not found in output:\n%s", row, textContent) |
| 135 | + } |
| 136 | + } |
| 137 | + expectedTotal := regexp.MustCompile("(?m)^\\s+40m\\s+60Mi\\s*$") |
| 138 | + if !expectedTotal.MatchString(textContent) { |
| 139 | + t.Errorf("Expected total row '%s' not found in output:\n%s", expectedTotal.String(), textContent) |
| 140 | + } |
| 141 | + }) |
| 142 | + podsTopNamespace, err := c.callTool("pods_top", map[string]interface{}{ |
| 143 | + "namespace": "ns-5", |
| 144 | + }) |
| 145 | + t.Run("pods_top[namespace=ns-5] returns pod metrics from provided namespace", func(t *testing.T) { |
| 146 | + if err != nil { |
| 147 | + t.Fatalf("call tool failed %v", err) |
| 148 | + } |
| 149 | + textContent := podsTopNamespace.Content[0].(mcp.TextContent).Text |
| 150 | + expectedRow := regexp.MustCompile("ns-5\\s+pod-ns-5-1\\s+container-1\\s+10m\\s+20Mi") |
| 151 | + if !expectedRow.MatchString(textContent) { |
| 152 | + t.Errorf("Expected row '%s' not found in output:\n%s", expectedRow.String(), textContent) |
| 153 | + } |
| 154 | + expectedTotal := regexp.MustCompile("(?m)^\\s+10m\\s+20Mi\\s*$") |
| 155 | + if !expectedTotal.MatchString(textContent) { |
| 156 | + t.Errorf("Expected total row '%s' not found in output:\n%s", expectedTotal.String(), textContent) |
| 157 | + } |
| 158 | + }) |
| 159 | + podsTopNamespaceName, err := c.callTool("pods_top", map[string]interface{}{ |
| 160 | + "namespace": "ns-5", |
| 161 | + "name": "pod-ns-5-5", |
| 162 | + }) |
| 163 | + t.Run("pods_top[namespace=ns-5,name=pod-ns-5-5] returns pod metrics from provided namespace and name", func(t *testing.T) { |
| 164 | + if err != nil { |
| 165 | + t.Fatalf("call tool failed %v", err) |
| 166 | + } |
| 167 | + textContent := podsTopNamespaceName.Content[0].(mcp.TextContent).Text |
| 168 | + expectedRow := regexp.MustCompile("ns-5\\s+pod-ns-5-5\\s+container-1\\s+13m\\s+37Mi") |
| 169 | + if !expectedRow.MatchString(textContent) { |
| 170 | + t.Errorf("Expected row '%s' not found in output:\n%s", expectedRow.String(), textContent) |
| 171 | + } |
| 172 | + expectedTotal := regexp.MustCompile("(?m)^\\s+13m\\s+37Mi\\s*$") |
| 173 | + if !expectedTotal.MatchString(textContent) { |
| 174 | + t.Errorf("Expected total row '%s' not found in output:\n%s", expectedTotal.String(), textContent) |
| 175 | + } |
| 176 | + }) |
| 177 | + podsTopNamespaceLabelSelector, err := c.callTool("pods_top", map[string]interface{}{ |
| 178 | + "label_selector": "app=pod-ns-5-42", |
| 179 | + }) |
| 180 | + t.Run("pods_top[label_selector=app=pod-ns-5-42] returns pod metrics from pods matching selector", func(t *testing.T) { |
| 181 | + if err != nil { |
| 182 | + t.Fatalf("call tool failed %v", err) |
| 183 | + } |
| 184 | + textContent := podsTopNamespaceLabelSelector.Content[0].(mcp.TextContent).Text |
| 185 | + expectedRow := regexp.MustCompile("ns-5\\s+pod-ns-5-42\\s+container-1\\s+42m\\s+42Mi") |
| 186 | + if !expectedRow.MatchString(textContent) { |
| 187 | + t.Errorf("Expected row '%s' not found in output:\n%s", expectedRow.String(), textContent) |
| 188 | + } |
| 189 | + expectedTotal := regexp.MustCompile("(?m)^\\s+42m\\s+42Mi\\s*$") |
| 190 | + if !expectedTotal.MatchString(textContent) { |
| 191 | + t.Errorf("Expected total row '%s' not found in output:\n%s", expectedTotal.String(), textContent) |
| 192 | + } |
| 193 | + }) |
| 194 | + }) |
| 195 | +} |
0 commit comments