-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathintegration_tests.go
More file actions
183 lines (157 loc) · 5.24 KB
/
integration_tests.go
File metadata and controls
183 lines (157 loc) · 5.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
mcpclient "github.com/advanced-security/codeql-development-mcp-server/client/internal/mcp"
itesting "github.com/advanced-security/codeql-development-mcp-server/client/internal/testing"
"github.com/spf13/cobra"
)
var integrationTestsCmd = &cobra.Command{
Use: "integration-tests",
Short: "Run MCP server integration tests from client/integration-tests/",
Long: `Discovers and runs integration test fixtures against a connected MCP server.
Test fixtures live in client/integration-tests/primitives/tools/<tool>/<test>/
and use test-config.json or monitoring-state.json to define tool parameters.`,
RunE: runIntegrationTests,
}
var integrationTestsFlags struct {
tools string
tests string
noInstall bool
timeout int
}
func init() {
rootCmd.AddCommand(integrationTestsCmd)
f := integrationTestsCmd.Flags()
f.StringVar(&integrationTestsFlags.tools, "tools", "", "Comma-separated list of tool names to test")
f.StringVar(&integrationTestsFlags.tests, "tests", "", "Comma-separated list of test case names to run")
f.BoolVar(&integrationTestsFlags.noInstall, "no-install-packs", false, "Skip CodeQL pack installation")
f.IntVar(&integrationTestsFlags.timeout, "timeout", 0, "Per-tool-call timeout in seconds (0 = use client defaults)")
}
// mcpToolCaller adapts the MCP client to the ToolCaller interface.
type mcpToolCaller struct {
client *mcpclient.Client
timeout time.Duration
}
func (c *mcpToolCaller) CallToolRaw(name string, params map[string]any) ([]mcpclient.ContentBlock, bool, error) {
ctx := context.Background()
if c.timeout > 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, c.timeout)
defer cancel()
}
result, err := mcpclient.CallTool(ctx, c.client, name, params)
if err != nil {
return nil, false, err
}
return result.Content, result.IsError, nil
}
func (c *mcpToolCaller) ListToolNames() ([]string, error) {
infos, err := mcpclient.ListTools(context.Background(), c.client)
if err != nil {
return nil, err
}
names := make([]string, len(infos))
for i, t := range infos {
names[i] = t.Name
}
return names, nil
}
func runIntegrationTests(cmd *cobra.Command, _ []string) error {
// Determine repo root
repoRoot, err := findRepoRoot()
if err != nil {
return fmt.Errorf("cannot determine repo root: %w", err)
}
// Change CWD to repo root so the MCP server subprocess resolves
// relative paths (from test-config.json, monitoring-state.json)
// correctly. The codeql CLI also resolves paths from CWD.
if err := os.Chdir(repoRoot); err != nil {
return fmt.Errorf("chdir to repo root: %w", err)
}
fmt.Printf("Working directory: %s\n", repoRoot)
// Set CODEQL_MCP_TMP_DIR so the MCP server subprocess uses the same
// tmp base as the Go runner's {{tmpdir}} placeholder (<repoRoot>/.tmp).
// Without this, the server defaults to <serverPkgRoot>/.tmp (i.e.
// server/.tmp/) which causes log directory validation failures.
tmpBase := filepath.Join(repoRoot, ".tmp")
if os.Getenv("CODEQL_MCP_TMP_DIR") == "" {
os.Setenv("CODEQL_MCP_TMP_DIR", tmpBase)
}
// Connect to MCP server
client := mcpclient.NewClient(mcpclient.Config{
Mode: MCPMode(),
Host: MCPHost(),
Port: MCPPort(),
})
fmt.Println("🔌 Connecting to MCP server...")
ctx := context.Background()
if err := client.Connect(ctx); err != nil {
return fmt.Errorf("connect to MCP server: %w", err)
}
fmt.Println("✅ Connected to MCP server")
// Parse filters
var filterTools, filterTests []string
if integrationTestsFlags.tools != "" {
filterTools = strings.Split(integrationTestsFlags.tools, ",")
}
if integrationTestsFlags.tests != "" {
filterTests = strings.Split(integrationTestsFlags.tests, ",")
}
// Create and run the test runner
runner := itesting.NewRunner(&mcpToolCaller{
client: client,
timeout: time.Duration(integrationTestsFlags.timeout) * time.Second,
}, itesting.RunnerOptions{
RepoRoot: repoRoot,
FilterTools: filterTools,
FilterTests: filterTests,
NoInstallPacks: integrationTestsFlags.noInstall,
})
allPassed, _ := runner.Run()
// Close the MCP client (and its stdio subprocess) before returning.
// The close may time out for stdio servers (Node.js doesn't always
// exit promptly) — log but don't fail the test run for this.
if err := client.Close(); err != nil {
fmt.Fprintf(os.Stderr, "warning: %v\n", err)
}
if !allPassed {
return fmt.Errorf("some integration tests failed")
}
return nil
}
// findRepoRoot walks up from the current directory to find the repo root
// (identified by the presence of codeql-workspace.yml).
func findRepoRoot() (string, error) {
// Try from current working directory
dir, err := os.Getwd()
if err != nil {
return "", err
}
for {
if _, err := os.Stat(filepath.Join(dir, "codeql-workspace.yml")); err == nil {
return dir, nil
}
parent := filepath.Dir(dir)
if parent == dir {
break
}
dir = parent
}
// Fallback: try relative to the binary
exe, err := os.Executable()
if err == nil {
dir = filepath.Dir(exe)
for i := 0; i < 5; i++ {
if _, err := os.Stat(filepath.Join(dir, "codeql-workspace.yml")); err == nil {
return dir, nil
}
dir = filepath.Dir(dir)
}
}
return "", fmt.Errorf("could not find repo root (codeql-workspace.yml)")
}