@@ -3,33 +3,40 @@ package mcp
33import (
44 "context"
55 "encoding/base64"
6+ "path/filepath"
7+ "runtime"
8+ "strings"
9+ "testing"
10+
611 "github.com/containers/kubernetes-mcp-server/pkg/config"
712 "github.com/mark3labs/mcp-go/mcp"
813 corev1 "k8s.io/api/core/v1"
914 "k8s.io/apimachinery/pkg/api/errors"
1015 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1116 "k8s.io/client-go/kubernetes"
12- "path/filepath"
13- "runtime"
17+ "k8s.io/klog/v2"
1418 "sigs.k8s.io/yaml"
15- "strings"
16- "testing"
1719)
1820
1921func TestHelmInstall (t * testing.T ) {
20- testCase (t , func (c * mcpContext ) {
21- c .withEnvTest ()
22- _ , file , _ , _ := runtime .Caller (0 )
22+ testCase (t , true , false , nil , func (c * mcpContext ) {
23+ _ , file , _ , ok := runtime .Caller (0 )
24+ if ! ok {
25+ t .Fatalf ("could not get caller info" )
26+ }
27+ ns := c .mcpServer .k .NamespaceOrDefault ("default" )
28+ klog .Infof ("namespace: %s will be used for helm chart installation" , ns )
2329 chartPath := filepath .Join (filepath .Dir (file ), "testdata" , "helm-chart-no-op" )
2430 toolResult , err := c .callTool ("helm_install" , map [string ]interface {}{
25- "chart" : chartPath ,
31+ "chart" : chartPath ,
32+ "namespace" : ns ,
2633 })
2734 t .Run ("helm_install with local chart and no release name, returns installed chart" , func (t * testing.T ) {
2835 if err != nil {
2936 t .Fatalf ("call tool failed %v" , err )
3037 }
3138 if toolResult .IsError {
32- t .Fatalf ("call tool failed" )
39+ t .Fatalf ("call tool failed %s" , toolResult . Content )
3340 }
3441 var decoded []map [string ]interface {}
3542 err = yaml .Unmarshal ([]byte (toolResult .Content [0 ].(mcp.TextContent ).Text ), & decoded )
@@ -60,12 +67,16 @@ func TestHelmInstall(t *testing.T) {
6067
6168func TestHelmInstallDenied (t * testing.T ) {
6269 deniedResourcesServer := & config.StaticConfig {DeniedResources : []config.GroupVersionKind {{Version : "v1" , Kind : "Secret" }}}
63- testCaseWithContext (t , & mcpContext {staticConfig : deniedResourcesServer }, func (c * mcpContext ) {
64- c .withEnvTest ()
70+ testCaseWithContext (t , & mcpContext {staticConfig : deniedResourcesServer , useEnvTestKubeConfig : true }, func (c * mcpContext ) {
6571 _ , file , _ , _ := runtime .Caller (0 )
6672 chartPath := filepath .Join (filepath .Dir (file ), "testdata" , "helm-chart-secret" )
73+
74+ ns := c .mcpServer .k .NamespaceOrDefault ("default" )
75+ klog .Infof ("namespace: %s will be used for helm chart installation" , ns )
76+
6777 helmInstall , _ := c .callTool ("helm_install" , map [string ]interface {}{
68- "chart" : chartPath ,
78+ "chart" : chartPath ,
79+ "namespace" : ns ,
6980 })
7081 t .Run ("helm_install has error" , func (t * testing.T ) {
7182 if ! helmInstall .IsError {
@@ -83,11 +94,16 @@ func TestHelmInstallDenied(t *testing.T) {
8394}
8495
8596func TestHelmList (t * testing.T ) {
86- testCase (t , func (c * mcpContext ) {
87- c .withEnvTest ()
97+ testCase (t , true , false , nil , func (c * mcpContext ) {
8898 kc := c .newKubernetesClient ()
8999 clearHelmReleases (c .ctx , kc )
90- toolResult , err := c .callTool ("helm_list" , map [string ]interface {}{})
100+
101+ ns := c .mcpServer .k .NamespaceOrDefault ("default" )
102+ klog .Infof ("namespace: %s will be used for helm chart installation" , ns )
103+
104+ toolResult , err := c .callTool ("helm_list" , map [string ]interface {}{
105+ "namespace" : ns ,
106+ })
91107 t .Run ("helm_list with no releases, returns not found" , func (t * testing.T ) {
92108 if err != nil {
93109 t .Fatalf ("call tool failed %v" , err )
@@ -99,7 +115,7 @@ func TestHelmList(t *testing.T) {
99115 t .Fatalf ("unexpected result %v" , toolResult .Content [0 ].(mcp.TextContent ).Text )
100116 }
101117 })
102- _ , _ = kc .CoreV1 ().Secrets ("default" ).Create (c .ctx , & corev1.Secret {
118+ _ , _ = kc .CoreV1 ().Secrets (ns ).Create (c .ctx , & corev1.Secret {
103119 ObjectMeta : metav1.ObjectMeta {
104120 Name : "sh.helm.release.v1.release-to-list" ,
105121 Labels : map [string ]string {"owner" : "helm" , "name" : "release-to-list" },
@@ -111,7 +127,9 @@ func TestHelmList(t *testing.T) {
111127 "}" ))),
112128 },
113129 }, metav1.CreateOptions {})
114- toolResult , err = c .callTool ("helm_list" , map [string ]interface {}{})
130+ toolResult , err = c .callTool ("helm_list" , map [string ]interface {}{
131+ "namespace" : ns ,
132+ })
115133 t .Run ("helm_list with deployed release, returns release" , func (t * testing.T ) {
116134 if err != nil {
117135 t .Fatalf ("call tool failed %v" , err )
@@ -173,12 +191,12 @@ func TestHelmList(t *testing.T) {
173191}
174192
175193func TestHelmUninstall (t * testing.T ) {
176- testCase (t , func (c * mcpContext ) {
177- c .withEnvTest ()
194+ testCase (t , true , false , nil , func (c * mcpContext ) {
178195 kc := c .newKubernetesClient ()
179196 clearHelmReleases (c .ctx , kc )
180197 toolResult , err := c .callTool ("helm_uninstall" , map [string ]interface {}{
181- "name" : "release-to-uninstall" ,
198+ "name" : "release-to-uninstall" ,
199+ "namespace" : "default" ,
182200 })
183201 t .Run ("helm_uninstall with no releases, returns not found" , func (t * testing.T ) {
184202 if err != nil {
@@ -204,7 +222,8 @@ func TestHelmUninstall(t *testing.T) {
204222 },
205223 }, metav1.CreateOptions {})
206224 toolResult , err = c .callTool ("helm_uninstall" , map [string ]interface {}{
207- "name" : "existent-release-to-uninstall" ,
225+ "name" : "existent-release-to-uninstall" ,
226+ "namespace" : "default" ,
208227 })
209228 t .Run ("helm_uninstall with deployed release, returns uninstalled" , func (t * testing.T ) {
210229 if err != nil {
@@ -226,8 +245,7 @@ func TestHelmUninstall(t *testing.T) {
226245
227246func TestHelmUninstallDenied (t * testing.T ) {
228247 deniedResourcesServer := & config.StaticConfig {DeniedResources : []config.GroupVersionKind {{Version : "v1" , Kind : "Secret" }}}
229- testCaseWithContext (t , & mcpContext {staticConfig : deniedResourcesServer }, func (c * mcpContext ) {
230- c .withEnvTest ()
248+ testCaseWithContext (t , & mcpContext {staticConfig : deniedResourcesServer , useEnvTestKubeConfig : true }, func (c * mcpContext ) {
231249 kc := c .newKubernetesClient ()
232250 clearHelmReleases (c .ctx , kc )
233251 _ , _ = kc .CoreV1 ().Secrets ("default" ).Create (c .ctx , & corev1.Secret {
@@ -244,7 +262,8 @@ func TestHelmUninstallDenied(t *testing.T) {
244262 },
245263 }, metav1.CreateOptions {})
246264 helmUninstall , _ := c .callTool ("helm_uninstall" , map [string ]interface {}{
247- "name" : "existent-release-to-uninstall" ,
265+ "name" : "existent-release-to-uninstall" ,
266+ "namespace" : "default" ,
248267 })
249268 t .Run ("helm_uninstall has error" , func (t * testing.T ) {
250269 if ! helmUninstall .IsError {
0 commit comments