1
1
package mcp
2
2
3
3
import (
4
+ "github.com/manusa/kubernetes-mcp-server/pkg/kubernetes"
4
5
"github.com/mark3labs/mcp-go/mcp"
6
+ "k8s.io/client-go/rest"
5
7
v1 "k8s.io/client-go/tools/clientcmd/api/v1"
6
8
"sigs.k8s.io/yaml"
7
9
"testing"
@@ -13,63 +15,116 @@ func TestConfigurationView(t *testing.T) {
13
15
t .Run ("configuration_view returns configuration" , func (t * testing.T ) {
14
16
if err != nil {
15
17
t .Fatalf ("call tool failed %v" , err )
16
- return
17
18
}
18
19
})
19
20
var decoded * v1.Config
20
21
err = yaml .Unmarshal ([]byte (toolResult .Content [0 ].(mcp.TextContent ).Text ), & decoded )
21
22
t .Run ("configuration_view has yaml content" , func (t * testing.T ) {
22
23
if err != nil {
23
24
t .Fatalf ("invalid tool result content %v" , err )
24
- return
25
25
}
26
26
})
27
27
t .Run ("configuration_view returns current-context" , func (t * testing.T ) {
28
28
if decoded .CurrentContext != "fake-context" {
29
29
t .Fatalf ("fake-context not found: %v" , decoded .CurrentContext )
30
- return
31
30
}
32
31
})
33
32
t .Run ("configuration_view returns context info" , func (t * testing.T ) {
34
33
if len (decoded .Contexts ) != 1 {
35
34
t .Fatalf ("invalid context count, expected 1, got %v" , len (decoded .Contexts ))
36
- return
37
35
}
38
36
if decoded .Contexts [0 ].Name != "fake-context" {
39
37
t .Fatalf ("fake-context not found: %v" , decoded .Contexts )
40
- return
41
38
}
42
39
if decoded .Contexts [0 ].Context .Cluster != "fake" {
43
40
t .Fatalf ("fake-cluster not found: %v" , decoded .Contexts )
44
- return
45
41
}
46
42
if decoded .Contexts [0 ].Context .AuthInfo != "fake" {
47
43
t .Fatalf ("fake-auth not found: %v" , decoded .Contexts )
48
- return
49
44
}
50
45
})
51
46
t .Run ("configuration_view returns cluster info" , func (t * testing.T ) {
52
47
if len (decoded .Clusters ) != 1 {
53
48
t .Fatalf ("invalid cluster count, expected 1, got %v" , len (decoded .Clusters ))
54
- return
55
49
}
56
50
if decoded .Clusters [0 ].Name != "fake" {
57
51
t .Fatalf ("fake-cluster not found: %v" , decoded .Clusters )
58
- return
59
52
}
60
53
if decoded .Clusters [0 ].Cluster .Server != "https://example.com" {
61
54
t .Fatalf ("fake-server not found: %v" , decoded .Clusters )
62
- return
63
55
}
64
56
})
65
57
t .Run ("configuration_view returns auth info" , func (t * testing.T ) {
66
58
if len (decoded .AuthInfos ) != 1 {
67
59
t .Fatalf ("invalid auth info count, expected 1, got %v" , len (decoded .AuthInfos ))
68
- return
69
60
}
70
61
if decoded .AuthInfos [0 ].Name != "fake" {
71
62
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 )
73
128
}
74
129
})
75
130
})
0 commit comments