-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathgeneric_test.go
More file actions
370 lines (341 loc) · 11.2 KB
/
generic_test.go
File metadata and controls
370 lines (341 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
// Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package component
import (
"testing"
"github.com/NVIDIA/aicr/pkg/bundler/config"
"github.com/NVIDIA/aicr/pkg/recipe"
)
func TestEnrichConfigFromRegistry(t *testing.T) {
// Get registry to verify expected values
registry, err := recipe.GetComponentRegistry()
if err != nil {
t.Fatalf("failed to load component registry: %v", err)
}
t.Run("enriches empty config from registry", func(t *testing.T) {
// Start with minimal config (only Name)
cfg := ComponentConfig{
Name: "gpu-operator",
}
enrichConfigFromRegistry(&cfg)
// Verify values were populated from registry
comp := registry.Get("gpu-operator")
if comp == nil {
t.Fatal("gpu-operator not found in registry")
}
if cfg.DisplayName == "" {
t.Error("DisplayName should be populated from registry")
}
if cfg.DisplayName != comp.DisplayName {
t.Errorf("DisplayName = %q, want %q", cfg.DisplayName, comp.DisplayName)
}
if len(cfg.ValueOverrideKeys) == 0 {
t.Error("ValueOverrideKeys should be populated from registry")
}
if cfg.DefaultHelmRepository == "" {
t.Error("DefaultHelmRepository should be populated from registry")
}
if cfg.DefaultHelmChart == "" {
t.Error("DefaultHelmChart should be populated from registry")
}
})
t.Run("does not override existing values", func(t *testing.T) {
// Start with fully populated config
cfg := ComponentConfig{
Name: "gpu-operator",
DisplayName: "Custom Display Name",
ValueOverrideKeys: []string{"custom-key"},
DefaultHelmRepository: "https://custom.repo",
DefaultHelmChart: "custom/chart",
SystemNodeSelectorPaths: []string{"custom.path"},
}
enrichConfigFromRegistry(&cfg)
// Verify existing values were preserved
if cfg.DisplayName != "Custom Display Name" {
t.Errorf("DisplayName should be preserved, got %q", cfg.DisplayName)
}
if len(cfg.ValueOverrideKeys) != 1 || cfg.ValueOverrideKeys[0] != "custom-key" {
t.Errorf("ValueOverrideKeys should be preserved, got %v", cfg.ValueOverrideKeys)
}
if cfg.DefaultHelmRepository != "https://custom.repo" {
t.Errorf("DefaultHelmRepository should be preserved, got %q", cfg.DefaultHelmRepository)
}
if cfg.DefaultHelmChart != "custom/chart" {
t.Errorf("DefaultHelmChart should be preserved, got %q", cfg.DefaultHelmChart)
}
if len(cfg.SystemNodeSelectorPaths) != 1 || cfg.SystemNodeSelectorPaths[0] != "custom.path" {
t.Errorf("SystemNodeSelectorPaths should be preserved, got %v", cfg.SystemNodeSelectorPaths)
}
})
t.Run("unknown component is unchanged", func(t *testing.T) {
cfg := ComponentConfig{
Name: "unknown-component",
DisplayName: "",
}
enrichConfigFromRegistry(&cfg)
// Verify nothing changed
if cfg.DisplayName != "" {
t.Errorf("DisplayName should remain empty for unknown component, got %q", cfg.DisplayName)
}
})
t.Run("enriches node scheduling paths", func(t *testing.T) {
cfg := ComponentConfig{
Name: "gpu-operator",
}
enrichConfigFromRegistry(&cfg)
// gpu-operator should have scheduling paths
if len(cfg.SystemNodeSelectorPaths) == 0 {
t.Error("SystemNodeSelectorPaths should be populated")
}
if len(cfg.SystemTolerationPaths) == 0 {
t.Error("SystemTolerationPaths should be populated")
}
if len(cfg.AcceleratedNodeSelectorPaths) == 0 {
t.Error("AcceleratedNodeSelectorPaths should be populated")
}
if len(cfg.AcceleratedTolerationPaths) == 0 {
t.Error("AcceleratedTolerationPaths should be populated")
}
})
}
func TestGenerateDefaultBundleMetadata(t *testing.T) {
tests := []struct {
name string
config map[string]string
componentName string
defaultHelmRepo string
defaultHelmChart string
wantNamespace string
wantHelmRepo string
wantHelmChart string
wantVersion string
}{
{
name: "with all config values",
config: map[string]string{"namespace": "custom-ns", "helm_repository": "https://custom.repo", "helm_chart_version": "v1.0.0", "bundler_version": "1.2.3", "recipe_version": "2.0.0"},
componentName: "test-component",
defaultHelmRepo: "https://default.repo",
defaultHelmChart: "default/chart",
wantNamespace: "custom-ns",
wantHelmRepo: "https://custom.repo",
wantHelmChart: "default/chart",
wantVersion: "1.2.3",
},
{
name: "with empty config uses defaults",
config: map[string]string{},
componentName: "my-component",
defaultHelmRepo: "https://default.repo",
defaultHelmChart: "nvidia/operator",
wantNamespace: "my-component",
wantHelmRepo: "https://default.repo",
wantHelmChart: "nvidia/operator",
wantVersion: "unknown", // GetBundlerVersion returns "unknown" when not set
},
{
name: "with nil config",
config: nil,
componentName: "test",
defaultHelmRepo: "https://repo.io",
defaultHelmChart: "chart",
wantNamespace: "test",
wantHelmRepo: "https://repo.io",
wantHelmChart: "chart",
wantVersion: "unknown", // GetBundlerVersion returns "unknown" when not set
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GenerateDefaultBundleMetadata(tt.config, tt.componentName, tt.defaultHelmRepo, tt.defaultHelmChart)
if got == nil {
t.Fatal("GenerateDefaultBundleMetadata returned nil")
}
if got.Namespace != tt.wantNamespace {
t.Errorf("Namespace = %q, want %q", got.Namespace, tt.wantNamespace)
}
if got.HelmRepository != tt.wantHelmRepo {
t.Errorf("HelmRepository = %q, want %q", got.HelmRepository, tt.wantHelmRepo)
}
if got.HelmChart != tt.wantHelmChart {
t.Errorf("HelmChart = %q, want %q", got.HelmChart, tt.wantHelmChart)
}
if got.HelmReleaseName != tt.componentName {
t.Errorf("HelmReleaseName = %q, want %q", got.HelmReleaseName, tt.componentName)
}
if got.Version != tt.wantVersion {
t.Errorf("Version = %q, want %q", got.Version, tt.wantVersion)
}
if got.Extensions == nil {
t.Error("Extensions map should not be nil")
}
})
}
}
func TestGenerateBundleMetadataWithExtensions(t *testing.T) {
tests := []struct {
name string
config map[string]string
componentConfig ComponentConfig
wantHelmChartVersion string
wantExtensionCount int
wantExtensionKey string
wantExtensionValue any
}{
{
name: "with extensions and default version",
config: map[string]string{},
componentConfig: ComponentConfig{
Name: "test-component",
DefaultHelmRepository: "https://repo.io",
DefaultHelmChart: "chart",
DefaultHelmChartVersion: "v1.2.3",
MetadataExtensions: map[string]any{
"CustomField": "custom-value",
"Enabled": true,
},
},
wantHelmChartVersion: "v1.2.3",
wantExtensionCount: 2,
wantExtensionKey: "CustomField",
wantExtensionValue: "custom-value",
},
{
name: "config version overrides default",
config: map[string]string{"helm_chart_version": "v2.0.0"},
componentConfig: ComponentConfig{
Name: "test",
DefaultHelmRepository: "https://repo.io",
DefaultHelmChart: "chart",
DefaultHelmChartVersion: "v1.0.0",
},
wantHelmChartVersion: "v2.0.0",
wantExtensionCount: 0,
},
{
name: "no extensions",
config: map[string]string{},
componentConfig: ComponentConfig{
Name: "test",
DefaultHelmRepository: "https://repo.io",
DefaultHelmChart: "chart",
},
wantHelmChartVersion: "",
wantExtensionCount: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := GenerateBundleMetadataWithExtensions(tt.config, tt.componentConfig)
if got == nil {
t.Fatal("GenerateBundleMetadataWithExtensions returned nil")
}
if got.HelmChartVersion != tt.wantHelmChartVersion {
t.Errorf("HelmChartVersion = %q, want %q", got.HelmChartVersion, tt.wantHelmChartVersion)
}
if len(got.Extensions) != tt.wantExtensionCount {
t.Errorf("Extensions count = %d, want %d", len(got.Extensions), tt.wantExtensionCount)
}
if tt.wantExtensionKey != "" {
if val, ok := got.Extensions[tt.wantExtensionKey]; !ok {
t.Errorf("Extension %q not found", tt.wantExtensionKey)
} else if val != tt.wantExtensionValue {
t.Errorf("Extension[%q] = %v, want %v", tt.wantExtensionKey, val, tt.wantExtensionValue)
}
}
})
}
}
func TestGetValueOverridesForComponent(t *testing.T) {
tests := []struct {
name string
configOverrides map[string]map[string]string
componentConfig ComponentConfig
wantOverrides map[string]string
}{
{
name: "finds by component name",
configOverrides: map[string]map[string]string{
"gpu-operator": {"driver.version": "550.0.0"},
},
componentConfig: ComponentConfig{
Name: "gpu-operator",
ValueOverrideKeys: []string{"gpuoperator"},
},
wantOverrides: map[string]string{"driver.version": "550.0.0"},
},
{
name: "finds by alternative key",
configOverrides: map[string]map[string]string{
"gpuoperator": {"mig.strategy": "mixed"},
},
componentConfig: ComponentConfig{
Name: "gpu-operator",
ValueOverrideKeys: []string{"gpuoperator", "gpu"},
},
wantOverrides: map[string]string{"mig.strategy": "mixed"},
},
{
name: "component name takes priority over alternative keys",
configOverrides: map[string]map[string]string{
"gpu-operator": {"version": "1.0.0"},
"gpuoperator": {"version": "2.0.0"},
},
componentConfig: ComponentConfig{
Name: "gpu-operator",
ValueOverrideKeys: []string{"gpuoperator"},
},
wantOverrides: map[string]string{"version": "1.0.0"},
},
{
name: "nil overrides returns nil",
configOverrides: nil,
componentConfig: ComponentConfig{
Name: "test",
},
wantOverrides: nil,
},
{
name: "no matching key returns nil",
configOverrides: map[string]map[string]string{
"other-component": {"key": "value"},
},
componentConfig: ComponentConfig{
Name: "test-component",
ValueOverrideKeys: []string{"test"},
},
wantOverrides: nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cfg := config.NewConfig(config.WithValueOverrides(tt.configOverrides))
b := NewBaseBundler(cfg, "test")
got := getValueOverridesForComponent(b, tt.componentConfig)
if tt.wantOverrides == nil {
if got != nil {
t.Errorf("got %v, want nil", got)
}
return
}
if got == nil {
t.Fatal("got nil, want non-nil")
}
for k, v := range tt.wantOverrides {
if got[k] != v {
t.Errorf("got[%q] = %q, want %q", k, got[k], v)
}
}
})
}
}