Commit 5308957d authored by lalyos's avatar lalyos

Sort arguments before joining them, for reproducible return string

parent 0ba80021
...@@ -18,17 +18,32 @@ package util ...@@ -18,17 +18,32 @@ package util
import ( import (
"fmt" "fmt"
"sort"
"strings" "strings"
) )
// BuildArgumentListFromMap takes two string-string maps, one with the base arguments and one with optional override arguments // BuildArgumentListFromMap takes two string-string maps, one with the base arguments and one
// with optional override arguments. In the return list override arguments will precede base
// arguments
func BuildArgumentListFromMap(baseArguments map[string]string, overrideArguments map[string]string) []string { func BuildArgumentListFromMap(baseArguments map[string]string, overrideArguments map[string]string) []string {
var command []string var command []string
for k, v := range overrideArguments { var keys []string
for k := range overrideArguments {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := overrideArguments[k]
// values of "" are allowed as well // values of "" are allowed as well
command = append(command, fmt.Sprintf("--%s=%s", k, v)) command = append(command, fmt.Sprintf("--%s=%s", k, v))
} }
for k, v := range baseArguments { keys = []string{}
for k := range baseArguments {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
v := baseArguments[k]
if _, overrideExists := overrideArguments[k]; !overrideExists { if _, overrideExists := overrideArguments[k]; !overrideExists {
command = append(command, fmt.Sprintf("--%s=%s", k, v)) command = append(command, fmt.Sprintf("--%s=%s", k, v))
} }
......
...@@ -39,8 +39,8 @@ func TestBuildArgumentListFromMap(t *testing.T) { ...@@ -39,8 +39,8 @@ func TestBuildArgumentListFromMap(t *testing.T) {
}, },
expected: []string{ expected: []string{
"--admission-control=NamespaceLifecycle,LimitRanger", "--admission-control=NamespaceLifecycle,LimitRanger",
"--insecure-bind-address=127.0.0.1",
"--allow-privileged=true", "--allow-privileged=true",
"--insecure-bind-address=127.0.0.1",
}, },
}, },
{ // add an argument that is not in base { // add an argument that is not in base
...@@ -53,8 +53,8 @@ func TestBuildArgumentListFromMap(t *testing.T) { ...@@ -53,8 +53,8 @@ func TestBuildArgumentListFromMap(t *testing.T) {
}, },
expected: []string{ expected: []string{
"--admission-control=NamespaceLifecycle,LimitRanger", "--admission-control=NamespaceLifecycle,LimitRanger",
"--insecure-bind-address=127.0.0.1",
"--allow-privileged=true", "--allow-privileged=true",
"--insecure-bind-address=127.0.0.1",
}, },
}, },
{ // allow empty strings in base { // allow empty strings in base
...@@ -68,8 +68,8 @@ func TestBuildArgumentListFromMap(t *testing.T) { ...@@ -68,8 +68,8 @@ func TestBuildArgumentListFromMap(t *testing.T) {
}, },
expected: []string{ expected: []string{
"--admission-control=NamespaceLifecycle,LimitRanger", "--admission-control=NamespaceLifecycle,LimitRanger",
"--insecure-bind-address=127.0.0.1",
"--allow-privileged=true", "--allow-privileged=true",
"--insecure-bind-address=127.0.0.1",
"--something-that-allows-empty-string=", "--something-that-allows-empty-string=",
}, },
}, },
...@@ -85,17 +85,15 @@ func TestBuildArgumentListFromMap(t *testing.T) { ...@@ -85,17 +85,15 @@ func TestBuildArgumentListFromMap(t *testing.T) {
}, },
expected: []string{ expected: []string{
"--admission-control=NamespaceLifecycle,LimitRanger", "--admission-control=NamespaceLifecycle,LimitRanger",
"--insecure-bind-address=127.0.0.1",
"--allow-privileged=true",
"--something-that-allows-empty-string=", "--something-that-allows-empty-string=",
"--allow-privileged=true",
"--insecure-bind-address=127.0.0.1",
}, },
}, },
} }
for _, rt := range tests { for _, rt := range tests {
actual := BuildArgumentListFromMap(rt.base, rt.overrides) actual := BuildArgumentListFromMap(rt.base, rt.overrides)
sort.Strings(actual)
sort.Strings(rt.expected)
if !reflect.DeepEqual(actual, rt.expected) { if !reflect.DeepEqual(actual, rt.expected) {
t.Errorf("failed BuildArgumentListFromMap:\nexpected:\n%v\nsaw:\n%v", rt.expected, actual) t.Errorf("failed BuildArgumentListFromMap:\nexpected:\n%v\nsaw:\n%v", rt.expected, actual)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment