Merge pull request #9108 from BinSquare/remove-SourceDateEpochOrNow

Refactor: Removing inherently flaky and unused SourceDateEpochOrNow function.
This commit is contained in:
Akihiro Suda 2023-09-18 17:58:40 +09:00 committed by GitHub
commit a8d078cc9b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 43 deletions

View File

@ -22,8 +22,6 @@ import (
"os" "os"
"strconv" "strconv"
"time" "time"
"github.com/containerd/containerd/log"
) )
// SourceDateEpochEnv is the SOURCE_DATE_EPOCH env var. // SourceDateEpochEnv is the SOURCE_DATE_EPOCH env var.
@ -44,19 +42,6 @@ func SourceDateEpoch() (*time.Time, error) {
return t, nil return t, nil
} }
// SourceDateEpochOrNow returns the SOURCE_DATE_EPOCH time if available,
// otherwise returns the current time.
func SourceDateEpochOrNow() time.Time {
epoch, err := SourceDateEpoch()
if err != nil {
log.L.WithError(err).Warnf("Invalid %s", SourceDateEpochEnv)
}
if epoch != nil {
return *epoch
}
return time.Now().UTC()
}
// ParseSourceDateEpoch parses the given source date epoch, as *time.Time. // ParseSourceDateEpoch parses the given source date epoch, as *time.Time.
// It returns an error if sourceDateEpoch is empty or not well-formatted. // It returns an error if sourceDateEpoch is empty or not well-formatted.
func ParseSourceDateEpoch(sourceDateEpoch string) (*time.Time, error) { func ParseSourceDateEpoch(sourceDateEpoch string) (*time.Time, error) {

View File

@ -19,25 +19,12 @@ package epoch
import ( import (
"fmt" "fmt"
"os" "os"
"runtime"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func rightAfter(t1, t2 time.Time) bool {
if t2.Equal(t1) {
return true
}
threshold := 10 * time.Millisecond
if runtime.GOOS == "windows" {
// Low timer resolution on Windows
threshold *= 10
}
return t2.After(t1) && t2.Before(t1.Add(threshold))
}
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)
@ -50,10 +37,6 @@ func TestSourceDateEpoch(t *testing.T) {
vp, err := SourceDateEpoch() vp, err := SourceDateEpoch()
require.NoError(t, err) require.NoError(t, err)
require.Nil(t, vp) require.Nil(t, vp)
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
}) })
t.Run("WithEmptySourceDateEpoch", func(t *testing.T) { t.Run("WithEmptySourceDateEpoch", func(t *testing.T) {
@ -67,10 +50,6 @@ func TestSourceDateEpoch(t *testing.T) {
vp, err = ParseSourceDateEpoch(emptyValue) vp, err = ParseSourceDateEpoch(emptyValue)
require.Error(t, err, "value is empty") require.Error(t, err, "value is empty")
require.Nil(t, vp) require.Nil(t, vp)
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
}) })
t.Run("WithSourceDateEpoch", func(t *testing.T) { t.Run("WithSourceDateEpoch", func(t *testing.T) {
@ -88,9 +67,6 @@ func TestSourceDateEpoch(t *testing.T) {
vp, err = ParseSourceDateEpoch(fmt.Sprintf("%d", sourceDateEpoch.Unix())) vp, err = ParseSourceDateEpoch(fmt.Sprintf("%d", sourceDateEpoch.Unix()))
require.NoError(t, err) require.NoError(t, err)
require.True(t, vp.Equal(sourceDateEpoch)) require.True(t, vp.Equal(sourceDateEpoch))
v := SourceDateEpochOrNow()
require.True(t, v.Equal(sourceDateEpoch))
}) })
t.Run("WithInvalidSourceDateEpoch", func(t *testing.T) { t.Run("WithInvalidSourceDateEpoch", func(t *testing.T) {
@ -104,9 +80,5 @@ func TestSourceDateEpoch(t *testing.T) {
vp, err = ParseSourceDateEpoch(invalidValue) vp, err = ParseSourceDateEpoch(invalidValue)
require.ErrorContains(t, err, "invalid value:") require.ErrorContains(t, err, "invalid value:")
require.Nil(t, vp) require.Nil(t, vp)
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
}) })
} }