Skip to content

Commit ce1feff

Browse files
Merge pull request #632 from austinvazquez/cleanup-cni-integ-test
fix: cleanup of network devices for CNI tests
2 parents 6b3e5af + 9176310 commit ce1feff

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

internal/network_test_utils.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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,

runtime/cni_integ_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ func TestCNISupport_Isolated(t *testing.T) {
6464
webpages[vmID] = fmt.Sprintf("Hello, my virtual machine %s\n", vmID)
6565
}
6666

67-
localServices, err := internal.NewLocalNetworkServices(webpages)
67+
localServices, err := internal.NewLocalNetworkServices(t, webpages)
6868
require.NoError(t, err, "failed to create local network test services")
6969

7070
cniNetworkName := "fcnet-test"
@@ -170,7 +170,7 @@ func TestAutomaticCNISupport_Isolated(t *testing.T) {
170170
webpages[taskID] = fmt.Sprintf("Hello, my task %s\n", taskID)
171171
}
172172

173-
localServices, err := internal.NewLocalNetworkServices(webpages)
173+
localServices, err := internal.NewLocalNetworkServices(t, webpages)
174174
require.NoError(t, err, "failed to create local network test services")
175175

176176
cniNetworkName := "fcnet"

0 commit comments

Comments
 (0)