From 28a08a22b914497e0965583907c9dcc2c7afd7ad Mon Sep 17 00:00:00 2001 From: Jason Hall Date: Mon, 19 Sep 2022 10:04:53 -0400 Subject: [PATCH 1/2] un-skip history test and fix golden mismatches Signed-off-by: Jason Hall (cherry picked from commit f5e224e94076c61e95d71d91b7ead715b969e547) Signed-off-by: Sebastiaan van Stijn --- cli/command/image/history_test.go | 26 ++++++++++++------- .../history-command-success.non-human.golden | 4 +-- .../history-command-success.simple.golden | 4 +-- 3 files changed, 20 insertions(+), 14 deletions(-) diff --git a/cli/command/image/history_test.go b/cli/command/image/history_test.go index 6641b74772..c60a97f3fc 100644 --- a/cli/command/image/history_test.go +++ b/cli/command/image/history_test.go @@ -44,12 +44,15 @@ func TestNewHistoryCommandErrors(t *testing.T) { } func notUTCTimezone() bool { - now := time.Now() - return now != now.UTC() + if _, offset := time.Now().Zone(); offset != 0 { + return true + } + return false } func TestNewHistoryCommandSuccess(t *testing.T) { skip.If(t, notUTCTimezone, "expected output requires UTC timezone") + testCases := []struct { name string args []string @@ -93,13 +96,16 @@ func TestNewHistoryCommandSuccess(t *testing.T) { }, } for _, tc := range testCases { - cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}) - cmd := NewHistoryCommand(cli) - cmd.SetOut(io.Discard) - cmd.SetArgs(tc.args) - err := cmd.Execute() - assert.NilError(t, err) - actual := cli.OutBuffer().String() - golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name)) + tc := tc + t.Run(tc.name, func(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}) + cmd := NewHistoryCommand(cli) + cmd.SetOut(io.Discard) + cmd.SetArgs(tc.args) + err := cmd.Execute() + assert.NilError(t, err) + actual := cli.OutBuffer().String() + golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name)) + }) } } diff --git a/cli/command/image/testdata/history-command-success.non-human.golden b/cli/command/image/testdata/history-command-success.non-human.golden index 4a83a3d837..5af1af2dc9 100644 --- a/cli/command/image/testdata/history-command-success.non-human.golden +++ b/cli/command/image/testdata/history-command-success.non-human.golden @@ -1,2 +1,2 @@ -IMAGE CREATED AT CREATED BY SIZE COMMENT -abcdef 2017-01-01T12:00:03Z rose 0 new history item! +IMAGE CREATED AT CREATED BY SIZE COMMENT +abcdef 2017-01-01T12:00:03Z rose 0 new history item! diff --git a/cli/command/image/testdata/history-command-success.simple.golden b/cli/command/image/testdata/history-command-success.simple.golden index 8aa590526f..2d963d3248 100644 --- a/cli/command/image/testdata/history-command-success.simple.golden +++ b/cli/command/image/testdata/history-command-success.simple.golden @@ -1,2 +1,2 @@ -IMAGE CREATED CREATED BY SIZE COMMENT -123456789012 Less than a second ago 0B +IMAGE CREATED CREATED BY SIZE COMMENT +123456789012 Less than a second ago 0B From b83959e001558470b52d33d2dcf8a10e85e020a3 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Mon, 28 Aug 2023 10:39:47 +0200 Subject: [PATCH 2/2] force TestNewHistoryCommandSuccess to use UTC timezone This test was skipped if the host was not using UTC timezone, because the output of timestamps would be different, causing the test to fail. This patch overrides the TZ env-var to make the test use UTC, so that we don't have to skip the test. Signed-off-by: Sebastiaan van Stijn (cherry picked from commit 42ac5d4bf9d24929ff90700b80e13af6b8462eeb) Signed-off-by: Sebastiaan van Stijn --- cli/command/image/history_test.go | 14 ++++---------- .../testdata/history-command-success.simple.golden | 2 +- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/cli/command/image/history_test.go b/cli/command/image/history_test.go index c60a97f3fc..4f3f54677b 100644 --- a/cli/command/image/history_test.go +++ b/cli/command/image/history_test.go @@ -11,7 +11,6 @@ import ( "github.com/pkg/errors" "gotest.tools/v3/assert" "gotest.tools/v3/golden" - "gotest.tools/v3/skip" ) func TestNewHistoryCommandErrors(t *testing.T) { @@ -43,16 +42,7 @@ func TestNewHistoryCommandErrors(t *testing.T) { } } -func notUTCTimezone() bool { - if _, offset := time.Now().Zone(); offset != 0 { - return true - } - return false -} - func TestNewHistoryCommandSuccess(t *testing.T) { - skip.If(t, notUTCTimezone, "expected output requires UTC timezone") - testCases := []struct { name string args []string @@ -65,6 +55,7 @@ func TestNewHistoryCommandSuccess(t *testing.T) { return []image.HistoryResponseItem{{ ID: "1234567890123456789", Created: time.Now().Unix(), + Comment: "none", }}, nil }, }, @@ -98,6 +89,9 @@ func TestNewHistoryCommandSuccess(t *testing.T) { for _, tc := range testCases { tc := tc t.Run(tc.name, func(t *testing.T) { + // Set to UTC timezone as timestamps in output are + // printed in the current timezone + t.Setenv("TZ", "UTC") cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}) cmd := NewHistoryCommand(cli) cmd.SetOut(io.Discard) diff --git a/cli/command/image/testdata/history-command-success.simple.golden b/cli/command/image/testdata/history-command-success.simple.golden index 2d963d3248..75758d65a6 100644 --- a/cli/command/image/testdata/history-command-success.simple.golden +++ b/cli/command/image/testdata/history-command-success.simple.golden @@ -1,2 +1,2 @@ IMAGE CREATED CREATED BY SIZE COMMENT -123456789012 Less than a second ago 0B +123456789012 Less than a second ago 0B none