Skip to content

kcl-test

A generic KCL testing framework for Crossplane compositions. This library provides a fluent assertion API for testing KCL-based Crossplane compositions with snapshot testing support.

Installation

go get github.com/karlderkaefer/kcl-test/pkg/kcltest

Features

  • Fluent API


    Chain assertions for readable, expressive tests

  • Generic


    Works with any KCL-based Crossplane composition

  • Snapshot Testing


    Built-in support for YAML snapshot comparisons

  • Flexible Input


    Load test inputs from YAML files with override support

  • Resource Discovery


    Find resources by kind, annotation, or other properties

  • Dot-notation Paths


    Use "spec.forProvider.region" instead of arrays

  • Type-safe Assertions


    Generic type-checked assertions for strings, ints, bools

  • Hook System


    Pre/post execution hooks for input defaults and transformations

Quick Start

package main

import (
    "testing"

    "github.com/karlderkaefer/kcl-test/pkg/kcltest"
)

func TestComposition(t *testing.T) {
    // Create assertion with test input
    assertion := kcltest.NewKCLAssertion(t, "../src/main.k").
        WithTestInput(
            kcltest.WithInputFile("input.yaml"),
            kcltest.WithStringOverride([]string{"oxr", "spec", "region"}, "eu10"),
        )

    // Fluent assertions with dot-notation paths
    assertion.
        AssertItemExists("Subaccount").
        AssertItemCount(5).
        AssertPath("Subaccount", "spec.forProvider.region", "eu10").
        AssertPathString("Subaccount", "spec.deletionPolicy", "Delete")

    // Scoped assertions for a specific kind
    assertion.ForKind("RoleCollectionAssignment").
        AssertCount(2).
        AssertAllHavePath("spec.forProvider.origin", "ldap").
        Done()

    // Snapshot testing
    kcltest.MatchSnapshot(t, assertion.GetYAML())
}

Scoped Assertions

Use ForKind() to focus assertions on a specific resource type for cleaner, more readable tests.

Next Steps

  • API Reference


    Detailed documentation of all assertion methods

  • Hooks


    Learn about pre/post execution hooks

  • Helpers


    Project-specific helper functions and utilities