Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pkg/lib/lib_as_cli_use_cases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/integration/prometheus/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/integration/prometheus/testdata/out-base-28d.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -392,4 +392,4 @@ groups:
sloth_service: svc01
sloth_slo: slo02
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -392,4 +392,4 @@ groups:
sloth_service: svc01
sloth_slo: slo02
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -452,4 +452,4 @@ groups:
sloth_service: svc01
sloth_slo: slo02
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
4 changes: 2 additions & 2 deletions test/integration/prometheus/testdata/out-base-k8s.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -344,4 +344,4 @@ groups:
sloth_service: svc01
sloth_slo: slo02
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
4 changes: 2 additions & 2 deletions test/integration/prometheus/testdata/out-base.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -392,4 +392,4 @@ groups:
sloth_service: svc01
sloth_slo: slo02
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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

---
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
8 changes: 4 additions & 4 deletions test/integration/prometheus/testdata/out-multifile.yaml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -787,4 +787,4 @@ groups:
sloth_service: svc02
sloth_slo: slo02
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,4 @@ groups:
sloth_service: svc01
sloth_slo: slo1-0
sloth_spec: openslo/v1alpha
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ groups:
sloth_service: svc01
sloth_slo: slo1
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
tier: "2"
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -173,5 +173,5 @@ groups:
sloth_service: svc01
sloth_slo: slo1
sloth_spec: prometheus/v1
sloth_version: {{ .version }}
sloth_version: {{ yamlValue .version }}
tier: "2"
28 changes: 28 additions & 0 deletions test/integration/testutils/template.go
Original file line number Diff line number Diff line change
@@ -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
},
}