2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
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"
|
|
|
|
"github.com/pkg/errors"
|
2018-06-08 12:24:26 -04:00
|
|
|
"gotest.tools/assert"
|
2017-03-30 20:21:14 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewPushCommandErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
imagePushFunc func(ref string, options types.ImagePushOptions) (io.ReadCloser, 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: "invalid-name",
|
|
|
|
args: []string{"UPPERCASE_REPO"},
|
|
|
|
expectedError: "invalid reference format: repository name must be lowercase",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push-failed",
|
|
|
|
args: []string{"image:repo"},
|
|
|
|
expectedError: "Failed to push",
|
|
|
|
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
|
|
|
return ioutil.NopCloser(strings.NewReader("")), errors.Errorf("Failed to push")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2017-07-05 14:34:16 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc})
|
2017-06-21 17:20:49 -04:00
|
|
|
cmd := NewPushCommand(cli)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPushCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2018-07-18 15:29:56 -04:00
|
|
|
name string
|
|
|
|
args []string
|
|
|
|
output string
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
2018-07-18 15:29:56 -04:00
|
|
|
name: "push",
|
2017-03-30 20:21:14 -04:00
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
2018-07-18 15:29:56 -04:00
|
|
|
{
|
|
|
|
name: "push quiet",
|
|
|
|
args: []string{"--quiet", "image:tag"},
|
|
|
|
output: `docker.io/library/image:tag
|
|
|
|
`,
|
|
|
|
},
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
2018-07-18 15:29:56 -04:00
|
|
|
tc := tc
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
|
|
|
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewPushCommand(cli)
|
|
|
|
cmd.SetOutput(cli.OutBuffer())
|
|
|
|
cmd.SetArgs(tc.args)
|
|
|
|
assert.NilError(t, cmd.Execute())
|
|
|
|
if tc.output != "" {
|
|
|
|
assert.Equal(t, tc.output, cli.OutBuffer().String())
|
|
|
|
}
|
2017-07-05 14:34:16 -04:00
|
|
|
})
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|