Merge pull request #25427 from yongtang/25404-service-update-env-add
Fix `service update --env-add` issue
This commit is contained in:
commit
59ca493121
|
@ -295,10 +295,22 @@ func updateLabels(flags *pflag.FlagSet, field *map[string]string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func updateEnvironment(flags *pflag.FlagSet, field *[]string) {
|
func updateEnvironment(flags *pflag.FlagSet, field *[]string) {
|
||||||
|
envSet := map[string]string{}
|
||||||
|
for _, v := range *field {
|
||||||
|
envSet[envKey(v)] = v
|
||||||
|
}
|
||||||
if flags.Changed(flagEnvAdd) {
|
if flags.Changed(flagEnvAdd) {
|
||||||
value := flags.Lookup(flagEnvAdd).Value.(*opts.ListOpts)
|
value := flags.Lookup(flagEnvAdd).Value.(*opts.ListOpts)
|
||||||
*field = append(*field, value.GetAll()...)
|
for _, v := range value.GetAll() {
|
||||||
|
envSet[envKey(v)] = v
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
*field = []string{}
|
||||||
|
for _, v := range envSet {
|
||||||
|
*field = append(*field, v)
|
||||||
|
}
|
||||||
|
|
||||||
toRemove := buildToRemoveSet(flags, flagEnvRemove)
|
toRemove := buildToRemoveSet(flags, flagEnvRemove)
|
||||||
*field = removeItems(*field, toRemove, envKey)
|
*field = removeItems(*field, toRemove, envKey)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package service
|
package service
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sort"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/docker/docker/pkg/testutil/assert"
|
"github.com/docker/docker/pkg/testutil/assert"
|
||||||
|
@ -68,8 +69,10 @@ func TestUpdateEnvironment(t *testing.T) {
|
||||||
|
|
||||||
updateEnvironment(flags, &envs)
|
updateEnvironment(flags, &envs)
|
||||||
assert.Equal(t, len(envs), 2)
|
assert.Equal(t, len(envs), 2)
|
||||||
assert.Equal(t, envs[0], "tokeep=value")
|
// Order has been removed in updateEnvironment (map)
|
||||||
assert.Equal(t, envs[1], "toadd=newenv")
|
sort.Strings(envs)
|
||||||
|
assert.Equal(t, envs[0], "toadd=newenv")
|
||||||
|
assert.Equal(t, envs[1], "tokeep=value")
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestUpdateEnvironmentWithDuplicateValues(t *testing.T) {
|
func TestUpdateEnvironmentWithDuplicateValues(t *testing.T) {
|
||||||
|
@ -84,6 +87,18 @@ func TestUpdateEnvironmentWithDuplicateValues(t *testing.T) {
|
||||||
assert.Equal(t, len(envs), 0)
|
assert.Equal(t, len(envs), 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestUpdateEnvironmentWithDuplicateKeys(t *testing.T) {
|
||||||
|
// Test case for #25404
|
||||||
|
flags := newUpdateCommand(nil).Flags()
|
||||||
|
flags.Set("env-add", "A=b")
|
||||||
|
|
||||||
|
envs := []string{"A=c"}
|
||||||
|
|
||||||
|
updateEnvironment(flags, &envs)
|
||||||
|
assert.Equal(t, len(envs), 1)
|
||||||
|
assert.Equal(t, envs[0], "A=b")
|
||||||
|
}
|
||||||
|
|
||||||
func TestUpdateMounts(t *testing.T) {
|
func TestUpdateMounts(t *testing.T) {
|
||||||
flags := newUpdateCommand(nil).Flags()
|
flags := newUpdateCommand(nil).Flags()
|
||||||
flags.Set("mount-add", "type=volume,target=/toadd")
|
flags.Set("mount-add", "type=volume,target=/toadd")
|
||||||
|
|
Loading…
Reference in New Issue