Skip to content

Integration tests#50

Open
janisz wants to merge 3 commits intomainfrom
integration_tests
Open

Integration tests#50
janisz wants to merge 3 commits intomainfrom
integration_tests

Conversation

@janisz
Copy link
Contributor

@janisz janisz commented Mar 5, 2026

Description

Run integration tests against wiremock

Validation

CI + Coverage

@github-actions
Copy link

github-actions bot commented Mar 5, 2026

E2E Test Results

Commit: b3fcf5c
Workflow Run: View Details

=== Evaluation Summary ===

  ✓ list-clusters (assertions: 3/3)
  ✓ cve-detected-workloads (assertions: 3/3)
  ✓ cve-detected-clusters (assertions: 3/3)
  ✗ cve-nonexistent (assertions: 2/3)
      one or more verification steps failed
      - MaxToolCalls: Too many tool calls: expected <= 3, got 12
  ✓ cve-cluster-does-exist (assertions: 3/3)
  ~ cve-cluster-does-not-exist (assertions: 2/3)
      - ToolsUsed: Required tool not called: server=stackrox-mcp, tool=, pattern=list_clusters
  ✓ cve-clusters-general (assertions: 3/3)
  ✓ cve-cluster-list (assertions: 3/3)
  ~ cve-log4shell (assertions: 2/3)
      - MaxToolCalls: Too many tool calls: expected <= 3, got 4
  ✓ cve-multiple (assertions: 3/3)
  ~ rhsa-not-supported (assertions: 1/2)
      - MaxToolCalls: Too many tool calls: expected <= 1, got 3

Tasks:      10/11 passed (90.91%)
Assertions: 28/32 passed (87.50%)

Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>
Co-authored-by: Claude Sonnet 4.5 <noreply@anthropic.com>
Co-authored-by: Mladen Todorovic <mtodor@gmail.com>

Add Go integration tests for MCP server with WireMock

Implements integration tests that verify MCP server functionality using
stdio transport and WireMock as a mock StackRox Central backend.

**Key Changes:**
- Created integration test suite in `integration/` with build tag
- Implemented stdio-based MCP client in `internal/testutil/mcp.go`
- Added WireMock readiness check in `internal/testutil/wiremock.go`
- Added test fixtures for expected WireMock response data
- Updated Makefile with integration test targets
- Updated GitHub Actions workflow to run integration tests in CI

**Test Coverage:**
- MCP protocol (initialize, list tools)
- Tool invocations (list_clusters, get_deployments_for_cve, etc.)
- Error handling (missing parameters)
- Success and error scenarios

Tests use stdio transport for simplicity and better control over the
MCP server lifecycle. Each test starts a fresh MCP server subprocess
and communicates via JSON-RPC over stdin/stdout.

TODO: Fix WireMock request matching for CVE-2021-44228 deployment query

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

fix: Fix WireMock JSONPath patterns for deployment CVE queries

Apply the same JSONPath fix from commit 01f58ab to deployments.json.

The original mappings used $.query[?(@.query =~ ...)] which looked for
a nested array structure, but gRPC protobuf-to-JSON conversion creates
a simple object with a 'query' field (lowercase).

Changed all deployment CVE mappings from:
  $.query[?(@.query =~ /.*CVE-XXX.*/)]
to:
  $[?(@.query =~ /.*CVE-XXX.*/)]

This fixes the TestIntegration_GetDeploymentsForCVE_Log4Shell test which
was failing because WireMock couldn't match the gRPC requests.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

test: Unskip Log4Shell integration test

The WireMock JSONPath fix for deployments.json resolves the issue
that was causing this test to fail.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

refactor: Simplify integration tests and remove manual protoc installation

This commit implements two major simplifications to the test infrastructure:

1. **Eliminate TestMain Pre-compilation Pattern**:
   - Extract main() body into new internal/app package with Run() function
   - Tests now call app.Run() in-process via io.Pipe() instead of subprocess
   - Removes 60+ lines of build/setup code from integration_test.go
   - Enables full code coverage (previously main.go had 0% coverage)
   - Faster test execution (no binary compilation overhead)
   - Better debugging (direct function calls vs exec)

2. **Remove Manual Protoc Installation from GitHub Actions**:
   - Delete manual protoc 3.20.1 download from workflow
   - Rely on Makefile's automatic protoc 32.1 installation
   - Single source of truth for protoc version
   - Eliminates version mismatch between CI and local dev

Changes:
- Create internal/app/app.go with Run() function extracted from main()
- Update server.Start() to accept optional stdin/stdout parameters
- Refactor testutil.NewMCPClient() to use ServerRunFunc callback
- Remove TestMain, buildMCPBinary, global vars from integration tests
- Update all integration test functions to use createMCPClient() helper
- Remove "Install protoc" step from .github/workflows/test.yml

Benefits:
- 43 net lines removed (-161 +118)
- Better code coverage
- Simpler test maintenance
- Aligned with Go testing best practices

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

fix: Add missing retry/timeout config to integration test setup

The test config was missing RequestTimeout, MaxRetries, InitialBackoff,
and MaxBackoff fields, causing the gRPC client to use zero values instead
of the defaults (30s timeout, 3 retries).

With MaxRetries=0, the retry loop never executed: `for attempt := range 0`
This caused "Request failed after all retries ... attempts=0" warnings
and empty responses from WireMock.

Solution: Explicitly set the retry/timeout fields to match the defaults
defined in internal/config/config.go.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>

Add Go integration tests for MCP server with WireMock

Implements integration tests that verify MCP server functionality using
stdio transport and WireMock as a mock StackRox Central backend.

