Chainsaw
repository·main·Indexed 20 days ago
https://github.com/kyverno/chainsawA declarative testing tool for Kubernetes operators and controllers that allows users to define and execute end-to-end tests for any Kubernetes objects. It includes a CLI for asserting cluster state, linting test documents, exporting JSON schemas, and migrating resources from KUTTL. The tool provides a Go Client interface for standard CRUD operations on Kubernetes objects with strict field validation.
What's inside chainsaw
- Chainsaw is an open-source tool that provides a declarative approach to testing Kubernetes operators and controllers. While its primary design goal is testing operators and controllers, it can be used to declaratively test any Kubernetes objects. It was originally developed to define and run end-to-end tests for Kyverno.
What is Chainsaw and when to use it
mainChainsaw is a tool designed for running end-to-end (E2E) tests within Kubernetes clusters. It is specifically optimized for testing Kubernetes operators by executing a sequence of steps and asserting that specific conditions are met.
A typical workflow involves:
- Creating a resource (e.g., a policy or a configuration).
- Creating a target resource to trigger the operator.
- Asserting that the operator acted as expected (e.g., checking for mutations, validations, or generated resources).
- Cleaning up the environment for the next test cycle.
Supported Chainsaw operations
mainChainsaw provides a variety of operations to define test steps. These operations allow you to manipulate Kubernetes resources, execute commands, or manage test flow. The available operations are:
Apply: Apply a resource to the cluster.Assert: Assert that a resource exists or matches specific criteria.Command: Execute a command.Create: Create a resource.Delete: Delete a resource.Error: Expect an error to occur.Patch: Patch a resource.Script: Run a script.Sleep: Pause execution for a specified duration.Update: Update a resource.
What is a StepTemplate and when to use it
mainAStepTemplateis a reusable resource in Chainsaw that allows you to factor out common step patterns into a shared definition. Unlike a standard test step which is defined directly within aTest, aStepTemplatelives in its own resource and can be invoked by multiple tests. This helps keep test suites DRY (Don't Repeat Yourself) by allowing you to define a pattern once and pass different arguments to it during each invocation.How `try` statements work in Chainsaw
mainA
trystatement is a sequential execution block used to define a series of test steps. Operations within atryblock are executed in the exact order they are declared.Failure Behavior: If any single operation within the sequence fails, the entire
trystatement is marked as failed, and subsequent operations in that sequence are not executed.How Chainsaw prioritizes configuration files
mainChainsaw follows a specific hierarchy when determining which configuration to use. It will use the first one it finds in this order:
- User-specified configuration: A file explicitly provided via the
--configcommand-line flag. - Default configuration file: A file named
.chainsaw.yamllocated in the current working directory. - Internal default configuration: An embedded default configuration within the Chainsaw binary used if no other file is found.
- User-specified configuration: A file explicitly provided via the
Understand KUBECONFIG behavior for scripts
mainUnless the--no-clusterflag is specified during execution, Chainsaw always executes script commands within the context of a temporaryKUBECONFIG. This temporary configuration is built from the configured target cluster and contains a single cluster, auth info, and context, all namedchainsaw.Use the create operation to define resources
mainThe
createoperation is used within a ChainsawTeststep to define Kubernetes resources that should be created in a cluster.Important Behavior: If the resource you are attempting to create already exists in the cluster, the step will fail.
Supported features for the
createoperation include:- Bindings
- Outputs
- Templating
- Operation checks
Use environment variables in commands
mainChainsaw expands environment variables using the
$VARIABLE_NAMEsyntax, matching Kubernetes behavior for container command and args fields.To include a literal
$sign in your command, escape it by using$$.How catch statements work in Chainsaw
mainA
catchstatement is a sequence of operations that executes only if the operations in a step'strystatement fail.Key Behavior:
- The
catchblock is triggered by a failure in the precedingtryblock. - All operations within a
catchstatement will be executed in sequence, regardless of whether individual operations within thecatchblock itself succeed or fail.
- The
Use operation outputs to reuse results across steps
mainChainsaw allows you to capture the results of an operation (like the standard output of a script) and register them as a new binding using the
outputsfield. Once registered, these outputs are available to subsequent operations in the test execution flow.Key behaviors:
- Registration: Use the
outputsfield within an operation to create a new binding. - Standard Output: By default, an output can capture the content of the standard output using the
($stdout)expression. - Scope: Outputs evaluated after an operation finishes are registered in the bindings and made available for following operations.
apiVersion: chainsaw.kyverno.io/v1alpha1 kind: Test metadata: name: example spec: steps: - try: - script: outputs: - name: OUTPUT value: ($stdout) content: echo "hello" - script: # The $OUTPUT binding from the previous step is now available env: - name: INPUT value: ($OUTPUT) content: echo $INPUT- Registration: Use the
Understand resource cardinality in file references
mainWhen you reference a file (or a glob pattern) that contains multiple Kubernetes resources, Chainsaw treats each resource as a separate instance of the operation.
Important: Because the operation is duplicated for every resource found in the file, any associated
bindingsoroutputswill also be evaluated for every single resource instance. Plan your test logic accordingly to ensure bindings and outputs correctly account for multiple resources.