helmut

package module
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2021 License: Apache-2.0 Imports: 19 Imported by: 0

README

Helmut

Go Reference

Helmut is a testing library for Unit Testing of Helm charts.

This library was inspired by the following project:

Status

⚠ This project is immature and API stability is not guaranteed.

Install

⚠ Currently you need replace in go.mod to import this library:

require (
	github.com/d-kuro/helmut v0.3.1
)

Usage

Example tests:

func TestChart(t *testing.T) {
	const (
		releaseName = "foo"
		chartName   = "test-chart"
	)

	tests := []struct {
		name          string
		assertOptions []assert.Option
		want          runtime.Object
	}{
		{
			name: "contains service account",
			assertOptions: []assert.Option{
				assert.WithIgnoreHelmManagedLabels(),
			},
			want: &corev1.ServiceAccount{
				ObjectMeta: metav1.ObjectMeta{
					Name: fmt.Sprintf("%s-%s", releaseName, chartName),
				},
			},
		},
		{
			name: "contains service",
			assertOptions: []assert.Option{
				assert.WithIgnoreLabelKeys(
					"app.kubernetes.io/managed-by",
					"app.kubernetes.io/version",
					"helm.sh/chart",
				),
			},
			want: &corev1.Service{
				ObjectMeta: metav1.ObjectMeta{
					Name: fmt.Sprintf("%s-%s", releaseName, chartName),
					Labels: map[string]string{
						"app.kubernetes.io/instance": releaseName,
						"app.kubernetes.io/name":     chartName,
					},
				},
				Spec: corev1.ServiceSpec{
					Type: corev1.ServiceTypeClusterIP,
					Ports: []corev1.ServicePort{
						{
							Port:       8080,
							TargetPort: intstr.FromString("http"),
							Protocol:   corev1.ProtocolTCP,
							Name:       "http",
						},
					},
					Selector: map[string]string{
						"app.kubernetes.io/name":     chartName,
						"app.kubernetes.io/instance": releaseName,
					},
				},
			},
		},
	}

	r := helmut.New()

	manifests, err := r.RenderTemplates(releaseName, filepath.Join("testdata", chartName))
	if err != nil {
		t.Fatalf("failed to render templates: %s", err)
	}

	for _, tt := range tests {
		tt := tt

		t.Run(tt.name, func(t *testing.T) {
			assert.Contains(t, manifests, tt.want, tt.assertOptions...)
		})
	}
}

Output:

=== RUN   TestChart
=== RUN   TestChart/contains_service_account
=== RUN   TestChart/contains_service
    template_test.go:163: service/foo-test-chart mismatch (-want +got):
          &v1.Service{
                TypeMeta:   {Kind: "Service", APIVersion: "v1"},
                ObjectMeta: {Name: "foo-test-chart", Labels: {"app.kubernetes.io/instance": "foo", "app.kubernetes.io/name": "test-chart"}},
                Spec: v1.ServiceSpec{
                        Ports: []v1.ServicePort{
                                {
                                        Name:        "http",
                                        Protocol:    "TCP",
                                        AppProtocol: nil,
        -                               Port:        8080,
        +                               Port:        80,
                                        TargetPort:  {Type: 1, StrVal: "http"},
                                        NodePort:    0,
                                },
                        },
                        Selector:  {"app.kubernetes.io/instance": "foo", "app.kubernetes.io/name": "test-chart"},
                        ClusterIP: "",
                        ... // 15 identical fields
                },
                Status: {},
          }
--- FAIL: TestChart (0.06s)
    --- PASS: TestChart/contains_service_account (0.00s)
    --- FAIL: TestChart/contains_service (0.00s)

Helmut uses github.com/google/go-cmp to display object diffs.

Render Options

You can specify options when rendering the Helm chart with RenderTemplates.

e.g.

r.RenderTemplates(releaseName, chartPath, helmut.WithSet("replicaCount=2"))

https://pkg.go.dev/github.com/d-kuro/helmut#Option

Assert Options

You can specify options when asserting.

e.g.

assert.Contains(t, manifests, object, assert.WithIgnoreHelmManagedLabels())

https://pkg.go.dev/github.com/d-kuro/helmut/assert#Option

Utils

Helmut provides utility functions for testing in the util package.

https://pkg.go.dev/github.com/d-kuro/helmut/util

Documentation

Overview

Package helmut provides functions to perform unit tests on the helm chart.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Manifests

type Manifests struct {
	// contains filtered or unexported fields
}

Manifests stores the rendered manifests.

func NewManifests

func NewManifests(options ...SchemeOption) *Manifests

NewManifests creates and returns new Manifests. If SchemeOption is omitted, the default scheme will be used.

func (*Manifests) Delete

