2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"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"
|
2018-03-05 18:53:52 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/assert"
|
2017-08-16 12:19:53 -04:00
|
|
|
"github.com/gotestyourself/gotestyourself/golden"
|
2018-03-08 13:20:42 -05:00
|
|
|
"github.com/gotestyourself/gotestyourself/skip"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
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}))
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
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 {
|
|
|
|
now := time.Now()
|
|
|
|
return now != now.UTC()
|
|
|
|
}
|
|
|
|
|
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")
|
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 {
|
2017-07-05 14:43:39 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imageHistoryFunc: tc.imageHistoryFunc})
|
|
|
|
cmd := NewHistoryCommand(cli)
|
2017-03-30 20:21:14 -04:00
|
|
|
cmd.SetOutput(ioutil.Discard)
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.Execute()
|
2018-03-06 14:44:13 -05:00
|
|
|
assert.NilError(t, err)
|
2017-07-05 14:43:39 -04:00
|
|
|
actual := cli.OutBuffer().String()
|
2017-12-20 17:54:31 -05:00
|
|
|
golden.Assert(t, actual, fmt.Sprintf("history-command-success.%s.golden", tc.name))
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|