From 0ec2599bd8a4cd3543d9755d49c2d414e45814c0 Mon Sep 17 00:00:00 2001 From: Marc Nuri Date: Tue, 26 Aug 2025 14:52:27 +0200 Subject: [PATCH] fix:test: prevent usage of real cluster in tests (#282) Signed-off-by: Marc Nuri --- pkg/http/http_test.go | 2 +- pkg/kubernetes/common_test.go | 19 +++++++++++++++++++ pkg/mcp/common_test.go | 3 +++ pkg/mcp/configuration_test.go | 5 +++-- 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 pkg/kubernetes/common_test.go diff --git a/pkg/http/http_test.go b/pkg/http/http_test.go index 0ceaf2e..566f1f8 100644 --- a/pkg/http/http_test.go +++ b/pkg/http/http_test.go @@ -69,7 +69,7 @@ func (c *httpContext) beforeEach(t *testing.T) { mockKubeConfig := c.mockServer.KubeConfig() kubeConfig := filepath.Join(t.TempDir(), "config") _ = clientcmd.WriteToFile(*mockKubeConfig, kubeConfig) - _ = os.Setenv("KUBECONFIG", kubeConfig) + c.StaticConfig.KubeConfig = kubeConfig // Capture logging c.klogState = klog.CaptureState() flags := flag.NewFlagSet("test", flag.ContinueOnError) diff --git a/pkg/kubernetes/common_test.go b/pkg/kubernetes/common_test.go new file mode 100644 index 0000000..21cd403 --- /dev/null +++ b/pkg/kubernetes/common_test.go @@ -0,0 +1,19 @@ +package kubernetes + +import ( + "os" + "testing" +) + +func TestMain(m *testing.M) { + // Set up + _ = os.Setenv("KUBECONFIG", "/dev/null") // Avoid interference from existing kubeconfig + _ = os.Setenv("KUBERNETES_SERVICE_HOST", "") // Avoid interference from in-cluster config + _ = os.Setenv("KUBERNETES_SERVICE_PORT", "") // Avoid interference from in-cluster config + + // Run tests + code := m.Run() + + // Tear down + os.Exit(code) +} diff --git a/pkg/mcp/common_test.go b/pkg/mcp/common_test.go index 8e4e49d..c372a14 100644 --- a/pkg/mcp/common_test.go +++ b/pkg/mcp/common_test.go @@ -52,6 +52,9 @@ var envTestUser = envtest.User{Name: "test-user", Groups: []string{"test:users"} func TestMain(m *testing.M) { // Set up + _ = os.Setenv("KUBECONFIG", "/dev/null") // Avoid interference from existing kubeconfig + _ = os.Setenv("KUBERNETES_SERVICE_HOST", "") // Avoid interference from in-cluster config + _ = os.Setenv("KUBERNETES_SERVICE_PORT", "") // Avoid interference from in-cluster config envTestDir, err := store.DefaultStoreDir() if err != nil { panic(err) diff --git a/pkg/mcp/configuration_test.go b/pkg/mcp/configuration_test.go index 57fea48..108ce62 100644 --- a/pkg/mcp/configuration_test.go +++ b/pkg/mcp/configuration_test.go @@ -1,12 +1,13 @@ package mcp import ( + "testing" + "github.com/containers/kubernetes-mcp-server/pkg/kubernetes" "github.com/mark3labs/mcp-go/mcp" "k8s.io/client-go/rest" v1 "k8s.io/client-go/tools/clientcmd/api/v1" "sigs.k8s.io/yaml" - "testing" ) func TestConfigurationView(t *testing.T) { @@ -78,7 +79,7 @@ func TestConfigurationView(t *testing.T) { }) t.Run("configuration_view with minified=false returns additional context info", func(t *testing.T) { if len(decoded.Contexts) != 2 { - t.Errorf("invalid context count, expected2, got %v", len(decoded.Contexts)) + t.Fatalf("invalid context count, expected2, got %v", len(decoded.Contexts)) } if decoded.Contexts[0].Name != "additional-context" { t.Errorf("additional-context not found: %v", decoded.Contexts)