mirror of
https://github.com/opentofu/opentofu.git
synced 2024-12-30 10:47:14 -06:00
cdd9464f9a
This is part of a general effort to move all of Terraform's non-library package surface under internal in order to reinforce that these are for internal use within Terraform only. If you were previously importing packages under this prefix into an external codebase, you could pin to an earlier release tag as an interim solution until you've make a plan to achieve the same functionality some other way.
86 lines
1.8 KiB
Go
86 lines
1.8 KiB
Go
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"),
|
|
cty.UnknownVal(cty.String),
|
|
true,
|
|
},
|
|
{ // Invalid format duration (day is not supported by ParseDuration)
|
|
cty.StringVal("2017-11-22T00:00:00Z"),
|
|
cty.StringVal("1d"),
|
|
cty.UnknownVal(cty.String),
|
|
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
|
|
} 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)
|
|
}
|
|
})
|
|
}
|
|
}
|