Browse code

Removal of the regex to replace ips

Signed-off-by: Mingzhen Feng <fmzhen@zju.edu.cn>

Mingzhen Feng authored on 2015/04/16 16:09:36
Showing 2 changed files
... ...
@@ -564,6 +564,41 @@ func (s *DockerDaemonSuite) TestDaemonBridgeIP(c *check.C) {
564 564
 	deleteInterface(c, defaultNetworkBridge)
565 565
 }
566 566
 
567
+func (s *DockerDaemonSuite) TestDaemonRestartWithBridgeIPChange(c *check.C) {
568
+	if err := s.d.Start(); err != nil {
569
+		c.Fatalf("Could not start daemon: %v", err)
570
+	}
571
+	defer s.d.Restart()
572
+	if err := s.d.Stop(); err != nil {
573
+		c.Fatalf("Could not stop daemon: %v", err)
574
+	}
575
+
576
+	// now we will change the docker0's IP and then try starting the daemon
577
+	bridgeIP := "192.169.100.1/24"
578
+	_, bridgeIPNet, _ := net.ParseCIDR(bridgeIP)
579
+
580
+	ipCmd := exec.Command("ifconfig", "docker0", bridgeIP)
581
+	stdout, stderr, _, err := runCommandWithStdoutStderr(ipCmd)
582
+	if err != nil {
583
+		c.Fatalf("failed to change docker0's IP association: %v, stdout: %q, stderr: %q", err, stdout, stderr)
584
+	}
585
+
586
+	if err := s.d.Start("--bip", bridgeIP); err != nil {
587
+		c.Fatalf("Could not start daemon: %v", err)
588
+	}
589
+
590
+	//check if the iptables contains new bridgeIP MASQUERADE rule
591
+	ipTablesSearchString := bridgeIPNet.String()
592
+	ipTablesCmd := exec.Command("iptables", "-t", "nat", "-nvL")
593
+	out, _, err := runCommandWithOutput(ipTablesCmd)
594
+	if err != nil {
595
+		c.Fatalf("Could not run iptables -nvL: %s, %v", out, err)
596
+	}
597
+	if !strings.Contains(out, ipTablesSearchString) {
598
+		c.Fatalf("iptables output should have contained new MASQUERADE rule with IP %q, but was %q", ipTablesSearchString, out)
599
+	}
600
+}
601
+
567 602
 func (s *DockerDaemonSuite) TestDaemonBridgeFixedCidr(c *check.C) {
568 603
 	d := s.d
569 604
 
... ...
@@ -5,7 +5,6 @@ import (
5 5
 	"fmt"
6 6
 	"net"
7 7
 	"os/exec"
8
-	"regexp"
9 8
 	"strconv"
10 9
 	"strings"
11 10
 	"sync"
... ...
@@ -267,14 +266,7 @@ func Exists(table Table, chain string, rule ...string) bool {
267 267
 	ruleString := strings.Join(rule, " ")
268 268
 	existingRules, _ := exec.Command(iptablesPath, "-t", string(table), "-S", chain).Output()
269 269
 
270
-	// regex to replace ips in rule
271
-	// because MASQUERADE rule will not be exactly what was passed
272
-	re := regexp.MustCompile(`[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/[0-9]{1,2}`)
273
-
274
-	return strings.Contains(
275
-		re.ReplaceAllString(string(existingRules), "?"),
276
-		re.ReplaceAllString(ruleString, "?"),
277
-	)
270
+	return strings.Contains(string(existingRules), ruleString)
278 271
 }
279 272
 
280 273
 // Call 'iptables' system command, passing supplied arguments