func (m *Manifests) Delete(key ObjectKey)

Delete deletes the object for a key.

func (*Manifests) GetKeys added in v0.0.4

func (m *Manifests) GetKeys() []ObjectKey

GetKeys returns a list of keys for an object.

func (*Manifests) GetScheme

func (m *Manifests) GetScheme() *runtime.Scheme

GetScheme returns the scheme.

func (*Manifests) Length

func (m *Manifests) Length() int

Length returns the number of elements in the stored manifest.

func (*Manifests) Load

func (m *Manifests) Load(key ObjectKey) (runtime.Object, bool)

Load returns the object stored in the manifests for a key, or nil if no value is present. The ok result indicates whether value was found in the manifests.

func (*Manifests) Store

func (m *Manifests) Store(key ObjectKey, value runtime.Object)

Store sets the object for a key.

type ObjectKey

type ObjectKey struct {
	Group     string
	Version   string
	Kind      string
	Namespace string
	Name      string
}

ObjectKey is that uniquely identifies an object. Used as the key for the map that holds the object.

func NewObjectKey

func NewObjectKey(namespace, name string, gvk schema.GroupVersionKind) ObjectKey

NewObjectKey creates and returns a new ObjectKey.

func NewObjectKeyFromObject

func NewObjectKeyFromObject(obj runtime.Object, options ...SchemeOption) (ObjectKey, error)

NewObjectKeyFromObject creates and returns an ObjectKey from an object. If SchemeOption is omitted, the default scheme will be used.

func (ObjectKey) GetGroupVersionKind

func (k ObjectKey) GetGroupVersionKind() schema.GroupVersionKind

GetGroupVersionKind returns group,version,kind.

func (ObjectKey) GetName

func (k ObjectKey) GetName() string

GetName returns the name of the object.

func (ObjectKey) GetNamespace

func (k ObjectKey) GetNamespace() string

GetNamespace returns the namespace of the object.

func (ObjectKey) String

func (k ObjectKey) String() string

String implements fmt.Stringer interface.

type Option

type Option func(*option)

Option is an option to specify when calling RenderTemplates.

func WithAPIVersions

func WithAPIVersions(apiVersions ...string) Option

WithAPIVersions specifies the apiVersions. This is equivalent to the "--api-versions" option of the "helm template" command.

func WithIncludeCRDs

func WithIncludeCRDs() Option

WithIncludeCRDs will include CRDs in the templated output. This is equivalent to the "--include-crds" option of the "helm template" command.

func WithNamespace

func WithNamespace(namespace string) Option

WithNamespace specifies the namespace. This is equivalent to the "--namespace" option of the "helm template" command.

func WithSet

func WithSet(values ...string) Option

WithSet set values just like the command line. (can specify multiple or separate values with commas: key1=val1,key2=val2) This is equivalent to the "--set" option of the "helm template" command.

func WithSetFile

func WithSetFile(files ...string) Option

WithSetFile set values just like the command line. (can specify multiple or separate values with commas: key1=val1,key2=val2) This is equivalent to the "set-file" option of the "helm template" command.

func WithSetString

func WithSetString(values ...string) Option

WithSetString set STRING values just like the command line. (can specify multiple or separate values with commas: key1=val1,key2=val2) This is equivalent to the "--set-string" option of the "helm template" command.

func WithValues

func WithValues(files ...string) Option

WithValues specifies values in a YAML file or a URL (can specify multiple). This is equivalent to the "--values" or "-f" option of the "helm template" command.

type Renderer

type Renderer struct {
	// contains filtered or unexported fields
}

Renderer will perform the equivalent of the "helm template" command to render the manifests.

func New

func New(options ...SchemeOption) *Renderer

New creates and returns a new Renderer. option allows you to specify the scheme to be used by the Renderer. For example, specify the scheme in which the custom resource is registered.

func (*Renderer) RenderTemplates

func (r *Renderer) RenderTemplates(name, chart string, options ...Option) (*Manifests, error)

RenderTemplates will execute the equivalent of the "helm template" command and return the result.

func (*Renderer) SplitManifests

func (r *Renderer) SplitManifests(data []byte) (*Manifests, error)

SplitManifests takes a single large manifest and splits it into individual manifests.

type SchemeOption

type SchemeOption func(*schemeOption)

SchemeOption is an option to specify the scheme. If not specified, the default scheme provided by client-go will be used.

func WithScheme

func WithScheme(scheme *runtime.Scheme) SchemeOption

WithScheme specifies the scheme.

Directories

Path Synopsis
Package assert provides a function to compare the object used in the test.
Package assert provides a function to compare the object used in the test.
Package util provides utilities for testing.
Package util provides utilities for testing.

Jump to

Keyboard shortcuts

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