|
| 1 | +// Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + v1 "k8s.io/api/core/v1" |
| 21 | + "k8s.io/apimachinery/pkg/api/resource" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | +) |
| 24 | + |
| 25 | +func TestDiscoverAKSRdmaCount(t *testing.T) { |
| 26 | + tests := []struct { |
| 27 | + name string |
| 28 | + node v1.Node |
| 29 | + want int |
| 30 | + }{ |
| 31 | + { |
| 32 | + // Standard_ND96isr_H100_v5 with the network-operator |
| 33 | + // rdma-shared-device-plugin: allocatable is advertised as "1k" |
| 34 | + // (the shared pool size), which resource.Quantity parses as 1000. |
| 35 | + name: "ND96isr_H100_v5 with shared RDMA pool", |
| 36 | + node: v1.Node{ |
| 37 | + ObjectMeta: metav1.ObjectMeta{ |
| 38 | + Labels: map[string]string{ |
| 39 | + "node.kubernetes.io/instance-type": "Standard_ND96isr_H100_v5", |
| 40 | + }, |
| 41 | + }, |
| 42 | + Status: v1.NodeStatus{ |
| 43 | + Allocatable: v1.ResourceList{ |
| 44 | + v1.ResourceName("nvidia.com/gpu"): resource.MustParse("8"), |
| 45 | + v1.ResourceName(aksRdmaSharedResource): resource.MustParse("1k"), |
| 46 | + }, |
| 47 | + }, |
| 48 | + }, |
| 49 | + want: 1000, |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "no RDMA shared device plugin (falls back to TCP)", |
| 53 | + node: v1.Node{ |
| 54 | + Status: v1.NodeStatus{ |
| 55 | + Allocatable: v1.ResourceList{ |
| 56 | + v1.ResourceName("nvidia.com/gpu"): resource.MustParse("8"), |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + want: 0, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "no allocatable at all", |
| 64 | + node: v1.Node{}, |
| 65 | + want: 0, |
| 66 | + }, |
| 67 | + } |
| 68 | + for _, tt := range tests { |
| 69 | + t.Run(tt.name, func(t *testing.T) { |
| 70 | + if got := discoverAKSRdmaCount(tt.node); got != tt.want { |
| 71 | + t.Errorf("discoverAKSRdmaCount() = %d, want %d", got, tt.want) |
| 72 | + } |
| 73 | + }) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +func TestApplyAKSTemplateData(t *testing.T) { |
| 78 | + const rdmaLine = ` rdma/hca_shared_devices_a: "1"` |
| 79 | + tests := []struct { |
| 80 | + name string |
| 81 | + allocatable v1.ResourceList |
| 82 | + wantLine string |
| 83 | + wantMaxMsgSize string |
| 84 | + }{ |
| 85 | + { |
| 86 | + name: "shared RDMA pool present keeps IB message size", |
| 87 | + allocatable: v1.ResourceList{ |
| 88 | + v1.ResourceName("nvidia.com/gpu"): resource.MustParse("8"), |
| 89 | + v1.ResourceName(aksRdmaSharedResource): resource.MustParse("1k"), |
| 90 | + }, |
| 91 | + wantLine: rdmaLine, |
| 92 | + wantMaxMsgSize: maxMessageSize, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "no RDMA pool falls back to TCP with reduced message size", |
| 96 | + allocatable: v1.ResourceList{ |
| 97 | + v1.ResourceName("nvidia.com/gpu"): resource.MustParse("8"), |
| 98 | + }, |
| 99 | + wantLine: "", |
| 100 | + wantMaxMsgSize: maxMessageSizeTCP, |
| 101 | + }, |
| 102 | + } |
| 103 | + for _, tt := range tests { |
| 104 | + t.Run(tt.name, func(t *testing.T) { |
| 105 | + config := &gpuConfiguration{ |
| 106 | + WorkerCount: 2, |
| 107 | + GPUCountPerNode: 8, |
| 108 | + TotalGPUCount: 16, |
| 109 | + Nodes: []v1.Node{ |
| 110 | + {Status: v1.NodeStatus{Allocatable: tt.allocatable}}, |
| 111 | + {Status: v1.NodeStatus{Allocatable: tt.allocatable}}, |
| 112 | + }, |
| 113 | + } |
| 114 | + templateData := map[string]string{"MAX_MESSAGE_SIZE": maxMessageSize} |
| 115 | + applyAKSTemplateData(config, templateData) |
| 116 | + if got := templateData["RDMA_RESOURCE_LIMITS"]; got != tt.wantLine { |
| 117 | + t.Errorf("RDMA_RESOURCE_LIMITS = %q, want %q", got, tt.wantLine) |
| 118 | + } |
| 119 | + if got := templateData["RDMA_RESOURCE_REQUESTS"]; got != tt.wantLine { |
| 120 | + t.Errorf("RDMA_RESOURCE_REQUESTS = %q, want %q", got, tt.wantLine) |
| 121 | + } |
| 122 | + if got := templateData["MAX_MESSAGE_SIZE"]; got != tt.wantMaxMsgSize { |
| 123 | + t.Errorf("MAX_MESSAGE_SIZE = %q, want %q", got, tt.wantMaxMsgSize) |
| 124 | + } |
| 125 | + }) |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func TestBuildAKSRdmaResourceLine(t *testing.T) { |
| 130 | + tests := []struct { |
| 131 | + name string |
| 132 | + rdmaCount int |
| 133 | + indent string |
| 134 | + want string |
| 135 | + }{ |
| 136 | + { |
| 137 | + // A worker always requests exactly 1 unit, never the pool size: |
| 138 | + // the rdma-shared-device-plugin grants access to every shared IB |
| 139 | + // device per unit requested. |
| 140 | + name: "shared pool of 1000 requests a single unit", |
| 141 | + rdmaCount: 1000, |
| 142 | + indent: " ", |
| 143 | + want: ` rdma/hca_shared_devices_a: "1"`, |
| 144 | + }, |
| 145 | + { |
| 146 | + name: "single device still requests one unit", |
| 147 | + rdmaCount: 1, |
| 148 | + indent: " ", |
| 149 | + want: ` rdma/hca_shared_devices_a: "1"`, |
| 150 | + }, |
| 151 | + { |
| 152 | + name: "no RDMA — empty string", |
| 153 | + rdmaCount: 0, |
| 154 | + indent: " ", |
| 155 | + want: "", |
| 156 | + }, |
| 157 | + } |
| 158 | + for _, tt := range tests { |
| 159 | + t.Run(tt.name, func(t *testing.T) { |
| 160 | + got := buildAKSRdmaResourceLine(tt.rdmaCount, tt.indent) |
| 161 | + if got != tt.want { |
| 162 | + t.Errorf("buildAKSRdmaResourceLine(%d) = %q, want %q", tt.rdmaCount, got, tt.want) |
| 163 | + } |
| 164 | + }) |
| 165 | + } |
| 166 | +} |
0 commit comments