|
| 1 | +/* |
| 2 | +Copyright 2022 The Katalyst 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 loadaware |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + v1 "k8s.io/api/core/v1" |
| 24 | + "k8s.io/apimachinery/pkg/api/resource" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/util/sets" |
| 27 | +) |
| 28 | + |
| 29 | +func TestOnNodeAdd(t *testing.T) { |
| 30 | + t.Parallel() |
| 31 | + |
| 32 | + p := &Plugin{ |
| 33 | + workers: 3, |
| 34 | + nodePoolMap: map[int32]sets.String{}, |
| 35 | + nodeStatDataMap: map[string]*NodeMetricData{}, |
| 36 | + } |
| 37 | + |
| 38 | + testNode1 := &v1.Node{ |
| 39 | + ObjectMeta: metav1.ObjectMeta{ |
| 40 | + Name: "testNode1", |
| 41 | + }, |
| 42 | + Status: v1.NodeStatus{ |
| 43 | + Allocatable: map[v1.ResourceName]resource.Quantity{ |
| 44 | + v1.ResourceCPU: resource.MustParse("16"), |
| 45 | + v1.ResourceMemory: resource.MustParse("32Gi"), |
| 46 | + }, |
| 47 | + }, |
| 48 | + } |
| 49 | + |
| 50 | + p.OnNodeAdd(testNode1) |
| 51 | + assert.NotNil(t, p.nodeStatDataMap["testNode1"]) |
| 52 | + assert.Equal(t, 2, len(p.nodeStatDataMap["testNode1"].TotalRes)) |
| 53 | + |
| 54 | + p.OnNodeDelete(testNode1) |
| 55 | + assert.Nil(t, p.nodeStatDataMap["testNode1"]) |
| 56 | +} |
| 57 | + |
| 58 | +func TestOnNodeUpdate(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + |
| 61 | + p := &Plugin{ |
| 62 | + nodeStatDataMap: map[string]*NodeMetricData{}, |
| 63 | + } |
| 64 | + |
| 65 | + testNode1 := &v1.Node{ |
| 66 | + ObjectMeta: metav1.ObjectMeta{ |
| 67 | + Name: "testNode1", |
| 68 | + }, |
| 69 | + Status: v1.NodeStatus{ |
| 70 | + Allocatable: map[v1.ResourceName]resource.Quantity{ |
| 71 | + v1.ResourceCPU: resource.MustParse("16"), |
| 72 | + v1.ResourceMemory: resource.MustParse("32Gi"), |
| 73 | + }, |
| 74 | + }, |
| 75 | + } |
| 76 | + |
| 77 | + p.OnNodeUpdate(nil, testNode1) |
| 78 | + assert.NotNil(t, p.nodeStatDataMap["testNode1"]) |
| 79 | + assert.Equal(t, 2, len(p.nodeStatDataMap["testNode1"].TotalRes)) |
| 80 | +} |
| 81 | + |
| 82 | +func TestOnPodAdd(t *testing.T) { |
| 83 | + t.Parallel() |
| 84 | + |
| 85 | + p := &Plugin{ |
| 86 | + nodeToPodsMap: map[string]map[string]struct{}{}, |
| 87 | + podUsageSelectorKey: "app", |
| 88 | + podUsageSelectorVal: "testPod", |
| 89 | + podUsageSelectorNamespace: "katalyst-system", |
| 90 | + } |
| 91 | + |
| 92 | + testPod1 := &v1.Pod{ |
| 93 | + ObjectMeta: metav1.ObjectMeta{ |
| 94 | + Name: "testPod1", |
| 95 | + Namespace: "katalyst-system", |
| 96 | + Labels: map[string]string{ |
| 97 | + "app": "testPod", |
| 98 | + }, |
| 99 | + }, |
| 100 | + Spec: v1.PodSpec{ |
| 101 | + NodeName: "testNode1", |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + p.OnPodAdd(testPod1) |
| 106 | + assert.NotNil(t, p.nodeToPodsMap["testNode1"]) |
| 107 | + assert.Equal(t, 1, len(p.nodeToPodsMap["testNode1"])) |
| 108 | + |
| 109 | + p.OnPodDelete(testPod1) |
| 110 | + assert.Equal(t, 0, len(p.nodeToPodsMap["testNode1"])) |
| 111 | + |
| 112 | + p.OnPodDelete("") |
| 113 | +} |
| 114 | + |
| 115 | +func TestOnPodUpdate(t *testing.T) { |
| 116 | + t.Parallel() |
| 117 | + |
| 118 | + p := &Plugin{ |
| 119 | + nodeToPodsMap: map[string]map[string]struct{}{}, |
| 120 | + podUsageSelectorKey: "app", |
| 121 | + podUsageSelectorVal: "testPod", |
| 122 | + podUsageSelectorNamespace: "katalyst-system", |
| 123 | + } |
| 124 | + |
| 125 | + testPod1 := &v1.Pod{ |
| 126 | + ObjectMeta: metav1.ObjectMeta{ |
| 127 | + Name: "testPod1", |
| 128 | + Namespace: "katalyst-system", |
| 129 | + Labels: map[string]string{ |
| 130 | + "app": "testPod", |
| 131 | + }, |
| 132 | + }, |
| 133 | + Spec: v1.PodSpec{ |
| 134 | + NodeName: "testNode1", |
| 135 | + }, |
| 136 | + } |
| 137 | + |
| 138 | + p.OnPodUpdate(nil, testPod1) |
| 139 | + assert.NotNil(t, p.nodeToPodsMap["testNode1"]) |
| 140 | + assert.Equal(t, 1, len(p.nodeToPodsMap["testNode1"])) |
| 141 | + |
| 142 | + p.OnPodUpdate(nil, "") |
| 143 | +} |
0 commit comments