function-unit-test

command module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 27, 2024 License: Apache-2.0 Imports: 13 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

function-unit-test

CI

function-unit-test is a Crossplane function that can run CEL expressions against your desired state. This allows you to test the results of a Composition pipeline.

Installing

Functions require Crossplane 1.14 or newer. Apply the following manifest to your cluster:

apiVersion: pkg.crossplane.io/v1beta1
kind: Function
metadata:
  name: function-unit-test
spec:
  package: index.docker.io/steve/function-unit-test:v0.1.0

Configuring Unit Tests

See tests in examples.

After all your desired resources have been rendered with other functions, call the unittest function and define CEL TestCases.

If errorOnFailedTest is set to true, failing tests will return an error. This is useful when running this function in CI pipelines via crossplane beta render.

 - step:
    functionRef:
      name: function-unit-test
    input:
      apiVersion: unittest.fn.crossplane.io/v1beta1
      kind: TestCases
      errorOnFailedTest: false
      testCases:
      - description: "test pass"
        assert: observed.composite.resource.spec.env == "dev"
      - description: "test fail"
        assert: observed.composite.resource.spec.env == "prod"
      # - description: "test error"
      #   assert: a == b
      - assert: |-
          "kind" in desired.resources['test-resource'].resource &&
          desired.resources['test-resource'].resource.kind == 'NopResource'
        description: all resources "test" is of "NopResource" kind
      - assert: |- 
          desired.resources.all(r, "labels" in desired.resources[r].resource.metadata && 
          "security-setting" in desired.resources[r].resource.metadata.labels &&
          desired.resources[r].resource.metadata.labels["security-setting"] == "true")
        description: All resources have the "security-setting" label

Building the Function

# Run code generation - see input/generate.go
$ go generate ./...

# Run tests - see fn_test.go
$ go test ./...

# Build the function's runtime image - see Dockerfile
$ docker build . --tag=function-unit-test-runtime

# Build a function package - see package/crossplane.yaml
$ crossplane xpkg build -f package --embed-runtime-image=function-unit-test-runtime

Documentation

Overview

Package main implements a Composition Function.

Directories

Path Synopsis
input
v1beta1
Package v1beta1 contains the input type for this Function +kubebuilder:object:generate=true +groupName=unittest.fn.crossplane.io +versionName=v1beta1
Package v1beta1 contains the input type for this Function +kubebuilder:object:generate=true +groupName=unittest.fn.crossplane.io +versionName=v1beta1

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL