Skip to content

Commit

Permalink
redpanda: implement snapshot testing
Browse files Browse the repository at this point in the history
This commit implements golden/snapshot based testing for the redpanda
chart by leveraging all the test files in `ci` and placing the resultant
YAML into `testdata`.

It includes a minor fix to `_configmap.tpl` that caused some
non-determinism. For other sources, `--set` flags are specified to work
around them.
  • Loading branch information
chrisseto committed Mar 15, 2024
1 parent e601078 commit 695a863
Show file tree
Hide file tree
Showing 40 changed files with 44,890 additions and 24 deletions.
2 changes: 2 additions & 0 deletions charts/redpanda/.helmignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@ README.md.gotmpl
.vscode/

*.go
testdata/
ci/
59 changes: 58 additions & 1 deletion charts/redpanda/chart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redpanda

import (
"os"
"os/exec"
"testing"

"github.com/redpanda-data/helm-charts/pkg/helm"
Expand All @@ -19,6 +20,11 @@ func TieredStorageStatic(t *testing.T) Values {
}

return Values{
Config: &Config{
Node: map[string]any{
"developer_mode": true,
},
},
Enterprise: &Enterprise{
License: os.Getenv("REDPANDA_LICENSE"),
},
Expand Down Expand Up @@ -55,6 +61,11 @@ func TieredStorageSecretRefs(t *testing.T, secret *corev1.Secret) Values {
}

return Values{
Config: &Config{
Node: map[string]any{
"developer_mode": true,
},
},
Enterprise: &Enterprise{
License: os.Getenv("REDPANDA_LICENSE"),
},
Expand All @@ -74,6 +85,44 @@ func TieredStorageSecretRefs(t *testing.T, secret *corev1.Secret) Values {
}
}

func TestTemplate(t *testing.T) {
ctx := testutil.Context(t)
client, err := helm.New(helm.Options{ConfigHome: testutil.TempDir(t)})
require.NoError(t, err)

// Chart deps are kept within ./charts as a tgz archive. Helm dep build
// will ensure that ./charts is in sync with Chart.lock.
_, err = exec.CommandContext(ctx, "helm", "dep", "build").CombinedOutput()
require.NoError(t, err, "failed to refresh helm dependencies")

values, err := os.ReadDir("./ci")
require.NoError(t, err)

for _, v := range values {
v := v
t.Run(v.Name(), func(t *testing.T) {
t.Parallel()

out, err := client.Template(ctx, ".", helm.TemplateOptions{
Name: "redpanda",
ValuesFile: "./ci/" + v.Name(),
Set: []string{
// Tests utilize some non-deterministic helpers (rng). We don't
// really care about the stability of their output, so globally
// disable them.
"tests.enabled=false",
// jwtSecret defaults to a random string. Can't have that
// in snapshot testing so set it to a static value.
"console.secret.login.jwtSecret=SECRETKEY",
},
})
require.NoError(t, err)

testutil.AssertGolden(t, testutil.Text, "./testdata/"+v.Name()+".golden", out)
})
}
}

func TestChart(t *testing.T) {
if testing.Short() {
t.Skipf("Skipping log running test...")
Expand All @@ -87,7 +136,15 @@ func TestChart(t *testing.T) {
credsSecret, err := kube.Create(ctx, env.Ctl(), TieredStorageSecret(env.Namespace()))
require.NoError(t, err)

rpRelease := env.Install(".", helm.InstallOptions{})
rpRelease := env.Install(".", helm.InstallOptions{
Values: Values{
Config: &Config{
Node: map[string]any{
"developer_mode": true,
},
},
},
})

rpk := Client{Ctl: env.Ctl(), Release: &rpRelease}

Expand Down
2 changes: 1 addition & 1 deletion charts/redpanda/templates/_configmap.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ rpk:
{{- define "rpk-config-external" -}}
{{- $brokers := list -}}
{{- $admin := list -}}
{{- $profile := keys .Values.listeners.kafka.external | first -}}
{{- $profile := keys .Values.listeners.kafka.external | sortAlpha | first -}}
{{- $kafkaListener := get .Values.listeners.kafka.external $profile -}}
{{- $adminListener := dict -}}
{{- if .Values.listeners.admin.external -}}
Expand Down
2 changes: 2 additions & 0 deletions charts/redpanda/templates/values.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{- /* Generated from "values.go" */ -}}

Loading

0 comments on commit 695a863

Please sign in to comment.