2017-03-30 20:21:14 -04:00
|
|
|
package image
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"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"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/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"},
|
2023-08-30 18:36:58 -04:00
|
|
|
expectedError: "invalid reference format: repository name (library/UPPERCASE_REPO) must be lowercase",
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "push-failed",
|
|
|
|
args: []string{"image:repo"},
|
|
|
|
expectedError: "Failed to push",
|
|
|
|
imagePushFunc: func(ref string, options types.ImagePushOptions) (io.ReadCloser, error) {
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), errors.Errorf("Failed to push")
|
2017-03-30 20:21:14 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
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)
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2022-02-25 07:10:34 -05:00
|
|
|
return io.NopCloser(strings.NewReader("")), nil
|
2018-07-18 15:29:56 -04:00
|
|
|
},
|
|
|
|
})
|
|
|
|
cmd := NewPushCommand(cli)
|
2020-05-07 08:25:59 -04:00
|
|
|
cmd.SetOut(cli.OutBuffer())
|
2018-07-18 15:29:56 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|