Skip to content

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

TypeScript
10.5 KB
Raw Blame
/**
 * E5.9–E5.20: Simple recipe lifecycle E2E tests.
 *
 * Validates the full WorkflowRecipe lifecycle in minikube:
 * apply, status transitions, resource creation, labels,
 * security context, delete, cascade cleanup, idempotency,
 * and error handling.
 *
 * Prerequisites: Run scripts/minikube-setup.sh before these tests.
 * These tests run AFTER bootstrap.test.ts (sequential mode).
 */
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import {
  MCP_SERVER_NAMESPACE,
  RECIPE_NAMESPACE,
  SANDBOX_NAMESPACE,
  kubectl,
  kubectlJson,
  sleep,
  waitForResource,
} from './helpers'

const SAMPLE_RECIPE = 'simple-nginx'
const RECIPE_FILE = `${__dirname}/../../samples/simple-nginx.yaml`

beforeAll(async () => {
  // Clean up any leftover recipe from previous runs
  try {
    kubectl(
      `delete workflowrecipe ${SAMPLE_RECIPE} -n ${RECIPE_NAMESPACE} --ignore-not-found --timeout=20s`
    )
  } catch (e) {
    console.warn('beforeAll cleanup warning:', e)
  }
  // Verify cleanup actually propagated (non-MCP workloads go to sandbox-recipes)
  for (const ns of [MCP_SERVER_NAMESPACE, SANDBOX_NAMESPACE]) {
    try {
      await waitForResource(`deploy -l clerum.io/recipe=${SAMPLE_RECIPE}`, ns, {
        shouldExist: false,
        timeoutMs: 15_000,
      })
    } catch {
      // If timeout, proceed anyway — resources may not have existed
    }
  }
})

afterAll(() => {
  // Ensure cleanup after all tests
  try {
    kubectl(`delete workflowrecipe ${SAMPLE_RECIPE} -n ${RECIPE_NAMESPACE} --ignore-not-found`)
  } catch {
    // ignore
  }
})

