Skip to content

Commit

Permalink
Add test for exec command and use the objectID for vethname
Browse files Browse the repository at this point in the history
  • Loading branch information
hwchiu committed Jun 26, 2018
1 parent dd01cbf commit 31dbc6f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
23 changes: 19 additions & 4 deletions src/networkcontroller/network_controller_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package networkcontroller

import (
"bytes"
"fmt"
"github.com/linkernetworks/vortex/src/entity"
"github.com/linkernetworks/vortex/src/kubernetes"
Expand All @@ -22,6 +23,14 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

func execute(suite *suite.Suite, cmd *exec.Cmd) {
w := bytes.NewBuffer(nil)
cmd.Stderr = w
err := cmd.Run()
suite.NoError(err)
fmt.Printf("Stderr: %s\n", string(w.Bytes()))
}

type NetworkControllerTestSuite struct {
suite.Suite
kubectl *kubernetes.KubeCtl
Expand Down Expand Up @@ -55,11 +64,17 @@ func (suite *NetworkControllerTestSuite) SetupSuite() {
suite.NoError(err)

//There's a length limit of link name
suite.ifName = namesgenerator.GetRandomName(0)[0:8]
pName := namesgenerator.GetRandomName(0)[0:8]
suite.ifName = bson.NewObjectId().Hex()[12:24]
pName := bson.NewObjectId().Hex()[12:24]
//Create a veth for testing
err = exec.Command("ip", "link", "add", suite.ifName, "type", "veth", "peer", "name", pName).Run()
suite.NoError(err)
fmt.Println("ip", "link", "add", suite.ifName, "type", "veth", "peer", "name", pName)
cmd := exec.Command("ip", "link", "add", suite.ifName, "type", "veth", "peer", "name", pName)
execute(&suite.Suite, cmd)
}

func (suite *NetworkControllerTestSuite) TearDownSuite() {
cmd := exec.Command("ip", "link", "del", suite.ifName)
execute(&suite.Suite, cmd)
}

func TestNetworkControllerSuite(t *testing.T) {
Expand Down
22 changes: 16 additions & 6 deletions src/server/handler_network_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"bytes"
"encoding/json"
"fmt"
"math/rand"
Expand Down Expand Up @@ -35,6 +36,14 @@ func init() {
rand.Seed(time.Now().UnixNano())
}

func execute(suite *suite.Suite, cmd *exec.Cmd) {
w := bytes.NewBuffer(nil)
cmd.Stderr = w
err := cmd.Run()
suite.NoError(err)
fmt.Printf("Stderr: %s\n", string(w.Bytes()))
}

type NetworkTestSuite struct {
suite.Suite
wc *restful.Container
Expand Down Expand Up @@ -79,16 +88,17 @@ func (suite *NetworkTestSuite) SetupSuite() {
suite.NoError(err)

//There's a length limit of link name
suite.ifName = namesgenerator.GetRandomName(0)[0:8]
pName := namesgenerator.GetRandomName(0)[0:8]
suite.ifName = bson.NewObjectId().Hex()[12:24]
pName := bson.NewObjectId().Hex()[12:24]
//Create a veth for testing
err = exec.Command("ip", "link", "add", suite.ifName, "type", "veth", "peer", "name", pName).Run()
suite.NoError(err)
fmt.Println("ip", "link", "add", suite.ifName, "type", "veth", "peer", "name", pName)
cmd := exec.Command("ip", "link", "add", suite.ifName, "type", "veth", "peer", "name", pName)
execute(&suite.Suite, cmd)
}

func (suite *NetworkTestSuite) TearDownSuite() {
err := exec.Command("ip", "link", "del", suite.ifName).Run()
suite.NoError(err)
cmd := exec.Command("ip", "link", "del", suite.ifName)
execute(&suite.Suite, cmd)
}

func TestNetworkSuite(t *testing.T) {
Expand Down

0 comments on commit 31dbc6f

Please sign in to comment.