ut: compare dns options without order

This commit is contained in:
Paco Xu 2022-09-20 11:45:33 +08:00
parent 468b2a2297
commit 3bbd025982
2 changed files with 21 additions and 21 deletions

View File

@ -280,26 +280,6 @@ func parseResolvConf(reader io.Reader) (nameservers []string, searches []string,
return nameservers, searches, options, utilerrors.NewAggregate(allErrors)
}
// appendOptions appends options to the given list, but does not add duplicates.
// append option will overwrite the previous one either in new line or in the same line.
func appendOptions(options []string, newOption ...string) []string {
var optionMap = make(map[string]string)
for _, option := range options {
optName := strings.Split(option, ":")[0]
optionMap[optName] = option
}
for _, option := range newOption {
optName := strings.Split(option, ":")[0]
optionMap[optName] = option
}
options = []string{}
for _, v := range optionMap {
options = append(options, v)
}
return options
}
func (c *Configurer) getHostDNSConfig() (*runtimeapi.DNSConfig, error) {
var hostDNS, hostSearch, hostOptions []string
// Get host DNS settings
@ -373,6 +353,26 @@ func mergeDNSOptions(existingDNSConfigOptions []string, dnsConfigOptions []v1.Po
return options
}
// appendOptions appends options to the given list, but does not add duplicates.
// append option will overwrite the previous one either in new line or in the same line.
func appendOptions(options []string, newOption ...string) []string {
var optionMap = make(map[string]string)
for _, option := range options {
optName := strings.Split(option, ":")[0]
optionMap[optName] = option
}
for _, option := range newOption {
optName := strings.Split(option, ":")[0]
optionMap[optName] = option
}
options = []string{}
for _, v := range optionMap {
options = append(options, v)
}
return options
}
// appendDNSConfig appends DNS servers, search paths and options given by
// PodDNSConfig to the existing DNS config. Duplicated entries will be merged.
// This assumes existingDNSConfig and dnsConfig are not nil.

View File

@ -105,7 +105,7 @@ func TestParseResolvConf(t *testing.T) {
require.NoError(t, err)
assert.EqualValues(t, tc.nameservers, ns, "test case [%d]: name servers", i)
assert.EqualValues(t, tc.searches, srch, "test case [%d] searches", i)
assert.EqualValues(t, tc.options, opts, "test case [%d] options", i)
assert.EqualValues(t, sets.NewString(tc.options...), sets.NewString(opts...), "test case [%d] options", i)
} else {
require.Error(t, err, "tc.searches %v", tc.searches)
}