Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jschwinger233 committed Jun 22, 2021
1 parent 9e24bc2 commit 29ec032
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cluster/calcium/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestDoUnlockAll(t *testing.T) {

// failed
lock.On("Unlock", mock.Anything).Return(types.ErrNoETCD)
c.doUnlockAll(context.Background(), locks)
c.doUnlockAll(context.Background(), locks, nil)
}

func TestWithWorkloadsLocked(t *testing.T) {
Expand Down
24 changes: 15 additions & 9 deletions cluster/calcium/realloc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,27 +97,29 @@ func TestRealloc(t *testing.T) {
}
}

store.On("GetWorkloads", mock.Anything, []string{"c1"}).Return(newC1, nil)
store.On("GetWorkload", mock.Anything, "c1").Return(newC1(context.TODO(), nil)[0], nil)

// failed by GetNode
store.On("GetNode", mock.Anything, "node1").Return(nil, types.ErrNoETCD).Once()
err := c.ReallocResource(ctx, newReallocOptions("c1", 0.1, 2*int64(units.GiB), nil, types.TriKeep, types.TriKeep))
assert.EqualError(t, err, "ETCD must be set")
store.AssertExpectations(t)
store.On("GetNode", mock.Anything, "node1").Return(node1, nil)

// failed by lock
store.On("CreateLock", mock.Anything, mock.Anything).Return(nil, types.ErrNoETCD).Once()
err := c.ReallocResource(ctx, newReallocOptions("c1", -1, 2*int64(units.GiB), nil, types.TriKeep, types.TriKeep))
err = c.ReallocResource(ctx, newReallocOptions("c1", -1, 2*int64(units.GiB), nil, types.TriKeep, types.TriKeep))
assert.EqualError(t, err, "ETCD must be set")
store.AssertExpectations(t)
store.On("CreateLock", mock.Anything, mock.Anything).Return(lock, nil)

// failed by newCPU < 0
store.On("GetWorkloads", mock.Anything, []string{"c1"}).Return(newC1, nil)
err = c.ReallocResource(ctx, newReallocOptions("c1", -1, 2*int64(units.GiB), nil, types.TriKeep, types.TriKeep))
assert.EqualError(t, err, "limit or request less than 0: bad `CPU` value")
store.AssertExpectations(t)

// failed by GetNode
store.On("GetNode", mock.Anything, "node1").Return(nil, types.ErrNoETCD).Once()
err = c.ReallocResource(ctx, newReallocOptions("c1", 0.1, 2*int64(units.GiB), nil, types.TriKeep, types.TriKeep))
assert.EqualError(t, err, "ETCD must be set")
store.AssertExpectations(t)

// failed by no enough mem
store.On("GetNode", mock.Anything, "node1").Return(node1, nil)
simpleMockScheduler := &schedulermocks.Scheduler{}
scheduler.InitSchedulerV1(simpleMockScheduler)
c.scheduler = simpleMockScheduler
Expand Down Expand Up @@ -165,6 +167,7 @@ func TestRealloc(t *testing.T) {
},
Engine: engine,
}
store.On("GetWorkload", mock.Anything, "c2").Return(newC2(nil, nil)[0], nil)
store.On("GetWorkloads", mock.Anything, []string{"c2"}).Return(newC2, nil)
err = c.ReallocResource(ctx, newReallocOptions("c2", 0.1, 2*int64(units.MiB), nil, types.TriKeep, types.TriKeep))
assert.EqualError(t, err, "workload ID must be length of 64")
Expand Down Expand Up @@ -251,6 +254,7 @@ func TestRealloc(t *testing.T) {
simpleMockScheduler.On("ReselectCPUNodes", mock.Anything, mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(scheduleInfos[0], nodeCPUPlans, 2, nil)
simpleMockScheduler.On("ReselectVolumeNodes", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(scheduleInfos[0], nodeVolumePlans, 2, nil).Once()
store.On("GetNode", mock.Anything, "node2").Return(node2, nil)
store.On("GetWorkload", mock.Anything, "c3").Return(c3, nil)
store.On("GetWorkloads", mock.Anything, []string{"c3"}).Return([]*types.Workload{c3}, nil)
store.On("UpdateWorkload", mock.Anything, mock.Anything).Return(types.ErrBadWorkloadID).Times(1)
err = c.ReallocResource(ctx, newReallocOptions("c3", 0.1, 2*int64(units.MiB), types.MustToVolumeBindings([]string{"AUTO:/data0:rw:-50"}), types.TriKeep, types.TriKeep))
Expand Down Expand Up @@ -341,6 +345,7 @@ func TestReallocBindCpu(t *testing.T) {
}

store.On("GetNode", mock.Anything, "node3").Return(node3, nil)
store.On("GetWorkload", mock.Anything, "c5").Return(c5, nil)
store.On("GetWorkloads", mock.Anything, []string{"c5"}).Return([]*types.Workload{c5}, nil)
engine.On("VirtualizationUpdateResource", mock.Anything, mock.Anything, mock.Anything).Return(nil)
store.On("UpdateWorkload", mock.Anything, mock.Anything).Return(nil)
Expand All @@ -358,6 +363,7 @@ func TestReallocBindCpu(t *testing.T) {
assert.Equal(t, 0, len(c5.ResourceMeta.CPU))
store.AssertExpectations(t)

store.On("GetWorkload", mock.Anything, "c6").Return(c6, nil)
store.On("GetWorkloads", mock.Anything, []string{"c6"}).Return([]*types.Workload{c6}, nil)
err = c.ReallocResource(ctx, newReallocOptions("c6", 0.1, 2*int64(units.MiB), nil, types.TriTrue, types.TriKeep))
assert.NoError(t, err)
Expand Down
1 change: 1 addition & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ func Reverse(s interface{}) {
}

// Unique return a index, where s[:index] is a unique slice
// Unique requires sorted slice
func Unique(s interface{}, getVal func(int) string) (j int) {
n := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
Expand Down
28 changes: 28 additions & 0 deletions utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/ioutil"
"os"
"reflect"
"strconv"
"testing"

Expand Down Expand Up @@ -232,3 +233,30 @@ func TestRange(t *testing.T) {
assert.Equal(t, i, res[i])
}
}

func TestReverse(t *testing.T) {
s1 := []string{"a", "b", "c"}
Reverse(s1)
assert.True(t, reflect.DeepEqual(s1, []string{"c", "b", "a"}))

s2 := []string{}
Reverse(s2)

s3 := []int{1, 2, 3, 4}
Reverse(s3)
assert.True(t, reflect.DeepEqual(s3, []int{4, 3, 2, 1}))
}

func TestUnique(t *testing.T) {
s1 := []int64{1, 2, 3}
s1 = s1[:Unique(s1, func(i int) string { return strconv.Itoa(int(s1[i])) })]
assert.True(t, reflect.DeepEqual(s1, []int64{1, 2, 3}))

s2 := []string{"a", "a", "a", "b", "b", "c"}
s2 = s2[:Unique(s2, func(i int) string { return s2[i] })]
assert.True(t, reflect.DeepEqual(s2, []string{"a", "b", "c"}))

s3 := []string{"", "1", "1", "1", "1"}
s3 = s3[:Unique(s3, func(i int) string { return s3[i] })]
assert.True(t, reflect.DeepEqual(s3, []string{"", "1"}))
}

0 comments on commit 29ec032

Please sign in to comment.