diff --git a/libnetwork/sandbox_dns_unix.go b/libnetwork/sandbox_dns_unix.go index c4da9272e5..3641f60f7c 100644 --- a/libnetwork/sandbox_dns_unix.go +++ b/libnetwork/sandbox_dns_unix.go @@ -369,11 +369,13 @@ dnsOpt: return fmt.Errorf("invalid ndots option %v", option) } if num, err := strconv.Atoi(parts[1]); err != nil { - return fmt.Errorf("invalid number for ndots option %v", option) - } else if num > 0 { + return fmt.Errorf("invalid number for ndots option: %v", parts[1]) + } else if num >= 0 { // if the user sets ndots, use the user setting sb.ndotsSet = true break dnsOpt + } else { + return fmt.Errorf("invalid number for ndots option: %v", num) } } } diff --git a/libnetwork/service_common_test.go b/libnetwork/service_common_test.go index eb2171dd7a..6ff14983c3 100644 --- a/libnetwork/service_common_test.go +++ b/libnetwork/service_common_test.go @@ -62,8 +62,8 @@ func TestDNSOptions(t *testing.T) { currRC, err := resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath) require.NoError(t, err) dnsOptionsList := resolvconf.GetOptions(currRC.Content) - assert.Equal(t, 1, len(dnsOptionsList), "There should be only 1 option instead:", dnsOptionsList) - assert.Equal(t, "ndots:0", dnsOptionsList[0], "The option must be ndots:0 instead:", dnsOptionsList[0]) + assert.Equal(t, 1, len(dnsOptionsList)) + assert.Equal(t, "ndots:0", dnsOptionsList[0]) sb.(*sandbox).config.dnsOptionsList = []string{"ndots:5"} err = sb.(*sandbox).setupDNS() @@ -71,14 +71,42 @@ func TestDNSOptions(t *testing.T) { currRC, err = resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath) require.NoError(t, err) dnsOptionsList = resolvconf.GetOptions(currRC.Content) - assert.Equal(t, 1, len(dnsOptionsList), "There should be only 1 option instead:", dnsOptionsList) - assert.Equal(t, "ndots:5", dnsOptionsList[0], "The option must be ndots:5 instead:", dnsOptionsList[0]) + assert.Equal(t, 1, len(dnsOptionsList)) + assert.Equal(t, "ndots:5", dnsOptionsList[0]) err = sb.(*sandbox).rebuildDNS() require.NoError(t, err) currRC, err = resolvconf.GetSpecific(sb.(*sandbox).config.resolvConfPath) require.NoError(t, err) dnsOptionsList = resolvconf.GetOptions(currRC.Content) - assert.Equal(t, 1, len(dnsOptionsList), "There should be only 1 option instead:", dnsOptionsList) - assert.Equal(t, "ndots:5", dnsOptionsList[0], "The option must be ndots:5 instead:", dnsOptionsList[0]) + assert.Equal(t, 1, len(dnsOptionsList)) + assert.Equal(t, "ndots:5", dnsOptionsList[0]) + + sb2, err := c.(*controller).NewSandbox("cnt2", nil) + require.NoError(t, err) + defer sb2.Delete() + sb2.(*sandbox).startResolver(false) + + sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:0"} + err = sb2.(*sandbox).setupDNS() + require.NoError(t, err) + err = sb2.(*sandbox).rebuildDNS() + require.NoError(t, err) + currRC, err = resolvconf.GetSpecific(sb2.(*sandbox).config.resolvConfPath) + require.NoError(t, err) + dnsOptionsList = resolvconf.GetOptions(currRC.Content) + assert.Equal(t, 1, len(dnsOptionsList)) + assert.Equal(t, "ndots:0", dnsOptionsList[0]) + + sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:foobar"} + err = sb2.(*sandbox).setupDNS() + require.NoError(t, err) + err = sb2.(*sandbox).rebuildDNS() + require.EqualError(t, err, "invalid number for ndots option: foobar") + + sb2.(*sandbox).config.dnsOptionsList = []string{"ndots:-1"} + err = sb2.(*sandbox).setupDNS() + require.NoError(t, err) + err = sb2.(*sandbox).rebuildDNS() + require.EqualError(t, err, "invalid number for ndots option: -1") }