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 72df219 commit 8eed692
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 0 deletions.
88 changes: 88 additions & 0 deletions cli/network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Copyright (c) 2018 Huawei Corporation.
//
// SPDX-License-Identifier: Apache-2.0
//

package main

import (
"flag"
"io/ioutil"
"os"
"testing"

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

var (
testAddInterfaceFuncReturnNil = func(sandboxID string, inf *grpc.Interface) (*grpc.Interface, error) {
return nil, nil
}
testRemoveInterfaceFuncReturnNil = func(sandboxID string, inf *grpc.Interface) (*grpc.Interface, error) {
return nil, nil
}
testListInterfacesFuncReturnNil = func(sandboxID string) ([]*grpc.Interface, error) {
return nil, nil
}
testUpdateRoutsFuncReturnNil = func(sandboxID string, routes []*grpc.Route) ([]*grpc.Route, error) {
return nil, nil
}
testListRoutesFuncReturnNil = func(sandboxID string) ([]*grpc.Route, error) {
return nil, nil
}
)

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

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

testingImpl.AddInterfaceFunc = testAddInterfaceFuncReturnNil
testingImpl.RemoveInterfaceFunc = testRemoveInterfaceFuncReturnNil
testingImpl.ListInterfacesFunc = testListInterfacesFuncReturnNil
testingImpl.UpdateRoutesFunc = testUpdateRoutsFuncReturnNil
testingImpl.ListRoutesFunc = testListRoutesFuncReturnNil

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.RemoveInterfaceFunc = nil
testingImpl.ListInterfacesFunc = nil
testingImpl.UpdateRoutesFunc = nil
testingImpl.ListRoutesFunc = nil
testingImpl.StatusContainerFunc = nil
}()

set := flag.NewFlagSet("", 0)
execCLICommandFunc(assert, addIfaceCommand, set, true)

set.Parse([]string{testContainerID})
execCLICommandFunc(assert, listIfacesCommand, set, false)
execCLICommandFunc(assert, listRoutesCommand, set, false)

f, err := ioutil.TempFile("", "interface")
defer os.Remove(f.Name())
assert.NoError(err)
assert.NotNil(f)
f.WriteString("{}")

set.Parse([]string{testContainerID, f.Name()})
execCLICommandFunc(assert, addIfaceCommand, set, false)
execCLICommandFunc(assert, delIfaceCommand, set, false)

f.Seek(0, 0)
f.WriteString("[{}]")
f.Close()
execCLICommandFunc(assert, updateRoutesCommand, set, false)
}
39 changes: 39 additions & 0 deletions virtcontainers/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"syscall"
"testing"

"github.com/kata-containers/agent/protocols/grpc"
"github.com/kata-containers/runtime/virtcontainers/pkg/mock"
specs "github.com/opencontainers/runtime-spec/specs-go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -2349,3 +2350,41 @@ func TestPauseResumeContainer(t *testing.T) {
err = ResumeContainer(s.ID(), contID)
assert.NoError(err)
}

func TestNetworkOperation(t *testing.T) {
if os.Geteuid() != 0 {
t.Skip(testDisabledAsNonRoot)
}

cleanUp()

assert := assert.New(t)
inf := &grpc.Interface{}

_, err := AddInterface("", inf)
assert.Error(err)

_, err = AddInterface("abc", inf)
assert.Error(err)

config := newTestSandboxConfigNoop()

s, _, err := createAndStartSandbox(config)
assert.NoError(err)
assert.NotNil(s)

_, err = AddInterface(s.ID(), inf)
assert.Error(err)

_, err = RemoveInterface(s.ID(), inf)
assert.NoError(err)

_, err = ListInterfaces(s.ID())
assert.NoError(err)

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

_, err = ListRoutes(s.ID())
assert.NoError(err)
}
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")
}
}
Loading

0 comments on commit 8eed692

Please sign in to comment.