|
| 1 | +package mcp |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/mark3labs/mcp-go/mcp" |
| 5 | + v1 "k8s.io/api/core/v1" |
| 6 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestEventsList(t *testing.T) { |
| 11 | + testCase(t, func(c *mcpContext) { |
| 12 | + c.withEnvTest() |
| 13 | + toolResult, err := c.callTool("events_list", map[string]interface{}{}) |
| 14 | + t.Run("events_list with no events returns OK", func(t *testing.T) { |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("call tool failed %v", err) |
| 17 | + } |
| 18 | + if toolResult.IsError { |
| 19 | + t.Fatalf("call tool failed") |
| 20 | + } |
| 21 | + if toolResult.Content[0].(mcp.TextContent).Text != "No events found" { |
| 22 | + t.Fatalf("unexpected result %v", toolResult.Content[0].(mcp.TextContent).Text) |
| 23 | + } |
| 24 | + }) |
| 25 | + client := c.newKubernetesClient() |
| 26 | + for _, ns := range []string{"default", "ns-1"} { |
| 27 | + _, _ = client.CoreV1().Events(ns).Create(c.ctx, &v1.Event{ |
| 28 | + ObjectMeta: metav1.ObjectMeta{ |
| 29 | + Name: "an-event-in-" + ns, |
| 30 | + }, |
| 31 | + InvolvedObject: v1.ObjectReference{ |
| 32 | + APIVersion: "v1", |
| 33 | + Kind: "Pod", |
| 34 | + Name: "a-pod", |
| 35 | + Namespace: ns, |
| 36 | + }, |
| 37 | + Type: "Normal", |
| 38 | + Message: "The event message", |
| 39 | + }, metav1.CreateOptions{}) |
| 40 | + } |
| 41 | + toolResult, err = c.callTool("events_list", map[string]interface{}{}) |
| 42 | + t.Run("events_list with events returns all OK", func(t *testing.T) { |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("call tool failed %v", err) |
| 45 | + } |
| 46 | + if toolResult.IsError { |
| 47 | + t.Fatalf("call tool failed") |
| 48 | + } |
| 49 | + if toolResult.Content[0].(mcp.TextContent).Text != "The following events (YAML format) were found:\n"+ |
| 50 | + "- InvolvedObject:\n"+ |
| 51 | + " Kind: Pod\n"+ |
| 52 | + " Name: a-pod\n"+ |
| 53 | + " apiVersion: v1\n"+ |
| 54 | + " Message: The event message\n"+ |
| 55 | + " Namespace: default\n"+ |
| 56 | + " Reason: \"\"\n"+ |
| 57 | + " Timestamp: 0001-01-01 00:00:00 +0000 UTC\n"+ |
| 58 | + " Type: Normal\n"+ |
| 59 | + "- InvolvedObject:\n"+ |
| 60 | + " Kind: Pod\n"+ |
| 61 | + " Name: a-pod\n"+ |
| 62 | + " apiVersion: v1\n"+ |
| 63 | + " Message: The event message\n"+ |
| 64 | + " Namespace: ns-1\n"+ |
| 65 | + " Reason: \"\"\n"+ |
| 66 | + " Timestamp: 0001-01-01 00:00:00 +0000 UTC\n"+ |
| 67 | + " Type: Normal\n" { |
| 68 | + t.Fatalf("unexpected result %v", toolResult.Content[0].(mcp.TextContent).Text) |
| 69 | + } |
| 70 | + }) |
| 71 | + toolResult, err = c.callTool("events_list", map[string]interface{}{ |
| 72 | + "namespace": "ns-1", |
| 73 | + }) |
| 74 | + t.Run("events_list in namespace with events returns from namespace OK", func(t *testing.T) { |
| 75 | + if err != nil { |
| 76 | + t.Fatalf("call tool failed %v", err) |
| 77 | + } |
| 78 | + if toolResult.IsError { |
| 79 | + t.Fatalf("call tool failed") |
| 80 | + } |
| 81 | + if toolResult.Content[0].(mcp.TextContent).Text != "The following events (YAML format) were found:\n"+ |
| 82 | + "- InvolvedObject:\n"+ |
| 83 | + " Kind: Pod\n"+ |
| 84 | + " Name: a-pod\n"+ |
| 85 | + " apiVersion: v1\n"+ |
| 86 | + " Message: The event message\n"+ |
| 87 | + " Namespace: ns-1\n"+ |
| 88 | + " Reason: \"\"\n"+ |
| 89 | + " Timestamp: 0001-01-01 00:00:00 +0000 UTC\n"+ |
| 90 | + " Type: Normal\n" { |
| 91 | + t.Fatalf("unexpected result %v", toolResult.Content[0].(mcp.TextContent).Text) |
| 92 | + } |
| 93 | + }) |
| 94 | + }) |
| 95 | +} |
0 commit comments