2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2022-02-25 07:10:34 -05:00
|
|
|
"io"
|
2017-03-30 20:21:14 -04:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2017-08-21 16:30:09 -04:00
|
|
|
"github.com/docker/cli/internal/test"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/docker/api/types/image"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
"gotest.tools/v3/golden"
|
|
|
|
"gotest.tools/v3/skip"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewHistoryCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
imageHistoryFunc func(img string) ([]image.HistoryResponseItem, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "wrong-args",
|
|
|
|
args: []string{},
|
2017-08-12 12:25:38 -04:00
|
|
|
expectedError: "requires exactly 1 argument.",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "client-error",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
expectedError: "something went wrong",
|
|
|
|
imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) {
|
|
|
|
return []image.HistoryResponseItem{{}}, errors.Errorf("something went wrong")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:43:39 -04:00
|
|
|
cmd := NewHistoryCommand(test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc}))
|
2022-02-25 07:10:34 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetArgs(tc.args)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-08 13:20:42 -05:00
|
|
|
func notUTCTimezone() bool {
|
2022-09-19 10:04:53 -04:00
|
|
|
if _, offset := time.Now().Zone(); offset != 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
2018-03-08 13:20:42 -05:00
|
|
|
}
|
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
func TestNewHistoryCommandSuccess(t *testing.T) {
|
2018-03-08 13:20:42 -05:00
|
|
|
skip.If(t, notUTCTimezone, "expected output requires UTC timezone")
|
2022-09-19 10:04:53 -04:00
|
|
|
|
2017-03-30 20:21:14 -04:00
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
imageHistoryFunc func(img string) ([]image.HistoryResponseItem, error)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) {
|
|
|
|
return []image.HistoryResponseItem{{
|
|
|
|
ID: "1234567890123456789",
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "quiet",
|
|
|
|
args: []string{"--quiet", "image:tag"},
|
|
|
|
},
|
|
|
|
{
|
2017-12-20 17:54:31 -05:00
|
|
|
name: "non-human",
|
|
|
|
args: []string{"--human=false", "image:tag"},
|
|
|
|
imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) {
|
|
|
|
return []image.HistoryResponseItem{{
|
|
|
|
ID: "abcdef",
|
|
|
|
Created: time.Date(2017, 1, 1, 12, 0, 3, 0, time.UTC).Unix(),
|
|
|
|
CreatedBy: "rose",
|
|
|
|
Comment: "new history item!",
|
|
|
|
}}, nil
|
|
|
|
},
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "quiet-no-trunc",
|
|
|
|
args: []string{"--quiet", "--no-trunc", "image:tag"},
|
|
|
|
imageHistoryFunc: func(img string) ([]image.HistoryResponseItem, error) {
|
|
|
|
return []image.HistoryResponseItem{{
|
|
|
|
ID: "1234567890123456789",
|
|
|
|
Created: time.Now().Unix(),
|
|
|
|
}}, nil
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2022-09-19 10:04:53 -04:00
|
|
|
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))
|
|
|
|
})
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|