|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/BurntSushi/toml" |
| 8 | + "github.com/containers/kubernetes-mcp-server/internal/test" |
| 9 | + "github.com/mark3labs/mcp-go/mcp" |
| 10 | + "github.com/stretchr/testify/suite" |
| 11 | +) |
| 12 | + |
| 13 | +type NodesTopSuite struct { |
| 14 | + BaseMcpSuite |
| 15 | + mockServer *test.MockServer |
| 16 | +} |
| 17 | + |
| 18 | +func (s *NodesTopSuite) SetupTest() { |
| 19 | + s.BaseMcpSuite.SetupTest() |
| 20 | + s.mockServer = test.NewMockServer() |
| 21 | + s.Cfg.KubeConfig = s.mockServer.KubeconfigFile(s.T()) |
| 22 | + s.mockServer.Handle(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 23 | + w.Header().Set("Content-Type", "application/json") |
| 24 | + // Request Performed by DiscoveryClient to Kube API (Get API Groups legacy -core-) |
| 25 | + if req.URL.Path == "/api" { |
| 26 | + _, _ = w.Write([]byte(`{"kind":"APIVersions","versions":[],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0"}]}`)) |
| 27 | + return |
| 28 | + } |
| 29 | + })) |
| 30 | +} |
| 31 | + |
| 32 | +func (s *NodesTopSuite) TearDownTest() { |
| 33 | + s.BaseMcpSuite.TearDownTest() |
| 34 | + if s.mockServer != nil { |
| 35 | + s.mockServer.Close() |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func (s *NodesTopSuite) WithMetricsServer() { |
| 40 | + s.mockServer.Handle(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 41 | + // Request Performed by DiscoveryClient to Kube API (Get API Groups) |
| 42 | + if req.URL.Path == "/apis" { |
| 43 | + _, _ = w.Write([]byte(`{"kind":"APIGroupList","apiVersion":"v1","groups":[{"name":"metrics.k8s.io","versions":[{"groupVersion":"metrics.k8s.io/v1beta1","version":"v1beta1"}],"preferredVersion":{"groupVersion":"metrics.k8s.io/v1beta1","version":"v1beta1"}}]}`)) |
| 44 | + return |
| 45 | + } |
| 46 | + // Request Performed by DiscoveryClient to Kube API (Get API Resources) |
| 47 | + if req.URL.Path == "/apis/metrics.k8s.io/v1beta1" { |
| 48 | + _, _ = w.Write([]byte(`{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"metrics.k8s.io/v1beta1","resources":[{"name":"nodes","singularName":"","namespaced":false,"kind":"NodeMetrics","verbs":["get","list"]}]}`)) |
| 49 | + return |
| 50 | + } |
| 51 | + })) |
| 52 | +} |
| 53 | + |
| 54 | +func (s *NodesTopSuite) TestNodesTop() { |
| 55 | + s.WithMetricsServer() |
| 56 | + s.mockServer.Handle(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { |
| 57 | + // List Nodes |
| 58 | + if req.URL.Path == "/api/v1/nodes" { |
| 59 | + _, _ = w.Write([]byte(`{ |
| 60 | + "apiVersion": "v1", |
| 61 | + "kind": "NodeList", |
| 62 | + "items": [ |
| 63 | + { |
| 64 | + "metadata": { |
| 65 | + "name": "node-1", |
| 66 | + "labels": { |
| 67 | + "node-role.kubernetes.io/worker": "" |
| 68 | + } |
| 69 | + }, |
| 70 | + "status": { |
| 71 | + "allocatable": { |
| 72 | + "cpu": "4", |
| 73 | + "memory": "16Gi" |
| 74 | + }, |
| 75 | + "nodeInfo": { |
| 76 | + "swap": { |
| 77 | + "capacity": 0 |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + }, |
| 82 | + { |
| 83 | + "metadata": { |
| 84 | + "name": "node-2", |
| 85 | + "labels": { |
| 86 | + "node-role.kubernetes.io/worker": "" |
| 87 | + } |
| 88 | + }, |
| 89 | + "status": { |
| 90 | + "allocatable": { |
| 91 | + "cpu": "4", |
| 92 | + "memory": "16Gi" |
| 93 | + }, |
| 94 | + "nodeInfo": { |
| 95 | + "swap": { |
| 96 | + "capacity": 0 |
| 97 | + } |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + ] |
| 102 | + }`)) |
| 103 | + return |
| 104 | + } |
| 105 | + // Get NodeMetrics |
| 106 | + if req.URL.Path == "/apis/metrics.k8s.io/v1beta1/nodes" { |
| 107 | + _, _ = w.Write([]byte(`{ |
| 108 | + "apiVersion": "metrics.k8s.io/v1beta1", |
| 109 | + "kind": "NodeMetricsList", |
| 110 | + "items": [ |
| 111 | + { |
| 112 | + "metadata": { |
| 113 | + "name": "node-1" |
| 114 | + }, |
| 115 | + "timestamp": "2025-10-29T09:00:00Z", |
| 116 | + "window": "30s", |
| 117 | + "usage": { |
| 118 | + "cpu": "500m", |
| 119 | + "memory": "2Gi" |
| 120 | + } |
| 121 | + }, |
| 122 | + { |
| 123 | + "metadata": { |
| 124 | + "name": "node-2" |
| 125 | + }, |
| 126 | + "timestamp": "2025-10-29T09:00:00Z", |
| 127 | + "window": "30s", |
| 128 | + "usage": { |
| 129 | + "cpu": "1000m", |
| 130 | + "memory": "4Gi" |
| 131 | + } |
| 132 | + } |
| 133 | + ] |
| 134 | + }`)) |
| 135 | + return |
| 136 | + } |
| 137 | + // Get specific NodeMetrics |
| 138 | + if req.URL.Path == "/apis/metrics.k8s.io/v1beta1/nodes/node-1" { |
| 139 | + _, _ = w.Write([]byte(`{ |
| 140 | + "apiVersion": "metrics.k8s.io/v1beta1", |
| 141 | + "kind": "NodeMetrics", |
| 142 | + "metadata": { |
| 143 | + "name": "node-1" |
| 144 | + }, |
| 145 | + "timestamp": "2025-10-29T09:00:00Z", |
| 146 | + "window": "30s", |
| 147 | + "usage": { |
| 148 | + "cpu": "500m", |
| 149 | + "memory": "2Gi" |
| 150 | + } |
| 151 | + }`)) |
| 152 | + return |
| 153 | + } |
| 154 | + w.WriteHeader(http.StatusNotFound) |
| 155 | + })) |
| 156 | + s.InitMcpClient() |
| 157 | + |
| 158 | + s.Run("nodes_top() - all nodes", func() { |
| 159 | + toolResult, err := s.CallTool("nodes_top", map[string]interface{}{}) |
| 160 | + s.Require().NotNil(toolResult, "toolResult should not be nil") |
| 161 | + s.Run("no error", func() { |
| 162 | + s.Falsef(toolResult.IsError, "call tool should succeed") |
| 163 | + s.Nilf(err, "call tool should not return error object") |
| 164 | + }) |
| 165 | + s.Run("returns metrics for all nodes", func() { |
| 166 | + content := toolResult.Content[0].(mcp.TextContent).Text |
| 167 | + s.Contains(content, "node-1", "expected metrics to contain node-1") |
| 168 | + s.Contains(content, "node-2", "expected metrics to contain node-2") |
| 169 | + s.Contains(content, "CPU(cores)", "expected header with CPU column") |
| 170 | + s.Contains(content, "MEMORY(bytes)", "expected header with MEMORY column") |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + s.Run("nodes_top(name=node-1) - specific node", func() { |
| 175 | + toolResult, err := s.CallTool("nodes_top", map[string]interface{}{ |
| 176 | + "name": "node-1", |
| 177 | + }) |
| 178 | + s.Require().NotNil(toolResult, "toolResult should not be nil") |
| 179 | + s.Run("no error", func() { |
| 180 | + s.Falsef(toolResult.IsError, "call tool should succeed") |
| 181 | + s.Nilf(err, "call tool should not return error object") |
| 182 | + }) |
| 183 | + s.Run("returns metrics for specific node", func() { |
| 184 | + content := toolResult.Content[0].(mcp.TextContent).Text |
| 185 | + s.Contains(content, "node-1", "expected metrics to contain node-1") |
| 186 | + s.Contains(content, "500m", "expected CPU usage of 500m") |
| 187 | + s.Contains(content, "2048Mi", "expected memory usage of 2048Mi") |
| 188 | + }) |
| 189 | + }) |
| 190 | + |
| 191 | + s.Run("nodes_top(label_selector=node-role.kubernetes.io/worker=)", func() { |
| 192 | + toolResult, err := s.CallTool("nodes_top", map[string]interface{}{ |
| 193 | + "label_selector": "node-role.kubernetes.io/worker=", |
| 194 | + }) |
| 195 | + s.Require().NotNil(toolResult, "toolResult should not be nil") |
| 196 | + s.Run("no error", func() { |
| 197 | + s.Falsef(toolResult.IsError, "call tool should succeed") |
| 198 | + s.Nilf(err, "call tool should not return error object") |
| 199 | + }) |
| 200 | + s.Run("returns metrics for filtered nodes", func() { |
| 201 | + content := toolResult.Content[0].(mcp.TextContent).Text |
| 202 | + s.Contains(content, "node-1", "expected metrics to contain node-1") |
| 203 | + s.Contains(content, "node-2", "expected metrics to contain node-2") |
| 204 | + }) |
| 205 | + }) |
| 206 | +} |
| 207 | + |
| 208 | +func (s *NodesTopSuite) TestNodesTopMetricsUnavailable() { |
| 209 | + s.InitMcpClient() |
| 210 | + |
| 211 | + s.Run("nodes_top() - metrics unavailable", func() { |
| 212 | + toolResult, err := s.CallTool("nodes_top", map[string]interface{}{}) |
| 213 | + s.Require().NotNil(toolResult, "toolResult should not be nil") |
| 214 | + s.Run("has error", func() { |
| 215 | + s.Truef(toolResult.IsError, "call tool should fail when metrics unavailable") |
| 216 | + s.Nilf(err, "call tool should not return error object") |
| 217 | + }) |
| 218 | + s.Run("describes metrics unavailable", func() { |
| 219 | + content := toolResult.Content[0].(mcp.TextContent).Text |
| 220 | + s.Contains(content, "failed to get nodes top", "expected error message about failing to get nodes top") |
| 221 | + }) |
| 222 | + }) |
| 223 | +} |
| 224 | + |
| 225 | +func (s *NodesTopSuite) TestNodesTopDenied() { |
| 226 | + s.Require().NoError(toml.Unmarshal([]byte(` |
| 227 | + denied_resources = [ { group = "metrics.k8s.io", version = "v1beta1" } ] |
| 228 | + `), s.Cfg), "Expected to parse denied resources config") |
| 229 | + s.WithMetricsServer() |
| 230 | + s.InitMcpClient() |
| 231 | + s.Run("nodes_top (denied)", func() { |
| 232 | + toolResult, err := s.CallTool("nodes_top", map[string]interface{}{}) |
| 233 | + s.Require().NotNil(toolResult, "toolResult should not be nil") |
| 234 | + s.Run("has error", func() { |
| 235 | + s.Truef(toolResult.IsError, "call tool should fail") |
| 236 | + s.Nilf(err, "call tool should not return error object") |
| 237 | + }) |
| 238 | + s.Run("describes denial", func() { |
| 239 | + expectedMessage := "failed to get nodes top: resource not allowed: metrics.k8s.io/v1beta1, Kind=NodeMetrics" |
| 240 | + s.Equalf(expectedMessage, toolResult.Content[0].(mcp.TextContent).Text, |
| 241 | + "expected descriptive error '%s', got %v", expectedMessage, toolResult.Content[0].(mcp.TextContent).Text) |
| 242 | + }) |
| 243 | + }) |
| 244 | +} |
| 245 | + |
| 246 | +func TestNodesTop(t *testing.T) { |
| 247 | + suite.Run(t, new(NodesTopSuite)) |
| 248 | +} |
0 commit comments