|
| 1 | +// /* |
| 2 | +// Copyright 2025 The Grove 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 mnnvl |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + grovecorev1alpha1 "github.com/ai-dynamo/grove/operator/api/core/v1alpha1" |
| 23 | + "github.com/ai-dynamo/grove/operator/internal/constants" |
| 24 | + testutils "github.com/ai-dynamo/grove/operator/test/utils" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + corev1 "k8s.io/api/core/v1" |
| 28 | + "k8s.io/apimachinery/pkg/api/resource" |
| 29 | +) |
| 30 | + |
| 31 | +func Test_hasGPURequirement(t *testing.T) { |
| 32 | + tests := []struct { |
| 33 | + name string |
| 34 | + pcs *grovecorev1alpha1.PodCliqueSet |
| 35 | + expected bool |
| 36 | + }{ |
| 37 | + { |
| 38 | + name: "container with GPU limits", |
| 39 | + pcs: createPCSWithGPU(nil), |
| 40 | + expected: true, |
| 41 | + }, |
| 42 | + { |
| 43 | + name: "container without GPU", |
| 44 | + pcs: createPCSWithoutGPU(nil), |
| 45 | + expected: false, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "empty cliques", |
| 49 | + pcs: &grovecorev1alpha1.PodCliqueSet{}, |
| 50 | + expected: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "GPU in init container", |
| 54 | + pcs: testutils.NewPodCliqueSetBuilder("test-pcs", "default", ""). |
| 55 | + WithPodCliqueTemplateSpec( |
| 56 | + testutils.NewPodCliqueTemplateSpecBuilder("worker"). |
| 57 | + WithInitContainer(corev1.Container{ |
| 58 | + Name: "init", |
| 59 | + Resources: corev1.ResourceRequirements{ |
| 60 | + Limits: corev1.ResourceList{ |
| 61 | + constants.GPUResourceName: resource.MustParse("1"), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }). |
| 65 | + Build(), |
| 66 | + ). |
| 67 | + Build(), |
| 68 | + expected: true, |
| 69 | + }, |
| 70 | + { |
| 71 | + name: "GPU in requests not limits", |
| 72 | + pcs: testutils.NewPodCliqueSetBuilder("test-pcs", "default", ""). |
| 73 | + WithPodCliqueTemplateSpec( |
| 74 | + testutils.NewPodCliqueTemplateSpecBuilder("worker"). |
| 75 | + WithContainer(corev1.Container{ |
| 76 | + Name: "train", |
| 77 | + Resources: corev1.ResourceRequirements{ |
| 78 | + Requests: corev1.ResourceList{ |
| 79 | + constants.GPUResourceName: resource.MustParse("2"), |
| 80 | + }, |
| 81 | + }, |
| 82 | + }). |
| 83 | + Build(), |
| 84 | + ). |
| 85 | + Build(), |
| 86 | + expected: true, |
| 87 | + }, |
| 88 | + { |
| 89 | + name: "GPU with zero quantity", |
| 90 | + pcs: testutils.NewPodCliqueSetBuilder("test-pcs", "default", ""). |
| 91 | + WithPodCliqueTemplateSpec( |
| 92 | + testutils.NewPodCliqueTemplateSpecBuilder("worker"). |
| 93 | + WithContainer(corev1.Container{ |
| 94 | + Name: "train", |
| 95 | + Resources: corev1.ResourceRequirements{ |
| 96 | + Limits: corev1.ResourceList{ |
| 97 | + constants.GPUResourceName: resource.MustParse("0"), |
| 98 | + }, |
| 99 | + }, |
| 100 | + }). |
| 101 | + Build(), |
| 102 | + ). |
| 103 | + Build(), |
| 104 | + expected: false, |
| 105 | + }, |
| 106 | + { |
| 107 | + name: "multiple cliques - one with GPU", |
| 108 | + pcs: testutils.NewPodCliqueSetBuilder("test-pcs", "default", ""). |
| 109 | + WithPodCliqueTemplateSpec( |
| 110 | + testutils.NewPodCliqueTemplateSpecBuilder("controller"). |
| 111 | + WithContainer(testutils.NewContainer("ctrl", "busybox")). |
| 112 | + Build(), |
| 113 | + ). |
| 114 | + WithPodCliqueTemplateSpec( |
| 115 | + testutils.NewPodCliqueTemplateSpecBuilder("worker"). |
| 116 | + WithContainer(testutils.NewGPUContainer("train", "nvidia/cuda:latest", 8)). |
| 117 | + Build(), |
| 118 | + ). |
| 119 | + Build(), |
| 120 | + expected: true, |
| 121 | + }, |
| 122 | + } |
| 123 | + |
| 124 | + for _, test := range tests { |
| 125 | + t.Run(test.name, func(t *testing.T) { |
| 126 | + result := hasGPURequirement(test.pcs) |
| 127 | + assert.Equal(t, test.expected, result) |
| 128 | + }) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +// createPCSWithGPU creates a PCS with GPU using the builder for tests in this package. |
| 133 | +func createPCSWithGPU(annotations map[string]string) *grovecorev1alpha1.PodCliqueSet { |
| 134 | + return testutils.NewPodCliqueSetBuilder("test-pcs", "default", ""). |
| 135 | + WithAnnotations(annotations). |
| 136 | + WithPodCliqueTemplateSpec( |
| 137 | + testutils.NewPodCliqueTemplateSpecBuilder("worker"). |
| 138 | + WithContainer(testutils.NewGPUContainer("train", "nvidia/cuda:latest", 8)). |
| 139 | + Build(), |
| 140 | + ). |
| 141 | + Build() |
| 142 | +} |
| 143 | + |
| 144 | +// createPCSWithoutGPU creates a PCS without GPU using the builder for tests in this package. |
| 145 | +func createPCSWithoutGPU(annotations map[string]string) *grovecorev1alpha1.PodCliqueSet { |
| 146 | + return testutils.NewPodCliqueSetBuilder("test-pcs", "default", ""). |
| 147 | + WithAnnotations(annotations). |
| 148 | + WithPodCliqueTemplateSpec( |
| 149 | + testutils.NewPodCliqueTemplateSpecBuilder("worker"). |
| 150 | + WithContainer(testutils.NewContainer("app", "nginx:latest")). |
| 151 | + Build(), |
| 152 | + ). |
| 153 | + Build() |
| 154 | +} |
0 commit comments