Selaa lähdekoodia

Merge pull request #2212 from thaJeztah/fix_duplicate_ndots

Fix duplicate ndots:0, and improve validation
Flavio Crisciani 7 vuotta sitten
vanhempi
commit
abc4c5c5d8
2 muutettua tiedostoa jossa 38 lisäystä ja 8 poistoa
  1. 4 2
      libnetwork/sandbox_dns_unix.go
  2. 34 6
      libnetwork/service_common_test.go

+ 4 - 2
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)
 					}
 				}
 			}

+ 34 - 6
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")
 }