Skip to content

Conversation

@sneaxhuh
Copy link
Contributor

Add MCP Integration Tests for Allocation and Asset APIs

Summary

Adds comprehensive integration tests to validate that the MCP (Model Context Protocol) tools return identical results to the HTTP API endpoints for both allocation and asset queries.

Changes

New Files

  • test/integration/mcp/allocation_mcp_vs_http_test.go – Tests comparing MCP get_allocation_costs tool with /allocation HTTP endpoint
  • test/integration/mcp/asset_mcp_vs_http_test.go – Tests comparing MCP get_asset_costs tool with /assets HTTP endpoint
  • test/integration/mcp/helpers.go – Shared helper functions and types for MCP session management
  • test/integration/mcp/test.bats – Bats test runner following repo conventions

Test Coverage

Allocation Tests (TestMCPAllocationVsHTTP)

Tests various allocation queries with historical time windows to ensure reproducible results:

  • Yesterday namespace aggregation
  • Historical cluster aggregation (2 days ago)
  • Last 3 days pod aggregation
  • Accumulate mode testing
  • Last 7 days controller aggregation
  • Last week (7–14 days ago) namespace aggregation

Validates:

  • All allocation names match between MCP and HTTP
  • Total costs match within tolerance (0.01 with 0.1% difference threshold)
  • Query results are identical across both interfaces

Asset Tests (TestMCPAssetVsHTTP)

Tests asset queries with historical time windows:

  • Yesterday assets
  • Last 3 days assets
  • Last 7 days assets

Validates:

  • Asset names match (with proper path extraction for HTTP API)
  • Total costs match within tolerance
  • All assets present in both responses

Key Features

  • Historical time windows only: Ensures consistent, reproducible results unaffected by new data
  • Proper MCP session handling: Implements full MCP protocol with initialization and session management
  • Shared helpers: Common session management and response utilities extracted into helpers.go

Test Results

All allocation tests passing (6/6)

  • Validates cost matching across 60+ allocations per test case
  • Tests multiple aggregation types (namespace, cluster, pod, controller)
  • Validates accumulate mode

All asset tests passing (3/3)

  • Validates cost matching for nodes and storage
  • Handles complex asset path extraction from HTTP API

Testing

Run all tests via Bats (recommended)

./test/bats/bin/bats ./test/integration/mcp/test.bats

@sneaxhuh sneaxhuh requested a review from a team as a code owner October 31, 2025 17:23
"clientInfo": map[string]string{
"name": "opencost-integration-test",
"version": "1.0.0",
},
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

@ameijer ameijer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

},
{
name: "2 days ago cluster aggregation",
window: fmt.Sprintf("%s,%s", twoDaysAgo.Format("2006-01-02T15:04:05Z"), yesterday.Format("2006-01-02T15:04:05Z")),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks good

@ameijer ameijer added this pull request to the merge queue Nov 13, 2025
Merged via the queue into opencost:main with commit 30337c1 Nov 13, 2025
2 checks passed
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.

2 participants