From ec56110bd212dce6e6bdf8de2e907a4d708165b0 Mon Sep 17 00:00:00 2001 From: "Robin H. Johnson" Date: Sat, 6 Jun 2026 21:47:33 -0700 Subject: [PATCH] test: render sloth_version golden label with YAML-correct quoting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The Prometheus generate integration tests and the library use-case tests both substitute the build version (from `git describe --tags --always`) into shared golden .tpl files. When no tag is reachable that resolves to the bare short git SHA, which is all decimal digits ~3.7% of the time (e.g. 6950793). Sloth's YAML serializers quote such a value ("6950793") so it round-trips as a string, but the templates emitted it unquoted, so the assertion failed only on those commits. Add a shared `yamlValue` template helper (testutils.GoldenTemplateFuncs) that marshals the version through yaml.v2 — matching both the Prometheus and k8s serializers — and use it for the sloth_version label from both test consumers. The comment line stays raw. The tests now pass for numeric SHAs, hex SHAs, and semver-style versions alike. Co-Authored-By: Claude Opus 4.8 (1M context) --- pkg/lib/lib_as_cli_use_cases_test.go | 3 +- test/integration/prometheus/generate_test.go | 2 +- .../prometheus/testdata/out-base-28d.yaml.tpl | 4 +-- .../out-base-custom-windows-7d.yaml.tpl | 4 +-- .../testdata/out-base-extra-labels.yaml.tpl | 4 +-- .../prometheus/testdata/out-base-k8s.yaml.tpl | 4 +-- .../testdata/out-base-no-alerts.yaml.tpl | 4 +-- .../prometheus/testdata/out-base.yaml.tpl | 4 +-- .../testdata/out-multifile-k8s.yaml.tpl | 8 +++--- .../testdata/out-multifile.yaml.tpl | 8 +++--- .../prometheus/testdata/out-openslo.yaml.tpl | 2 +- .../testdata/out-sli-plugin.yaml.tpl | 2 +- .../testdata/out-slo-plugin-k8s.yaml.tpl | 2 +- .../testdata/out-slo-plugin.yaml.tpl | 2 +- test/integration/testutils/template.go | 28 +++++++++++++++++++ 15 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 test/integration/testutils/template.go diff --git a/pkg/lib/lib_as_cli_use_cases_test.go b/pkg/lib/lib_as_cli_use_cases_test.go index fb57683a..568adb8c 100644 --- a/pkg/lib/lib_as_cli_use_cases_test.go +++ b/pkg/lib/lib_as_cli_use_cases_test.go @@ -12,6 +12,7 @@ import ( "github.com/slok/sloth/internal/plugin" "github.com/slok/sloth/pkg/common/model" "github.com/slok/sloth/pkg/lib" + "github.com/slok/sloth/test/integration/testutils" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) @@ -279,7 +280,7 @@ func getExpData(t *testing.T, path string) []byte { require.NoError(t, err) var b bytes.Buffer - err = template.Must(template.New("").Parse(string(expOutData))).Execute(&b, map[string]string{ + err = template.Must(template.New("").Funcs(testutils.GoldenTemplateFuncs).Parse(string(expOutData))).Execute(&b, map[string]string{ "version": info.Version, }) require.NoError(t, err) diff --git a/test/integration/prometheus/generate_test.go b/test/integration/prometheus/generate_test.go index 69b0c84f..071e4d03 100644 --- a/test/integration/prometheus/generate_test.go +++ b/test/integration/prometheus/generate_test.go @@ -24,7 +24,7 @@ func (e expecteOutLoader) mustLoadExp(path string) string { panic(err) } - tmpl := template.Must(template.New("").Parse(string(fileData))) + tmpl := template.Must(template.New("").Funcs(testutils.GoldenTemplateFuncs).Parse(string(fileData))) data := map[string]string{"version": e.version} var b bytes.Buffer diff --git a/test/integration/prometheus/testdata/out-base-28d.yaml.tpl b/test/integration/prometheus/testdata/out-base-28d.yaml.tpl index c21c1982..ca081cd3 100644 --- a/test/integration/prometheus/testdata/out-base-28d.yaml.tpl +++ b/test/integration/prometheus/testdata/out-base-28d.yaml.tpl @@ -170,7 +170,7 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-alerts-svc01-slo1 rules: - alert: myServiceAlert @@ -392,4 +392,4 @@ groups: sloth_service: svc01 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-base-custom-windows-7d.yaml.tpl b/test/integration/prometheus/testdata/out-base-custom-windows-7d.yaml.tpl index 681d8f1d..093f09dd 100644 --- a/test/integration/prometheus/testdata/out-base-custom-windows-7d.yaml.tpl +++ b/test/integration/prometheus/testdata/out-base-custom-windows-7d.yaml.tpl @@ -170,7 +170,7 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-alerts-svc01-slo1 rules: - alert: myServiceAlert @@ -392,4 +392,4 @@ groups: sloth_service: svc01 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-base-extra-labels.yaml.tpl b/test/integration/prometheus/testdata/out-base-extra-labels.yaml.tpl index f24adba0..54d58afa 100644 --- a/test/integration/prometheus/testdata/out-base-extra-labels.yaml.tpl +++ b/test/integration/prometheus/testdata/out-base-extra-labels.yaml.tpl @@ -200,7 +200,7 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-alerts-svc01-slo1 rules: - alert: myServiceAlert @@ -452,4 +452,4 @@ groups: sloth_service: svc01 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-base-k8s.yaml.tpl b/test/integration/prometheus/testdata/out-base-k8s.yaml.tpl index b7de7ecc..e67faa34 100644 --- a/test/integration/prometheus/testdata/out-base-k8s.yaml.tpl +++ b/test/integration/prometheus/testdata/out-base-k8s.yaml.tpl @@ -178,7 +178,7 @@ spec: sloth_service: svc01 sloth_slo: slo1 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info - name: sloth-slo-alerts-svc01-slo1 rules: @@ -400,5 +400,5 @@ spec: sloth_service: svc01 sloth_slo: slo02 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info diff --git a/test/integration/prometheus/testdata/out-base-no-alerts.yaml.tpl b/test/integration/prometheus/testdata/out-base-no-alerts.yaml.tpl index a582a2b5..94d4fb1c 100644 --- a/test/integration/prometheus/testdata/out-base-no-alerts.yaml.tpl +++ b/test/integration/prometheus/testdata/out-base-no-alerts.yaml.tpl @@ -170,7 +170,7 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-sli-recordings-svc01-slo02 rules: - record: slo:sli_error:ratio_rate5m @@ -344,4 +344,4 @@ groups: sloth_service: svc01 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-base.yaml.tpl b/test/integration/prometheus/testdata/out-base.yaml.tpl index b62f83ca..5df3570a 100644 --- a/test/integration/prometheus/testdata/out-base.yaml.tpl +++ b/test/integration/prometheus/testdata/out-base.yaml.tpl @@ -170,7 +170,7 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-alerts-svc01-slo1 rules: - alert: myServiceAlert @@ -392,4 +392,4 @@ groups: sloth_service: svc01 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-multifile-k8s.yaml.tpl b/test/integration/prometheus/testdata/out-multifile-k8s.yaml.tpl index 9f7f4312..34500ab7 100644 --- a/test/integration/prometheus/testdata/out-multifile-k8s.yaml.tpl +++ b/test/integration/prometheus/testdata/out-multifile-k8s.yaml.tpl @@ -178,7 +178,7 @@ spec: sloth_service: svc01 sloth_slo: slo1 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info - name: sloth-slo-alerts-svc01-slo1 rules: @@ -400,7 +400,7 @@ spec: sloth_service: svc01 sloth_slo: slo02 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info --- @@ -582,7 +582,7 @@ spec: sloth_service: svc02 sloth_slo: slo1 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info - name: sloth-slo-alerts-svc02-slo1 rules: @@ -804,5 +804,5 @@ spec: sloth_service: svc02 sloth_slo: slo02 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info diff --git a/test/integration/prometheus/testdata/out-multifile.yaml.tpl b/test/integration/prometheus/testdata/out-multifile.yaml.tpl index 3e2c0402..4e2e666a 100644 --- a/test/integration/prometheus/testdata/out-multifile.yaml.tpl +++ b/test/integration/prometheus/testdata/out-multifile.yaml.tpl @@ -170,7 +170,7 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-alerts-svc01-slo1 rules: - alert: myServiceAlert @@ -392,7 +392,7 @@ groups: sloth_service: svc01 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} --- # Code generated by Sloth ({{ .version }}): https://github.com/slok/sloth. @@ -565,7 +565,7 @@ groups: sloth_service: svc02 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} - name: sloth-slo-alerts-svc02-slo1 rules: - alert: myServiceAlert @@ -787,4 +787,4 @@ groups: sloth_service: svc02 sloth_slo: slo02 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-openslo.yaml.tpl b/test/integration/prometheus/testdata/out-openslo.yaml.tpl index ab442d71..ea5f4d16 100644 --- a/test/integration/prometheus/testdata/out-openslo.yaml.tpl +++ b/test/integration/prometheus/testdata/out-openslo.yaml.tpl @@ -196,4 +196,4 @@ groups: sloth_service: svc01 sloth_slo: slo1-0 sloth_spec: openslo/v1alpha - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} diff --git a/test/integration/prometheus/testdata/out-sli-plugin.yaml.tpl b/test/integration/prometheus/testdata/out-sli-plugin.yaml.tpl index 7ec95551..eeb231f5 100644 --- a/test/integration/prometheus/testdata/out-sli-plugin.yaml.tpl +++ b/test/integration/prometheus/testdata/out-sli-plugin.yaml.tpl @@ -176,5 +176,5 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} tier: "2" diff --git a/test/integration/prometheus/testdata/out-slo-plugin-k8s.yaml.tpl b/test/integration/prometheus/testdata/out-slo-plugin-k8s.yaml.tpl index 33a3d9e1..dedfc3bd 100644 --- a/test/integration/prometheus/testdata/out-slo-plugin-k8s.yaml.tpl +++ b/test/integration/prometheus/testdata/out-slo-plugin-k8s.yaml.tpl @@ -182,5 +182,5 @@ spec: sloth_service: svc01 sloth_slo: slo1 sloth_spec: sloth.slok.dev/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} record: sloth_slo_info diff --git a/test/integration/prometheus/testdata/out-slo-plugin.yaml.tpl b/test/integration/prometheus/testdata/out-slo-plugin.yaml.tpl index 501b1964..9ad193ce 100644 --- a/test/integration/prometheus/testdata/out-slo-plugin.yaml.tpl +++ b/test/integration/prometheus/testdata/out-slo-plugin.yaml.tpl @@ -173,5 +173,5 @@ groups: sloth_service: svc01 sloth_slo: slo1 sloth_spec: prometheus/v1 - sloth_version: {{ .version }} + sloth_version: {{ yamlValue .version }} tier: "2" diff --git a/test/integration/testutils/template.go b/test/integration/testutils/template.go new file mode 100644 index 00000000..e20d87ce --- /dev/null +++ b/test/integration/testutils/template.go @@ -0,0 +1,28 @@ +package testutils + +import ( + "strings" + "text/template" + + yaml "gopkg.in/yaml.v2" +) + +// GoldenTemplateFuncs are the template helpers used to render the golden output +// .tpl files. They are shared by the Prometheus CLI integration tests and the +// library use-case tests, both of which render the same templates and must +// therefore agree on how values are rendered. +var GoldenTemplateFuncs = template.FuncMap{ + // yamlValue renders a string the way Sloth's YAML serializers do when it is + // emitted as a mapping value. The version comes from `git describe` and can + // be an all-digit short git SHA (e.g. "6950793"), which the serializers quote + // so it is not parsed back as a number. Using this for the sloth_version label + // keeps the golden files correct regardless of whether the version happens to + // look numeric. + "yamlValue": func(v string) (string, error) { + b, err := yaml.Marshal(v) + if err != nil { + return "", err + } + return strings.TrimRight(string(b), "\n"), nil + }, +}