Skip to content

Commit

Permalink
fixed test for ovs_netdev and ovs_system
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Lin committed Jul 12, 2018
1 parent 664ad3c commit 3f0b324
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 449 deletions.
14 changes: 4 additions & 10 deletions src/entity/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ const (
NetworkCollectionName string = "networks"
)

type PhyInterfaces struct {
type PhyInterface struct {
Name string `bson:"name" json:"name"`
PCIID string `bson:"pciID" json:"pciID"`
}

type Node struct {
Name string `bson:"name" json:"name"`
PhyInterfaces []PhyInterfaces `bson:"physicalInterface" json:"physicalInterface"`
Name string `bson:"name" json:"name"`
PhyInterfaces []PhyInterface `bson:"physicalInterfaces" json:"physicalInterfaces"`

// Fake fields for restful testing
FakeParameter string `json:"fakeParameter"`
ShouldFail bool `json:"shoulFail"`
ShouldFail bool `json:"shouldFail"`
}

type Network struct {
Expand All @@ -41,12 +41,6 @@ type Network struct {
BridgeName string `bson:"bridgeName" json:"bridgeName"`
Nodes []Node `bson:"nodes" json:"nodes"`
CreatedAt *time.Time `bson:"createdAt,omitempty" json:"createdAt,omitempty"`

// Clusterwise bool `bson:"clusterwise" json:"clusterwise"`
// NodeName string `bson:"nodeName,omitempty" json:"nodeName,omitempty"`
// OVS OVSNetwork `bson:"ovs,omitempty" json:"ovs"`
// OVSUserspace OVSUserspaceNetwork `bson:"ovsUserspace,omitempty" json:"ovsUserspace"`
// Fake FakeNetwork `json:"fake"` //FakeNetwork, for restful testing.
}

// GetCollection - get model mongo collection name.
Expand Down
80 changes: 36 additions & 44 deletions src/networkcontroller/network_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func New(serverAddress string) (*NetworkController, error) {
}, nil
}

func (nc *NetworkController) CreateOVSNetwork(datapathType string, bridgeName string, phyIface entity.PhyInterface, vlanTags []int32) error {
func (nc *NetworkController) CreateOVSNetwork(datapathType string, bridgeName string, phyIfaces []entity.PhyInterface, vlanTags []int32) error {
if _, err := nc.ClientCtl.CreateBridge(
nc.Context,
&pb.CreateBridgeRequest{
Expand All @@ -41,34 +41,36 @@ func (nc *NetworkController) CreateOVSNetwork(datapathType string, bridgeName st
return err
}

_, err := nc.ClientCtl.AddPort(
nc.Context,
&pb.AddPortRequest{
BridgeName: bridgeName,
IfaceName: phyIface.Name,
})
if err != nil {
return err
}

if len(vlanTags) > 0 {
_, err := nc.ClientCtl.SetPort(
for _, phyIface := range phyIfaces {
_, err := nc.ClientCtl.AddPort(
nc.Context,
&pb.SetPortRequest{
IfaceName: phyIface.Name,
Options: &pb.PortOptions{
VLANMode: "trunk",
Trunk: vlanTags,
},
&pb.AddPortRequest{
BridgeName: bridgeName,
IfaceName: phyIface.Name,
})
if err != nil {
return err
}

if len(vlanTags) > 0 {
_, err := nc.ClientCtl.SetPort(
nc.Context,
&pb.SetPortRequest{
IfaceName: phyIface.Name,
Options: &pb.PortOptions{
VLANMode: "trunk",
Trunk: vlanTags,
},
})
if err != nil {
return err
}
}
}
return nil
}

func (nc *NetworkController) CreateOVSDPDKNetwork(bridgeName string, phyIface entity.PhyInterface, vlanTags []int32) error {
func (nc *NetworkController) CreateOVSDPDKNetwork(bridgeName string, phyIfaces []entity.PhyInterface, vlanTags []int32) error {
if _, err := nc.ClientCtl.CreateBridge(
nc.Context,
&pb.CreateBridgeRequest{
Expand All @@ -78,7 +80,7 @@ func (nc *NetworkController) CreateOVSDPDKNetwork(bridgeName string, phyIface en
return err
}

if phyIface.IsDPDKPort == true {
for _, phyIface := range phyIfaces {
_, err := nc.ClientCtl.AddDPDKPort(
nc.Context,
&pb.AddPortRequest{
Expand All @@ -89,30 +91,20 @@ func (nc *NetworkController) CreateOVSDPDKNetwork(bridgeName string, phyIface en
if err != nil {
return err
}
} else {
_, err := nc.ClientCtl.AddPort(
nc.Context,
&pb.AddPortRequest{
BridgeName: bridgeName,
IfaceName: phyIface.Name,
})
if err != nil {
return err
}
}

if len(vlanTags) > 0 {
_, err := nc.ClientCtl.SetPort(
nc.Context,
&pb.SetPortRequest{
IfaceName: phyIface.Name,
Options: &pb.PortOptions{
VLANMode: "trunk",
Trunk: vlanTags,
},
})
if err != nil {
return err
if len(vlanTags) > 0 {
_, err := nc.ClientCtl.SetPort(
nc.Context,
&pb.SetPortRequest{
IfaceName: phyIface.Name,
Options: &pb.PortOptions{
VLANMode: "trunk",
Trunk: vlanTags,
},
})
if err != nil {
return err
}
}
}
return nil
Expand Down
89 changes: 47 additions & 42 deletions src/networkcontroller/network_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,88 +100,93 @@ func (suite *NetworkControllerTestSuite) TestNew() {
}

func (suite *NetworkControllerTestSuite) TestCreateNetwork() {
//Parameters
eth1 := entity.PhysicalPort{
Name: suite.ifName,
MTU: 1500,
VlanTags: []int32{2043, 2143, 2243},
}

tName := namesgenerator.GetRandomName(0)
network := entity.Network{
Name: tName,
OVS: entity.OVSNetwork{
BridgeName: tName,
PhysicalPorts: []entity.PhysicalPort{eth1},
Type: entity.OVSKernelspaceNetworkType,
Name: tName,
VLANTags: []int32{0, 2048, 4095},
BridgeName: tName,
Nodes: []entity.Node{
entity.Node{
Name: suite.nodeName,
PhyInterfaces: []entity.PhyInterface{
entity.PhyInterface{
Name: suite.ifName,
PCIID: "",
},
},
},
},
Type: "ovs",
NodeName: suite.nodeName,
}

nodeIP, err := suite.kubectl.GetNodeExternalIP(suite.nodeName)
suite.NoError(err)
nc, err := New(net.JoinHostPort(nodeIP, DEFAULT_CONTROLLER_PORT))
suite.NoError(err)
err = nc.CreateOVSNetwork(tName, network.OVSUserspace.PhysicalPorts)
err = nc.CreateOVSNetwork("system", tName, network.Nodes[0].PhyInterfaces, network.VLANTags)
suite.NoError(err)

//TODO we need support the list function to check the ovs is existed
defer exec.Command("ovs-vsctl", "del-br", tName).Run()
}

func (suite *NetworkControllerTestSuite) TestCreateOVSUserpsaceNetwork() {
eth1 := entity.PhysicalPort{
Name: suite.ifName,
MTU: 1500,
VlanTags: []int32{2043, 2143, 2243},
}

tName := namesgenerator.GetRandomName(0)
network := entity.Network{
Name: tName,
OVSUserspace: entity.OVSUserspaceNetwork{
BridgeName: tName,
PhysicalPorts: []entity.PhysicalPort{eth1},
Type: entity.OVSUserspaceNetworkType,
IsDPDKPort: false,
Name: tName,
VLANTags: []int32{0, 2048, 4095},
BridgeName: tName,
Nodes: []entity.Node{
entity.Node{
Name: suite.nodeName,
PhyInterfaces: []entity.PhyInterface{
entity.PhyInterface{
Name: suite.ifName,
PCIID: "",
},
},
},
},
Type: "netdev",
NodeName: suite.nodeName,
}

nodeIP, err := suite.kubectl.GetNodeExternalIP(suite.nodeName)
suite.NoError(err)
nc, err := New(net.JoinHostPort(nodeIP, DEFAULT_CONTROLLER_PORT))
suite.NoError(err)
err = nc.CreateOVSUserpsaceNetwork(tName, network.OVS.PhysicalPorts)
err = nc.CreateOVSNetwork("netdev", tName, network.Nodes[0].PhyInterfaces, network.VLANTags)
suite.NoError(err)

//TODO we need support the list function to check the ovs is existed
defer exec.Command("ovs-vsctl", "del-br", tName).Run()
}

func (suite *NetworkControllerTestSuite) TestDeleteNetwork() {
//Parameters
eth1 := entity.PhysicalPort{
Name: suite.ifName,
MTU: 1500,
VlanTags: []int32{2043, 2143, 2243},
}

tName := namesgenerator.GetRandomName(0)
network := entity.Network{
Name: tName,
OVS: entity.OVSNetwork{
BridgeName: tName,
PhysicalPorts: []entity.PhysicalPort{eth1},
Type: entity.OVSKernelspaceNetworkType,
Name: tName,
VLANTags: []int32{0, 2048, 4095},
BridgeName: tName,
Nodes: []entity.Node{
entity.Node{
Name: suite.nodeName,
PhyInterfaces: []entity.PhyInterface{
entity.PhyInterface{
Name: suite.ifName,
PCIID: "",
},
},
},
},
Type: "ovs",
NodeName: suite.nodeName,
}

nodeIP, err := suite.kubectl.GetNodeExternalIP(suite.nodeName)
suite.NoError(err)
nc, err := New(net.JoinHostPort(nodeIP, DEFAULT_CONTROLLER_PORT))
suite.NoError(err)
err = nc.CreateOVSNetwork(tName, network.OVS.PhysicalPorts)
err = nc.CreateOVSNetwork("system", tName, network.Nodes[0].PhyInterfaces, network.VLANTags)
suite.NoError(err)

err = nc.DeleteOVSNetwork(tName)
Expand All @@ -193,6 +198,6 @@ func (suite *NetworkControllerTestSuite) TestCreateNetworkWithInvalidAddress() {
suite.NoError(err)

tName := namesgenerator.GetRandomName(0)
err = nc.CreateOVSNetwork(tName, []entity.PhysicalPort{})
err = nc.CreateOVSNetwork("system", tName, []entity.PhyInterface{}, []int32{})
suite.Error(err)
}
14 changes: 9 additions & 5 deletions src/networkprovider/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ import (
"github.com/linkernetworks/vortex/src/serviceprovider"
)

type FakeNetworkProvider struct {
nodes []entity.Node
type fakeNetworkProvider struct {
networkName string
bridgeName string
vlanTags []int32
nodes []entity.Node
isDPDKPort bool
}

func (fnp FakeNetworkProvider) ValidateBeforeCreating(sp *serviceprovider.Container, net *entity.Network) error {
func (fnp fakeNetworkProvider) ValidateBeforeCreating(sp *serviceprovider.Container) error {
for _, node := range fnp.nodes {
if node.FakeParameter == "" {
return fmt.Errorf("Fail to validate but don't worry, I'm fake network")
Expand All @@ -20,7 +24,7 @@ func (fnp FakeNetworkProvider) ValidateBeforeCreating(sp *serviceprovider.Contai
return nil
}

func (fnp FakeNetworkProvider) CreateNetwork(sp *serviceprovider.Container, net *entity.Network) error {
func (fnp fakeNetworkProvider) CreateNetwork(sp *serviceprovider.Container) error {
for _, node := range fnp.nodes {
if node.ShouldFail {
return fmt.Errorf("Fail to validate but don't worry, I'm fake network")
Expand All @@ -29,7 +33,7 @@ func (fnp FakeNetworkProvider) CreateNetwork(sp *serviceprovider.Container, net
return nil
}

func (fnp FakeNetworkProvider) DeleteNetwork(sp *serviceprovider.Container, net *entity.Network) error {
func (fnp fakeNetworkProvider) DeleteNetwork(sp *serviceprovider.Container) error {
for _, node := range fnp.nodes {
if node.ShouldFail {
return fmt.Errorf("Fail to delete network but don't worry, I'm fake network")
Expand Down
Loading

0 comments on commit 3f0b324

Please sign in to comment.