Skip to content

Commit

Permalink
add network tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CMGS committed Nov 16, 2018
1 parent a5f66f3 commit f7eaf39
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,4 @@ cloc:
cloc --exclude-dir=vendor,3rdmocks,mocks --not-match-f=test .

unit-test:
go test --race -cover ./utils/... ./types/... ./store/etcdv3/... ./scheduler/complex/... ./source/common/...
go test --race -cover ./utils/... ./types/... ./store/etcdv3/... ./scheduler/complex/... ./source/common/... ./network/sdn/...
21 changes: 11 additions & 10 deletions network/sdn/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ import (
log "github.com/sirupsen/logrus"
)

type titanium struct{}
// Titanium for calico sdn
type Titanium struct{}

// connect to network with ipv4 address
func (t *titanium) ConnectToNetwork(ctx context.Context, containerID, networkID, ipv4 string) error {
// ConnectToNetwork to network with ipv4 address
func (t *Titanium) ConnectToNetwork(ctx context.Context, containerID, networkID, ipv4 string) error {
if len(containerID) != 64 {
return types.ErrBadContainerID
}
Expand Down Expand Up @@ -46,8 +47,8 @@ func (t *titanium) ConnectToNetwork(ctx context.Context, containerID, networkID,
return engine.NetworkConnect(ctx, networkID, containerID, config)
}

// disconnect from network
func (t *titanium) DisconnectFromNetwork(ctx context.Context, containerID, networkID string) error {
// DisconnectFromNetwork from network
func (t *Titanium) DisconnectFromNetwork(ctx context.Context, containerID, networkID string) error {
if len(containerID) != 64 {
return types.ErrBadContainerID
}
Expand All @@ -62,8 +63,8 @@ func (t *titanium) DisconnectFromNetwork(ctx context.Context, containerID, netwo
return engine.NetworkDisconnect(ctx, networkID, containerID, false)
}

// list networks from context
func (t *titanium) ListNetworks(ctx context.Context, driver string) ([]*types.Network, error) {
// ListNetworks networks from context
func (t *Titanium) ListNetworks(ctx context.Context, driver string) ([]*types.Network, error) {
networks := []*types.Network{}
engine, ok := utils.GetDockerEngineFromContext(ctx)
if !ok {
Expand All @@ -90,7 +91,7 @@ func (t *titanium) ListNetworks(ctx context.Context, driver string) ([]*types.Ne
return networks, nil
}

//New a titanium obj
func New() *titanium {
return &titanium{}
// New a titanium obj
func New() *Titanium {
return &Titanium{}
}
98 changes: 98 additions & 0 deletions network/sdn/plugin_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package sdn

import (
"context"
"errors"
"testing"

enginetypes "github.com/docker/docker/api/types"
enginenetwork "github.com/docker/docker/api/types/network"
enginemocks "github.com/projecteru2/core/3rdmocks"
"github.com/projecteru2/core/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)

func TestConnectToNetwork(t *testing.T) {
s := New()
containerID := "1234567812345678123456781234567812345678123456781234567812345678"
mockEngine := &enginemocks.APIClient{}
// container id not 64
err := s.ConnectToNetwork(context.Background(), "", "", "")
assert.Error(t, err)
// no engine
err = s.ConnectToNetwork(context.Background(), containerID, "", "")
assert.Error(t, err)

ctx := utils.ContextWithDockerEngine(context.Background(), mockEngine)
mockEngine.On("NetworkConnect",
mock.AnythingOfType("*context.valueCtx"), mock.Anything, mock.Anything, mock.Anything,
).Return(nil)
// no ipv4
err = s.ConnectToNetwork(ctx, containerID, "", "")
assert.NoError(t, err)
// invaild ipv4
err = s.ConnectToNetwork(ctx, containerID, "", "x.0.1.8")
assert.Error(t, err)
// vaild ipv4
err = s.ConnectToNetwork(ctx, containerID, "", "127.0.0.1")
assert.NoError(t, err)
}

func TestDisconnectFromNetwork(t *testing.T) {
s := New()
containerID := "1234567812345678123456781234567812345678123456781234567812345678"
mockEngine := &enginemocks.APIClient{}
// container id not 64
err := s.ConnectToNetwork(context.Background(), "", "", "")
assert.Error(t, err)
// no engine
err = s.ConnectToNetwork(context.Background(), containerID, "", "")
assert.Error(t, err)
ctx := utils.ContextWithDockerEngine(context.Background(), mockEngine)
mockEngine.On("NetworkDisconnect",
mock.AnythingOfType("*context.valueCtx"), mock.Anything, mock.Anything, mock.Anything,
).Return(nil)
err = s.DisconnectFromNetwork(ctx, containerID, "")
assert.NoError(t, err)
}

func TestListNetworksFail(t *testing.T) {
s := New()
mockEngine := &enginemocks.APIClient{}
// no engine
_, err := s.ListNetworks(context.Background(), "")
assert.Error(t, err)
ctx := utils.ContextWithDockerEngine(context.Background(), mockEngine)
mockEngine.On("NetworkList",
mock.AnythingOfType("*context.valueCtx"), mock.Anything,
).Return(nil, errors.New("test"))
// List failed
_, err = s.ListNetworks(ctx, "")
assert.Error(t, err)
}

func TestListNetworks(t *testing.T) {
s := New()
mockEngine := &enginemocks.APIClient{}
ctx := utils.ContextWithDockerEngine(context.Background(), mockEngine)
// List
networkName := "test"
subnet := "10.2.0.0/16"
result := []enginetypes.NetworkResource{
enginetypes.NetworkResource{
Name: networkName,
IPAM: enginenetwork.IPAM{
Config: []enginenetwork.IPAMConfig{
enginenetwork.IPAMConfig{Subnet: subnet},
}}},
}
mockEngine.On("NetworkList",
mock.AnythingOfType("*context.valueCtx"), mock.Anything,
).Return(result, nil)
ns, err := s.ListNetworks(ctx, "")
assert.NoError(t, err)
assert.Equal(t, len(ns), 1)
assert.Equal(t, ns[0].Name, networkName)
assert.Equal(t, ns[0].Subnets[0], subnet)
}
4 changes: 2 additions & 2 deletions store/etcdv3/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
enginetypes "github.com/docker/docker/api/types"
engineapi "github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
enginemock "github.com/projecteru2/core/3rdmocks"
enginemocks "github.com/projecteru2/core/3rdmocks"
"github.com/projecteru2/core/types"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/mock"
Expand All @@ -33,7 +33,7 @@ func dumpFromString(ca, cert, key *os.File, caStr, certStr, keyStr string) error
}

func makeMockClient() (engineapi.APIClient, error) {
engine := &enginemock.APIClient{}
engine := &enginemocks.APIClient{}
engine.On("Info", mock.AnythingOfType("*context.emptyCtx")).Return(
enginetypes.Info{NCPU: 1, MemTotal: types.GByte + 100}, nil)
return engine, nil
Expand Down
4 changes: 2 additions & 2 deletions types/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"github.com/stretchr/testify/assert"

enginetypes "github.com/docker/docker/api/types"
"github.com/projecteru2/core/3rdmocks"
enginemocks "github.com/projecteru2/core/3rdmocks"
)

func TestNode(t *testing.T) {
mockEngine := &mocks.APIClient{}
mockEngine := &enginemocks.APIClient{}
r := enginetypes.Info{ID: "test"}
mockEngine.On("Info", mock.AnythingOfType("*context.timerCtx")).Return(r, nil)

Expand Down

0 comments on commit f7eaf39

Please sign in to comment.