@@ -20,6 +20,7 @@ import (
2020 "net"
2121 "net/http"
2222 "os/exec"
23+ "testing"
2324
2425 "github.com/miekg/dns"
2526 "github.com/pkg/errors"
@@ -55,7 +56,7 @@ type LocalNetworkServices interface {
5556// "domain" to that ip. The HTTP server will serve a pages at
5657// paths defined in the keys of "webpages", with the content set
5758// in the values of that map.
58- func NewLocalNetworkServices (webpages map [string ]string ) (LocalNetworkServices , error ) {
59+ func NewLocalNetworkServices (t * testing. T , webpages map [string ]string ) (LocalNetworkServices , error ) {
5960 testDevName := "testdev0"
6061 ipAddr := "10.0.0.1"
6162 ipCidr := fmt .Sprintf ("%s/32" , ipAddr )
@@ -64,16 +65,28 @@ func NewLocalNetworkServices(webpages map[string]string) (LocalNetworkServices,
6465 if err != nil {
6566 return nil , errors .Wrapf (err , `failed to add tun dev, "ip" command output: %s` , string (output ))
6667 }
68+ cleanupTap := func () {
69+ exec .Command ("ip" , "tuntap" , "del" , testDevName , "mode" , "tun" ).Run ()
70+ }
71+ t .Cleanup (cleanupTap )
6772
6873 output , err = exec .Command ("ip" , "addr" , "add" , ipCidr , "dev" , testDevName ).CombinedOutput ()
6974 if err != nil {
7075 return nil , errors .Wrapf (err , `failed to assign ip to tun dev, "ip" command output: %s` , string (output ))
7176 }
77+ cleanupAddress := func () {
78+ exec .Command ("ip" , "addr" , "del" , ipCidr , "dev" , testDevName ).Run ()
79+ }
80+ t .Cleanup (cleanupAddress )
7281
7382 output , err = exec .Command ("ip" , "link" , "set" , "dev" , testDevName , "up" ).CombinedOutput ()
7483 if err != nil {
7584 return nil , errors .Wrapf (err , `failed to set tun dev up, "ip" command output: %s` , string (output ))
7685 }
86+ cleanupLink := func () {
87+ exec .Command ("ip" , "link" , "del" , "dev" , testDevName ).Run ()
88+ }
89+ t .Cleanup (cleanupLink )
7790
7891 return & localNetworkServices {
7992 domain : domainName ,
0 commit comments