]> git.feebdaed.xyz Git - 0xmirror/kubernetes.git/commitdiff
resourceAllocationScorer: add unit test for DRA nodeMatches
authorEd Bartosh <eduard.bartosh@intel.com>
Thu, 27 Nov 2025 14:20:10 +0000 (16:20 +0200)
committerEd Bartosh <eduard.bartosh@intel.com>
Fri, 12 Dec 2025 13:48:13 +0000 (15:48 +0200)
pkg/scheduler/framework/plugins/noderesources/resource_allocation_test.go

index 0cbb2a248080768ee6fbc993fd962d9f30e10cd0..2216c733a929f87e09c65781125d1ddbc3ec09cf 100644 (file)
@@ -453,7 +453,7 @@ func TestCalculateResourceAllocatableRequest(t *testing.T) {
                },
                "DRA-backed-resource-with-per-device-node-selection": {
                        enableDRAExtendedResource: true,
-                       node:                      st.MakeNode().Name(nodeName).Obj(),
+                       node:                      st.MakeNode().Name(nodeName).Label("zone", "us-east-1a").Obj(),
                        extendedResource:          explicitExtendedResource,
                        objects: []apiruntime.Object{
                                &resourceapi.DeviceClass{
@@ -502,13 +502,26 @@ func TestCalculateResourceAllocatableRequest(t *testing.T) {
                                                                },
                                                        },
                                                        {
-                                                               Name:     "device-3",
-                                                               AllNodes: ptr.To(true), // This device matches all nodes
+                                                               Name: "device-3",
+                                                               // Use a node selector to ensure nodeMatches is exercised for this device
+                                                               NodeSelector: &v1.NodeSelector{
+                                                                       NodeSelectorTerms: []v1.NodeSelectorTerm{
+                                                                               {
+                                                                                       MatchExpressions: []v1.NodeSelectorRequirement{
+                                                                                               {
+                                                                                                       Key:      "zone",
+                                                                                                       Operator: v1.NodeSelectorOpIn,
+                                                                                                       Values:   []string{"us-east-1a"},
+                                                                                               },
+                                                                                       },
+                                                                               },
+                                                                       },
+                                                               },
                                                                Attributes: map[resourceapi.QualifiedName]resourceapi.DeviceAttribute{
                                                                        "model": {StringValue: ptr.To("SOME-XZY")},
                                                                },
                                                                Capacity: map[resourceapi.QualifiedName]resourceapi.DeviceCapacity{
-                                                                       "memory": {Value: resource.MustParse("24Gi")}, // 24GB GPU - matches CEL (>= 8GB)
+                                                                       "memory": {Value: resource.MustParse("24Gi")}, // 24GB GPU - matches CEL (>= 8GiB)
                                                                },
                                                        },
                                                },
@@ -533,7 +546,7 @@ func TestCalculateResourceAllocatableRequest(t *testing.T) {
                                        Obj(),
                        },
                        podRequest:          1,
-                       expectedAllocatable: 2, // Only device-1 (matches test-node) and device-3 (matches all nodes)
+                       expectedAllocatable: 2, // device-1 matches the test node and device-3 matches via its selector
                        expectedRequested:   2, // 1 allocated (device-1) + 1 requested
                },
        }
@@ -852,6 +865,45 @@ func TestNodeMatchCaching(t *testing.T) {
        }
 }
 
+func TestNodeMatchesCacheHit(t *testing.T) {
+       scorer := &resourceAllocationScorer{
+               DRACaches: DRACaches{
+                       celCache: cel.NewCache(1, cel.Features{}),
+               },
+       }
+
+       node := st.MakeNode().Name("cache-node").Label("zone", "us-east-1a").Obj()
+       selector := &v1.NodeSelector{
+               NodeSelectorTerms: []v1.NodeSelectorTerm{
+                       {
+                               MatchExpressions: []v1.NodeSelectorRequirement{
+                                       {
+                                               Key:      "zone",
+                                               Operator: v1.NodeSelectorOpIn,
+                                               Values:   []string{"us-east-1a"},
+                                       },
+                               },
+                       },
+               },
+       }
+
+       firstMatch, err := scorer.nodeMatches(node, "", false, selector)
+       if err != nil {
+               t.Fatalf("unexpected error while evaluating selector: %v", err)
+       }
+       if !firstMatch {
+               t.Fatalf("expected nodeMatches to return true for the selector")
+       }
+
+       secondMatch, err := scorer.nodeMatches(node, "", false, selector)
+       if err != nil {
+               t.Fatalf("unexpected error while hitting cache: %v", err)
+       }
+       if !secondMatch {
+               t.Fatalf("expected cached nodeMatches to be true")
+       }
+}
+
 func BenchmarkNodeMatchCaching(b *testing.B) {
        // Create test node
        testNode := &v1.Node{