2017-09-26 18:15:04 -04:00
|
|
|
package manifest
|
|
|
|
|
|
|
|
import (
|
2018-05-03 21:02:44 -04:00
|
|
|
"context"
|
2022-02-25 07:11:27 -05:00
|
|
|
"io"
|
2017-09-26 18:15:04 -04:00
|
|
|
"testing"
|
|
|
|
|
2022-02-25 07:11:27 -05:00
|
|
|
"github.com/docker/cli/cli/manifest/store"
|
2017-09-26 18:15:04 -04:00
|
|
|
manifesttypes "github.com/docker/cli/cli/manifest/types"
|
|
|
|
"github.com/docker/cli/internal/test"
|
|
|
|
"github.com/docker/distribution/reference"
|
|
|
|
"github.com/pkg/errors"
|
2020-02-22 12:12:14 -05:00
|
|
|
"gotest.tools/v3/assert"
|
|
|
|
is "gotest.tools/v3/assert/cmp"
|
|
|
|
"gotest.tools/v3/golden"
|
2017-09-26 18:15:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestManifestCreateErrors(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
args []string
|
|
|
|
expectedError string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
args: []string{"too-few-arguments"},
|
|
|
|
expectedError: "requires at least 2 arguments",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
args: []string{"th!si'sa/fa!ke/li$t/name", "example.com/alpine:3.0"},
|
|
|
|
expectedError: "error parsing name for manifest list",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
cmd := newCreateListCommand(cli)
|
|
|
|
cmd.SetArgs(tc.args)
|
2022-02-25 07:11:27 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2018-03-06 14:03:47 -05:00
|
|
|
assert.ErrorContains(t, cmd.Execute(), tc.expectedError)
|
2017-09-26 18:15:04 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create a manifest list, then overwrite it, and inspect to see if the old one is still there
|
|
|
|
func TestManifestCreateAmend(t *testing.T) {
|
2022-02-25 07:11:27 -05:00
|
|
|
store := store.NewStore(t.TempDir())
|
2017-09-26 18:15:04 -04:00
|
|
|
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
cli.SetManifestStore(store)
|
|
|
|
|
|
|
|
namedRef := ref(t, "alpine:3.0")
|
|
|
|
imageManifest := fullImageManifest(t, namedRef)
|
|
|
|
err := store.Save(ref(t, "list:v1"), namedRef, imageManifest)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 18:15:04 -04:00
|
|
|
namedRef = ref(t, "alpine:3.1")
|
|
|
|
imageManifest = fullImageManifest(t, namedRef)
|
|
|
|
err = store.Save(ref(t, "list:v1"), namedRef, imageManifest)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 18:15:04 -04:00
|
|
|
|
|
|
|
cmd := newCreateListCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"example.com/list:v1", "example.com/alpine:3.1"})
|
|
|
|
cmd.Flags().Set("amend", "true")
|
2022-02-25 07:11:27 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-09-26 18:15:04 -04:00
|
|
|
err = cmd.Execute()
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 18:15:04 -04:00
|
|
|
|
|
|
|
// make a new cli to clear the buffers
|
|
|
|
cli = test.NewFakeCli(nil)
|
|
|
|
cli.SetManifestStore(store)
|
|
|
|
inspectCmd := newInspectCommand(cli)
|
|
|
|
inspectCmd.SetArgs([]string{"example.com/list:v1"})
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, inspectCmd.Execute())
|
2017-09-26 18:15:04 -04:00
|
|
|
actual := cli.OutBuffer()
|
|
|
|
expected := golden.Get(t, "inspect-manifest-list.golden")
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.Check(t, is.Equal(string(expected), actual.String()))
|
2017-09-26 18:15:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to overwrite a saved manifest and get refused
|
|
|
|
func TestManifestCreateRefuseAmend(t *testing.T) {
|
2022-02-25 07:11:27 -05:00
|
|
|
store := store.NewStore(t.TempDir())
|
2017-09-26 18:15:04 -04:00
|
|
|
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
cli.SetManifestStore(store)
|
|
|
|
namedRef := ref(t, "alpine:3.0")
|
|
|
|
imageManifest := fullImageManifest(t, namedRef)
|
|
|
|
err := store.Save(ref(t, "list:v1"), namedRef, imageManifest)
|
2018-03-05 18:53:52 -05:00
|
|
|
assert.NilError(t, err)
|
2017-09-26 18:15:04 -04:00
|
|
|
|
|
|
|
cmd := newCreateListCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"example.com/list:v1", "example.com/alpine:3.0"})
|
2022-02-25 07:11:27 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-09-26 18:15:04 -04:00
|
|
|
err = cmd.Execute()
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "refusing to amend an existing manifest list with no --amend flag")
|
2017-09-26 18:15:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// attempt to make a manifest list without valid images
|
|
|
|
func TestManifestCreateNoManifest(t *testing.T) {
|
2022-02-25 07:11:27 -05:00
|
|
|
store := store.NewStore(t.TempDir())
|
2017-09-26 18:15:04 -04:00
|
|
|
|
|
|
|
cli := test.NewFakeCli(nil)
|
|
|
|
cli.SetManifestStore(store)
|
|
|
|
cli.SetRegistryClient(&fakeRegistryClient{
|
|
|
|
getManifestFunc: func(_ context.Context, ref reference.Named) (manifesttypes.ImageManifest, error) {
|
|
|
|
return manifesttypes.ImageManifest{}, errors.Errorf("No such image: %v", ref)
|
|
|
|
},
|
|
|
|
getManifestListFunc: func(ctx context.Context, ref reference.Named) ([]manifesttypes.ImageManifest, error) {
|
|
|
|
return nil, errors.Errorf("No such manifest: %s", ref)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
cmd := newCreateListCommand(cli)
|
|
|
|
cmd.SetArgs([]string{"example.com/list:v1", "example.com/alpine:3.0"})
|
2022-02-25 07:11:27 -05:00
|
|
|
cmd.SetOut(io.Discard)
|
2017-09-26 18:15:04 -04:00
|
|
|
err := cmd.Execute()
|
2018-03-06 15:54:24 -05:00
|
|
|
assert.Error(t, err, "No such image: example.com/alpine:3.0")
|
2017-09-26 18:15:04 -04:00
|
|
|
}
|