Merge pull request #651 from thaJeztah/fix-extra-host-sorting

Preserve sort-order of extra hosts, and allow duplicate entries
Upstream-commit: 96b8d15bdd
Component: cli
This commit is contained in:
Sebastiaan van Stijn
2017-10-30 20:38:10 +01:00
committed by GitHub
7 changed files with 119 additions and 17 deletions
+4 -3
View File
@@ -868,6 +868,10 @@ func updateReplicas(flags *pflag.FlagSet, serviceMode *swarm.ServiceMode) error
return nil
}
// updateHosts performs a diff between existing host entries, entries to be
// removed, and entries to be added. Host entries preserve the order in which they
// were added, as the specification mentions that in case multiple entries for a
// host exist, the first entry should be used (by default).
func updateHosts(flags *pflag.FlagSet, hosts *[]string) error {
// Combine existing Hosts (in swarmkit format) with the host to add (convert to swarmkit format)
if flags.Changed(flagHostAdd) {
@@ -902,9 +906,6 @@ func updateHosts(flags *pflag.FlagSet, hosts *[]string) error {
}
}
// Sort so that result is predictable.
sort.Strings(newHosts)
*hosts = newHosts
return nil
}
@@ -366,12 +366,23 @@ func TestUpdateHosts(t *testing.T) {
testutil.ErrorContains(t, flags.Set("host-add", "$example.com$"), `bad format for add-host: "$example.com$"`)
hosts := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 example.net"}
expected := []string{"1.2.3.4 example.com", "4.3.2.1 example.org", "2001:db8:abc8::1 ipv6.net"}
updateHosts(flags, &hosts)
require.Len(t, hosts, 3)
assert.Equal(t, "1.2.3.4 example.com", hosts[0])
assert.Equal(t, "2001:db8:abc8::1 ipv6.net", hosts[1])
assert.Equal(t, "4.3.2.1 example.org", hosts[2])
err := updateHosts(flags, &hosts)
assert.NoError(t, err)
assert.Equal(t, expected, hosts)
}
func TestUpdateHostsPreservesOrder(t *testing.T) {
flags := newUpdateCommand(nil).Flags()
flags.Set("host-add", "foobar:127.0.0.2")
flags.Set("host-add", "foobar:127.0.0.1")
flags.Set("host-add", "foobar:127.0.0.3")
hosts := []string{}
err := updateHosts(flags, &hosts)
assert.NoError(t, err)
assert.Equal(t, []string{"127.0.0.2 foobar", "127.0.0.1 foobar", "127.0.0.3 foobar"}, hosts)
}
func TestUpdatePortsRmWithProtocol(t *testing.T) {