pkg/epoch: fix tests on macOS

These tests were failing on my macOS; could be the precision issue (like on
Windows), or just because they're "too fast".

    === RUN   TestSourceDateEpoch/WithoutSourceDateEpoch
        epoch_test.go:51:
                Error Trace:	/Users/thajeztah/go/src/github.com/containerd/containerd/pkg/epoch/epoch_test.go:51
                Error:      	Should be true
                Test:       	TestSourceDateEpoch/WithoutSourceDateEpoch
                Messages:   	now: 2023-06-23 11:47:09.93118 +0000 UTC, v: 2023-06-23 11:47:09.93118 +0000 UTC

This patch:

- updates the rightAfter utility to allow the timestamps to be "equal"
- updates the asserts to provide some details about the timestamps
- uses UTC for the value we're comparing to, to match the timestamps
  that are generated.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2023-06-23 13:53:44 +02:00
parent 44e2b26a87
commit 9924e56f42
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C

View File

@ -26,11 +26,15 @@ import (
)
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
return (t2.After(t1) && t2.Before(t1.Add(100*time.Millisecond))) || t2.Equal(t1)
threshold *= 10
}
return t2.After(t1) && t2.Before(t1.Add(10*time.Millisecond))
return t2.After(t1) && t2.Before(t1.Add(threshold))
}
func TestSourceDateEpoch(t *testing.T) {
@ -46,9 +50,9 @@ func TestSourceDateEpoch(t *testing.T) {
require.NoError(t, err)
require.Nil(t, vp)
now := time.Now()
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v))
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
})
t.Run("WithEmptySourceDateEpoch", func(t *testing.T) {
@ -58,9 +62,9 @@ func TestSourceDateEpoch(t *testing.T) {
require.NoError(t, err)
require.Nil(t, vp)
now := time.Now()
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v))
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
})
t.Run("WithSourceDateEpoch", func(t *testing.T) {
@ -85,8 +89,8 @@ func TestSourceDateEpoch(t *testing.T) {
require.ErrorContains(t, err, "invalid SOURCE_DATE_EPOCH value")
require.Nil(t, vp)
now := time.Now()
now := time.Now().UTC()
v := SourceDateEpochOrNow()
require.True(t, rightAfter(now, v))
require.True(t, rightAfter(now, v), "now: %s, v: %s", now, v)
})
}