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 12, 2018
1 parent f7f1757 commit c4c3828
Show file tree
Hide file tree
Showing 3 changed files with 194 additions and 0 deletions.
55 changes: 55 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 @@ -2458,3 +2459,57 @@ func TestPauseResumeContainer(t *testing.T) {
err = ResumeContainer(s.ID(), contID)
assert.NoError(err)
}

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

cleanUp()

assert := assert.New(t)

inf := &grpc.Interface{
Device: "tap1",
Name: "eth0",
Mtu: 1500,
HwAddr: "00:e0:4c:97:ad:66",
}
ip := grpc.IPAddress{
Family: 0,
Address: "192.168.0.101",
Mask: "24",
}
inf.IPAddresses = append(inf.IPAddresses, &ip)

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

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

contID := "100"
config := newTestSandboxConfigNoop()

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

contConfig := newTestContainerConfigNoop(contID)
_, c, err := CreateContainer(s.ID(), contConfig)
assert.NoError(err)
assert.NotNil(c)

contDir := filepath.Join(sandboxDir, contID)
_, err = os.Stat(contDir)
assert.NoError(err)

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

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

_, err = ListInterfaces(s.ID())
assert.NoError(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
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 c4c3828

Please sign in to comment.