|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + |
| 10 | + mcpclient "github.com/advanced-security/codeql-development-mcp-server/client/internal/mcp" |
| 11 | + itesting "github.com/advanced-security/codeql-development-mcp-server/client/internal/testing" |
| 12 | + "github.com/mark3labs/mcp-go/mcp" |
| 13 | + "github.com/spf13/cobra" |
| 14 | +) |
| 15 | + |
| 16 | +var integrationTestsCmd = &cobra.Command{ |
| 17 | + Use: "integration-tests", |
| 18 | + Short: "Run MCP server integration tests from client/integration-tests/", |
| 19 | + Long: `Discovers and runs integration test fixtures against a connected MCP server. |
| 20 | +
|
| 21 | +Test fixtures live in client/integration-tests/primitives/tools/<tool>/<test>/ |
| 22 | +and use test-config.json or monitoring-state.json to define tool parameters.`, |
| 23 | + RunE: runIntegrationTests, |
| 24 | +} |
| 25 | + |
| 26 | +var integrationTestsFlags struct { |
| 27 | + tools string |
| 28 | + tests string |
| 29 | + noInstall bool |
| 30 | + timeout int |
| 31 | +} |
| 32 | + |
| 33 | +func init() { |
| 34 | + rootCmd.AddCommand(integrationTestsCmd) |
| 35 | + |
| 36 | + f := integrationTestsCmd.Flags() |
| 37 | + f.StringVar(&integrationTestsFlags.tools, "tools", "", "Comma-separated list of tool names to test") |
| 38 | + f.StringVar(&integrationTestsFlags.tests, "tests", "", "Comma-separated list of test case names to run") |
| 39 | + f.BoolVar(&integrationTestsFlags.noInstall, "no-install-packs", false, "Skip CodeQL pack installation") |
| 40 | + f.IntVar(&integrationTestsFlags.timeout, "timeout", 30, "Per-tool-call timeout in seconds") |
| 41 | +} |
| 42 | + |
| 43 | +// mcpToolCaller adapts the MCP client to the ToolCaller interface. |
| 44 | +type mcpToolCaller struct { |
| 45 | + client *mcpclient.Client |
| 46 | +} |
| 47 | + |
| 48 | +func (c *mcpToolCaller) CallToolRaw(name string, params map[string]any) ([]itesting.ContentBlock, bool, error) { |
| 49 | + result, err := c.client.CallTool(context.Background(), name, params) |
| 50 | + if err != nil { |
| 51 | + return nil, false, err |
| 52 | + } |
| 53 | + |
| 54 | + var blocks []itesting.ContentBlock |
| 55 | + for _, item := range result.Content { |
| 56 | + if textContent, ok := item.(mcp.TextContent); ok { |
| 57 | + blocks = append(blocks, itesting.ContentBlock{ |
| 58 | + Type: "text", |
| 59 | + Text: textContent.Text, |
| 60 | + }) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + return blocks, result.IsError, nil |
| 65 | +} |
| 66 | + |
| 67 | +func (c *mcpToolCaller) ListToolNames() ([]string, error) { |
| 68 | + tools, err := c.client.ListTools(context.Background()) |
| 69 | + if err != nil { |
| 70 | + return nil, err |
| 71 | + } |
| 72 | + names := make([]string, len(tools)) |
| 73 | + for i, t := range tools { |
| 74 | + names[i] = t.Name |
| 75 | + } |
| 76 | + return names, nil |
| 77 | +} |
| 78 | + |
| 79 | +func runIntegrationTests(cmd *cobra.Command, _ []string) error { |
| 80 | + // Determine repo root |
| 81 | + repoRoot, err := findRepoRoot() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("cannot determine repo root: %w", err) |
| 84 | + } |
| 85 | + |
| 86 | + // Change CWD to repo root so the MCP server subprocess resolves |
| 87 | + // relative paths (from test-config.json, monitoring-state.json) |
| 88 | + // correctly. The codeql CLI also resolves paths from CWD. |
| 89 | + if err := os.Chdir(repoRoot); err != nil { |
| 90 | + return fmt.Errorf("chdir to repo root: %w", err) |
| 91 | + } |
| 92 | + fmt.Printf("Working directory: %s\n", repoRoot) |
| 93 | + |
| 94 | + // Connect to MCP server |
| 95 | + client := mcpclient.NewClient(mcpclient.Config{ |
| 96 | + Mode: MCPMode(), |
| 97 | + Host: MCPHost(), |
| 98 | + Port: MCPPort(), |
| 99 | + }) |
| 100 | + |
| 101 | + fmt.Println("🔌 Connecting to MCP server...") |
| 102 | + ctx := context.Background() |
| 103 | + if err := client.Connect(ctx); err != nil { |
| 104 | + return fmt.Errorf("connect to MCP server: %w", err) |
| 105 | + } |
| 106 | + fmt.Println("✅ Connected to MCP server") |
| 107 | + |
| 108 | + // Parse filters |
| 109 | + var filterTools, filterTests []string |
| 110 | + if integrationTestsFlags.tools != "" { |
| 111 | + filterTools = strings.Split(integrationTestsFlags.tools, ",") |
| 112 | + } |
| 113 | + if integrationTestsFlags.tests != "" { |
| 114 | + filterTests = strings.Split(integrationTestsFlags.tests, ",") |
| 115 | + } |
| 116 | + |
| 117 | + // Create and run the test runner |
| 118 | + runner := itesting.NewRunner(&mcpToolCaller{client: client}, itesting.RunnerOptions{ |
| 119 | + RepoRoot: repoRoot, |
| 120 | + FilterTools: filterTools, |
| 121 | + FilterTests: filterTests, |
| 122 | + }) |
| 123 | + |
| 124 | + allPassed, _ := runner.Run() |
| 125 | + |
| 126 | + // Close the MCP client (and its stdio subprocess) before returning. |
| 127 | + client.Close() |
| 128 | + |
| 129 | + if !allPassed { |
| 130 | + return fmt.Errorf("some integration tests failed") |
| 131 | + } |
| 132 | + return nil |
| 133 | +} |
| 134 | + |
| 135 | +// findRepoRoot walks up from the current directory to find the repo root |
| 136 | +// (identified by the presence of codeql-workspace.yml). |
| 137 | +func findRepoRoot() (string, error) { |
| 138 | + // Try from current working directory |
| 139 | + dir, err := os.Getwd() |
| 140 | + if err != nil { |
| 141 | + return "", err |
| 142 | + } |
| 143 | + |
| 144 | + for { |
| 145 | + if _, err := os.Stat(filepath.Join(dir, "codeql-workspace.yml")); err == nil { |
| 146 | + return dir, nil |
| 147 | + } |
| 148 | + parent := filepath.Dir(dir) |
| 149 | + if parent == dir { |
| 150 | + break |
| 151 | + } |
| 152 | + dir = parent |
| 153 | + } |
| 154 | + |
| 155 | + // Fallback: try relative to the binary |
| 156 | + exe, err := os.Executable() |
| 157 | + if err == nil { |
| 158 | + dir = filepath.Dir(exe) |
| 159 | + for i := 0; i < 5; i++ { |
| 160 | + if _, err := os.Stat(filepath.Join(dir, "codeql-workspace.yml")); err == nil { |
| 161 | + return dir, nil |
| 162 | + } |
| 163 | + dir = filepath.Dir(dir) |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + return "", fmt.Errorf("could not find repo root (codeql-workspace.yml)") |
| 168 | +} |
0 commit comments