Skip to content

Commit 12e4ab7

Browse files
committed
nrt: test: use common pseudo-conformance tests for all the impls
Use the new common GetCachedNRTCopy tests for all the cache implementations, which must conform to the common API for the common test cases. Cache-specific testcases will be implemented in the cache-specific test code, but the common parts are better served with shared tests. Signed-off-by: Francesco Romani <[email protected]>
1 parent 5c7c469 commit 12e4ab7

File tree

3 files changed

+89
-141
lines changed

3 files changed

+89
-141
lines changed

pkg/noderesourcetopology/cache/discardreserved_test.go

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -25,65 +25,35 @@ import (
2525
corev1 "k8s.io/api/core/v1"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727
"k8s.io/apimachinery/pkg/types"
28+
podlisterv1 "k8s.io/client-go/listers/core/v1"
2829

29-
tu "sigs.k8s.io/scheduler-plugins/test/util"
30+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
3031
)
3132

32-
func TestDiscardReservedNodesGetNRTCopy(t *testing.T) {
33-
fakeClient, err := tu.NewFakeClient()
34-
if err != nil {
35-
t.Fatal(err)
36-
}
33+
func TestDiscardReservedNodesGetCachedNRTCopy(t *testing.T) {
34+
testNodeName := "worker-node-1"
35+
nrt := makeTestNRT(testNodeName)
3736

38-
ctx := context.Background()
39-
nrtCache := NewDiscardReserved(fakeClient)
40-
var nrtObj *topologyv1alpha2.NodeResourceTopology
41-
nrtObj, _ = nrtCache.GetCachedNRTCopy(ctx, "node1", &corev1.Pod{})
42-
if nrtObj != nil {
43-
t.Fatalf("non-empty object from empty cache")
44-
}
45-
46-
nodeTopologies := []*topologyv1alpha2.NodeResourceTopology{
37+
testCases := []testCaseGetCachedNRTCopy{
4738
{
48-
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
49-
TopologyPolicies: []string{string(topologyv1alpha2.SingleNUMANodeContainerLevel)},
50-
Zones: topologyv1alpha2.ZoneList{
51-
{
52-
Name: "node-0",
53-
Type: "Node",
54-
Resources: topologyv1alpha2.ResourceInfoList{
55-
MakeTopologyResInfo(cpu, "20", "4"),
56-
MakeTopologyResInfo(memory, "8Gi", "8Gi"),
57-
MakeTopologyResInfo(nicResourceName, "30", "10"),
58-
},
59-
},
60-
{
61-
Name: "node-1",
62-
Type: "Node",
63-
Resources: topologyv1alpha2.ResourceInfoList{
64-
MakeTopologyResInfo(cpu, "30", "8"),
65-
MakeTopologyResInfo(memory, "8Gi", "8Gi"),
66-
MakeTopologyResInfo(nicResourceName, "30", "10"),
67-
},
68-
},
39+
name: "data present with foreign pods",
40+
nodeTopologies: []*topologyv1alpha2.NodeResourceTopology{
41+
nrt,
6942
},
43+
nodeName: testNodeName,
44+
hasForeignPods: true,
45+
expectedNRT: nrt,
46+
expectedOK: true,
7047
},
7148
}
72-
for _, obj := range nodeTopologies {
73-
if err := fakeClient.Create(ctx, obj.DeepCopy()); err != nil {
74-
t.Fatal(err)
75-
}
76-
}
7749

78-
nrtObj, ok := nrtCache.GetCachedNRTCopy(ctx, "node1", &corev1.Pod{})
79-
if !isNRTEqual(nrtObj, nodeTopologies[0]) {
80-
t.Fatalf("unexpected object from cache\ngot: %s\nexpected: %s\n",
81-
dumpNRT(nrtObj), dumpNRT(nodeTopologies[0]))
82-
}
83-
84-
if !ok {
85-
t.Fatalf("expecting GetCachedNRTCopy to return true not false")
86-
}
50+
checkGetCachedNRTCopy(
51+
t,
52+
func(client ctrlclient.Client, _ podlisterv1.PodLister) (Interface, error) {
53+
return NewDiscardReserved(client), nil
54+
},
55+
testCases...,
56+
)
8757
}
8858

8959
func TestDiscardReservedNodesGetNRTCopyFails(t *testing.T) {

pkg/noderesourcetopology/cache/overreserve_test.go

Lines changed: 18 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package cache
1818

1919
import (
2020
"context"
21-
"encoding/json"
2221
"reflect"
2322
"sort"
2423
"testing"
@@ -207,57 +206,30 @@ func TestDirtyNodesUnmarkedOnReserve(t *testing.T) {
207206
}
208207
}
209208

210-
func TestGetCachedNRTCopy(t *testing.T) {
211-
fakeClient, err := tu.NewFakeClient()
212-
if err != nil {
213-
t.Fatal(err)
214-
}
215-
216-
fakePodLister := &fakePodLister{}
217-
218-
nrtCache := mustOverReserve(t, fakeClient, fakePodLister)
209+
func TestOverreserveGetCachedNRTCopy(t *testing.T) {
210+
testNodeName := "worker-node-1"
211+
nrt := makeTestNRT(testNodeName)
219212

220-
ctx := context.Background()
221-
var nrtObj *topologyv1alpha2.NodeResourceTopology
222-
nrtObj, _ = nrtCache.GetCachedNRTCopy(ctx, "node1", &corev1.Pod{})
223-
if nrtObj != nil {
224-
t.Fatalf("non-empty object from empty cache")
225-
}
226-
227-
nodeTopologies := []*topologyv1alpha2.NodeResourceTopology{
213+
testCases := []testCaseGetCachedNRTCopy{
228214
{
229-
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
230-
TopologyPolicies: []string{string(topologyv1alpha2.SingleNUMANodeContainerLevel)},
231-
Zones: topologyv1alpha2.ZoneList{
232-
{
233-
Name: "node-0",
234-
Type: "Node",
235-
Resources: topologyv1alpha2.ResourceInfoList{
236-
MakeTopologyResInfo(cpu, "20", "4"),
237-
MakeTopologyResInfo(memory, "8Gi", "8Gi"),
238-
MakeTopologyResInfo(nicResourceName, "30", "10"),
239-
},
240-
},
241-
{
242-
Name: "node-1",
243-
Type: "Node",
244-
Resources: topologyv1alpha2.ResourceInfoList{
245-
MakeTopologyResInfo(cpu, "30", "8"),
246-
MakeTopologyResInfo(memory, "8Gi", "8Gi"),
247-
MakeTopologyResInfo(nicResourceName, "30", "10"),
248-
},
249-
},
215+
name: "data present with foreign pods",
216+
nodeTopologies: []*topologyv1alpha2.NodeResourceTopology{
217+
nrt,
250218
},
219+
nodeName: testNodeName,
220+
hasForeignPods: true,
221+
expectedNRT: nil,
222+
expectedOK: false,
251223
},
252224
}
253-
for _, obj := range nodeTopologies {
254-
nrtCache.Store().Update(obj)
255-
}
256225

257-
nrtObj, _ = nrtCache.GetCachedNRTCopy(ctx, "node1", &corev1.Pod{})
258-
if !reflect.DeepEqual(nrtObj, nodeTopologies[0]) {
259-
t.Fatalf("unexpected object from cache\ngot: %s\nexpected: %s\n", dumpNRT(nrtObj), dumpNRT(nodeTopologies[0]))
260-
}
226+
checkGetCachedNRTCopy(
227+
t,
228+
func(client ctrlclient.Client, podLister podlisterv1.PodLister) (Interface, error) {
229+
return NewOverReserve(nil, client, podLister)
230+
},
231+
testCases...,
232+
)
261233
}
262234

263235
func TestGetCachedNRTCopyReserve(t *testing.T) {
@@ -751,51 +723,6 @@ func TestNodeWithForeignPods(t *testing.T) {
751723
}
752724
}
753725

754-
func dumpNRT(nrtObj *topologyv1alpha2.NodeResourceTopology) string {
755-
nrtJson, err := json.MarshalIndent(nrtObj, "", " ")
756-
if err != nil {
757-
return "marshallingError"
758-
}
759-
return string(nrtJson)
760-
}
761-
762-
func MakeTopologyResInfo(name, capacity, available string) topologyv1alpha2.ResourceInfo {
763-
return topologyv1alpha2.ResourceInfo{
764-
Name: name,
765-
Capacity: resource.MustParse(capacity),
766-
Available: resource.MustParse(available),
767-
}
768-
}
769-
770-
func makeDefaultTestTopology() []*topologyv1alpha2.NodeResourceTopology {
771-
return []*topologyv1alpha2.NodeResourceTopology{
772-
{
773-
ObjectMeta: metav1.ObjectMeta{Name: "node1"},
774-
TopologyPolicies: []string{string(topologyv1alpha2.SingleNUMANodeContainerLevel)},
775-
Zones: topologyv1alpha2.ZoneList{
776-
{
777-
Name: "node-0",
778-
Type: "Node",
779-
Resources: topologyv1alpha2.ResourceInfoList{
780-
MakeTopologyResInfo(cpu, "32", "30"),
781-
MakeTopologyResInfo(memory, "64Gi", "60Gi"),
782-
MakeTopologyResInfo(nicResourceName, "16", "16"),
783-
},
784-
},
785-
{
786-
Name: "node-1",
787-
Type: "Node",
788-
Resources: topologyv1alpha2.ResourceInfoList{
789-
MakeTopologyResInfo(cpu, "32", "30"),
790-
MakeTopologyResInfo(memory, "64Gi", "60Gi"),
791-
MakeTopologyResInfo(nicResourceName, "16", "16"),
792-
},
793-
},
794-
},
795-
},
796-
}
797-
}
798-
799726
func mustOverReserve(t *testing.T, client ctrlclient.Client, podLister podlisterv1.PodLister) *OverReserve {
800727
obj, err := NewOverReserve(nil, client, podLister)
801728
if err != nil {
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
"testing"
21+
22+
topologyv1alpha2 "github.com/k8stopologyawareschedwg/noderesourcetopology-api/pkg/apis/topology/v1alpha2"
23+
podlisterv1 "k8s.io/client-go/listers/core/v1"
24+
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
25+
)
26+
27+
func TestPassthroughGetCachedNRTCopy(t *testing.T) {
28+
testNodeName := "worker-node-1"
29+
nrt := makeTestNRT(testNodeName)
30+
31+
testCases := []testCaseGetCachedNRTCopy{
32+
{
33+
name: "data present with foreign pods",
34+
nodeTopologies: []*topologyv1alpha2.NodeResourceTopology{
35+
nrt,
36+
},
37+
nodeName: testNodeName,
38+
hasForeignPods: true,
39+
expectedNRT: nrt,
40+
expectedOK: true,
41+
},
42+
}
43+
44+
checkGetCachedNRTCopy(
45+
t,
46+
func(client ctrlclient.Client, _ podlisterv1.PodLister) (Interface, error) {
47+
return NewPassthrough(client), nil
48+
},
49+
testCases...,
50+
)
51+
}

0 commit comments

Comments
 (0)