epoch: fix unit test when SOURCE_DATE_EPOCH is set
Fixes https://github.com/containerd/containerd/issues/8200 Signed-off-by: Samuel Karp <samuelkarp@google.com>
This commit is contained in:
parent
7a77da2c26
commit
8ce3e4e159
@ -34,7 +34,7 @@ const SourceDateEpochEnv = "SOURCE_DATE_EPOCH"
|
|||||||
// If the env var is not set, SourceDateEpoch returns nil without an error.
|
// If the env var is not set, SourceDateEpoch returns nil without an error.
|
||||||
func SourceDateEpoch() (*time.Time, error) {
|
func SourceDateEpoch() (*time.Time, error) {
|
||||||
v, ok := os.LookupEnv(SourceDateEpochEnv)
|
v, ok := os.LookupEnv(SourceDateEpochEnv)
|
||||||
if !ok {
|
if !ok || v == "" {
|
||||||
return nil, nil // not an error
|
return nil, nil // not an error
|
||||||
}
|
}
|
||||||
i64, err := strconv.ParseInt(v, 10, 64)
|
i64, err := strconv.ParseInt(v, 10, 64)
|
||||||
|
@ -36,7 +36,9 @@ func rightAfter(t1, t2 time.Time) bool {
|
|||||||
func TestSourceDateEpoch(t *testing.T) {
|
func TestSourceDateEpoch(t *testing.T) {
|
||||||
if s, ok := os.LookupEnv(SourceDateEpochEnv); ok {
|
if s, ok := os.LookupEnv(SourceDateEpochEnv); ok {
|
||||||
t.Logf("%s is already set to %q, unsetting", SourceDateEpochEnv, s)
|
t.Logf("%s is already set to %q, unsetting", SourceDateEpochEnv, s)
|
||||||
|
// see https://github.com/golang/go/issues/52817#issuecomment-1131339120
|
||||||
t.Setenv(SourceDateEpochEnv, "")
|
t.Setenv(SourceDateEpochEnv, "")
|
||||||
|
os.Unsetenv(SourceDateEpochEnv)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Run("WithoutSourceDateEpoch", func(t *testing.T) {
|
t.Run("WithoutSourceDateEpoch", func(t *testing.T) {
|
||||||
@ -49,6 +51,18 @@ func TestSourceDateEpoch(t *testing.T) {
|
|||||||
require.True(t, rightAfter(now, v))
|
require.True(t, rightAfter(now, v))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("WithEmptySourceDateEpoch", func(t *testing.T) {
|
||||||
|
t.Setenv(SourceDateEpochEnv, "")
|
||||||
|
|
||||||
|
vp, err := SourceDateEpoch()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Nil(t, vp)
|
||||||
|
|
||||||
|
now := time.Now()
|
||||||
|
v := SourceDateEpochOrNow()
|
||||||
|
require.True(t, rightAfter(now, v))
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("WithSourceDateEpoch", func(t *testing.T) {
|
t.Run("WithSourceDateEpoch", func(t *testing.T) {
|
||||||
sourceDateEpoch, err := time.Parse(time.RFC3339, "2022-01-23T12:34:56Z")
|
sourceDateEpoch, err := time.Parse(time.RFC3339, "2022-01-23T12:34:56Z")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
Loading…
Reference in New Issue
Block a user