fix:test: prevent usage of real cluster in tests (#282)

Signed-off-by: Marc Nuri <marc@marcnuri.com>
This commit is contained in:
Marc Nuri
2025-08-26 14:52:27 +02:00
committed by GitHub
parent f63ac7efb5
commit 0ec2599bd8
4 changed files with 26 additions and 3 deletions

View File

@@ -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)

View File

@@ -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)
}

View File

@@ -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)

View File

@@ -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)