Skip to content

Commit 0dd3552

Browse files
authored
Merge pull request #4083 from biubiubiuboomboomboom/fix/hardcode-in-testcase
[CI] fix hardcode in TestImageHistory
2 parents 5f6102c + d1b827a commit 0dd3552

File tree

2 files changed

+96
-6
lines changed

2 files changed

+96
-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: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
Copyright The containerd Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package formatter
18+
19+
import (
20+
"testing"
21+
"time"
22+
23+
"gotest.tools/v3/assert"
24+
)
25+
26+
func TestTimeSinceInHuman(t *testing.T) {
27+
now := time.Now()
28+
t.Parallel()
29+
30+
tests := []struct {
31+
name string
32+
input time.Time
33+
expected string
34+
}{
35+
{
36+
name: "1 second ago",
37+
input: now.Add(-1 * time.Second),
38+
expected: "1 second ago",
39+
},
40+
{
41+
name: "59 seconds ago",
42+
input: now.Add(-59 * time.Second),
43+
expected: "59 seconds ago",
44+
},
45+
{
46+
name: "1 minute ago",
47+
input: now.Add(-1 * time.Minute),
48+
expected: "About a minute ago",
49+
},
50+
{
51+
name: "1 hour ago",
52+
input: now.Add(-1 * time.Hour),
53+
expected: "About an hour ago",
54+
},
55+
{
56+
name: "1 day ago",
57+
input: now.Add(-24 * time.Hour),
58+
expected: "24 hours ago",
59+
},
60+
{
61+
name: "4 days ago",
62+
input: now.Add(-4 * 24 * time.Hour),
63+
expected: "4 days ago",
64+
},
65+
{
66+
name: "1 year ago",
67+
input: now.Add(-365 * 24 * time.Hour),
68+
expected: "12 months ago",
69+
},
70+
{
71+
name: "4 years ago",
72+
input: now.Add(-4 * 365 * 24 * time.Hour),
73+
expected: "4 years ago",
74+
},
75+
{
76+
name: "zero duration",
77+
input: now,
78+
expected: "Less than a second ago",
79+
},
80+
}
81+
82+
for _, tt := range tests {
83+
t.Run(tt.name, func(t *testing.T) {
84+
t.Parallel()
85+
result := TimeSinceInHuman(tt.input)
86+
assert.Equal(t, tt.expected, result)
87+
})
88+
}
89+
}

0 commit comments

Comments
 (0)