This commit changes the state package so that errors encountered while
expanding templates for custom labels and annotations are returned
from the function. This is not used at present, but will be used in the
future as we look at how to offer better feedback to users who don't
have access to logs, for example our customers who use Hosted Grafana.
// err should be an ExpandError that contains the template for the Summary and an error
varexpandErrtemplate.ExpandError
require.True(t,errors.As(err,&expandErr))
require.EqualError(t,expandErr,"failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}Instance {{ $labels. }} has been down for more than 5 minutes': error parsing template __alert_test: template: __alert_test:1: unexpected <.> in operand")
})
t.Run("originals are returned with two errors",func(t*testing.T){
original:=map[string]string{
"Summary":`Instance {{$labels.}} has been down for more than 5 minutes`,
"Description":"The instance has been down for {{ $value minutes, please check the instance is online",
require.EqualError(t,expandErr1,"failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}Instance {{ $labels. }} has been down for more than 5 minutes': error parsing template __alert_test: template: __alert_test:1: unexpected <.> in operand")
require.EqualError(t,expandErr2,"failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}The instance has been down for {{ $value minutes, please check the instance is online': error parsing template __alert_test: template: __alert_test:1: function \"minutes\" not defined")
})
t.Run("expanded and original is returned when there is one error",func(t*testing.T){
original:=map[string]string{
"Summary":`Instance {{$labels.instance}} has been down for more than 5 minutes`,
"Description":"The instance has been down for {{ $value minutes, please check the instance is online",
}
expected:=map[string]string{
"Summary":"Instance host1 has been down for more than 5 minutes",
"Description":"The instance has been down for {{ $value minutes, please check the instance is online",
// TODO: Please update this test in issue https://github.com/grafana/grafana/issues/63686
varmultierr*multierror.Error
require.True(t,errors.As(err,&multierr))
require.Equal(t,multierr.Len(),1)
// assert each error matches the expected error
varexpandErrtemplate.ExpandError
require.True(t,errors.As(err,&expandErr))
require.EqualError(t,expandErr,"failed to expand template '{{- $labels := .Labels -}}{{- $values := .Values -}}{{- $value := .Value -}}The instance has been down for {{ $value minutes, please check the instance is online': error parsing template __alert_test: template: __alert_test:1: function \"minutes\" not defined")