Skip to content

Commit 6f3a2e0

Browse files
authored
test(mcp): refactor WatchKubeConfig tests to use testify and improve structure (#432)
Signed-off-by: Marc Nuri <[email protected]>
1 parent d6f99e9 commit 6f3a2e0

File tree

2 files changed

+39
-30
lines changed

2 files changed

+39
-30
lines changed

internal/test/mcp.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport
2323
var err error
2424
ret := &McpClient{ctx: t.Context()}
2525
ret.testServer = httptest.NewServer(mcpHttpServer)
26+
options = append(options, transport.WithContinuousListening())
2627
ret.Client, err = client.NewStreamableHttpClient(ret.testServer.URL+"/mcp", options...)
2728
require.NoError(t, err, "Expected no error creating MCP client")
2829
err = ret.Start(t.Context())

pkg/mcp/mcp_test.go

Lines changed: 38 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"net/http"
66
"os"
7-
"path/filepath"
87
"runtime"
98
"testing"
109
"time"
@@ -15,38 +14,47 @@ import (
1514
"github.com/stretchr/testify/suite"
1615
)
1716

18-
func TestWatchKubeConfig(t *testing.T) {
17+
type WatchKubeConfigSuite struct {
18+
BaseMcpSuite
19+
}
20+
21+
func (s *WatchKubeConfigSuite) SetupTest() {
22+
s.BaseMcpSuite.SetupTest()
23+
kubeconfig := test.KubeConfigFake()
24+
s.Cfg.KubeConfig = test.KubeconfigFile(s.T(), kubeconfig)
25+
}
26+
27+
func (s *WatchKubeConfigSuite) TestNotifiesToolsChange() {
1928
if runtime.GOOS != "linux" && runtime.GOOS != "darwin" {
20-
t.Skip("Skipping test on non-Unix-like platforms")
29+
s.T().Skip("Skipping test on non-Unix-like platforms")
2130
}
22-
testCase(t, func(c *mcpContext) {
23-
// Given
24-
withTimeout, cancel := context.WithTimeout(c.ctx, 5*time.Second)
25-
defer cancel()
26-
var notification *mcp.JSONRPCNotification
27-
c.mcpClient.OnNotification(func(n mcp.JSONRPCNotification) {
28-
notification = &n
29-
})
30-
// When
31-
f, _ := os.OpenFile(filepath.Join(c.tempDir, "config"), os.O_APPEND|os.O_WRONLY, 0644)
32-
_, _ = f.WriteString("\n")
33-
for notification == nil {
34-
select {
35-
case <-withTimeout.Done():
36-
default:
37-
time.Sleep(100 * time.Millisecond)
38-
}
39-
}
40-
// Then
41-
t.Run("WatchKubeConfig notifies tools change", func(t *testing.T) {
42-
if notification == nil {
43-
t.Fatalf("WatchKubeConfig did not notify")
44-
}
45-
if notification.Method != "notifications/tools/list_changed" {
46-
t.Fatalf("WatchKubeConfig did not notify tools change, got %s", notification.Method)
47-
}
48-
})
31+
// Given
32+
s.InitMcpClient()
33+
withTimeout, cancel := context.WithTimeout(s.T().Context(), 5*time.Second)
34+
defer cancel()
35+
var notification *mcp.JSONRPCNotification
36+
s.OnNotification(func(n mcp.JSONRPCNotification) {
37+
notification = &n
4938
})
39+
// When
40+
f, _ := os.OpenFile(s.Cfg.KubeConfig, os.O_APPEND|os.O_WRONLY, 0644)
41+
_, _ = f.WriteString("\n")
42+
_ = f.Close()
43+
for notification == nil {
44+
select {
45+
case <-withTimeout.Done():
46+
s.FailNow("timeout waiting for WatchKubeConfig notification")
47+
default:
48+
time.Sleep(100 * time.Millisecond)
49+
}
50+
}
51+
// Then
52+
s.NotNil(notification, "WatchKubeConfig did not notify")
53+
s.Equal("notifications/tools/list_changed", notification.Method, "WatchKubeConfig did not notify tools change")
54+
}
55+
56+
func TestWatchKubeConfig(t *testing.T) {
57+
suite.Run(t, new(WatchKubeConfigSuite))
5058
}
5159

5260
type McpHeadersSuite struct {

0 commit comments

Comments
 (0)