From 173abec216fd5186cdd221bb80c433c60f3e488b Mon Sep 17 00:00:00 2001 From: CMGS Date: Wed, 25 Nov 2020 15:54:40 +0800 Subject: [PATCH] unittests: fix resource bugs, add more tests --- resources/cpumem/cpumem_test.go | 34 +++++++++++++++++++++++++--- resources/storage/storage.go | 2 +- resources/storage/storage_test.go | 37 ++++++++++++++++++++++++------- resources/volume/volume.go | 6 ++++- resources/volume/volume_test.go | 25 ++++++++++++++++----- 5 files changed, 85 insertions(+), 19 deletions(-) diff --git a/resources/cpumem/cpumem_test.go b/resources/cpumem/cpumem_test.go index 3f9eeeadd..b2e966997 100644 --- a/resources/cpumem/cpumem_test.go +++ b/resources/cpumem/cpumem_test.go @@ -73,6 +73,26 @@ func TestType(t *testing.T) { assert.True(t, req.Type()&types.ResourceCPUBind > 0) } +func TestRate(t *testing.T) { + req, err := MakeRequest(types.ResourceOptions{ + MemoryRequest: 0, + MemoryLimit: 1, + }) + assert.Nil(t, err) + node := types.Node{ + InitCPU: types.CPUMap{"1": 100, "2": 100}, + InitMemCap: 100, + } + assert.Equal(t, req.Rate(node), 0.01) + req, err = MakeRequest(types.ResourceOptions{ + CPUQuotaRequest: 0, + CPUQuotaLimit: 2, + CPUBind: true, + }) + assert.Nil(t, err) + assert.Equal(t, req.Rate(node), 1.0) +} + func TestRequestCpuNode(t *testing.T) { run(t, newRequestCPUNodeTest()) } @@ -104,6 +124,11 @@ type nodeSchdulerTest interface { } func run(t *testing.T, test nodeSchdulerTest) { + resourceRequest, err := MakeRequest(test.getRequestOptions()) + assert.NoError(t, err) + _, _, err = resourceRequest.MakeScheduler()([]types.NodeInfo{}) + assert.Error(t, err) + s := test.getScheduler() prevSche, _ := scheduler.GetSchedulerV1() scheduler.InitSchedulerV1(s) @@ -111,8 +136,8 @@ func run(t *testing.T, test nodeSchdulerTest) { scheduler.InitSchedulerV1(prevSche) }() - resourceRequest, err := MakeRequest(test.getRequestOptions()) - assert.Nil(t, err) + resourceRequest, err = MakeRequest(test.getRequestOptions()) + assert.NoError(t, err) sche := resourceRequest.MakeScheduler() @@ -121,6 +146,9 @@ func run(t *testing.T, test nodeSchdulerTest) { var node = test.getNode() + assert.True(t, plans.Type()&(types.ResourceCPU|types.ResourceMemory) > 0) + assert.NotNil(t, plans.Capacity()) + plans.ApplyChangesOnNode(node, 0) test.assertAfterChanges(t) @@ -161,7 +189,7 @@ func newRequestCPUNodeTest() nodeSchdulerTest { CPUPlan: []types.CPUMap{{"0": 10000, "1": 10000}}, }, }, - cpuMap: map[string][]types.CPUMap{"TestNode": []types.CPUMap{{"0": 10000, "1": 10000}}}, + cpuMap: map[string][]types.CPUMap{"TestNode": {{"0": 10000, "1": 10000}}}, } } diff --git a/resources/storage/storage.go b/resources/storage/storage.go index 7558a8c2f..8bcf2e870 100644 --- a/resources/storage/storage.go +++ b/resources/storage/storage.go @@ -60,7 +60,7 @@ func (s storageRequest) MakeScheduler() resourcetypes.SchedulerV2 { // Rate . func (s storageRequest) Rate(node types.Node) float64 { - return float64(0) / float64(node.Volume.Total()) + return float64(s.request) / float64(node.InitStorageCap) } // ResourcePlans . diff --git a/resources/storage/storage_test.go b/resources/storage/storage_test.go index ce54d0334..b460dd7a3 100644 --- a/resources/storage/storage_test.go +++ b/resources/storage/storage_test.go @@ -36,6 +36,24 @@ func TestMakeRequest(t *testing.T) { StorageLimit: 0, }) assert.Nil(t, err) + + _, err = MakeRequest(types.ResourceOptions{ + StorageRequest: 2024, + StorageLimit: 1024, + }) + assert.Nil(t, err) +} + +func TestRate(t *testing.T) { + req, err := MakeRequest(types.ResourceOptions{ + StorageRequest: 1024, + StorageLimit: 1024, + }) + assert.Nil(t, err) + node := types.Node{ + InitStorageCap: 1024, + } + assert.Equal(t, req.Rate(node), 1.0) } func TestStorage(t *testing.T) { @@ -57,21 +75,22 @@ func TestStorage(t *testing.T) { "SelectStorageNodes", mock.Anything, mock.Anything, ).Return(nodeInfos, 1, nil) - prevSche, _ := scheduler.GetSchedulerV1() - scheduler.InitSchedulerV1(mockScheduler) - defer func() { - scheduler.InitSchedulerV1(prevSche) - }() - resourceRequest, err := MakeRequest(types.ResourceOptions{ StorageRequest: 1024, StorageLimit: 1024, }) - assert.Nil(t, err) + assert.NoError(t, err) + _, _, err = resourceRequest.MakeScheduler()([]types.NodeInfo{}) + assert.Error(t, err) + assert.True(t, resourceRequest.Type()&types.ResourceStorage > 0) + prevSche, _ := scheduler.GetSchedulerV1() + scheduler.InitSchedulerV1(mockScheduler) + defer func() { + scheduler.InitSchedulerV1(prevSche) + }() sche := resourceRequest.MakeScheduler() - plans, _, err := sche(nodeInfos) assert.Nil(t, err) @@ -87,6 +106,8 @@ func TestStorage(t *testing.T) { assert.True(t, plans.Type()&types.ResourceStorage > 0) + assert.NotNil(t, plans.Capacity()) + plans.ApplyChangesOnNode(&node, 0) assert.Less(t, node.StorageCap, storage) diff --git a/resources/volume/volume.go b/resources/volume/volume.go index ba7790242..42bbd2937 100644 --- a/resources/volume/volume.go +++ b/resources/volume/volume.go @@ -99,7 +99,11 @@ func (v volumeRequest) MakeScheduler() resourcetypes.SchedulerV2 { // Rate . func (v volumeRequest) Rate(node types.Node) float64 { - return float64(node.VolumeUsed) / float64(node.Volume.Total()) + var totalRequest int64 + for i := 0; i < v.requests; i++ { + totalRequest += v.request[i].SizeInBytes + } + return float64(totalRequest) / float64(node.Volume.Total()) } // ResourcePlans . diff --git a/resources/volume/volume_test.go b/resources/volume/volume_test.go index 51c64c751..0a2a102bd 100644 --- a/resources/volume/volume_test.go +++ b/resources/volume/volume_test.go @@ -15,7 +15,7 @@ import ( func TestMakeRequest(t *testing.T) { _, err := MakeRequest(types.ResourceOptions{ VolumeLimit: []*types.VolumeBinding{ - &types.VolumeBinding{ + { Source: "/data1", Destination: "/data1", }, @@ -25,17 +25,17 @@ func TestMakeRequest(t *testing.T) { _, err = MakeRequest(types.ResourceOptions{ VolumeRequest: []*types.VolumeBinding{ - &types.VolumeBinding{ + { Source: "/data1", Destination: "/data1", }, }, VolumeLimit: []*types.VolumeBinding{ - &types.VolumeBinding{ + { Source: "/data1", Destination: "/data1", }, - &types.VolumeBinding{ + { Source: "/data2", Destination: "/data2", }, @@ -93,13 +93,13 @@ func TestStorage(t *testing.T) { resourceRequest, err := MakeRequest(types.ResourceOptions{ VolumeRequest: []*types.VolumeBinding{ - &types.VolumeBinding{ + { Source: "/data1", Destination: "/data1", }, }, VolumeLimit: []*types.VolumeBinding{ - &types.VolumeBinding{ + { Source: "/data1", Destination: "/data1", }, @@ -127,6 +127,7 @@ func TestStorage(t *testing.T) { InitVolume: types.VolumeMap{"/data1": 512, "/data2": 512}, } + assert.NotNil(t, plans.Capacity()) plans.ApplyChangesOnNode(&node, 0) assert.Less(t, node.Volume["/data1"], int64(512)) @@ -153,3 +154,15 @@ func TestStorage(t *testing.T) { _, err = plans.Dispense(opts, r) assert.Nil(t, err) } + +func TestRate(t *testing.T) { + req, err := MakeRequest(types.ResourceOptions{ + VolumeRequest: types.VolumeBindings{&types.VolumeBinding{SizeInBytes: 1024}}, + VolumeLimit: types.VolumeBindings{&types.VolumeBinding{SizeInBytes: 1024}}, + }) + assert.Nil(t, err) + node := types.Node{ + Volume: types.VolumeMap{"1": 1024}, + } + assert.Equal(t, req.Rate(node), 1.0) +}