From 31dc5c0a9a8bdc11c7ad335aebb753ed527caa5a Mon Sep 17 00:00:00 2001 From: Vincent Demeester Date: Tue, 27 Feb 2018 12:14:07 +0100 Subject: [PATCH] Fix `--label` behavior on run Commit 2b17f4c8a8caad552025edb05a73db683fb8a5c6 fixed the way empty labels are taken into account (i.e. not interpolated from environment variable), but it created a regression. `ValidateLabel` functions doesn't allow empty label value, but it has always been possible to pass an empty label via the cli (`docker run --label foo`). This fixes that by not validating the label flag. Signed-off-by: Vincent Demeester --- cli/command/container/client_test.go | 8 ++++++++ cli/command/container/opts.go | 2 +- cli/command/container/run_test.go | 25 +++++++++++++++++++++++++ 3 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 cli/command/container/run_test.go diff --git a/cli/command/container/client_test.go b/cli/command/container/client_test.go index e0ffd2a66eb1..ed04920b16e2 100644 --- a/cli/command/container/client_test.go +++ b/cli/command/container/client_test.go @@ -16,6 +16,7 @@ type fakeClient struct { execInspectFunc func(execID string) (types.ContainerExecInspect, error) execCreateFunc func(container string, config types.ExecConfig) (types.IDResponse, error) createContainerFunc func(config *container.Config, hostConfig *container.HostConfig, networkingConfig *network.NetworkingConfig, containerName string) (container.ContainerCreateCreatedBody, error) + containerStartFunc func(container string, options types.ContainerStartOptions) error imageCreateFunc func(parentReference string, options types.ImageCreateOptions) (io.ReadCloser, error) infoFunc func() (types.Info, error) containerStatPathFunc func(container, path string) (types.ContainerPathStat, error) @@ -116,3 +117,10 @@ func (f *fakeClient) ContainerWait(_ context.Context, container string, _ contai } return nil, nil } + +func (f *fakeClient) ContainerStart(_ context.Context, container string, options types.ContainerStartOptions) error { + if f.containerStartFunc != nil { + return f.containerStartFunc(container, options) + } + return nil +} diff --git a/cli/command/container/opts.go b/cli/command/container/opts.go index 0adecca759b3..b8ff5e4a4a32 100644 --- a/cli/command/container/opts.go +++ b/cli/command/container/opts.go @@ -145,7 +145,7 @@ func addFlags(flags *pflag.FlagSet) *containerOptions { expose: opts.NewListOpts(nil), extraHosts: opts.NewListOpts(opts.ValidateExtraHost), groupAdd: opts.NewListOpts(nil), - labels: opts.NewListOpts(opts.ValidateLabel), + labels: opts.NewListOpts(nil), labelsFile: opts.NewListOpts(nil), linkLocalIPs: opts.NewListOpts(nil), links: opts.NewListOpts(opts.ValidateLink), diff --git a/cli/command/container/run_test.go b/cli/command/container/run_test.go new file mode 100644 index 000000000000..ca08fb3362ab --- /dev/null +++ b/cli/command/container/run_test.go @@ -0,0 +1,25 @@ +package container + +import ( + "testing" + + "github.com/docker/cli/internal/test" + "github.com/docker/docker/api/types/container" + "github.com/docker/docker/api/types/network" + "github.com/stretchr/testify/assert" +) + +func TestRunLabel(t *testing.T) { + cli := test.NewFakeCli(&fakeClient{ + createContainerFunc: func(_ *container.Config, _ *container.HostConfig, _ *network.NetworkingConfig, _ string) (container.ContainerCreateCreatedBody, error) { + return container.ContainerCreateCreatedBody{ + ID: "id", + }, nil + }, + Version: "1.36", + }) + cmd := NewRunCommand(cli) + cmd.Flags().Set("detach", "true") + cmd.SetArgs([]string{"--label", "foo", "busybox"}) + assert.NoError(t, cmd.Execute()) +}