Skip to content

Commit 8318437

Browse files
author
y.li
committed
fix hardcode in TestImageHistory
Signed-off-by: y.li <[email protected]>
1 parent 7efb818 commit 8318437

File tree

2 files changed

+78
-6
lines changed

2 files changed

+78
-6
lines changed

cmd/nerdctl/image/image_history_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/containerd/nerdctl/mod/tigron/require"
3030
"github.com/containerd/nerdctl/mod/tigron/test"
3131

32+
"github.com/containerd/nerdctl/v2/pkg/formatter"
3233
"github.com/containerd/nerdctl/v2/pkg/testutil"
3334
"github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest"
3435
)
@@ -93,11 +94,6 @@ func TestImageHistory(t *testing.T) {
9394
history, err := decode(stdout)
9495
assert.NilError(t, err, info)
9596
assert.Equal(t, len(history), 2, info)
96-
assert.Equal(t, history[0].Size, "0B", info)
97-
// FIXME: how is this going to age?
98-
assert.Equal(t, history[0].CreatedSince, "4 years ago", info)
99-
assert.Equal(t, history[0].Snapshot, "<missing>", info)
100-
assert.Equal(t, history[0].Comment, "", info)
10197

10298
localTimeL1, _ := time.Parse(time.RFC3339, "2021-03-31T10:21:23-07:00")
10399
localTimeL2, _ := time.Parse(time.RFC3339, "2021-03-31T10:21:21-07:00")
@@ -108,8 +104,13 @@ func TestImageHistory(t *testing.T) {
108104
assert.Equal(t, compTime2.UTC().String(), localTimeL2.UTC().String(), info)
109105
assert.Equal(t, history[1].CreatedBy, "/bin/sh -c #(nop) ADD file:3b16ffee2b26d8af5…", info)
110106

107+
assert.Equal(t, history[0].Size, "0B", info)
108+
assert.Equal(t, history[0].CreatedSince, formatter.TimeSinceInHuman(compTime1), info)
109+
assert.Equal(t, history[0].Snapshot, "<missing>", info)
110+
assert.Equal(t, history[0].Comment, "", info)
111+
111112
assert.Equal(t, history[1].Size, "5.947MB", info)
112-
assert.Equal(t, history[1].CreatedSince, "4 years ago", info)
113+
assert.Equal(t, history[1].CreatedSince, formatter.TimeSinceInHuman(compTime2), info)
113114
assert.Equal(t, history[1].Snapshot, "sha256:56bf55b8eed1f0b4794a30386e4d1d3da949c…", info)
114115
assert.Equal(t, history[1].Comment, "", info)
115116
}),

pkg/formatter/formatter_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package formatter
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"gotest.tools/v3/assert"
8+
)
9+
10+
func TestTimeSinceInHuman(t *testing.T) {
11+
now := time.Now()
12+
13+
tests := []struct {
14+
name string
15+
input time.Time
16+
expected string
17+
}{
18+
{
19+
name: "1 second ago",
20+
input: now.Add(-1 * time.Second),
21+
expected: "1 second ago",
22+
},
23+
{
24+
name: "59 seconds ago",
25+
input: now.Add(-59 * time.Second),
26+
expected: "59 seconds ago",
27+
},
28+
{
29+
name: "1 minute ago",
30+
input: now.Add(-1 * time.Minute),
31+
expected: "About a minute ago",
32+
},
33+
{
34+
name: "1 hour ago",
35+
input: now.Add(-1 * time.Hour),
36+
expected: "About an hour ago",
37+
},
38+
{
39+
name: "1 day ago",
40+
input: now.Add(-24 * time.Hour),
41+
expected: "24 hours ago",
42+
},
43+
{
44+
name: "4 days ago",
45+
input: now.Add(-4 * 24 * time.Hour),
46+
expected: "4 days ago",
47+
},
48+
{
49+
name: "1 year ago",
50+
input: now.Add(-365 * 24 * time.Hour),
51+
expected: "12 months ago",
52+
},
53+
{
54+
name: "4 years ago",
55+
input: now.Add(-4 * 365 * 24 * time.Hour),
56+
expected: "4 years ago",
57+
},
58+
{
59+
name: "zero duration",
60+
input: now,
61+
expected: "Less than a second ago",
62+
},
63+
}
64+
65+
for _, tt := range tests {
66+
t.Run(tt.name, func(t *testing.T) {
67+
result := TimeSinceInHuman(tt.input)
68+
assert.Equal(t, tt.expected, result)
69+
})
70+
}
71+
}

0 commit comments

Comments
 (0)