Skip to main content

3. Test your composition

In the previous guide, you used an embedded function to create composition logic for your cloud resources. This guide walks through how to create a test plan. Tests allow you to:

  • Validate your composition configuration without impacting actual resources in your control planes.
  • Catch errors earlier in your workflow
  • Verify resource creation
  • Validate input parameters

Prerequisites

Make sure you've completed the previous guide and have:

If you missed any of the previous steps, go to the project foundations guide to get started.

Generate a composition function test

Create the test scaffolding:

up test generate storagebucket

This command:

  • Creates a test directory in tests/test-storagebucket
  • Generates the basic structure for writing tests
note

The default testing language is KCL. You can also specify --language=python or --language=go to generate tests in Python or Go.

Set up your test imports

Open tests/test-storagebucket/main.k and replace the scaffolding with the following configuration:

Review your test

Import the testing models

Define your test case

Create the main test structure:

tests/test-storagebucket/main.k
_items = [
metav1alpha1.CompositionTest{
metadata.name="test-storagebucket"
spec= {
assertResources: [
# Test assertions will go here
]
compositionPath: "apis/storagebuckets/composition.yaml"
xrPath: "examples/storagebucket/example.yaml"
xrdPath: "apis/storagebuckets/definition.yaml"
timeoutSeconds: 120
validate: False
}
}
]

This section:

  • Identifies your test case
  • Sets up the assertResources section to define expected outputs
  • Links to your composition, XR and XRD files
  • Defines how long to wait for the test to complete
  • Defines Whether to validate against live schemas

Test the input XR

This assertion verifies:

  • Your composition receives the user's XR
  • Your control plane processes the acl, region and versioning parameters
  • The control plane maintains the resource name and structure

Test the storage resource

note

For all managed resources, strip away all default fields. This follows the best practice for composition functions to return only fields whose values they care about, as they become the owners of those fields.

Final test structure

Your complete test now:

  • Simulates a user's StorageBucket XR
  • Verifies resource creation
  • Ensures resources are linked together
  • Confirms user inputs flow through to created resources

Run your tests

Next, run your composition tests with the up test run command.

up test run tests/*
✓ Parsing tests
✓ Checking dependencies
✓ Generating language schemas
✓ Building functions
✓ Checking dependencies
✓ Generating language schemas
✓ Building functions
✓ Building configuration package
✓ Pushing embedded functions to local daemon
✓ Assert test-storagebucket
SUCCESS
SUCCESS Tests Summary:
SUCCESS ------------------
SUCCESS Total Tests Executed: 1
SUCCESS Passed tests: 1
SUCCESS Failed tests: 0

Understanding test results

Your test results return:

  • Pass/fail status: Whether each assertion succeeded
  • Resource validation: Confirmation for resource creation
  • Error details: Specific information about any failures
  • Coverage summary: Overview of the assertions

What successful tests prove

Passing tests show:

  • Your composition function logic is correct
  • User parameters processed properly
  • All required resources are created
  • Security configurations are applied consistently
  • Conditional logic works as expected

Next steps

Now that you have tested your composition:

  • Deploy with confidence: Your tests prove the composition works
  • Iterate safely: Make changes knowing tests will catch regressions
  • Add more test cases: Test different parameter combinations and edge cases
  • Integrate with CI/CD: Automate testing as part of your development workflow

In the next guide, you'll deploy your tested composition to a control plane and see your infrastructure API in action.