2020-10-20 11:29:26 +00:00
|
|
|
package flagutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
2023-09-01 07:27:51 +00:00
|
|
|
"time"
|
2020-10-20 11:29:26 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestDurationSetFailure(t *testing.T) {
|
|
|
|
f := func(value string) {
|
|
|
|
t.Helper()
|
2024-10-17 11:47:48 +00:00
|
|
|
var d RetentionDuration
|
2020-10-20 11:29:26 +00:00
|
|
|
if err := d.Set(value); err == nil {
|
|
|
|
t.Fatalf("expecting non-nil error in d.Set(%q)", value)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("foobar")
|
|
|
|
f("5foobar")
|
|
|
|
f("ah")
|
|
|
|
f("134xd")
|
|
|
|
f("2.43sdfw")
|
|
|
|
|
|
|
|
// Too big value in months
|
|
|
|
f("12345")
|
|
|
|
|
2021-02-15 10:57:56 +00:00
|
|
|
// Too big duration
|
2024-06-14 13:20:21 +00:00
|
|
|
f("999y")
|
2021-02-15 10:57:56 +00:00
|
|
|
f("100000000000y")
|
|
|
|
|
2020-10-20 11:29:26 +00:00
|
|
|
// Negative duration
|
|
|
|
f("-1")
|
|
|
|
f("-34h")
|
|
|
|
|
2024-10-17 11:47:48 +00:00
|
|
|
// RetentionDuration in minutes is confused with duration in months
|
2020-10-20 11:29:26 +00:00
|
|
|
f("1m")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDurationSetSuccess(t *testing.T) {
|
|
|
|
f := func(value string, expectedMsecs int64) {
|
|
|
|
t.Helper()
|
2024-10-17 11:47:48 +00:00
|
|
|
var d RetentionDuration
|
2020-10-20 11:29:26 +00:00
|
|
|
if err := d.Set(value); err != nil {
|
|
|
|
t.Fatalf("unexpected error in d.Set(%q): %s", value, err)
|
|
|
|
}
|
2023-09-03 08:51:29 +00:00
|
|
|
if d.Milliseconds() != expectedMsecs {
|
|
|
|
t.Fatalf("unexpected result; got %d; want %d", d.Milliseconds(), expectedMsecs)
|
2020-10-20 11:29:26 +00:00
|
|
|
}
|
|
|
|
valueString := d.String()
|
|
|
|
valueExpected := strings.ToLower(value)
|
|
|
|
if valueString != valueExpected {
|
|
|
|
t.Fatalf("unexpected valueString; got %q; want %q", valueString, valueExpected)
|
|
|
|
}
|
|
|
|
}
|
2024-02-07 18:53:01 +00:00
|
|
|
f("", 0)
|
2020-10-20 11:29:26 +00:00
|
|
|
f("0", 0)
|
2023-09-03 08:33:37 +00:00
|
|
|
f("1", msecsPer31Days)
|
|
|
|
f("123.456", 123.456*msecsPer31Days)
|
2020-10-20 11:29:26 +00:00
|
|
|
f("1h", 3600*1000)
|
|
|
|
f("1.5d", 1.5*24*3600*1000)
|
|
|
|
f("2.3W", 2.3*7*24*3600*1000)
|
2022-05-02 07:12:05 +00:00
|
|
|
f("1w", 7*24*3600*1000)
|
2020-10-20 11:29:26 +00:00
|
|
|
f("0.25y", 0.25*365*24*3600*1000)
|
2024-06-14 13:20:21 +00:00
|
|
|
f("100y", 100*365*24*3600*1000)
|
2020-10-20 11:29:26 +00:00
|
|
|
}
|
2023-09-01 07:27:51 +00:00
|
|
|
|
|
|
|
func TestDurationDuration(t *testing.T) {
|
|
|
|
f := func(value string, expected time.Duration) {
|
|
|
|
t.Helper()
|
2024-10-17 11:47:48 +00:00
|
|
|
var d RetentionDuration
|
2023-09-01 07:27:51 +00:00
|
|
|
if err := d.Set(value); err != nil {
|
|
|
|
t.Fatalf("unexpected error in d.Set(%q): %s", value, err)
|
|
|
|
}
|
|
|
|
if d.Duration() != expected {
|
|
|
|
t.Fatalf("unexpected result; got %v; want %v", d.Duration().String(), expected.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
f("0", 0)
|
|
|
|
f("1", 31*24*time.Hour)
|
|
|
|
f("1h", time.Hour)
|
|
|
|
f("1.5d", 1.5*24*time.Hour)
|
|
|
|
f("1w", 7*24*time.Hour)
|
|
|
|
f("0.25y", 0.25*365*24*time.Hour)
|
|
|
|
}
|