From 8318437f07b104c0638aefb02c59eef64719ef11 Mon Sep 17 00:00:00 2001 From: "y.li" Date: Fri, 4 Apr 2025 21:26:50 +0800 Subject: [PATCH 1/3] fix hardcode in TestImageHistory Signed-off-by: y.li --- cmd/nerdctl/image/image_history_test.go | 13 ++--- pkg/formatter/formatter_test.go | 71 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 6 deletions(-) create mode 100644 pkg/formatter/formatter_test.go diff --git a/cmd/nerdctl/image/image_history_test.go b/cmd/nerdctl/image/image_history_test.go index 16667641600..699a7cf6fe1 100644 --- a/cmd/nerdctl/image/image_history_test.go +++ b/cmd/nerdctl/image/image_history_test.go @@ -29,6 +29,7 @@ import ( "github.com/containerd/nerdctl/mod/tigron/require" "github.com/containerd/nerdctl/mod/tigron/test" + "github.com/containerd/nerdctl/v2/pkg/formatter" "github.com/containerd/nerdctl/v2/pkg/testutil" "github.com/containerd/nerdctl/v2/pkg/testutil/nerdtest" ) @@ -93,11 +94,6 @@ func TestImageHistory(t *testing.T) { history, err := decode(stdout) assert.NilError(t, err, info) assert.Equal(t, len(history), 2, info) - assert.Equal(t, history[0].Size, "0B", info) - // FIXME: how is this going to age? - assert.Equal(t, history[0].CreatedSince, "4 years ago", info) - assert.Equal(t, history[0].Snapshot, "", info) - assert.Equal(t, history[0].Comment, "", info) localTimeL1, _ := time.Parse(time.RFC3339, "2021-03-31T10:21:23-07:00") localTimeL2, _ := time.Parse(time.RFC3339, "2021-03-31T10:21:21-07:00") @@ -108,8 +104,13 @@ func TestImageHistory(t *testing.T) { assert.Equal(t, compTime2.UTC().String(), localTimeL2.UTC().String(), info) assert.Equal(t, history[1].CreatedBy, "/bin/sh -c #(nop) ADD file:3b16ffee2b26d8af5…", info) + assert.Equal(t, history[0].Size, "0B", info) + assert.Equal(t, history[0].CreatedSince, formatter.TimeSinceInHuman(compTime1), info) + assert.Equal(t, history[0].Snapshot, "", info) + assert.Equal(t, history[0].Comment, "", info) + assert.Equal(t, history[1].Size, "5.947MB", info) - assert.Equal(t, history[1].CreatedSince, "4 years ago", info) + assert.Equal(t, history[1].CreatedSince, formatter.TimeSinceInHuman(compTime2), info) assert.Equal(t, history[1].Snapshot, "sha256:56bf55b8eed1f0b4794a30386e4d1d3da949c…", info) assert.Equal(t, history[1].Comment, "", info) }), diff --git a/pkg/formatter/formatter_test.go b/pkg/formatter/formatter_test.go new file mode 100644 index 00000000000..3d3890ba25b --- /dev/null +++ b/pkg/formatter/formatter_test.go @@ -0,0 +1,71 @@ +package formatter + +import ( + "testing" + "time" + + "gotest.tools/v3/assert" +) + +func TestTimeSinceInHuman(t *testing.T) { + now := time.Now() + + tests := []struct { + name string + input time.Time + expected string + }{ + { + name: "1 second ago", + input: now.Add(-1 * time.Second), + expected: "1 second ago", + }, + { + name: "59 seconds ago", + input: now.Add(-59 * time.Second), + expected: "59 seconds ago", + }, + { + name: "1 minute ago", + input: now.Add(-1 * time.Minute), + expected: "About a minute ago", + }, + { + name: "1 hour ago", + input: now.Add(-1 * time.Hour), + expected: "About an hour ago", + }, + { + name: "1 day ago", + input: now.Add(-24 * time.Hour), + expected: "24 hours ago", + }, + { + name: "4 days ago", + input: now.Add(-4 * 24 * time.Hour), + expected: "4 days ago", + }, + { + name: "1 year ago", + input: now.Add(-365 * 24 * time.Hour), + expected: "12 months ago", + }, + { + name: "4 years ago", + input: now.Add(-4 * 365 * 24 * time.Hour), + expected: "4 years ago", + }, + { + name: "zero duration", + input: now, + expected: "Less than a second ago", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := TimeSinceInHuman(tt.input) + assert.Equal(t, tt.expected, result) + }) + } +} From 4efae1649b5b858265a35126c9d23d3e64cc525a Mon Sep 17 00:00:00 2001 From: "y.li" Date: Fri, 4 Apr 2025 22:42:47 +0800 Subject: [PATCH 2/3] ltag Signed-off-by: y.li --- pkg/formatter/formatter_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/pkg/formatter/formatter_test.go b/pkg/formatter/formatter_test.go index 3d3890ba25b..296cecf5041 100644 --- a/pkg/formatter/formatter_test.go +++ b/pkg/formatter/formatter_test.go @@ -1,3 +1,19 @@ +/* + Copyright The containerd Authors. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + package formatter import ( From d1b827a9820c8c9f72687936f18d90e4037a33ee Mon Sep 17 00:00:00 2001 From: "y.li" Date: Sat, 5 Apr 2025 00:00:33 +0800 Subject: [PATCH 3/3] add parallel Signed-off-by: y.li --- pkg/formatter/formatter_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pkg/formatter/formatter_test.go b/pkg/formatter/formatter_test.go index 296cecf5041..6e039039e11 100644 --- a/pkg/formatter/formatter_test.go +++ b/pkg/formatter/formatter_test.go @@ -25,6 +25,7 @@ import ( func TestTimeSinceInHuman(t *testing.T) { now := time.Now() + t.Parallel() tests := []struct { name string @@ -80,6 +81,7 @@ func TestTimeSinceInHuman(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + t.Parallel() result := TimeSinceInHuman(tt.input) assert.Equal(t, tt.expected, result) })