describe('Simple Recipe Lifecycle E2E', () => {
  // E5.9: Apply simple-nginx
  it('E5.9 — Apply simple-nginx recipe', () => {
    const result = kubectl(`apply -f ${RECIPE_FILE}`)
    expect(result).toContain('workflowrecipe')
    expect(result).toMatch(/created|configured/)
  })

  // E5.10: Status transitions
  // Note: The WRC reconciler currently creates K8s resources but does not yet
  // update the WorkflowRecipe status subresource (requires CustomObject API
  // patchStatus calls, planned for Phase 6/7). We validate that the recipe
  // exists and resources are created — status updates will be tested once
  // the reconciler loop is connected to the CRD watcher.
  it('E5.10 — Recipe exists and reconciler has processed it', async () => {
    // Wait for the reconciler to create the Deployment (non-MCP → sandbox-recipes)
    await waitForResource(`deploy -l clerum.io/recipe=${SAMPLE_RECIPE}`, SANDBOX_NAMESPACE, {
      timeoutMs: 30_000,
    })

    // Verify the recipe exists in the platform-owned WorkflowRecipe namespace.
    const wr = kubectlJson<{
      metadata: { name: string }
      status?: { phase?: string }
    }>(`get workflowrecipe ${SAMPLE_RECIPE} -n ${RECIPE_NAMESPACE}`)

    expect(wr.metadata.name).toBe(SAMPLE_RECIPE)

    // Status phase may be set or empty depending on reconciler implementation
    const phase = wr.status?.phase ?? ''
    if (phase) {
      expect(['active', 'deploying', 'candidate', 'approved']).toContain(phase)
    }
    // If empty, the reconciler processed the recipe but hasn't updated status yet — acceptable for Phase 5
  })

  // E5.11: Deployment created (non-MCP workload → sandbox-recipes via namespace splitting)
  // Note: Cross-namespace ownerReferences are not supported by K8s GC.
  // For cross-namespace workloads, cleanup is handled by the WRC finalizer
  // (reconcileDelete), not by ownerRef cascade. ownerRef is only set when
  // recipe and workload share the same namespace.
  it('E5.11 — Deployment created with recipe label', async () => {
    await waitForResource(`deploy -l clerum.io/recipe=${SAMPLE_RECIPE}`, SANDBOX_NAMESPACE, {
      timeoutMs: 30_000,
    })

    const deploys = kubectlJson<{
      items: Array<{
        metadata: {
          name: string
          labels: Record<string, string>
          ownerReferences?: Array<{ kind: string; name: string }>
        }
      }>
    }>(`get deploy -l clerum.io/recipe=${SAMPLE_RECIPE} -n ${SANDBOX_NAMESPACE}`)

    expect(deploys.items.length).toBeGreaterThan(0)
    expect(deploys.items[0].metadata.labels['clerum.io/recipe']).toBe(SAMPLE_RECIPE)

    // Cross-namespace: ownerRef not present (K8s GC limitation).
    // Cleanup is handled by WRC finalizer instead.
    // Rendered MCP transport resources are checked separately in delegation.test.ts.
  })

  // E5.12: Service created (non-MCP → sandbox-recipes)
  it('E5.12 — Service created for nginx workload', async () => {
    await waitForResource(`svc -l clerum.io/recipe=${SAMPLE_RECIPE}`, SANDBOX_NAMESPACE, {
      timeoutMs: 15_000,
    })

    const svcs = kubectlJson<{
      items: Array<{
        metadata: { name: string }
        spec: { type: string }
      }>
    }>(`get svc -l clerum.io/recipe=${SAMPLE_RECIPE} -n ${SANDBOX_NAMESPACE}`)

    expect(svcs.items.length).toBeGreaterThan(0)
    expect(svcs.items[0].spec.type).toBe('ClusterIP')
  })

  // E5.13: Labels correct (non-MCP deploy is in sandbox-recipes)
  it('E5.13 — Labels include managed-by, recipe, and workload', async () => {
    const deploys = kubectlJson<{
      items: Array<{
        metadata: { labels: Record<string, string> }
      }>
    }>(`get deploy -l clerum.io/recipe=${SAMPLE_RECIPE} -n ${SANDBOX_NAMESPACE}`)

    expect(deploys.items.length).toBeGreaterThan(0)
    const labels = deploys.items[0].metadata.labels
    expect(labels['clerum.io/managed-by']).toBe('workflow-recipes')
    expect(labels['clerum.io/recipe']).toBe(SAMPLE_RECIPE)
    expect(labels['clerum.io/workload']).toBeDefined()
  })

  // E5.14: Security context applied
  // Note: We check the Deployment spec (not the pod) because the pod may not
  // be Ready yet. The security context is applied at manifest build time.
  // For "minimal" isolation, securityContext is on the CONTAINER, not the pod.
  it('E5.14 — Security context is applied (minimal isolation)', async () => {
    const deploys = kubectlJson<{
      items: Array<{
        spec: {
          template: {
            spec: {
              containers: Array<{
                securityContext?: {
                  runAsNonRoot?: boolean
                  allowPrivilegeEscalation?: boolean
                  readOnlyRootFilesystem?: boolean
                  capabilities?: { drop?: string[] }
                  seccompProfile?: { type?: string }
                }
              }>
            }
          }
        }
      }>
    }>(`get deploy -l clerum.io/recipe=${SAMPLE_RECIPE} -n ${SANDBOX_NAMESPACE}`)

    expect(deploys.items.length).toBeGreaterThan(0)
    const containerSec = deploys.items[0].spec.template.spec.containers[0].securityContext

    // Minimal isolation: runAsNonRoot is false (allows root), but still drops capabilities
    expect(containerSec?.runAsNonRoot).toBe(false)
    expect(containerSec?.allowPrivilegeEscalation).toBe(false)
    expect(containerSec?.capabilities?.drop).toContain('ALL')
  })

  // E5.15: Status subresource
  // Note: Status updates require the reconciler to call patchStatus on the
  // CustomObject API, which is not yet wired in the K8s watcher loop.
  // For Phase 5 we validate that the CRD has the status subresource defined.
  it('E5.15 — Status subresource is defined on WorkflowRecipe CRD', () => {
    const crd = kubectlJson<{
      spec: {
        versions: Array<{
          name: string
          subresources?: { status?: Record<string, unknown> }
        }>
      }
    }>('get crd workflowrecipes.clerum.io')

    const v1alpha1 = crd.spec.versions.find(v => v.name === 'v1alpha1')
    expect(v1alpha1).toBeDefined()
    expect(v1alpha1!.subresources?.status).toBeDefined()
  })

  // E5.16: Delete recipe
  it('E5.16 — Delete recipe succeeds', () => {
    const result = kubectl(`delete workflowrecipe ${SAMPLE_RECIPE} -n ${RECIPE_NAMESPACE}`)
    expect(result).toContain('deleted')
  })

  // E5.17: Cascade delete — Deployment cleaned up by WRC finalizer
  it(
    'E5.17 — Cascade delete cleans up Deployment via WRC finalizer',
    { timeout: 90_000 },
    async () => {
      await waitForResource(`deploy -l clerum.io/recipe=${SAMPLE_RECIPE}`, SANDBOX_NAMESPACE, {
        shouldExist: false,
        timeoutMs: 60_000,
      })

      const deploys = kubectlJson<{ items: unknown[] }>(
        `get deploy -l clerum.io/recipe=${SAMPLE_RECIPE} -n ${SANDBOX_NAMESPACE}`
      )
      expect(deploys.items.length).toBe(0)
    }
  )

  // E5.18: Status after delete — recipe is gone
  it('E5.18 — Recipe is NotFound after delete', () => {
    expect(() => {
      kubectl(`get workflowrecipe ${SAMPLE_RECIPE} -n ${RECIPE_NAMESPACE}`)
    }).toThrow()
  })

  // E5.19: Idempotent apply (non-MCP deploy goes to sandbox-recipes)
  it('E5.19 — Idempotent apply does not create duplicates', async () => {
    // Apply and wait for deployment to exist
    kubectl(`apply -f ${RECIPE_FILE}`)
    await waitForResource(`deploy -l clerum.io/recipe=${SAMPLE_RECIPE}`, SANDBOX_NAMESPACE, {
      timeoutMs: 30_000,
    })

    // Apply again — should be idempotent
    const result = kubectl(`apply -f ${RECIPE_FILE}`)
    expect(result).toMatch(/unchanged|configured/)

    await sleep(3_000)

    const deploys = kubectlJson<{ items: unknown[] }>(
      `get deploy -l clerum.io/recipe=${SAMPLE_RECIPE} -n ${SANDBOX_NAMESPACE}`
    )
    // Should have exactly 1 deployment (not 0, not 2)
    expect(deploys.items.length).toBe(1)

    // Cleanup
    kubectl(`delete workflowrecipe ${SAMPLE_RECIPE} -n ${RECIPE_NAMESPACE} --ignore-not-found`)
    await sleep(3_000)
  })

  // E5.20: Invalid recipe rejected
  it('E5.20 — Invalid recipe with unknown workload type is rejected', () => {
    let error: Error | null = null
    try {
      kubectl(`apply -f - <<'EOF'
apiVersion: clerum.io/v1alpha1
kind: WorkflowRecipe
metadata:
  name: invalid-recipe
  namespace: ${RECIPE_NAMESPACE}
spec:
  description: "Invalid recipe for testing"
  workloads:
    - id: bad
      type: unknownType
      image: bad:latest
  security:
    isolationLevel: minimal
EOF`)
    } catch (e) {
      error = e as Error
    }

    // Either K8s admission rejects it or the reconciler sets status to failed
    if (error) {
      // Admission rejection — good
      expect(error.message).toBeDefined()
    } else {
      // Reconciler may accept but set status to failed
      // Wait briefly and check
      const wr = kubectlJson<{
        status?: { phase?: string }
      }>(`get workflowrecipe invalid-recipe -n ${RECIPE_NAMESPACE}`)
      // Clean up
      kubectl(`delete workflowrecipe invalid-recipe -n ${RECIPE_NAMESPACE} --ignore-not-found`)
      // Status should reflect the error
      expect(wr.status?.phase).toMatch(/failed|error|candidate/)
    }
  })
})