|
| 1 | +/* |
| 2 | +Copyright 2023 The Multi-Cluster App Dispatcher Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package core |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + "unsafe" |
| 22 | + |
| 23 | + tree "github.com/project-codeflare/multi-cluster-app-dispatcher/pkg/quotaplugins/quota-forest/quota-manager/tree" |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | + "k8s.io/utils/strings/slices" |
| 26 | +) |
| 27 | + |
| 28 | +// TestQuotaTreeAllocation : test quota tree allocation and de-allocation |
| 29 | +func TestQuotaTreeAllocation(t *testing.T) { |
| 30 | + // create a test quota tree |
| 31 | + testTreeName := "test-tree" |
| 32 | + resourceNames := []string{"CPU"} |
| 33 | + testRootNode := createTestRootNode(t) |
| 34 | + testQuotaTree := NewQuotaTree(testTreeName, testRootNode, resourceNames) |
| 35 | + assert.NotNil(t, testQuotaTree, "Expecting non-nil quota tree") |
| 36 | + |
| 37 | + // create consumers |
| 38 | + consumer1 := NewConsumer("C1", testTreeName, "B", &Allocation{x: []int{2}}, 0, 0, false) |
| 39 | + assert.NotNil(t, consumer1, "Expecting non-nil consumer1") |
| 40 | + consumer2 := NewConsumer("C2", testTreeName, "B", &Allocation{x: []int{1}}, 0, 0, false) |
| 41 | + assert.NotNil(t, consumer2, "Expecting non-nil consumer2") |
| 42 | + consumer3 := NewConsumer("C3", testTreeName, "C", &Allocation{x: []int{1}}, 0, 0, false) |
| 43 | + assert.NotNil(t, consumer3, "Expecting non-nil consumer3") |
| 44 | + consumer4P := NewConsumer("C4P", testTreeName, "B", &Allocation{x: []int{2}}, 1, 0, false) |
| 45 | + assert.NotNil(t, consumer4P, "Expecting non-nil consumer4P") |
| 46 | + consumerLarge := NewConsumer("CL", testTreeName, "C", &Allocation{x: []int{4}}, 0, 0, false) |
| 47 | + assert.NotNil(t, consumerLarge, "Expecting non-nil consumerLarge") |
| 48 | + |
| 49 | + // allocate consumers |
| 50 | + preemptedConsumers := &[]string{} |
| 51 | + |
| 52 | + // C1 -> A (does not fit on requested node B) |
| 53 | + allocated1 := testQuotaTree.Allocate(consumer1, preemptedConsumers) |
| 54 | + assert.True(t, allocated1, "Expecting consumer 1 to be allocated") |
| 55 | + node1 := consumer1.GetNode().GetID() |
| 56 | + assert.Equal(t, "A", node1, "Expecting consumer 1 to be allocated on node A") |
| 57 | + |
| 58 | + // C2 -> B |
| 59 | + allocated2 := testQuotaTree.Allocate(consumer2, preemptedConsumers) |
| 60 | + assert.True(t, allocated2, "Expecting consumer 2 to be allocated") |
| 61 | + node2 := consumer2.GetNode().GetID() |
| 62 | + assert.Equal(t, "B", node2, "Expecting consumer 2 to be allocated on node B") |
| 63 | + |
| 64 | + // C3 -> C (preempts C1 as C3 fits on its requested node C) |
| 65 | + allocated3 := testQuotaTree.Allocate(consumer3, preemptedConsumers) |
| 66 | + assert.True(t, allocated3, "Expecting consumer 3 to be allocated") |
| 67 | + node3 := consumer3.GetNode().GetID() |
| 68 | + assert.Equal(t, "C", node3, "Expecting consumer 3 to be allocated on node C") |
| 69 | + consumer1Preempted := slices.Contains(*preemptedConsumers, "C1") |
| 70 | + assert.True(t, consumer1Preempted, "Expecting consumer 1 to get preempted") |
| 71 | + |
| 72 | + // C4P -> A (high priority C4P preempts C2) |
| 73 | + allocated4P := testQuotaTree.Allocate(consumer4P, preemptedConsumers) |
| 74 | + assert.True(t, allocated4P, "Expecting consumer 4P to be allocated") |
| 75 | + node4P := consumer4P.GetNode().GetID() |
| 76 | + assert.Equal(t, "A", node4P, "Expecting consumer 4P to be allocated on node A") |
| 77 | + consumer2Preempted := slices.Contains(*preemptedConsumers, "C2") |
| 78 | + assert.True(t, consumer2Preempted, "Expecting consumer 2 to get preempted") |
| 79 | + |
| 80 | + // CL large consumer does not fit |
| 81 | + allocatedLarge := testQuotaTree.Allocate(consumerLarge, preemptedConsumers) |
| 82 | + assert.False(t, allocatedLarge, "Expecting large consumer not to be allocated") |
| 83 | + |
| 84 | + // CL -> C (large consumer allocated by force on specified node C, no preemptions) |
| 85 | + forceAllocatedLarge := testQuotaTree.ForceAllocate(consumerLarge, "C") |
| 86 | + assert.True(t, forceAllocatedLarge, "Expecting large consumer to be allocated by force") |
| 87 | + nodeLarge := consumerLarge.GetNode().GetID() |
| 88 | + assert.Equal(t, "C", nodeLarge, "Expecting large consumer to be force allocated on node C") |
| 89 | + |
| 90 | + // C3 de-allocated |
| 91 | + quotaNode3 := consumer3.GetNode() |
| 92 | + deallocated3 := testQuotaTree.DeAllocate(consumer3) |
| 93 | + assert.True(t, deallocated3, "Expecting consumer 3 to be de-allocated") |
| 94 | + node3x := consumer3.GetNode() |
| 95 | + assert.Nil(t, node3x, "Expecting consumer 3 to have a nil node") |
| 96 | + consumer3OnNode := consumerInList(consumer3, quotaNode3.GetConsumers()) |
| 97 | + assert.False(t, consumer3OnNode, "Expecting consumer 3 to be removed from its allocated node") |
| 98 | + |
| 99 | + // C3 de-allocated again |
| 100 | + deallocated3Again := testQuotaTree.DeAllocate(consumer3) |
| 101 | + assert.False(t, deallocated3Again, "Expecting non-existing consumer 3 not to be de-allocated") |
| 102 | +} |
| 103 | + |
| 104 | +// createTestRootNode : create a test quota node as a root for a tree |
| 105 | +func createTestRootNode(t *testing.T) *QuotaNode { |
| 106 | + |
| 107 | + // create three quota nodes A[2], B[1], and C[1] |
| 108 | + quotaNodeA, err := NewQuotaNode("A", &Allocation{x: []int{3}}) |
| 109 | + assert.NoError(t, err, "No error expected when creating quota node A") |
| 110 | + |
| 111 | + quotaNodeB, err := NewQuotaNode("B", &Allocation{x: []int{1}}) |
| 112 | + assert.NoError(t, err, "No error expected when creating quota node B") |
| 113 | + |
| 114 | + quotaNodeC, err := NewQuotaNode("C", &Allocation{x: []int{1}}) |
| 115 | + assert.NoError(t, err, "No error expected when creating quota node C") |
| 116 | + |
| 117 | + // connect nodes: A -> ( B C ) |
| 118 | + addedB := quotaNodeA.AddChild((*tree.Node)(unsafe.Pointer(quotaNodeB))) |
| 119 | + assert.True(t, addedB, "Expecting node B added as child to node A") |
| 120 | + addedC := quotaNodeA.AddChild((*tree.Node)(unsafe.Pointer(quotaNodeC))) |
| 121 | + assert.True(t, addedC, "Expecting node C added as child to node A") |
| 122 | + |
| 123 | + return quotaNodeA |
| 124 | +} |
| 125 | + |
| 126 | +func consumerInList(consumer *Consumer, list []*Consumer) bool { |
| 127 | + consumerID := consumer.GetID() |
| 128 | + for _, c := range list { |
| 129 | + if c.GetID() == consumerID { |
| 130 | + return true |
| 131 | + } |
| 132 | + } |
| 133 | + return false |
| 134 | +} |
0 commit comments