@@ -7,23 +7,50 @@ import (
7
7
"testing"
8
8
)
9
9
10
- func TestPodsTop (t * testing.T ) {
10
+ func TestPodsTopMetricsUnavailable (t * testing.T ) {
11
11
testCase (t , func (c * mcpContext ) {
12
12
mockServer := NewMockServer ()
13
13
defer mockServer .Close ()
14
14
c .withKubeConfig (mockServer .config )
15
- metricsApiAvailable := false
16
15
mockServer .Handle (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
17
- println ("Request received:" , req .Method , req .URL .Path ) // TODO: REMOVE LINE
18
16
w .Header ().Set ("Content-Type" , "application/json" )
19
17
// Request Performed by DiscoveryClient to Kube API (Get API Groups legacy -core-)
20
18
if req .URL .Path == "/api" {
21
- if ! metricsApiAvailable {
19
+ _ , _ = w .Write ([]byte (`{"kind":"APIVersions","versions":[],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0"}]}` ))
20
+ return
21
+ }
22
+ // Request Performed by DiscoveryClient to Kube API (Get API Groups)
23
+ if req .URL .Path == "/apis" {
24
+ _ , _ = w .Write ([]byte (`{"kind":"APIGroupList","apiVersion":"v1","groups":[]}` ))
25
+ return
26
+ }
27
+ }))
28
+ podsTopMetricsApiUnavailable , err := c .callTool ("pods_top" , map [string ]interface {}{})
29
+ t .Run ("pods_top with metrics API not available" , func (t * testing.T ) {
30
+ if err != nil {
31
+ t .Fatalf ("call tool failed %v" , err )
32
+ }
33
+ if ! podsTopMetricsApiUnavailable .IsError {
34
+ t .Errorf ("call tool should have returned an error" )
35
+ }
36
+ if podsTopMetricsApiUnavailable .Content [0 ].(mcp.TextContent ).Text != "failed to get pods top: metrics API is not available" {
37
+ t .Errorf ("call tool returned unexpected content: %s" , podsTopMetricsApiUnavailable .Content [0 ].(mcp.TextContent ).Text )
38
+ }
39
+ })
40
+ })
41
+ }
22
42
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
- }
43
+ func TestPodsTopMetricsAvailable (t * testing.T ) {
44
+ testCase (t , func (c * mcpContext ) {
45
+ mockServer := NewMockServer ()
46
+ defer mockServer .Close ()
47
+ c .withKubeConfig (mockServer .config )
48
+ mockServer .Handle (http .HandlerFunc (func (w http.ResponseWriter , req * http.Request ) {
49
+ println ("Request received:" , req .Method , req .URL .Path ) // TODO: REMOVE LINE
50
+ w .Header ().Set ("Content-Type" , "application/json" )
51
+ // Request Performed by DiscoveryClient to Kube API (Get API Groups legacy -core-)
52
+ if req .URL .Path == "/api" {
53
+ _ , _ = w .Write ([]byte (`{"kind":"APIVersions","versions":["metrics.k8s.io/v1beta1"],"serverAddressByClientCIDRs":[{"clientCIDR":"0.0.0.0/0"}]}` ))
27
54
return
28
55
}
29
56
// Request Performed by DiscoveryClient to Kube API (Get API Groups)
@@ -32,12 +59,12 @@ func TestPodsTop(t *testing.T) {
32
59
return
33
60
}
34
61
// Request Performed by DiscoveryClient to Kube API (Get API Resources)
35
- if metricsApiAvailable && req .URL .Path == "/apis/metrics.k8s.io/v1beta1" {
62
+ if req .URL .Path == "/apis/metrics.k8s.io/v1beta1" {
36
63
_ , _ = w .Write ([]byte (`{"kind":"APIResourceList","apiVersion":"v1","groupVersion":"metrics.k8s.io/v1beta1","resources":[{"name":"pods","singularName":"","namespaced":true,"kind":"PodMetrics","verbs":["get","list"]}]}` ))
37
64
return
38
65
}
39
66
// Pod Metrics from all namespaces
40
- if metricsApiAvailable && req .URL .Path == "/apis/metrics.k8s.io/v1beta1/pods" {
67
+ if req .URL .Path == "/apis/metrics.k8s.io/v1beta1/pods" {
41
68
if req .URL .Query ().Get ("labelSelector" ) == "app=pod-ns-5-42" {
42
69
_ , _ = w .Write ([]byte (`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` +
43
70
`{"metadata":{"name":"pod-ns-5-42","namespace":"ns-5"},"containers":[{"name":"container-1","usage":{"cpu":"42m","memory":"42Mi"}}]}` +
@@ -52,42 +79,27 @@ func TestPodsTop(t *testing.T) {
52
79
return
53
80
}
54
81
// Pod Metrics from configured namespace
55
- if metricsApiAvailable && req .URL .Path == "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods" {
82
+ if req .URL .Path == "/apis/metrics.k8s.io/v1beta1/namespaces/default/pods" {
56
83
_ , _ = w .Write ([]byte (`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` +
57
84
`{"metadata":{"name":"pod-1","namespace":"default"},"containers":[{"name":"container-1","usage":{"cpu":"10m","memory":"20Mi"}},{"name":"container-2","usage":{"cpu":"30m","memory":"40Mi"}}]}` +
58
85
`]}` ))
59
86
return
60
87
}
61
88
// Pod Metrics from ns-5 namespace
62
- if metricsApiAvailable && req .URL .Path == "/apis/metrics.k8s.io/v1beta1/namespaces/ns-5/pods" {
89
+ if req .URL .Path == "/apis/metrics.k8s.io/v1beta1/namespaces/ns-5/pods" {
63
90
_ , _ = w .Write ([]byte (`{"kind":"PodMetricsList","apiVersion":"metrics.k8s.io/v1beta1","items":[` +
64
91
`{"metadata":{"name":"pod-ns-5-1","namespace":"ns-5"},"containers":[{"name":"container-1","usage":{"cpu":"10m","memory":"20Mi"}}]}` +
65
92
`]}` ))
66
93
return
67
94
}
68
95
// 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" {
96
+ if req .URL .Path == "/apis/metrics.k8s.io/v1beta1/namespaces/ns-5/pods/pod-ns-5-5" {
70
97
_ , _ = w .Write ([]byte (`{"kind":"PodMetrics","apiVersion":"metrics.k8s.io/v1beta1",` +
71
98
`"metadata":{"name":"pod-ns-5-5","namespace":"ns-5"},` +
72
99
`"containers":[{"name":"container-1","usage":{"cpu":"13m","memory":"37Mi"}}]` +
73
100
`}` ))
74
101
}
75
102
}))
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
- c .mcpServer .k .Derived (t .Context ()).CacheInvalidate () // Force discovery client to refresh
91
103
podsTopDefaults , err := c .callTool ("pods_top" , map [string ]interface {}{})
92
104
t .Run ("pods_top defaults returns pod metrics from all namespaces" , func (t * testing.T ) {
93
105
if err != nil {
0 commit comments