2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-05-22 18:11:31 -05:00
|
|
|
package funcs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTimestamp(t *testing.T) {
|
|
|
|
currentTime := time.Now().UTC()
|
|
|
|
result, err := Timestamp()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
resultTime, err := time.Parse(time.RFC3339, result.AsString())
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error parsing timestamp: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resultTime.Sub(currentTime).Seconds() > 10.0 {
|
|
|
|
t.Fatalf("Timestamp Diff too large. Expected: %s\nReceived: %s", currentTime.Format(time.RFC3339), result.AsString())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTimeadd(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Time cty.Value
|
|
|
|
Duration cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("1s"),
|
|
|
|
cty.StringVal("2017-11-22T00:00:01Z"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("10m1s"),
|
|
|
|
cty.StringVal("2017-11-22T00:10:01Z"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // also support subtraction
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("-1h"),
|
|
|
|
cty.StringVal("2017-11-21T23:00:00Z"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // Invalid format timestamp
|
|
|
|
cty.StringVal("2017-11-22"),
|
|
|
|
cty.StringVal("-1h"),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.String).RefineNotNull(),
|
2018-05-22 18:11:31 -05:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
{ // Invalid format duration (day is not supported by ParseDuration)
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("1d"),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.String).RefineNotNull(),
|
2018-05-22 18:11:31 -05:00
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("TimeAdd(%#v, %#v)", test.Time, test.Duration), func(t *testing.T) {
|
|
|
|
got, err := TimeAdd(test.Time, test.Duration)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
2018-05-23 11:38:12 -05:00
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2018-05-22 18:11:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2022-08-24 18:37:22 -05:00
|
|
|
|
|
|
|
func TestTimeCmp(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
TimeA, TimeB cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.Zero,
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("2017-11-22T01:00:00+01:00"),
|
|
|
|
cty.Zero,
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:01Z"),
|
|
|
|
cty.StringVal("2017-11-22T01:00:00+01:00"),
|
|
|
|
cty.NumberIntVal(1),
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T01:00:00Z"),
|
|
|
|
cty.StringVal("2017-11-22T00:59:00-01:00"),
|
|
|
|
cty.NumberIntVal(-1),
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T01:00:00+01:00"),
|
|
|
|
cty.StringVal("2017-11-22T01:00:00-01:00"),
|
|
|
|
cty.NumberIntVal(-1),
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T01:00:00-01:00"),
|
|
|
|
cty.StringVal("2017-11-22T01:00:00+01:00"),
|
|
|
|
cty.NumberIntVal(1),
|
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.StringVal("bloop"),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.String).RefineNotNull(),
|
2022-08-24 18:37:22 -05:00
|
|
|
`not a valid RFC3339 timestamp: cannot use "bloop" as year`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22 00:00:00Z"),
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.String).RefineNotNull(),
|
2022-08-24 18:37:22 -05:00
|
|
|
`not a valid RFC3339 timestamp: missing required time introducer 'T'`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
|
|
cty.UnknownVal(cty.String),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.Number).RefineNotNull(),
|
2022-08-24 18:37:22 -05:00
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.Number).RefineNotNull(),
|
2022-08-24 18:37:22 -05:00
|
|
|
``,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
cty.UnknownVal(cty.String),
|
2023-02-07 16:07:25 -06:00
|
|
|
cty.UnknownVal(cty.Number).RefineNotNull(),
|
2022-08-24 18:37:22 -05:00
|
|
|
``,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("TimeCmp(%#v, %#v)", test.TimeA, test.TimeB), func(t *testing.T) {
|
|
|
|
got, err := TimeCmp(test.TimeA, test.TimeB)
|
|
|
|
|
|
|
|
if test.Err != "" {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
if got := err.Error(); got != test.Err {
|
|
|
|
t.Errorf("wrong error message\ngot: %s\nwant: %s", got, test.Err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|