Browse Source

tear down device at end of test

Song Gao 6 years ago
parent
commit
240a3d7b51
2 changed files with 20 additions and 0 deletions
  1. 10 0
      ipv4_darwin_test.go
  2. 10 0
      ipv4_linux_test.go

+ 10 - 0
ipv4_darwin_test.go

@@ -21,6 +21,15 @@ func setupIfce(t *testing.T, self net.IP, remote net.IP, dev string) {
 	}
 }
 
+func teardownIfce(t *testing.T, ifce *Interface) {
+	if err := ifce.Close(); err != nil {
+		t.Fatal(err)
+	}
+	if err := exec.Command("ifconfig", ifce.Name(), "down").Run(); err != nil {
+		t.Fatal(err)
+	}
+}
+
 func TestP2PTUN(t *testing.T) {
 	var (
 		self   = net.IPv4(10, 0, 42, 1)
@@ -31,6 +40,7 @@ func TestP2PTUN(t *testing.T) {
 	if err != nil {
 		t.Fatalf("creating TUN error: %v\n", err)
 	}
+	defer teardownIfce(t, ifce)
 
 	dataCh := make(chan []byte)
 	errCh := make(chan error)

+ 10 - 0
ipv4_linux_test.go

@@ -24,6 +24,15 @@ func setupIfce(t *testing.T, ipNet net.IPNet, dev string) {
 	}
 }
 
+func teardownIfce(t *testing.T, ifce *Interface) {
+	if err := ifce.Close(); err != nil {
+		t.Fatal(err)
+	}
+	if err := exec.Command("ip", "link", "set", ifce.Name(), "down").Run(); err != nil {
+		t.Fatal(err)
+	}
+}
+
 func TestBroadcastTAP(t *testing.T) {
 	var (
 		self = net.IPv4(10, 0, 42, 1)
@@ -35,6 +44,7 @@ func TestBroadcastTAP(t *testing.T) {
 	if err != nil {
 		t.Fatalf("creating TAP error: %v\n", err)
 	}
+	defer teardownIfce(t, ifce)
 
 	setupIfce(t, net.IPNet{IP: self, Mask: mask}, ifce.Name())
 	startBroadcast(t, brd)