|
| 1 | +/* |
| 2 | +Copyright 2023 The Kubernetes 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 cache |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "testing" |
| 22 | + |
| 23 | + topologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha2" |
| 24 | + faketopologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/clientset/versioned/fake" |
| 25 | + topologyinformers "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/generated/informers/externalversions" |
| 26 | + corev1 "k8s.io/api/core/v1" |
| 27 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 28 | + "k8s.io/apimachinery/pkg/types" |
| 29 | +) |
| 30 | + |
| 31 | +func TestDiscardReservedNodesGetNRTCopy(t *testing.T) { |
| 32 | + fakeClient := faketopologyv1alpha2.NewSimpleClientset() |
| 33 | + fakeInformer := topologyinformers.NewSharedInformerFactory(fakeClient, 0).Topology().V1alpha2().NodeResourceTopologies() |
| 34 | + |
| 35 | + nrtCache := NewDiscardReserved(fakeInformer.Lister()) |
| 36 | + var nrtObj *topologyv1alpha2.NodeResourceTopology |
| 37 | + nrtObj, _ = nrtCache.GetCachedNRTCopy("node1", &corev1.Pod{}) |
| 38 | + if nrtObj != nil { |
| 39 | + t.Fatalf("non-empty object from empty cache") |
| 40 | + } |
| 41 | + |
| 42 | + nodeTopologies := []*topologyv1alpha2.NodeResourceTopology{ |
| 43 | + { |
| 44 | + ObjectMeta: metav1.ObjectMeta{Name: "node1"}, |
| 45 | + TopologyPolicies: []string{string(topologyv1alpha2.SingleNUMANodeContainerLevel)}, |
| 46 | + Zones: topologyv1alpha2.ZoneList{ |
| 47 | + { |
| 48 | + Name: "node-0", |
| 49 | + Type: "Node", |
| 50 | + Resources: topologyv1alpha2.ResourceInfoList{ |
| 51 | + MakeTopologyResInfo(cpu, "20", "4"), |
| 52 | + MakeTopologyResInfo(memory, "8Gi", "8Gi"), |
| 53 | + MakeTopologyResInfo(nicResourceName, "30", "10"), |
| 54 | + }, |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: "node-1", |
| 58 | + Type: "Node", |
| 59 | + Resources: topologyv1alpha2.ResourceInfoList{ |
| 60 | + MakeTopologyResInfo(cpu, "30", "8"), |
| 61 | + MakeTopologyResInfo(memory, "8Gi", "8Gi"), |
| 62 | + MakeTopologyResInfo(nicResourceName, "30", "10"), |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + } |
| 68 | + for _, obj := range nodeTopologies { |
| 69 | + fakeInformer.Informer().GetStore().Update(obj) |
| 70 | + } |
| 71 | + |
| 72 | + nrtObj, ok := nrtCache.GetCachedNRTCopy("node1", &corev1.Pod{}) |
| 73 | + if !reflect.DeepEqual(nrtObj, nodeTopologies[0]) { |
| 74 | + t.Fatalf("unexpected object from cache\ngot: %s\nexpected: %s\n", dumpNRT(nrtObj), dumpNRT(nodeTopologies[0])) |
| 75 | + } |
| 76 | + |
| 77 | + if !ok { |
| 78 | + t.Fatalf("expecting GetCachedNRTCopy to return true not false") |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestDiscardReservedNodesGetNRTCopyFails(t *testing.T) { |
| 83 | + nrtCache := DiscardReserved{ |
| 84 | + reservationMap: map[string]map[types.UID]bool{ |
| 85 | + "node1": { |
| 86 | + types.UID("some-uid"): true, |
| 87 | + }, |
| 88 | + }, |
| 89 | + } |
| 90 | + |
| 91 | + nrtObj, ok := nrtCache.GetCachedNRTCopy("node1", &corev1.Pod{}) |
| 92 | + if ok { |
| 93 | + t.Fatal("expected false\ngot true\n") |
| 94 | + } |
| 95 | + if nrtObj != nil { |
| 96 | + t.Fatalf("non-empty object") |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +func TestDiscardReservedNodesReserveNodeResources(t *testing.T) { |
| 101 | + nrtCache := DiscardReserved{ |
| 102 | + reservationMap: make(map[string]map[types.UID]bool), |
| 103 | + } |
| 104 | + |
| 105 | + nrtCache.ReserveNodeResources("node1", &corev1.Pod{ |
| 106 | + ObjectMeta: metav1.ObjectMeta{ |
| 107 | + Name: "pod", |
| 108 | + Namespace: "test", |
| 109 | + UID: "some-uid", |
| 110 | + }, |
| 111 | + }) |
| 112 | + nodePods, ok := nrtCache.reservationMap["node1"] |
| 113 | + if !ok { |
| 114 | + t.Fatal("expected reservationMap to have entry for node1") |
| 115 | + } |
| 116 | + |
| 117 | + if len(nodePods) != 1 { |
| 118 | + t.Fatalf("expected reservationMap entry for node1 to have len 1 not: %d", len(nodePods)) |
| 119 | + } |
| 120 | + |
| 121 | + pod, ok := nodePods[types.UID("some-uid")] |
| 122 | + if !ok { |
| 123 | + t.Fatal("expected reservationMap for node1 to have some-uid") |
| 124 | + } |
| 125 | + |
| 126 | + if !pod { |
| 127 | + t.Fatal("expected reservationMap entry node1 to have some-uid set to true not false") |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestDiscardReservedNodesRemoveReservationForNode(t *testing.T) { |
| 132 | + nrtCache := DiscardReserved{ |
| 133 | + reservationMap: make(map[string]map[types.UID]bool), |
| 134 | + } |
| 135 | + |
| 136 | + pod := &corev1.Pod{ |
| 137 | + ObjectMeta: metav1.ObjectMeta{ |
| 138 | + Name: "pod", |
| 139 | + Namespace: "test", |
| 140 | + UID: "some-uid", |
| 141 | + }, |
| 142 | + } |
| 143 | + |
| 144 | + nrtCache.ReserveNodeResources("node1", pod) |
| 145 | + nodePods, ok := nrtCache.reservationMap["node1"] |
| 146 | + if !ok { |
| 147 | + t.Fatal("expected reservationMap to have entry for node1") |
| 148 | + } |
| 149 | + |
| 150 | + if len(nodePods) != 1 { |
| 151 | + t.Fatalf("expected reservationMap entry for node1 to have len 1 not: %d", len(nodePods)) |
| 152 | + } |
| 153 | + |
| 154 | + podExists, ok := nodePods[types.UID("some-uid")] |
| 155 | + if !ok { |
| 156 | + t.Fatal("expected reservationMap for node1 to have some-uid") |
| 157 | + } |
| 158 | + |
| 159 | + if !podExists { |
| 160 | + t.Fatal("expected reservationMap entry node1 to have some-uid set to true not false") |
| 161 | + } |
| 162 | + |
| 163 | + nrtCache.removeReservationForNode("node1", pod) |
| 164 | + nodePods, ok = nrtCache.reservationMap["node1"] |
| 165 | + if !ok { |
| 166 | + t.Fatal("expected reservationMap to have entry for node1") |
| 167 | + } |
| 168 | + |
| 169 | + if len(nodePods) != 0 { |
| 170 | + t.Fatalf("expected reservationMap entry for node1 to have len 0 not: %d", len(nodePods)) |
| 171 | + } |
| 172 | +} |
0 commit comments