2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2017-06-21 17:20:49 -04:00
|
|
|
"github.com/docker/cli/cli/config/configfile"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/docker/cli/cli/internal/test"
|
|
|
|
"github.com/docker/docker/api/types"
|
2017-04-25 19:24:07 -04:00
|
|
|
"github.com/docker/docker/pkg/testutil"
|
2017-03-30 20:21:14 -04:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
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{},
|
|
|
|
expectedError: "requires exactly 1 argument(s).",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
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")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "trust-error",
|
|
|
|
args: []string{"--disable-content-trust=false", "image:repo"},
|
|
|
|
expectedError: "you are not authorized to perform this operation: server returned 401.",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf := new(bytes.Buffer)
|
2017-06-21 17:20:49 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{imagePushFunc: tc.imagePushFunc}, buf)
|
2017-06-27 10:31:38 -04:00
|
|
|
cli.SetConfigfile(configfile.New("filename"))
|
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)
|
2017-04-25 19:24:07 -04:00
|
|
|
testutil.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-03-30 20:21:14 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewPushCommandSuccess(t *testing.T) {
|
|
|
|
testCases := []struct {
|
2017-06-09 12:32:14 -04:00
|
|
|
name string
|
|
|
|
args []string
|
2017-03-30 20:21:14 -04:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple",
|
|
|
|
args: []string{"image:tag"},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
|
|
buf := new(bytes.Buffer)
|
2017-06-21 17:20:49 -04:00
|
|
|
cli := test.NewFakeCli(&fakeClient{
|
2017-03-30 20:21:14 -04:00
|
|
|
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
|
|
|
return ioutil.NopCloser(strings.NewReader("")), nil
|
|
|
|
},
|
2017-06-21 17:20:49 -04:00
|
|
|
}, buf)
|
2017-06-27 10:31:38 -04:00
|
|
|
cli.SetConfigfile(configfile.New("filename"))
|
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)
|
|
|
|
assert.NoError(t, cmd.Execute())
|
|
|
|
}
|
|
|
|
}
|