|
| 1 | +/* |
| 2 | + Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + or more contributor license agreements. See the NOTICE file |
| 4 | + distributed with this work for additional information |
| 5 | + regarding copyright ownership. The ASF licenses this file |
| 6 | + to you under the Apache License, Version 2.0 (the |
| 7 | + "License"); you may not use this file except in compliance |
| 8 | + with the License. You may obtain a copy of the License at |
| 9 | +
|
| 10 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +
|
| 12 | + Unless required by applicable law or agreed to in writing, software |
| 13 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + See the License for the specific language governing permissions and |
| 16 | + limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +package objects |
| 20 | + |
| 21 | +import ( |
| 22 | + "testing" |
| 23 | + |
| 24 | + "gotest.tools/v3/assert" |
| 25 | + |
| 26 | + "github.com/apache/yunikorn-core/pkg/common/configs" |
| 27 | + "github.com/apache/yunikorn-core/pkg/common/resources" |
| 28 | +) |
| 29 | + |
| 30 | +func TestQuotaChangeCheckPreconditions(t *testing.T) { |
| 31 | + parentConfig := configs.QueueConfig{ |
| 32 | + Name: "parent", |
| 33 | + Parent: true, |
| 34 | + Resources: configs.Resources{ |
| 35 | + Max: map[string]string{"memory": "1000"}, |
| 36 | + }, |
| 37 | + } |
| 38 | + parent, err := NewConfiguredQueue(parentConfig, nil, false, nil) |
| 39 | + assert.NilError(t, err) |
| 40 | + |
| 41 | + leafRes := configs.Resources{ |
| 42 | + Max: map[string]string{"memory": "1000"}, |
| 43 | + } |
| 44 | + leaf, err := NewConfiguredQueue(configs.QueueConfig{ |
| 45 | + Name: "leaf", |
| 46 | + Resources: leafRes, |
| 47 | + }, parent, false, nil) |
| 48 | + assert.NilError(t, err) |
| 49 | + |
| 50 | + dynamicLeaf, err := NewConfiguredQueue(configs.QueueConfig{ |
| 51 | + Name: "dynamic-leaf", |
| 52 | + Resources: leafRes, |
| 53 | + }, parent, false, nil) |
| 54 | + assert.NilError(t, err) |
| 55 | + dynamicLeaf.isManaged = false |
| 56 | + |
| 57 | + alreadyPreemptionRunning, err := NewConfiguredQueue(configs.QueueConfig{ |
| 58 | + Name: "leaf-already-preemption-running", |
| 59 | + Resources: leafRes, |
| 60 | + }, parent, false, nil) |
| 61 | + assert.NilError(t, err) |
| 62 | + alreadyPreemptionRunning.MarkQuotaChangePreemptionRunning() |
| 63 | + |
| 64 | + alreadyTriggeredPreemption, err := NewConfiguredQueue(configs.QueueConfig{ |
| 65 | + Name: "leaf-already-triggerred-running", |
| 66 | + Resources: leafRes, |
| 67 | + }, parent, false, nil) |
| 68 | + assert.NilError(t, err) |
| 69 | + alreadyTriggeredPreemption.MarkTriggerredQuotaChangePreemption() |
| 70 | + |
| 71 | + usageExceededMaxQueue, err := NewConfiguredQueue(configs.QueueConfig{ |
| 72 | + Name: "leaf-usage-exceeded-max", |
| 73 | + Resources: leafRes, |
| 74 | + }, parent, false, nil) |
| 75 | + assert.NilError(t, err) |
| 76 | + usageExceededMaxQueue.allocatedResource = resources.NewResourceFromMap(map[string]resources.Quantity{"memory": 2000}) |
| 77 | + |
| 78 | + testCases := []struct { |
| 79 | + name string |
| 80 | + queue *Queue |
| 81 | + preconditionResult bool |
| 82 | + }{ |
| 83 | + {"parent queue", parent, false}, |
| 84 | + {"leaf queue", leaf, false}, |
| 85 | + {"dynamic leaf queue", dynamicLeaf, false}, |
| 86 | + {"leaf queue, already preemption process started or running", alreadyPreemptionRunning, false}, |
| 87 | + {"leaf queue, already triggerred preemption", alreadyTriggeredPreemption, false}, |
| 88 | + {"leaf queue, usage exceeded max resources", usageExceededMaxQueue, true}, |
| 89 | + } |
| 90 | + for _, tc := range testCases { |
| 91 | + t.Run(tc.name, func(t *testing.T) { |
| 92 | + preemptor := NewQuotaChangePreemptor(tc.queue) |
| 93 | + assert.Equal(t, preemptor.CheckPreconditions(), tc.preconditionResult) |
| 94 | + if tc.preconditionResult { |
| 95 | + preemptor.tryPreemption() |
| 96 | + assert.Equal(t, tc.queue.HasTriggerredQuotaChangePreemption(), true) |
| 97 | + assert.Equal(t, tc.queue.IsQuotaChangePreemptionRunning(), true) |
| 98 | + } |
| 99 | + }) |
| 100 | + } |
| 101 | +} |
0 commit comments