11package mcp
22
33import (
4+ "github.com/manusa/kubernetes-mcp-server/pkg/kubernetes"
45 "github.com/mark3labs/mcp-go/mcp"
6+ "k8s.io/client-go/rest"
57 v1 "k8s.io/client-go/tools/clientcmd/api/v1"
68 "sigs.k8s.io/yaml"
79 "testing"
@@ -13,63 +15,116 @@ func TestConfigurationView(t *testing.T) {
1315 t .Run ("configuration_view returns configuration" , func (t * testing.T ) {
1416 if err != nil {
1517 t .Fatalf ("call tool failed %v" , err )
16- return
1718 }
1819 })
1920 var decoded * v1.Config
2021 err = yaml .Unmarshal ([]byte (toolResult .Content [0 ].(mcp.TextContent ).Text ), & decoded )
2122 t .Run ("configuration_view has yaml content" , func (t * testing.T ) {
2223 if err != nil {
2324 t .Fatalf ("invalid tool result content %v" , err )
24- return
2525 }
2626 })
2727 t .Run ("configuration_view returns current-context" , func (t * testing.T ) {
2828 if decoded .CurrentContext != "fake-context" {
2929 t .Fatalf ("fake-context not found: %v" , decoded .CurrentContext )
30- return
3130 }
3231 })
3332 t .Run ("configuration_view returns context info" , func (t * testing.T ) {
3433 if len (decoded .Contexts ) != 1 {
3534 t .Fatalf ("invalid context count, expected 1, got %v" , len (decoded .Contexts ))
36- return
3735 }
3836 if decoded .Contexts [0 ].Name != "fake-context" {
3937 t .Fatalf ("fake-context not found: %v" , decoded .Contexts )
40- return
4138 }
4239 if decoded .Contexts [0 ].Context .Cluster != "fake" {
4340 t .Fatalf ("fake-cluster not found: %v" , decoded .Contexts )
44- return
4541 }
4642 if decoded .Contexts [0 ].Context .AuthInfo != "fake" {
4743 t .Fatalf ("fake-auth not found: %v" , decoded .Contexts )
48- return
4944 }
5045 })
5146 t .Run ("configuration_view returns cluster info" , func (t * testing.T ) {
5247 if len (decoded .Clusters ) != 1 {
5348 t .Fatalf ("invalid cluster count, expected 1, got %v" , len (decoded .Clusters ))
54- return
5549 }
5650 if decoded .Clusters [0 ].Name != "fake" {
5751 t .Fatalf ("fake-cluster not found: %v" , decoded .Clusters )
58- return
5952 }
6053 if decoded .Clusters [0 ].Cluster .Server != "https://example.com" {
6154 t .Fatalf ("fake-server not found: %v" , decoded .Clusters )
62- return
6355 }
6456 })
6557 t .Run ("configuration_view returns auth info" , func (t * testing.T ) {
6658 if len (decoded .AuthInfos ) != 1 {
6759 t .Fatalf ("invalid auth info count, expected 1, got %v" , len (decoded .AuthInfos ))
68- return
6960 }
7061 if decoded .AuthInfos [0 ].Name != "fake" {
7162 t .Fatalf ("fake-auth not found: %v" , decoded .AuthInfos )
72- return
63+ }
64+ })
65+ })
66+ }
67+
68+ func TestConfigurationViewInCluster (t * testing.T ) {
69+ kubernetes .InClusterConfig = func () (* rest.Config , error ) {
70+ return & rest.Config {
71+ Host : "https://kubernetes.default.svc" ,
72+ BearerToken : "fake-token" ,
73+ }, nil
74+ }
75+ defer func () {
76+ kubernetes .InClusterConfig = rest .InClusterConfig
77+ }()
78+ testCase (t , func (c * mcpContext ) {
79+ toolResult , err := c .callTool ("configuration_view" , map [string ]interface {}{})
80+ t .Run ("configuration_view returns configuration" , func (t * testing.T ) {
81+ if err != nil {
82+ t .Fatalf ("call tool failed %v" , err )
83+ }
84+ })
85+ var decoded * v1.Config
86+ err = yaml .Unmarshal ([]byte (toolResult .Content [0 ].(mcp.TextContent ).Text ), & decoded )
87+ t .Run ("configuration_view has yaml content" , func (t * testing.T ) {
88+ if err != nil {
89+ t .Fatalf ("invalid tool result content %v" , err )
90+ }
91+ })
92+ t .Run ("configuration_view returns current-context" , func (t * testing.T ) {
93+ if decoded .CurrentContext != "context" {
94+ t .Fatalf ("context not found: %v" , decoded .CurrentContext )
95+ }
96+ })
97+ t .Run ("configuration_view returns context info" , func (t * testing.T ) {
98+ if len (decoded .Contexts ) != 1 {
99+ t .Fatalf ("invalid context count, expected 1, got %v" , len (decoded .Contexts ))
100+ }
101+ if decoded .Contexts [0 ].Name != "context" {
102+ t .Fatalf ("context not found: %v" , decoded .Contexts )
103+ }
104+ if decoded .Contexts [0 ].Context .Cluster != "cluster" {
105+ t .Fatalf ("cluster not found: %v" , decoded .Contexts )
106+ }
107+ if decoded .Contexts [0 ].Context .AuthInfo != "user" {
108+ t .Fatalf ("user not found: %v" , decoded .Contexts )
109+ }
110+ })
111+ t .Run ("configuration_view returns cluster info" , func (t * testing.T ) {
112+ if len (decoded .Clusters ) != 1 {
113+ t .Fatalf ("invalid cluster count, expected 1, got %v" , len (decoded .Clusters ))
114+ }
115+ if decoded .Clusters [0 ].Name != "cluster" {
116+ t .Fatalf ("cluster not found: %v" , decoded .Clusters )
117+ }
118+ if decoded .Clusters [0 ].Cluster .Server != "https://kubernetes.default.svc" {
119+ t .Fatalf ("server not found: %v" , decoded .Clusters )
120+ }
121+ })
122+ t .Run ("configuration_view returns auth info" , func (t * testing.T ) {
123+ if len (decoded .AuthInfos ) != 1 {
124+ t .Fatalf ("invalid auth info count, expected 1, got %v" , len (decoded .AuthInfos ))
125+ }
126+ if decoded .AuthInfos [0 ].Name != "user" {
127+ t .Fatalf ("user not found: %v" , decoded .AuthInfos )
73128 }
74129 })
75130 })
0 commit comments