Skip to content

Commit

Permalink
test: add UTs for network hotplug
Browse files Browse the repository at this point in the history
add UTs for network hotplug related fuctions

Fixes kata-containers#113

Signed-off-by: Ruidong Cao <[email protected]>
  • Loading branch information
caoruidong committed Aug 16, 2018
1 parent 9334c05 commit 5d64559
Show file tree
Hide file tree
Showing 6 changed files with 316 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cli/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package main

import (
"flag"
"os"
"testing"

"github.com/kata-containers/agent/protocols/grpc"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli"
)

var testAddInterfaceFuncReturnNil = func(sid string, inf *grpc.Interface) (*grpc.Interface, error) {
return nil, nil
}

func TestNetworkCliAction(t *testing.T) {
assert := assert.New(t)

flagSet := flag.NewFlagSet("kata-network", flag.ContinueOnError)

// create a new fake context
ctx := createCLIContext(flagSet)

// get Action function
actionFunc, ok := addIfaceCommand.Action.(func(ctx *cli.Context) error)
assert.True(ok)

err := actionFunc(ctx)
assert.Error(err, "Missing container ID")
}

func TestNetwrokCliFunctionAddInterface(t *testing.T) {
assert := assert.New(t)

state := vc.State{
State: vc.StateRunning,
}

testingImpl.AddInterfaceFunc = testAddInterfaceFuncReturnNil

path, err := createTempContainerIDMapping(testContainerID, testSandboxID)
assert.NoError(err)
defer os.RemoveAll(path)

testingImpl.StatusContainerFunc = func(sandboxID, containerID string) (vc.ContainerStatus, error) {
return newSingleContainerStatus(testContainerID, state, map[string]string{}), nil
}

defer func() {
testingImpl.AddInterfaceFunc = nil
testingImpl.StatusContainerFunc = nil
}()

set := flag.NewFlagSet("", 0)

execCLICommandFunc(assert, addIfaceCommand, set, true)
}
32 changes: 32 additions & 0 deletions virtcontainers/hyperstart_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,35 @@ func TestHyperReseedAPI(t *testing.T) {
err := h.reseedRNG([]byte{})
assert.Nil(err)
}

func TestHyperUpdateInterface(t *testing.T) {
assert := assert.New(t)

h := &hyper{}
_, err := h.updateInterface(nil)
assert.Nil(err)
}

func TestHyperListInterfaces(t *testing.T) {
assert := assert.New(t)

h := &hyper{}
_, err := h.listInterfaces()
assert.Nil(err)
}

func TestHyperUpdateRoutes(t *testing.T) {
assert := assert.New(t)

h := &hyper{}
_, err := h.updateRoutes(nil)
assert.Nil(err)
}

func TestHyperListRoutes(t *testing.T) {
assert := assert.New(t)

h := &hyper{}
_, err := h.listRoutes()
assert.Nil(err)
}
8 changes: 8 additions & 0 deletions virtcontainers/kata_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,14 @@ func (p *gRPCProxy) UpdateRoutes(ctx context.Context, req *pb.UpdateRoutesReques
return nil, nil
}

func (p *gRPCProxy) ListInterfaces(ctx context.Context, req *pb.ListInterfacesRequest) (*pb.Interfaces, error) {
return nil, nil
}

func (p *gRPCProxy) ListRoutes(ctx context.Context, req *pb.ListRoutesRequest) (*pb.Routes, error) {
return nil, nil
}

func (p *gRPCProxy) OnlineCPUMem(ctx context.Context, req *pb.OnlineCPUMemRequest) (*gpb.Empty, error) {
return emptyResp, nil
}
Expand Down
54 changes: 54 additions & 0 deletions virtcontainers/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,60 @@ func TestVhostUserEndpointAttach(t *testing.T) {
}
}

func TestVhostUserEndpoint_HotAttach(t *testing.T) {
assert := assert.New(t)
v := &VhostUserEndpoint{
SocketPath: "/tmp/sock",
HardAddr: "mac-addr",
EndpointType: VhostUserEndpointType,
}

h := &mockHypervisor{}

err := v.HotAttach(h)
assert.Error(err)
}

func TestVhostUserEndpoint_HotDetach(t *testing.T) {
assert := assert.New(t)
v := &VhostUserEndpoint{
SocketPath: "/tmp/sock",
HardAddr: "mac-addr",
EndpointType: VhostUserEndpointType,
}

h := &mockHypervisor{}

err := v.HotDetach(h, true, "")
assert.Error(err)
}

func TestPhysicalEndpoint_HotAttach(t *testing.T) {
assert := assert.New(t)
v := &PhysicalEndpoint{
IfaceName: "eth0",
HardAddr: net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
}

h := &mockHypervisor{}

err := v.HotAttach(h)
assert.Error(err)
}

func TestPhysicalEndpoint_HotDetach(t *testing.T) {
assert := assert.New(t)
v := &PhysicalEndpoint{
IfaceName: "eth0",
HardAddr: net.HardwareAddr{0x02, 0x00, 0xca, 0xfe, 0x00, 0x04}.String(),
}

h := &mockHypervisor{}

err := v.HotDetach(h, true, "")
assert.Error(err)
}

