controller-testtools

module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2023 License: MIT

README

Kubernetes controller-testtools Project

The Kubernetes controller-testtools Project is a set of go libraries or cli tools for testing Controllers.

Go packages

1. pkg/snap: Gomega Matcher for Kubernetes Object Snapshot

Snapshot testing is a type of output comparison testing. It is mainly used for frontend like UI or html dom, but we think it is useful for Kubernetes Objects.

This Gomega Matcher can be integrated with kubebuilder's test. __snapshot__ directory is created in the same directory as a target *_test.go

import (
	. "github.com/cosmo-workspace/controller-testtools/pkg/snap"
	. "github.com/onsi/ginkgo/v2"
	. "github.com/onsi/gomega"
    // ...
)

var _ = Describe("Workspace controller", func() {
    Context("when creating a Workspace CRD resource", func() {
        It("should do reconcile once and create child resources", func() {
            ctx := context.Background()

            ws := cosmov1alpha1.Workspace{
                ObjectMeta: metav1.ObjectMeta{
                    Name:      "testws",
                    Namespace: "testns",
                },
                Spec: cosmov1alpha1.WorkspaceSpec{
                    Template: cosmov1alpha1.TemplateRef{
                        Name: "testtmpl",
                    },
                    Replicas: pointer.Int64(1),
                    Vars: map[string]string{
                        "{{DOMAIN}}":    "example.com",
                        "{{IMAGE_TAG}}": "latest",
                    },
                },
            }
            err := k8sClient.Create(ctx, &ws)
            Expect(err).ShouldNot(HaveOccurred())

            var createdChild cosmov1alpha1.Instance
            Eventually(func() error {
                return k8sClient.Get(ctx, client.ObjectKey{Name: ws.Name, Namespace: ws.Namespace}, &createdChild)
            }, time.Second*10).Should(Succeed())

            // Snapshot testing
            Expect(ObjectSnapshot(&createdChild)).To(MatchSnapShot())
        })
    })
})

CLI tools

1. chartsnap: Snapshot testing for helm chart
Install

Download release binary or

go install github.com/cosmo/controller-testtools/cmd/chartsnap
Quickstart
chartsnap --chart YOUR_CHART_DIR

Create new snapshot or compare with existing snapshot with default values. __snapshot__ directory is created in chart directory.

Test Specification

The test specification is a Values file.

You can create a variation of Values files of your chart as test cases.

Input test values file:

chartsnap --chart YOUR_CHART_DIR -f YOUR_CHART_DIR/test/test-values.yaml

__snapshot__ directory is created in the same directory as test-values.yaml in this case.

In addition, Values files can have a testSpec property that can detail the test specification for chartsnap.

# chartsnap test specification
testSpec:
  desc: only required values and the rest is default
  # dynamicFields defines values that are dynamically generated by Helm function like 'randAlphaNum'
  # https://helm.sh/docs/chart_template_guide/function_list/#randalphanum-randalpha-randnumeric-and-randascii
  # Replace outputs with fixed values to prevent unmatched outputs at each snapshot.
  dynamicFields:
    - apiVersion: v1
      kind: Secret
      name: cosmo-auth-env
      jsonPath:
        - /data/COOKIE_HASHKEY
        - /data/COOKIE_BLOCKKEY
        - /data/COOKIE_HASHKEY
        - /data/COOKIE_SESSION_NAME

# Others can be any your chart value.
# ...

Directories

Path Synopsis
cmd
pkg

Jump to

Keyboard shortcuts

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