|
| 1 | +//go:build integration |
| 2 | + |
| 3 | +package integration |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/stackrox/stackrox-mcp/internal/testutil" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +// TestIntegration_ListTools verifies that all expected tools are registered. |
| 15 | +func TestIntegration_ListTools(t *testing.T) { |
| 16 | + client := testutil.SetupInitializedClient(t, testutil.CreateIntegrationMCPClient) |
| 17 | + |
| 18 | + ctx := context.Background() |
| 19 | + result, err := client.ListTools(ctx) |
| 20 | + require.NoError(t, err) |
| 21 | + |
| 22 | + // Verify we have tools registered |
| 23 | + assert.NotEmpty(t, result.Tools, "should have tools registered") |
| 24 | + |
| 25 | + // Check for specific tools we expect |
| 26 | + toolNames := make([]string, 0, len(result.Tools)) |
| 27 | + for _, tool := range result.Tools { |
| 28 | + toolNames = append(toolNames, tool.Name) |
| 29 | + } |
| 30 | + |
| 31 | + assert.Contains(t, toolNames, "get_deployments_for_cve", "should have get_deployments_for_cve tool") |
| 32 | + assert.Contains(t, toolNames, "list_clusters", "should have list_clusters tool") |
| 33 | +} |
| 34 | + |
| 35 | +// TestIntegration_ToolCalls tests successful tool calls using table-driven tests. |
| 36 | +func TestIntegration_ToolCalls(t *testing.T) { |
| 37 | + tests := map[string]struct { |
| 38 | + toolName string |
| 39 | + args map[string]any |
| 40 | + expectedJSON string // expected JSON response for exact comparison |
| 41 | + }{ |
| 42 | + "get_deployments_for_cve with Log4Shell": { |
| 43 | + toolName: "get_deployments_for_cve", |
| 44 | + args: map[string]any{"cveName": Log4ShellFixture.CVEName}, |
| 45 | + expectedJSON: Log4ShellFixture.ExpectedJSON, |
| 46 | + }, |
| 47 | + "get_deployments_for_cve with non-existent CVE": { |
| 48 | + toolName: "get_deployments_for_cve", |
| 49 | + args: map[string]any{"cveName": "CVE-9999-99999"}, |
| 50 | + expectedJSON: EmptyDeploymentsJSON, |
| 51 | + }, |
| 52 | + "list_clusters": { |
| 53 | + toolName: "list_clusters", |
| 54 | + args: map[string]any{}, |
| 55 | + expectedJSON: AllClustersFixture.ExpectedJSON, |
| 56 | + }, |
| 57 | + "get_clusters_with_orchestrator_cve": { |
| 58 | + toolName: "get_clusters_with_orchestrator_cve", |
| 59 | + args: map[string]any{"cveName": "CVE-2099-00001"}, |
| 60 | + expectedJSON: EmptyClustersForCVEJSON, |
| 61 | + }, |
| 62 | + } |
| 63 | + |
| 64 | + for name, tt := range tests { |
| 65 | + t.Run(name, func(t *testing.T) { |
| 66 | + client := testutil.SetupInitializedClient(t, testutil.CreateIntegrationMCPClient) |
| 67 | + result := testutil.CallToolAndGetResult(t, client, tt.toolName, tt.args) |
| 68 | + |
| 69 | + responseText := testutil.GetTextContent(t, result) |
| 70 | + assert.JSONEq(t, tt.expectedJSON, responseText) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestIntegration_ToolCallErrors tests error handling using table-driven tests. |
| 76 | +func TestIntegration_ToolCallErrors(t *testing.T) { |
| 77 | + tests := map[string]struct { |
| 78 | + toolName string |
| 79 | + args map[string]any |
| 80 | + expectedErrorMsg string |
| 81 | + }{ |
| 82 | + "get_deployments_for_cve missing CVE name": { |
| 83 | + toolName: "get_deployments_for_cve", |
| 84 | + args: map[string]any{}, |
| 85 | + expectedErrorMsg: "cveName", |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for name, tt := range tests { |
| 90 | + t.Run(name, func(t *testing.T) { |
| 91 | + client := testutil.SetupInitializedClient(t, testutil.CreateIntegrationMCPClient) |
| 92 | + |
| 93 | + ctx := context.Background() |
| 94 | + _, err := client.CallTool(ctx, tt.toolName, tt.args) |
| 95 | + |
| 96 | + // Validation errors are returned as protocol errors, not tool errors |
| 97 | + require.Error(t, err, "should receive protocol error for invalid params") |
| 98 | + assert.Contains(t, err.Error(), tt.expectedErrorMsg) |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments