Skip to content

Evenfire is an intelligence layer platform for running LLM agents that take real actions, on your infrastructure

TypeScript
13.9 KB
Raw Blame
/**
 * E11.1–E11.7: MongoDB MCP Server lifecycle E2E tests.
 *
 * Validates the COMPLETE operational lifecycle of a MongoDB MCP server:
 * apply mcpserver → StatefulSet ready → PVC without ownerRef →
 * MongoDB connection → MCP tools/list → MCP tools/call (find/insert) →
 * delete → PVC retained → recreate → data persists.
 *
 * Unlike basic structural tests, this verifies that MongoDB MCP server
 * actually FUNCTIONS — not just that K8s resources exist.
 *
 * Special focus on StatefulSet behavior (vs Deployment):
 *   - Stable network identity (mongodb-server-0)
 *   - PVC retention without ownerReferences
 *   - Data persistence across pod restarts
 *
 * Prerequisites:
 *   - Run scripts/minikube-setup.sh
 *   - minikube cluster running with CRDs installed
 *   - MongoDB instance available (set MONGODB_CONNECTION_STRING env var)
 *   - Context CRD created (e.g., kubectl apply -f charts/clerum-crds/examples/context1.yaml)
 */
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { ChildProcess } from 'node:child_process'
import {
  MCP_SERVER_NAMESPACE,
  SANDBOX_NAMESPACE,
  kubectl,
  kubectlJson,
  mcpCallTool,
  mcpInitSession,
  mcpListTools,
  sleep,
  startPortForward,
  waitForPodReady,
  waitForResource,
} from './helpers'

const SERVER_NAME = 'mongodb-server'
const MCP_SERVER_FILE = `${__dirname}/../../../mcp-servers/mongodb/mcpserver.yaml`
const SECRET_FILE = `${__dirname}/../../../mcp-servers/mongodb/example.secret.yaml`
const LOCAL_PORT = 18086
const MCP_REMOTE_PORT = 3000
const HEALTH_REMOTE_PORT = 3001

// Expected MongoDB MCP tool names (from us-central1-docker.pkg.dev/${GCP_PROJECT}/clerum/mongodb-mcp-server:latest)
// Verified 2026-03-06: Server returns 15 READ-ONLY tools (no insert/update/delete)
// Actual tools: aggregate, collection-indexes, collection-schema, collection-storage-size,
//               connect, count, db-stats, explain, export, find, list-collections,
//               list-databases, list-knowledge-sources, mongodb-logs, search-knowledge
const EXPECTED_MONGO_TOOLS = [
  'aggregate',
  'find',
  'count',
  'list-collections',
  'list-databases',
  'collection-indexes',
  'collection-schema',
  'collection-storage-size',
  'db-stats',
  'explain',
] as const

let mcpPortForward: { process: ChildProcess; url: string } | null = null
let healthPortForward: { process: ChildProcess; url: string } | null = null

/**
 * Get MongoDB connection string from env or use default for minikube testing.
 *
 * For local testing with minikube, you can:
 * 1. Deploy MongoDB in the cluster: kubectl apply -f mcp-servers/mongodb/k8s/mongodb-deployment.yaml
 * 2. Use an external MongoDB instance: export MONGODB_CONNECTION_STRING="mongodb://..."
 */
function getMongoConnectionString(): string {
  const envConnection = process.env.MONGODB_CONNECTION_STRING
  if (envConnection) {
    return envConnection
  }

  // Default to localhost for local development (requires port-forward or nodeport)
  return 'mongodb://localhost:27017/e2e_test?directConnection=true'
}

/**
 * Create or update the MongoDB credentials secret.
 */
async function ensureMongoSecret(): Promise<void> {
  const connectionString = getMongoConnectionString()

  // Create secret in both runtime namespaces: mcp-server for rendered MCP
  // transport children and sandbox-recipes for non-MCP recipe runtime.
  for (const ns of [MCP_SERVER_NAMESPACE, SANDBOX_NAMESPACE]) {
    try {
      kubectl(`delete secret mcp-mongodb-credentials -n ${ns} --ignore-not-found`)
    } catch {
      /* ignore */
    }

    // Use --from-literal directly (no pipe/heredoc — more reliable in test environments)
    kubectl(
      `create secret generic mcp-mongodb-credentials -n ${ns} --from-literal=connection-string=${connectionString}`
    )
  }
}

/**
 * Get a pod name by label selector.
 */
function getPodName(labelSelector: string, namespace: string): string {
  return kubectl(
    `get pod -l ${labelSelector} -n ${namespace} -o jsonpath='{.items[0].metadata.name}'`
  )
}

/**
 * Wait for port-forward to be established by listening for kubectl's
 * "Forwarding from" stdout message.
 */
function startAndWaitPortForward(
  podSelector: string,
  namespace: string,
  localPort: number,
  remotePort: number
): Promise<{ process: ChildProcess; url: string }> {
  return new Promise((resolve, reject) => {
    const podName = getPodName(podSelector, namespace)

    const child = startPortForward(`pod/${podName}`, namespace, localPort, remotePort)
    let settled = false

    const timer = setTimeout(() => {
      if (!settled) {
        settled = true
        child.process.kill()
        reject(new Error(`port-forward to ${podName}:${remotePort} timed out`))
      }
    }, 30_000)

    child.process.stdout?.on('data', (chunk: Buffer) => {
      if (!settled && chunk.toString().includes('Forwarding from')) {
        settled = true
        clearTimeout(timer)
        resolve(child)
      }
    })

    child.process.stderr?.on('data', (chunk: Buffer) => {
      console.error(`[port-forward stderr] ${chunk.toString().trim()}`)
    })

    child.process.on('exit', code => {
      if (!settled) {
        settled = true
        clearTimeout(timer)
        reject(new Error(`port-forward exited with code ${code}`))
      }
    })
  })
}

beforeAll(async () => {
  // Clean up leftovers from previous runs
  try {
    kubectl(
      `delete mcpserver ${SERVER_NAME} -n ${MCP_SERVER_NAMESPACE} --ignore-not-found --timeout=20s`
    )
  } catch {
    /* ignore */
  }

  await waitForResource(`deploy -l clerum.io/mcpserver=${SERVER_NAME}`, MCP_SERVER_NAMESPACE, {
    shouldExist: false,
    timeoutMs: 15_000,
  }).catch(() => {})

  await sleep(3_000)

  // Ensure secret exists
  await ensureMongoSecret()
}, 60_000)

afterAll(async () => {
  // Kill port-forwards
  if (mcpPortForward) mcpPortForward.process.kill()
  if (healthPortForward) healthPortForward.process.kill()

  // Clean up McpServer
  try {
    kubectl(
      `delete mcpserver ${SERVER_NAME} -n ${MCP_SERVER_NAMESPACE} --ignore-not-found --timeout=30s`
    )
  } catch {
    /* ignore */
  }
  await sleep(5_000)
})

describe('MongoDB MCP Server - E2E Lifecycle', () => {
  // E11.1: Apply mcpserver.yaml → Deployment created (HCC creates Deployments for managed McpServer CRDs)
  it('E11.1 — Apply mcpserver.yaml creates Deployment', { timeout: 60_000 }, async () => {
    const result = kubectl(`apply -f ${MCP_SERVER_FILE}`)
    expect(result).toContain('mcpserver')
    expect(result).toMatch(/created|configured/)

    // Wait for Deployment to appear (HCC creates Deployments for managed:true McpServer CRDs)
    await waitForResource(`deploy -l clerum.io/mcpserver=${SERVER_NAME}`, MCP_SERVER_NAMESPACE, {
      shouldExist: true,
      timeoutMs: 30_000,
    })

    const deploys = kubectlJson<{
      items: Array<{
        metadata: { name: string; labels: Record<string, string> }
        spec: { replicas: number }
      }>
    }>(`get deploy -l clerum.io/mcpserver=${SERVER_NAME} -n ${MCP_SERVER_NAMESPACE}`)

    expect(deploys.items.length).toBe(1)
    expect(deploys.items[0].metadata.name).toBe(SERVER_NAME)
    expect(deploys.items[0].spec.replicas).toBe(1)
  })

  // E11.2: HCC-managed McpServer creates a Deployment (not StatefulSet), so no PVC is auto-created.
  // Verify the Service is created instead (HCC creates Service for transport resolution).
  it('E11.2 — Service created for McpServer transport', { timeout: 60_000 }, async () => {
    await waitForResource(`svc ${SERVER_NAME}`, MCP_SERVER_NAMESPACE, {
      shouldExist: true,
      timeoutMs: 30_000,
    })

    const svc = kubectlJson<{
      metadata: { name: string; namespace: string }
      spec: { type: string; ports: Array<{ port: number }> }
    }>(`get svc ${SERVER_NAME} -n ${MCP_SERVER_NAMESPACE}`)

    expect(svc.metadata.name).toBe(SERVER_NAME)
    expect(svc.metadata.namespace).toBe(MCP_SERVER_NAMESPACE)
    expect(svc.spec.type).toBe('ClusterIP')
    expect(svc.spec.ports[0].port).toBe(3000)
  })

  // E11.3: Pod Ready + MongoDB connection successful
  it('E11.3 — Pod Ready and MCP server healthy', { timeout: 180_000 }, async () => {
    // Wait for Deployment pod to be Ready
    await waitForPodReady(`clerum.io/mcpserver=${SERVER_NAME}`, MCP_SERVER_NAMESPACE, 120_000)

    // Verify pod is Ready
    await waitForPodReady(`clerum.io/mcpserver=${SERVER_NAME}`, MCP_SERVER_NAMESPACE, 10_000)

    // Verify health endpoint responds
    healthPortForward = await startAndWaitPortForward(
      `clerum.io/mcpserver=${SERVER_NAME}`,
      MCP_SERVER_NAMESPACE,
      LOCAL_PORT + 1000, // 19086 for health
      HEALTH_REMOTE_PORT
    )

    await sleep(2_000)

    const start = Date.now()
    let healthy = false
    while (Date.now() - start < 15_000) {
      try {
        const response = await fetch(`${healthPortForward.url}/health`)
        if (response.status === 200) {
          const body = (await response.json()) as { status: string }
          expect(body.status).toBe('ok')
          healthy = true
          break
        }
      } catch {
        /* not ready */
      }
      await sleep(1_000)
    }
    expect(healthy).toBe(true)

    // Clean up health port-forward
    healthPortForward.process.kill()
    healthPortForward = null
  })

  // E11.4: MCP tools/list retorna herramientas MongoDB
  it('E11.4 — MCP tools/list returns MongoDB tools', { timeout: 60_000 }, async () => {
    mcpPortForward = await startAndWaitPortForward(
      `clerum.io/mcpserver=${SERVER_NAME}`,
      MCP_SERVER_NAMESPACE,
      LOCAL_PORT,
      MCP_REMOTE_PORT
    )

    await sleep(2_000)

    const sessionId = await mcpInitSession(mcpPortForward.url)
    expect(sessionId).toBeTruthy()

    const tools = await mcpListTools(mcpPortForward.url, sessionId)
    const toolNames = tools.map(t => t.name)

    // Verify all expected MongoDB tools are present
    for (const tool of EXPECTED_MONGO_TOOLS) {
      expect(toolNames).toContain(tool)
    }
  })

  // E11.5: MCP tools/call list-databases operation
  // Note: Without a real MongoDB instance (MONGODB_CONNECTION_STRING env var),
  // the tool call may return isError: true. We validate the MCP protocol works
  // end-to-end regardless of whether the underlying MongoDB is connected.
  it(
    'E11.5 — MCP tools/call list-databases returns valid MCP response',
    { timeout: 60_000 },
    async () => {
      expect(mcpPortForward).not.toBeNull()

      const sessionId = await mcpInitSession(mcpPortForward!.url)
      const result = await mcpCallTool(mcpPortForward!.url, sessionId, 'list-databases', {})

      // MCP protocol response is well-formed regardless of DB connectivity
      expect(result.content).toBeDefined()
      expect(result.content.length).toBeGreaterThan(0)
      expect(result.content[0].type).toBe('text')

      if (process.env.MONGODB_CONNECTION_STRING) {
        // Real MongoDB connected — verify actual data
        expect(result.isError).toBeFalsy()
        const databases = JSON.parse(result.content[0].text) as string[]
        expect(Array.isArray(databases)).toBe(true)
        expect(databases.length).toBeGreaterThanOrEqual(3)
      } else {
        // No real MongoDB — tool returns error but MCP protocol is correct
        console.log(
          '[E2E MongoDB] No MONGODB_CONNECTION_STRING set — tool returned isError (expected)'
        )
      }
    }
  )

  // E11.6: MCP tools/call count operation (read-only server — no insert/update/delete)
  // Same caveat as E11.5: without real MongoDB, tool returns isError.
  it('E11.6 — MCP tools/call count returns valid MCP response', { timeout: 60_000 }, async () => {
    expect(mcpPortForward).not.toBeNull()

    const sessionId = await mcpInitSession(mcpPortForward!.url)

    const countResult = await mcpCallTool(mcpPortForward!.url, sessionId, 'count', {
      database: 'admin',
      collection: 'system.version',
    })

    // MCP protocol response structure is valid
    expect(countResult.content).toBeDefined()
    expect(countResult.content.length).toBeGreaterThan(0)
    expect(countResult.content[0].type).toBe('text')

    if (process.env.MONGODB_CONNECTION_STRING) {
      expect(countResult.isError).toBeFalsy()
    } else {
      console.log(
        '[E2E MongoDB] No MONGODB_CONNECTION_STRING set — count tool returned isError (expected)'
      )
    }
  })

  // E11.7: Delete → Deployment removed → Recreate → pod comes back
  // Note: HCC-managed McpServer uses Deployment (not StatefulSet), so no PVC retention test.
  // Data persistence depends on the external MongoDB instance (connection string), not local PVCs.
  it(
    'E11.7 — Delete mcpserver removes Deployment, recreate restores it',
    { timeout: 180_000 },
    async () => {
      // Clean up port-forward before delete
      if (mcpPortForward) {
        mcpPortForward.process.kill()
        mcpPortForward = null
      }

      // Delete McpServer
      const deleteResult = kubectl(`delete mcpserver ${SERVER_NAME} -n ${MCP_SERVER_NAMESPACE}`)
      expect(deleteResult).toContain('deleted')

      // Wait for Deployment to be deleted
      await waitForResource(`deploy -l clerum.io/mcpserver=${SERVER_NAME}`, MCP_SERVER_NAMESPACE, {
        shouldExist: false,
        timeoutMs: 60_000,
      })

      // Re-apply mcpserver
      const applyResult = kubectl(`apply -f ${MCP_SERVER_FILE}`)
      expect(applyResult).toMatch(/created|configured/)

      // Wait for pod to be Ready again
      await waitForPodReady(`clerum.io/mcpserver=${SERVER_NAME}`, MCP_SERVER_NAMESPACE, 120_000)

      // Wait for pod to be fully running before port-forward
      await sleep(5_000)

      // Restart port-forward and verify MCP still works
      mcpPortForward = await startAndWaitPortForward(
        `clerum.io/mcpserver=${SERVER_NAME}`,
        MCP_SERVER_NAMESPACE,
        LOCAL_PORT,
        MCP_REMOTE_PORT
      )

      await sleep(3_000)

      const newSessionId = await mcpInitSession(mcpPortForward.url)
      expect(newSessionId).toBeTruthy()

      const tools = await mcpListTools(mcpPortForward.url, newSessionId)
      const toolNames = tools.map(t => t.name)
      expect(toolNames).toContain('find')
    }
  )
})