Skip to content

Commit

Permalink
fix race condition causing netns tests to fail
Browse files Browse the repository at this point in the history
  • Loading branch information
alacuku committed Jun 25, 2021
1 parent 4bfbff1 commit 79cc3c6
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
20 changes: 14 additions & 6 deletions pkg/liqonet/netns/netns_suite_test.go
@@ -1,6 +1,7 @@
package netns

import (
"errors"
"testing"

"github.com/containernetworking/plugins/pkg/ns"
Expand Down Expand Up @@ -47,11 +48,18 @@ func setUpNetns(name string) {
defer newNs.Close()
}

func tearDownNetns(name string) {
err := netns.DeleteNamed(name)
if err != nil {
Expect(err).Should(Equal(unix.ENOENT))
return
func cleanUpEnv() error {
err := netns.DeleteNamed(netnsName)
if err != nil && !errors.Is(err, unix.ENOENT) {
return err
}
Expect(err).ShouldNot(HaveOccurred())
// Get the veth dev living in host network.
veth, err := netlink.LinkByName(hostVeth)
if err != nil && err.Error() != "Link not found" {
return err
}
if veth != nil {
return netlink.LinkDel(veth)
}
return nil
}
41 changes: 19 additions & 22 deletions pkg/liqonet/netns/netns_test.go
Expand Up @@ -11,7 +11,17 @@ import (
"github.com/liqotech/liqo/pkg/liqonet/errors"
)

var (
hostVeth = "originVeth"
gatewayVeth = "dstVeth"
)

var _ = Describe("Netns", func() {

JustAfterEach(func() {
Expect(cleanUpEnv()).NotTo(HaveOccurred())
})

Describe("creating new network namespace", func() {
Context("when network namespace does not exist and we want to create it", func() {
It("should return a new network namespace and nil", func() {
Expand All @@ -22,17 +32,14 @@ var _ = Describe("Netns", func() {
netnsNew, err := netns.GetFromName(netnsName)
Expect(err).ShouldNot(HaveOccurred())
Expect(netnsNew).ShouldNot(BeNil())
tearDownNetns(netnsName)
})
})

Context("when network namespace does exist and we want to create it", func() {
JustBeforeEach(func() {
setUpNetns(netnsName)
})
JustAfterEach(func() {
tearDownNetns(netnsName)
})

It("should remove the old one and create a new one, returning the new netns and nil", func() {
netnamespace, err := CreateNetns(netnsName)
defer netnamespace.Close()
Expand All @@ -55,9 +62,7 @@ var _ = Describe("Netns", func() {
JustBeforeEach(func() {
setUpNetns(netnsName)
})
JustAfterEach(func() {
tearDownNetns(netnsName)
})

It("should remove the existing namespace and return nil", func() {
err := DeleteNetns(netnsName)
Expect(err).ShouldNot(HaveOccurred())
Expand All @@ -80,18 +85,16 @@ var _ = Describe("Netns", func() {
JustBeforeEach(func() {
setUpNetns(netnsName)
})
JustAfterEach(func() {
tearDownNetns(netnsName)
})

It("should add veth pair and return nil", func() {
err := CreateVethPair("originVeth", "dstVeth", originNetns, newNetns, 1500)
err := CreateVethPair(hostVeth, gatewayVeth, originNetns, newNetns, 1500)
Expect(err).ShouldNot(HaveOccurred())
// Get originVeth
or, err := netlink.LinkByName("originVeth")
or, err := netlink.LinkByName(hostVeth)
Expect(err).ShouldNot(HaveOccurred())
// Get dstVeth
err = newNetns.Do(func(netNS ns.NetNS) error {
_, err = netlink.LinkByName("dstVeth")
_, err = netlink.LinkByName(gatewayVeth)
if err != nil {
return err
}
Expand All @@ -107,26 +110,20 @@ var _ = Describe("Netns", func() {
JustBeforeEach(func() {
setUpNetns(netnsName)
})
JustAfterEach(func() {
tearDownNetns(netnsName)
})

It("should return error", func() {
err := CreateVethPair("originVeth", "foo", originNetns, newNetns, 1500)
err := CreateVethPair(hostVeth, "foo", originNetns, newNetns, 1500)
Expect(err).Should(HaveOccurred())
})
})

Context("when dst network namespace does not exist", func() {
It("should return error", func() {
err := CreateVethPair("originVeth", "dstVeth", nil, nil, 1500)
err := CreateVethPair(hostVeth, gatewayVeth, nil, nil, 1500)
Expect(err).Should(Equal(&errors.WrongParameter{
Reason: errors.NotNil,
Parameter: "originNetns and dstNetns",
}))
// Remove dangling veth pair.
veth, err := netlink.LinkByName("originVeth")
Expect(err).ShouldNot(HaveOccurred())
Expect(netlink.LinkDel(veth)).ShouldNot(HaveOccurred())
})
})
})
Expand Down

0 comments on commit 79cc3c6

Please sign in to comment.