# README
Helmut
Helmut is a testing library for Unit Testing of Helm charts.
This library was inspired by the following project:
- github.com/Waterdrips/helmunit
- How to unit-test your helm charts with Golang – Alistair Hey – Cloud Native Platform Engineer
Status
:warning: This project is immature and API stability is not guaranteed.
Install
:warning: 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.
# Functions
New creates and returns a new Renderer.
NewManifests creates and returns new Manifests.
NewObjectKey creates and returns a new ObjectKey.
NewObjectKeyFromObject creates and returns an ObjectKey from an object.
WithAPIVersions specifies the apiVersions.
WithIncludeCRDs will include CRDs in the templated output.
WithNamespace specifies the namespace.
WithScheme specifies the scheme.
WithSet set values just like the command line.
WithSetFile set values just like the command line.
WithSetString set STRING values just like the command line.
WithValues specifies values in a YAML file or a URL (can specify multiple).
# Type aliases
Option is an option to specify when calling RenderTemplates.
SchemeOption is an option to specify the scheme.