Add handling of timezones in unixToTime tests (#9707)

**What this PR does / why we need it**:

The unit tests for unixToTime do not take the current timezone into
account.


**Checklist**
- [x] Reviewed the
[`CONTRIBUTING.md`](https://github.com/grafana/loki/blob/main/CONTRIBUTING.md)
guide (**required**)
- [ ] Documentation added
- [x] Tests updated
- [ ] `CHANGELOG.md` updated
- [ ] Changes that require user attention or interaction to upgrade are
documented in `docs/sources/upgrading/_index.md`
- [ ] For Helm chart changes bump the Helm chart version in
`production/helm/loki/Chart.yaml` and update
`production/helm/loki/CHANGELOG.md` and
`production/helm/loki/README.md`. [Example
PR](d10549e3ec)

---------

Signed-off-by: Michel Hollands <michel.hollands@grafana.com>
pull/9706/head^2
Michel Hollands 2 years ago committed by GitHub
parent 8abe080fd6
commit 79485cd30d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 29
      pkg/logql/log/fmt_test.go

@ -3,6 +3,7 @@ package log
import (
"fmt"
"testing"
"time"
"github.com/prometheus/prometheus/model/labels"
"github.com/stretchr/testify/require"
@ -498,6 +499,14 @@ func newMustLineFormatter(tmpl string) *LineFormatter {
}
func Test_labelsFormatter_Format(t *testing.T) {
// These variables are used to test unixToTime.
// They resolve to the local timezone so it works everywhere.
epochDay19503 := time.Unix(19503*86400, 0)
epochSeconds1679577215 := time.Unix(1679577215, 0)
epochMilliseconds1257894000000 := time.UnixMilli(1257894000000)
epochMicroseconds1673798889902000 := time.UnixMicro(1673798889902000)
epochNanoseconds1000000000000000000 := time.Unix(0, 1000000000000000000)
tests := []struct {
name string
fmter *LabelsFormatter
@ -657,47 +666,47 @@ func Test_labelsFormatter_Format(t *testing.T) {
},
{
"unixToTime days",
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime | date "2006-01-02" }}`)}),
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime }}`)}),
labels.Labels{{Name: "foo", Value: ""}, {Name: "bar", Value: "19503"}},
labels.Labels{
{Name: "bar", Value: "19503"},
{Name: "foo", Value: "2023-05-26"},
{Name: "foo", Value: epochDay19503.String()},
},
},
{
"unixToTime seconds",
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime | date "2006-01-02" }}`)}),
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime }}`)}),
labels.Labels{{Name: "foo", Value: ""}, {Name: "bar", Value: "1679577215"}},
labels.Labels{
{Name: "bar", Value: "1679577215"},
{Name: "foo", Value: "2023-03-23"},
{Name: "foo", Value: epochSeconds1679577215.String()},
},
},
{
"unixToTime milliseconds",
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime | date "2006-01-02" }}`)}),
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime }}`)}),
labels.Labels{{Name: "foo", Value: ""}, {Name: "bar", Value: "1257894000000"}},
labels.Labels{
{Name: "bar", Value: "1257894000000"},
{Name: "foo", Value: "2009-11-10"},
{Name: "foo", Value: epochMilliseconds1257894000000.String()},
},
},
{
"unixToTime microseconds",
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime | date "2006-01-02" }}`)}),
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime }}`)}),
labels.Labels{{Name: "foo", Value: ""}, {Name: "bar", Value: "1673798889902000"}},
labels.Labels{
{Name: "bar", Value: "1673798889902000"},
{Name: "foo", Value: "2023-01-15"},
{Name: "foo", Value: epochMicroseconds1673798889902000.String()},
},
},
{
"unixToTime nanoseconds",
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime | date "Jan 2, 2006" }}`)}),
mustNewLabelsFormatter([]LabelFmt{NewTemplateLabelFmt("foo", `{{ .bar | unixToTime }}`)}),
labels.Labels{{Name: "foo", Value: ""}, {Name: "bar", Value: "1000000000000000000"}},
labels.Labels{
{Name: "bar", Value: "1000000000000000000"},
{Name: "foo", Value: "Sep 9, 2001"},
{Name: "foo", Value: epochNanoseconds1000000000000000000.String()},
},
},
}

Loading…
Cancel
Save