Skip to content

Commit 0ec2599

Browse files
authored
fix:test: prevent usage of real cluster in tests (#282)
Signed-off-by: Marc Nuri <[email protected]>
1 parent f63ac7e commit 0ec2599

File tree

4 files changed

+26
-3
lines changed

4 files changed

+26
-3
lines changed

pkg/http/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (c *httpContext) beforeEach(t *testing.T) {
6969
mockKubeConfig := c.mockServer.KubeConfig()
7070
kubeConfig := filepath.Join(t.TempDir(), "config")
7171
_ = clientcmd.WriteToFile(*mockKubeConfig, kubeConfig)
72-
_ = os.Setenv("KUBECONFIG", kubeConfig)
72+
c.StaticConfig.KubeConfig = kubeConfig
7373
// Capture logging
7474
c.klogState = klog.CaptureState()
7575
flags := flag.NewFlagSet("test", flag.ContinueOnError)

pkg/kubernetes/common_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package kubernetes
2+
3+
import (
4+
"os"
5+
"testing"
6+
)
7+
8+
func TestMain(m *testing.M) {
9+
// Set up
10+
_ = os.Setenv("KUBECONFIG", "/dev/null") // Avoid interference from existing kubeconfig
11+
_ = os.Setenv("KUBERNETES_SERVICE_HOST", "") // Avoid interference from in-cluster config
12+
_ = os.Setenv("KUBERNETES_SERVICE_PORT", "") // Avoid interference from in-cluster config
13+
14+
// Run tests
15+
code := m.Run()
16+
17+
// Tear down
18+
os.Exit(code)
19+
}

pkg/mcp/common_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ var envTestUser = envtest.User{Name: "test-user", Groups: []string{"test:users"}
5252

5353
func TestMain(m *testing.M) {
5454
// Set up
55+
_ = os.Setenv("KUBECONFIG", "/dev/null") // Avoid interference from existing kubeconfig
56+
_ = os.Setenv("KUBERNETES_SERVICE_HOST", "") // Avoid interference from in-cluster config
57+
_ = os.Setenv("KUBERNETES_SERVICE_PORT", "") // Avoid interference from in-cluster config
5558
envTestDir, err := store.DefaultStoreDir()
5659
if err != nil {
5760
panic(err)

pkg/mcp/configuration_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package mcp
22

33
import (
4+
"testing"
5+
46
"github.com/containers/kubernetes-mcp-server/pkg/kubernetes"
57
"github.com/mark3labs/mcp-go/mcp"
68
"k8s.io/client-go/rest"
79
v1 "k8s.io/client-go/tools/clientcmd/api/v1"
810
"sigs.k8s.io/yaml"
9-
"testing"
1011
)
1112

1213
func TestConfigurationView(t *testing.T) {
@@ -78,7 +79,7 @@ func TestConfigurationView(t *testing.T) {
7879
})
7980
t.Run("configuration_view with minified=false returns additional context info", func(t *testing.T) {
8081
if len(decoded.Contexts) != 2 {
81-
t.Errorf("invalid context count, expected2, got %v", len(decoded.Contexts))
82+
t.Fatalf("invalid context count, expected2, got %v", len(decoded.Contexts))
8283
}
8384
if decoded.Contexts[0].Name != "additional-context" {
8485
t.Errorf("additional-context not found: %v", decoded.Contexts)

0 commit comments

Comments
 (0)