|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/mark3labs/mcp-go/mcp" |
| 8 | + corev1 "k8s.io/api/core/v1" |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | +) |
| 11 | + |
| 12 | +func TestNodeLog(t *testing.T) { |
| 13 | + testCase(t, func(c *mcpContext) { |
| 14 | + c.withEnvTest() |
| 15 | + |
| 16 | + // Create test node |
| 17 | + kubernetesAdmin := c.newKubernetesClient() |
| 18 | + node := &corev1.Node{ |
| 19 | + ObjectMeta: metav1.ObjectMeta{ |
| 20 | + Name: "test-node-log", |
| 21 | + }, |
| 22 | + Status: corev1.NodeStatus{ |
| 23 | + Addresses: []corev1.NodeAddress{ |
| 24 | + {Type: corev1.NodeInternalIP, Address: "192.168.1.10"}, |
| 25 | + }, |
| 26 | + }, |
| 27 | + } |
| 28 | + |
| 29 | + _, _ = kubernetesAdmin.CoreV1().Nodes().Create(c.ctx, node, metav1.CreateOptions{}) |
| 30 | + |
| 31 | + // Test node_log tool |
| 32 | + toolResult, err := c.callTool("node_log", map[string]interface{}{ |
| 33 | + "name": "test-node-log", |
| 34 | + }) |
| 35 | + |
| 36 | + t.Run("node_log returns successfully or with expected error", func(t *testing.T) { |
| 37 | + if err != nil { |
| 38 | + t.Fatalf("call tool failed: %v", err) |
| 39 | + } |
| 40 | + // Node logs might not be available in test environment |
| 41 | + // We just check that the tool call completes |
| 42 | + if toolResult.IsError { |
| 43 | + content := toolResult.Content[0].(mcp.TextContent).Text |
| 44 | + // Expected error messages in test environment |
| 45 | + if !strings.Contains(content, "failed to get node logs") && |
| 46 | + !strings.Contains(content, "not logged any message yet") { |
| 47 | + t.Logf("tool returned error (expected in test environment): %v", content) |
| 48 | + } |
| 49 | + } |
| 50 | + }) |
| 51 | + }) |
| 52 | +} |
| 53 | + |
| 54 | +func TestNodeLogMissingArguments(t *testing.T) { |
| 55 | + testCase(t, func(c *mcpContext) { |
| 56 | + c.withEnvTest() |
| 57 | + |
| 58 | + t.Run("node_log requires name", func(t *testing.T) { |
| 59 | + toolResult, err := c.callTool("node_log", map[string]interface{}{}) |
| 60 | + |
| 61 | + if err == nil && !toolResult.IsError { |
| 62 | + t.Fatal("expected error when name is missing") |
| 63 | + } |
| 64 | + }) |
| 65 | + }) |
| 66 | +} |
0 commit comments