Fix test failure in different local time zones

TestParseDuration relied on an elapsed time calculation which
would vary based on the system local time. Fix the test by not relying
on the system time location. Also make the test more deterministic
by injecting time in tests rather than using system time.

Fixes #4529.
This commit is contained in:
Zach Kipp 2020-09-06 16:50:16 -07:00 committed by Nick Craig-Wood
parent cdaea62932
commit 6156f90601
2 changed files with 25 additions and 9 deletions

View File

@ -88,8 +88,9 @@ func parseDurationDates(age string, epoch time.Time) (t time.Duration, err error
return t, err return t, err
} }
// ParseDuration parses a duration string. Accept ms|s|m|h|d|w|M|y suffixes. Defaults to second if not provided // parseDurationFromNow parses a duration string. Allows ParseDuration to match the time
func ParseDuration(age string) (d time.Duration, err error) { // package and easier testing within the fs package.
func parseDurationFromNow(age string, getNow func() time.Time) (d time.Duration, err error) {
if age == "off" { if age == "off" {
return time.Duration(DurationOff), nil return time.Duration(DurationOff), nil
} }
@ -105,7 +106,7 @@ func ParseDuration(age string) (d time.Duration, err error) {
return d, nil return d, nil
} }
d, err = parseDurationDates(age, time.Now()) d, err = parseDurationDates(age, getNow())
if err == nil { if err == nil {
return d, nil return d, nil
} }
@ -113,6 +114,11 @@ func ParseDuration(age string) (d time.Duration, err error) {
return d, err return d, err
} }
// ParseDuration parses a duration string. Accept ms|s|m|h|d|w|M|y suffixes. Defaults to second if not provided
func ParseDuration(age string) (time.Duration, error) {
return parseDurationFromNow(age, time.Now)
}
// ReadableString parses d into a human readable duration. // ReadableString parses d into a human readable duration.
// Based on https://github.com/hako/durafmt // Based on https://github.com/hako/durafmt
func (d Duration) ReadableString() string { func (d Duration) ReadableString() string {

View File

@ -15,6 +15,11 @@ import (
var _ pflag.Value = (*Duration)(nil) var _ pflag.Value = (*Duration)(nil)
func TestParseDuration(t *testing.T) { func TestParseDuration(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
getNow := func() time.Time {
return now
}
for _, test := range []struct { for _, test := range []struct {
in string in string
want time.Duration want time.Duration
@ -37,12 +42,12 @@ func TestParseDuration(t *testing.T) {
{"1x", 0, true}, {"1x", 0, true},
{"off", time.Duration(DurationOff), false}, {"off", time.Duration(DurationOff), false},
{"1h2m3s", time.Hour + 2*time.Minute + 3*time.Second, false}, {"1h2m3s", time.Hour + 2*time.Minute + 3*time.Second, false},
{"2001-02-03", time.Since(time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)), false}, {"2001-02-03", now.Sub(time.Date(2001, 2, 3, 0, 0, 0, 0, time.UTC)), false},
{"2001-02-03 10:11:12", time.Since(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false}, {"2001-02-03 10:11:12", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false},
{"2001-02-03T10:11:12", time.Since(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false}, {"2001-02-03T10:11:12", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 0, time.UTC)), false},
{"2001-02-03T10:11:12.123Z", time.Since(time.Date(2001, 2, 3, 10, 11, 12, 123, time.UTC)), false}, {"2001-02-03T10:11:12.123Z", now.Sub(time.Date(2001, 2, 3, 10, 11, 12, 123, time.UTC)), false},
} { } {
duration, err := ParseDuration(test.in) duration, err := parseDurationFromNow(test.in, getNow)
if test.err { if test.err {
require.Error(t, err) require.Error(t, err)
} else { } else {
@ -58,6 +63,11 @@ func TestParseDuration(t *testing.T) {
} }
func TestDurationString(t *testing.T) { func TestDurationString(t *testing.T) {
now := time.Date(2020, 9, 5, 8, 15, 5, 250, time.UTC)
getNow := func() time.Time {
return now
}
for _, test := range []struct { for _, test := range []struct {
in time.Duration in time.Duration
want string want string
@ -88,7 +98,7 @@ func TestDurationString(t *testing.T) {
got := Duration(test.in).String() got := Duration(test.in).String()
assert.Equal(t, test.want, got) assert.Equal(t, test.want, got)
// Test the reverse // Test the reverse
reverse, err := ParseDuration(test.want) reverse, err := parseDurationFromNow(test.want, getNow)
assert.NoError(t, err) assert.NoError(t, err)
assert.Equal(t, test.in, reverse) assert.Equal(t, test.in, reverse)
} }