func TestGetNetNsFromBindMount(t *testing.T) {
assert := assert.New(t)

Expand Down
32 changes: 32 additions & 0 deletions virtcontainers/noop_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,35 @@ func TestNoopAgentReseedRNG(t *testing.T) {
t.Fatal("reseedRNG failed")
}
}

func TestNoopAgentUpdateInterface(t *testing.T) {
n := &noopAgent{}
_, err := n.updateInterface(nil)
if err != nil {
t.Fatal("updateInterface failed")
}
}

func TestNoopAgentListInterfaces(t *testing.T) {
n := &noopAgent{}
_, err := n.listInterfaces()
if err != nil {
t.Fatal("listInterfaces failed")
}
}

func TestNoopAgentUpdateRoutes(t *testing.T) {
n := &noopAgent{}
_, err := n.updateRoutes(nil)
if err != nil {
t.Fatal("updateRoutes failed")
}
}

func TestNoopAgentListRoutes(t *testing.T) {
n := &noopAgent{}
_, err := n.listRoutes()
if err != nil {
t.Fatal("listRoutes failed")
}
}
131 changes: 131 additions & 0 deletions virtcontainers/pkg/vcmock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"syscall"
"testing"

"github.com/kata-containers/agent/protocols/grpc"
vc "github.com/kata-containers/runtime/virtcontainers"
"github.com/kata-containers/runtime/virtcontainers/factory"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -710,3 +711,133 @@ func TestVCMockSetVMFactory(t *testing.T) {
m.SetFactory(f)
assert.Equal(factoryTriggered, 1)
}

func TestVCMockAddInterface(t *testing.T) {
assert := assert.New(t)

m := &VCMock{}
config := &vc.SandboxConfig{}
assert.Nil(m.AddInterfaceFunc)

_, err := m.AddInterface(config.ID, nil)
assert.Error(err)
assert.True(IsMockError(err))

m.AddInterfaceFunc = func(sid string, inf *grpc.Interface) (*grpc.Interface, error) {
return nil, nil
}

_, err = m.AddInterface(config.ID, nil)
assert.NoError(err)

// reset
m.AddInterfaceFunc = nil

_, err = m.AddInterface(config.ID, nil)
assert.Error(err)
assert.True(IsMockError(err))
}

func TestVCMockRemoveInterface(t *testing.T) {
assert := assert.New(t)

m := &VCMock{}
config := &vc.SandboxConfig{}
assert.Nil(m.RemoveInterfaceFunc)

_, err := m.RemoveInterface(config.ID, nil)
assert.Error(err)
assert.True(IsMockError(err))

m.RemoveInterfaceFunc = func(sid string, inf *grpc.Interface) (*grpc.Interface, error) {
return nil, nil
}

_, err = m.RemoveInterface(config.ID, nil)
assert.NoError(err)

// reset
m.RemoveInterfaceFunc = nil

_, err = m.RemoveInterface(config.ID, nil)
assert.Error(err)
assert.True(IsMockError(err))
}

func TestVCMockListInterfaces(t *testing.T) {
assert := assert.New(t)

m := &VCMock{}
config := &vc.SandboxConfig{}
assert.Nil(m.ListInterfacesFunc)

_, err := m.ListInterfaces(config.ID)
assert.Error(err)
assert.True(IsMockError(err))

m.ListInterfacesFunc = func(sid string) ([]*grpc.Interface, error) {
return nil, nil
}

_, err = m.ListInterfaces(config.ID)
assert.NoError(err)

// reset
m.ListInterfacesFunc = nil

_, err = m.ListInterfaces(config.ID)
assert.Error(err)
assert.True(IsMockError(err))
}

func TestVCMockUpdateRoutes(t *testing.T) {
assert := assert.New(t)

m := &VCMock{}
config := &vc.SandboxConfig{}
assert.Nil(m.UpdateRoutesFunc)

_, err := m.UpdateRoutes(config.ID, nil)
assert.Error(err)
assert.True(IsMockError(err))

m.UpdateRoutesFunc = func(sid string, routes []*grpc.Route) ([]*grpc.Route, error) {
return nil, nil
}

_, err = m.UpdateRoutes(config.ID, nil)
assert.NoError(err)

// reset
m.UpdateRoutesFunc = nil

_, err = m.UpdateRoutes(config.ID, nil)
assert.Error(err)
assert.True(IsMockError(err))
}

func TestVCMockListRoutes(t *testing.T) {
assert := assert.New(t)

m := &VCMock{}
config := &vc.SandboxConfig{}
assert.Nil(m.ListRoutesFunc)

_, err := m.ListRoutes(config.ID)
assert.Error(err)
assert.True(IsMockError(err))

m.ListRoutesFunc = func(sid string) ([]*grpc.Route, error) {
return nil, nil
}

_, err = m.ListRoutes(config.ID)
assert.NoError(err)

// reset
m.ListRoutesFunc = nil

_, err = m.ListRoutes(config.ID)
assert.Error(err)
assert.True(IsMockError(err))
}

0 comments on commit 5d64559

Please sign in to comment.