Skip to content

Commit

Permalink
update unitests
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Dec 27, 2021
1 parent 1c13c4f commit fa7142f
Show file tree
Hide file tree
Showing 18 changed files with 63 additions and 45 deletions.
2 changes: 1 addition & 1 deletion 3rdmocks/ServerStream.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

20 changes: 14 additions & 6 deletions cluster/calcium/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,24 @@ func TestListPodNodes(t *testing.T) {
store := &storemocks.Store{}
c.store = store
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
_, err := c.ListPodNodes(ctx, "", nil, false)
_, err := c.ListPodNodes(ctx, &types.ListNodesOptions{})
assert.Error(t, err)
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nodes, nil)
ns, err := c.ListPodNodes(ctx, "", nil, false)
ns, err := c.ListPodNodes(ctx, &types.ListNodesOptions{})
nss := []*types.Node{}
for n := range ns {
nss = append(nss, n)
}
assert.NoError(t, err)
assert.Equal(t, len(ns), 2)
assert.Equal(t, ns[0].Name, name1)
ns, err = c.ListPodNodes(ctx, "", nil, true)
assert.Equal(t, len(nss), 2)
assert.Equal(t, nss[0].Name, name1)
ns, err = c.ListPodNodes(ctx, &types.ListNodesOptions{})
assert.NoError(t, err)
assert.Equal(t, len(ns), 2)
cnt := 0
for range ns {
cnt++
}
assert.Equal(t, cnt, 2)
}

func TestGetNode(t *testing.T) {
Expand Down
3 changes: 1 addition & 2 deletions cluster/calcium/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ func (c *Calcium) PodResource(ctx context.Context, podname string) (chan *types.
pool.Go(ctx, func() {
nodeResource, err := c.doGetNodeResource(ctx, node.Name, false)
if err != nil {
logger.Err(ctx, err)
nodeResource = &types.NodeResource{
Name: node.Name, Diffs: []string{err.Error()},
Name: node.Name, Diffs: []string{logger.Err(ctx, err).Error()},
}
}
ch <- nodeResource
Expand Down
25 changes: 16 additions & 9 deletions cluster/calcium/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ func TestPodResource(t *testing.T) {
store := &storemocks.Store{}
c.store = store
lock := &lockmocks.DistributedLock{}
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
lock.On("Lock", mock.Anything).Return(context.TODO(), nil)
lock.On("Unlock", mock.Anything).Return(nil)
// failed by GetNodesByPod
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
_, err := c.PodResource(ctx, podname)
ch, err := c.PodResource(ctx, podname)
assert.Error(t, err)
store.AssertExpectations(t)

// failed by ListNodeWorkloads
node := &types.Node{
NodeMeta: types.NodeMeta{
Name: nodename,
Expand All @@ -49,10 +51,14 @@ func TestPodResource(t *testing.T) {
}
store.On("GetNodesByPod", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return([]*types.Node{node}, nil)
store.On("GetNode", mock.Anything, mock.Anything).Return(node, nil)
// failed by ListNodeWorkloads
store.On("ListNodeWorkloads", mock.Anything, mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
_, err = c.PodResource(ctx, podname)
assert.Error(t, err)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)
ch, err = c.PodResource(ctx, podname)
assert.NoError(t, err)
msg := <-ch
assert.True(t, strings.Contains(msg.Diffs[0], types.ErrNoETCD.Error()))
store.AssertExpectations(t)

workloads := []*types.Workload{
{
ResourceMeta: types.ResourceMeta{
Expand Down Expand Up @@ -84,10 +90,11 @@ func TestPodResource(t *testing.T) {
// success
r, err := c.PodResource(ctx, podname)
assert.NoError(t, err)
assert.Equal(t, r.NodesResource[0].CPUPercent, 0.9)
assert.Equal(t, r.NodesResource[0].MemoryPercent, 0.5)
assert.Equal(t, r.NodesResource[0].StoragePercent, 0.1)
assert.NotEmpty(t, r.NodesResource[0].Diffs)
first := <-r
assert.Equal(t, first.CPUPercent, 0.9)
assert.Equal(t, first.MemoryPercent, 0.5)
assert.Equal(t, first.StoragePercent, 0.1)
assert.NotEmpty(t, first.Diffs)
}

func TestNodeResource(t *testing.T) {
Expand Down
28 changes: 14 additions & 14 deletions cluster/mocks/Cluster.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion engine/docker/mocks/APIClient.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion engine/mocks/API.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lock/mocks/DistributedLock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion rpc/mocks/CoreRPC_RunAndWaitServer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions rpc/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func (v *Vibranium) GetPodResource(ctx context.Context, opts *pb.GetPodOptions)
return podResource, nil
}

// PodResourceStream returns a stream of NodeResource
func (v *Vibranium) PodResourceStream(opts *pb.GetPodOptions, stream pb.CoreRPC_PodResourceStreamServer) error {
ctx := v.taskAdd(stream.Context(), "PodResourceStream", false)
defer v.taskDone(ctx, "PodResourceStream", false)
Expand Down Expand Up @@ -217,6 +218,7 @@ func (v *Vibranium) ListPodNodes(ctx context.Context, opts *pb.ListNodesOptions)
return &pb.Nodes{Nodes: nodes}, nil
}

// PodNodesStream returns a stream of Node
func (v *Vibranium) PodNodesStream(opts *pb.ListNodesOptions, stream pb.CoreRPC_PodNodesStreamServer) error {
ctx := v.taskAdd(stream.Context(), "PodNodesStream", false)
defer v.taskDone(ctx, "PodNodesStream", false)
Expand Down
2 changes: 1 addition & 1 deletion scheduler/mocks/Scheduler.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion source/mocks/Source.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/etcdv3/meta/mocks/ETCDClientV3.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/etcdv3/meta/mocks/KV.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/etcdv3/meta/mocks/Txn.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion store/mocks/Store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions types/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"math"
"reflect"
"strings"
"testing"

enginemocks "github.com/projecteru2/core/engine/mocks"
Expand All @@ -25,13 +26,13 @@ func TestNode(t *testing.T) {
assert.Equal(t, node.Volume, VolumeMap{})
assert.Equal(t, node.InitVolume, VolumeMap{})
ctx := context.Background()
_, err := node.Info(ctx)
err := node.Info(ctx)
assert.Error(t, err)

node.Engine = mockEngine
info, err := node.Info(ctx)
err = node.Info(ctx)
assert.NoError(t, err)
assert.Equal(t, info.ID, "test")
assert.True(t, strings.Contains(node.NodeInfo, "test"))

node.CPUUsed = 0.0
node.SetCPUUsed(1.0, IncrUsage)
Expand Down
1 change: 1 addition & 0 deletions types/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ func (o *ReplaceOptions) Normalize() {
}
}

// ListNodesOptions for list nodes
type ListNodesOptions struct {
Podname string
Labels map[string]string
Expand Down

0 comments on commit fa7142f

Please sign in to comment.