@@ -20,6 +20,7 @@ import (
20
20
"net"
21
21
"net/http"
22
22
"os/exec"
23
+ "testing"
23
24
24
25
"github.com/miekg/dns"
25
26
"github.com/pkg/errors"
@@ -55,7 +56,7 @@ type LocalNetworkServices interface {
55
56
// "domain" to that ip. The HTTP server will serve a pages at
56
57
// paths defined in the keys of "webpages", with the content set
57
58
// 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 ) {
59
60
testDevName := "testdev0"
60
61
ipAddr := "10.0.0.1"
61
62
ipCidr := fmt .Sprintf ("%s/32" , ipAddr )
@@ -64,16 +65,28 @@ func NewLocalNetworkServices(webpages map[string]string) (LocalNetworkServices,
64
65
if err != nil {
65
66
return nil , errors .Wrapf (err , `failed to add tun dev, "ip" command output: %s` , string (output ))
66
67
}
68
+ cleanupTap := func () {
69
+ exec .Command ("ip" , "tuntap" , "del" , testDevName , "mode" , "tun" ).Run ()
70
+ }
71
+ t .Cleanup (cleanupTap )
67
72
68
73
output , err = exec .Command ("ip" , "addr" , "add" , ipCidr , "dev" , testDevName ).CombinedOutput ()
69
74
if err != nil {
70
75
return nil , errors .Wrapf (err , `failed to assign ip to tun dev, "ip" command output: %s` , string (output ))
71
76
}
77
+ cleanupAddress := func () {
78
+ exec .Command ("ip" , "addr" , "del" , ipCidr , "dev" , testDevName ).Run ()
79
+ }
80
+ t .Cleanup (cleanupAddress )
72
81
73
82
output , err = exec .Command ("ip" , "link" , "set" , "dev" , testDevName , "up" ).CombinedOutput ()
74
83
if err != nil {
75
84
return nil , errors .Wrapf (err , `failed to set tun dev up, "ip" command output: %s` , string (output ))
76
85
}
86
+ cleanupLink := func () {
87
+ exec .Command ("ip" , "link" , "del" , "dev" , testDevName ).Run ()
88
+ }
89
+ t .Cleanup (cleanupLink )
77
90
78
91
return & localNetworkServices {
79
92
domain : domainName ,
0 commit comments