**Key Changes:**
- Created integration test suite in `integration/` with build tag
- Implemented stdio-based MCP client in `internal/testutil/mcp.go`
- Added WireMock readiness check in `internal/testutil/wiremock.go`
- Added test fixtures for expected WireMock response data
- Updated Makefile with integration test targets
- Updated GitHub Actions workflow to run integration tests in CI

**Test Coverage:**
- MCP protocol (initialize, list tools)
- Tool invocations (list_clusters, get_deployments_for_cve, etc.)
- Error handling (missing parameters)
- Success and error scenarios

Tests use stdio transport for simplicity and better control over the
MCP server lifecycle. Each test starts a fresh MCP server subprocess
and communicates via JSON-RPC over stdin/stdout.

TODO: Fix WireMock request matching for CVE-2021-44228 deployment query

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

fix: Fix WireMock JSONPath patterns for deployment CVE queries

Apply the same JSONPath fix from commit 01f58ab to deployments.json.

The original mappings used $.query[?(@.query =~ ...)] which looked for
a nested array structure, but gRPC protobuf-to-JSON conversion creates
a simple object with a 'query' field (lowercase).

Changed all deployment CVE mappings from:
  $.query[?(@.query =~ /.*CVE-XXX.*/)]
to:
  $[?(@.query =~ /.*CVE-XXX.*/)]

This fixes the TestIntegration_GetDeploymentsForCVE_Log4Shell test which
was failing because WireMock couldn't match the gRPC requests.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

test: Unskip Log4Shell integration test

The WireMock JSONPath fix for deployments.json resolves the issue
that was causing this test to fail.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

refactor: Simplify integration tests and remove manual protoc installation

This commit implements two major simplifications to the test infrastructure:

1. **Eliminate TestMain Pre-compilation Pattern**:
   - Extract main() body into new internal/app package with Run() function
   - Tests now call app.Run() in-process via io.Pipe() instead of subprocess
   - Removes 60+ lines of build/setup code from integration_test.go
   - Enables full code coverage (previously main.go had 0% coverage)
   - Faster test execution (no binary compilation overhead)
   - Better debugging (direct function calls vs exec)

2. **Remove Manual Protoc Installation from GitHub Actions**:
   - Delete manual protoc 3.20.1 download from workflow
   - Rely on Makefile's automatic protoc 32.1 installation
   - Single source of truth for protoc version
   - Eliminates version mismatch between CI and local dev

Changes:
- Create internal/app/app.go with Run() function extracted from main()
- Update server.Start() to accept optional stdin/stdout parameters
- Refactor testutil.NewMCPClient() to use ServerRunFunc callback
- Remove TestMain, buildMCPBinary, global vars from integration tests
- Update all integration test functions to use createMCPClient() helper
- Remove "Install protoc" step from .github/workflows/test.yml

Benefits:
- 43 net lines removed (-161 +118)
- Better code coverage
- Simpler test maintenance
- Aligned with Go testing best practices

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

fix: Add missing retry/timeout config to integration test setup

The test config was missing RequestTimeout, MaxRetries, InitialBackoff,
and MaxBackoff fields, causing the gRPC client to use zero values instead
of the defaults (30s timeout, 3 retries).

With MaxRetries=0, the retry loop never executed: `for attempt := range 0`
This caused "Request failed after all retries ... attempts=0" warnings
and empty responses from WireMock.

Solution: Explicitly set the retry/timeout fields to match the defaults
defined in internal/config/config.go.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>

cleanup

Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>

Refactor integration tests and migrate to official MCP SDK

This commit refactors the integration test suite and replaces the custom
MCP client implementation with the official MCP Go SDK.

Changes:

1. Table-Driven Tests Refactoring:
   - Consolidated 5 individual test functions into 2 table-driven tests
   - TestIntegration_ToolCalls: 4 successful tool call scenarios
   - TestIntegration_ToolCallErrors: error handling scenarios
   - Reduced code duplication by ~50 lines
   - Added helper functions: setupInitializedClient, callToolAndGetResult

2. Removed TestMain:
   - Eliminated WireMock readiness check from TestMain
   - Removed unused imports (fmt, os)
   - Simplified test setup (13 lines removed)

3. Migrated to Official MCP Go SDK:
   - Replaced custom internal/testutil/mcp.go (202 lines)
   - Created internal/testutil/mcp_client.go (141 lines) using SDK
   - Uses official mcp.Client and mcp.ClientSession
   - Proper type-safe content handling with *mcp.TextContent
   - Better error handling (protocol vs tool errors)

Benefits:
- Total code reduction: 99 lines removed
- Better maintainability with table-driven tests
- Future-proof with official SDK
- Consistent with server-side SDK usage
- All tests pass (3 test functions, 5 subtests total)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>

fmt

Signed-off-by: Tomasz Janiszewski <tomek@redhat.com>
@janisz janisz force-pushed the integration_tests branch from 42b1ca1 to 1d35a3d Compare March 6, 2026 14:26
janisz and others added 2 commits March 6, 2026 15:45
Fixed 17 linter issues across multiple packages:
- errcheck: Added error checks for Close() calls
- gosec: Added security comment for test utility
- lll: Split long function signature
- nlreturn: Added blank lines before returns
- wrapcheck: Wrapped external package errors with context
- wsl: Fixed whitespace requirements
- noctx: Used CommandContext instead of Command

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Split the Start function into smaller helper methods:
- startStdio: handles stdio transport setup
- startHTTP: handles HTTP server lifecycle

This reduces the Start function from 61 to 11 lines, resolving
the funlen linter error (max 60 lines).

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant