From 8ce3e4e159ffe5b98272836a33e5c6c5657fdb87 Mon Sep 17 00:00:00 2001 From: Samuel Karp Date: Fri, 3 Mar 2023 14:54:23 -0800 Subject: [PATCH] epoch: fix unit test when SOURCE_DATE_EPOCH is set Fixes https://github.com/containerd/containerd/issues/8200 Signed-off-by: Samuel Karp --- pkg/epoch/epoch.go | 2 +- pkg/epoch/epoch_test.go | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/pkg/epoch/epoch.go b/pkg/epoch/epoch.go index 0ba8ed3b1..124e8edb5 100644 --- a/pkg/epoch/epoch.go +++ b/pkg/epoch/epoch.go @@ -34,7 +34,7 @@ const SourceDateEpochEnv = "SOURCE_DATE_EPOCH" // If the env var is not set, SourceDateEpoch returns nil without an error. func SourceDateEpoch() (*time.Time, error) { v, ok := os.LookupEnv(SourceDateEpochEnv) - if !ok { + if !ok || v == "" { return nil, nil // not an error } i64, err := strconv.ParseInt(v, 10, 64) diff --git a/pkg/epoch/epoch_test.go b/pkg/epoch/epoch_test.go index 101d332df..f25c77eb2 100644 --- a/pkg/epoch/epoch_test.go +++ b/pkg/epoch/epoch_test.go @@ -36,7 +36,9 @@ func rightAfter(t1, t2 time.Time) bool { func TestSourceDateEpoch(t *testing.T) { if s, ok := os.LookupEnv(SourceDateEpochEnv); ok { t.Logf("%s is already set to %q, unsetting", SourceDateEpochEnv, s) + // see https://github.com/golang/go/issues/52817#issuecomment-1131339120 t.Setenv(SourceDateEpochEnv, "") + os.Unsetenv(SourceDateEpochEnv) } t.Run("WithoutSourceDateEpoch", func(t *testing.T) { @@ -49,6 +51,18 @@ func TestSourceDateEpoch(t *testing.T) { 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) { sourceDateEpoch, err := time.Parse(time.RFC3339, "2022-01-23T12:34:56Z") require.NoError(t, err)