-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtesting_test.go
More file actions
352 lines (301 loc) · 9.06 KB
/
testing_test.go
File metadata and controls
352 lines (301 loc) · 9.06 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
// 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 (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/NVIDIA/aicr/pkg/bundler/result"
"github.com/NVIDIA/aicr/pkg/measurement"
"github.com/NVIDIA/aicr/pkg/recipe"
)
// Mock bundler for testing
type mockBundler struct {
makeFunc func(ctx context.Context, input recipe.RecipeInput, outputDir string) (*result.Result, error)
}
func (m *mockBundler) Make(ctx context.Context, input recipe.RecipeInput, outputDir string) (*result.Result, error) {
if m.makeFunc != nil {
return m.makeFunc(ctx, input, outputDir)
}
res := result.New("mock")
res.AddFile(filepath.Join(outputDir, "mock", "test.txt"), 100)
res.Success = true
return res, nil
}
func TestTestHarness_NewTestHarness(t *testing.T) {
h := NewTestHarness(t, "test-bundler")
if h == nil {
t.Fatal("NewTestHarness() returned nil")
}
if h.bundlerName != "test-bundler" {
t.Errorf("bundlerName = %s, want test-bundler", h.bundlerName)
}
if h.expectedFiles == nil {
t.Error("expectedFiles should be initialized")
}
}
func TestTestHarness_WithExpectedFiles(t *testing.T) {
files := []string{"file1.txt", "file2.yaml"}
h := NewTestHarness(t, "test").WithExpectedFiles(files)
if len(h.expectedFiles) != 2 {
t.Errorf("expectedFiles length = %d, want 2", len(h.expectedFiles))
}
}
func TestTestHarness_WithRecipeBuilder(t *testing.T) {
h := NewTestHarness(t, "test").WithRecipeBuilder(NewRecipeBuilder)
if h.recipeBuilder == nil {
t.Error("recipeBuilder should be set")
}
}
func TestTestHarness_TestMake(t *testing.T) {
tmpDir := t.TempDir()
// Create mock bundler that creates expected files
mock := &mockBundler{
makeFunc: func(ctx context.Context, input recipe.RecipeInput, outputDir string) (*result.Result, error) {
bundleDir := filepath.Join(outputDir, "test-bundler")
os.MkdirAll(bundleDir, 0755)
os.WriteFile(filepath.Join(bundleDir, "test.txt"), []byte("test"), 0644)
res := result.New("test-bundler")
res.AddFile(filepath.Join(bundleDir, "test.txt"), 4)
res.Success = true
return res, nil
},
}
h := NewTestHarness(t, "test-bundler").
WithExpectedFiles([]string{"test.txt"}).
WithRecipeBuilder(func() *RecipeBuilder {
return NewRecipeBuilder().WithK8sMeasurement(
ConfigSubtype(map[string]any{"version": "1.28.0"}),
)
})
// Create a custom testMake function for testing
testMakeFunc := func(bundler BundlerInterface) {
ctx := context.Background()
rec := h.getRecipe()
result, err := bundler.Make(ctx, rec, tmpDir)
if err != nil {
t.Fatalf("Make() error = %v", err)
}
h.AssertResult(result, tmpDir)
}
testMakeFunc(mock)
}
func TestTestHarness_AssertResult(t *testing.T) {
tmpDir := t.TempDir()
bundleDir := filepath.Join(tmpDir, "test-bundler")
os.MkdirAll(bundleDir, 0755)
os.WriteFile(filepath.Join(bundleDir, "test.txt"), []byte("test"), 0644)
res := result.New("test-bundler")
res.AddFile(filepath.Join(bundleDir, "test.txt"), 4)
res.Success = true
h := NewTestHarness(t, "test-bundler").
WithExpectedFiles([]string{"test.txt"})
// Should not panic
h.AssertResult(res, tmpDir)
}
func TestTestHarness_AssertFileExists(t *testing.T) {
tmpDir := t.TempDir()
bundleDir := filepath.Join(tmpDir, "test-bundler")
os.MkdirAll(bundleDir, 0755)
os.WriteFile(filepath.Join(bundleDir, "test.txt"), []byte("test"), 0644)
h := NewTestHarness(t, "test-bundler")
// Should not panic for existing file
h.AssertFileExists(tmpDir, "test.txt")
}
func TestTestHarness_getRecipe(t *testing.T) {
t.Run("with custom builder", func(t *testing.T) {
h := NewTestHarness(t, "test").
WithRecipeBuilder(func() *RecipeBuilder {
return NewRecipeBuilder().WithK8sMeasurement(
ConfigSubtype(map[string]any{"custom": "value"}),
)
})
rec := h.getRecipe()
if rec == nil {
t.Fatal("getRecipe() returned nil")
}
if len(rec.Measurements) == 0 {
t.Error("Recipe should have measurements")
}
})
t.Run("with default recipe", func(t *testing.T) {
h := NewTestHarness(t, "test")
rec := h.getRecipe()
if rec == nil {
t.Fatal("getRecipe() returned nil")
}
})
}
func TestTestTemplateGetter(t *testing.T) {
templates := map[string]string{
"test1": "content1",
"test2": "content2",
}
getter := func(name string) (string, bool) {
tmpl, ok := templates[name]
return tmpl, ok
}
TestTemplateGetter(t, getter, []string{"test1", "test2"})
}
func TestRecipeBuilder_NewRecipeBuilder(t *testing.T) {
rb := NewRecipeBuilder()
if rb == nil {
t.Fatal("NewRecipeBuilder() returned nil")
}
if rb.measurements == nil {
t.Error("measurements should be initialized")
}
}
func TestRecipeBuilder_WithK8sMeasurement(t *testing.T) {
rb := NewRecipeBuilder().
WithK8sMeasurement(
ConfigSubtype(map[string]any{"version": "1.28.0"}),
)
rec := rb.Build()
if len(rec.Measurements) != 1 {
t.Errorf("measurements length = %d, want 1", len(rec.Measurements))
}
if rec.Measurements[0].Type != measurement.TypeK8s {
t.Errorf("measurement type = %v, want TypeK8s", rec.Measurements[0].Type)
}
}
func TestRecipeBuilder_WithGPUMeasurement(t *testing.T) {
rb := NewRecipeBuilder().
WithGPUMeasurement(
SMISubtype(map[string]string{"driver": "580"}),
)
rec := rb.Build()
if len(rec.Measurements) != 1 {
t.Errorf("measurements length = %d, want 1", len(rec.Measurements))
}
if rec.Measurements[0].Type != measurement.TypeGPU {
t.Errorf("measurement type = %v, want TypeGPU", rec.Measurements[0].Type)
}
}
func TestRecipeBuilder_WithOSMeasurement(t *testing.T) {
rb := NewRecipeBuilder().
WithOSMeasurement(
ConfigSubtype(map[string]any{"kernel": "5.15"}),
)
rec := rb.Build()
if len(rec.Measurements) != 1 {
t.Errorf("measurements length = %d, want 1", len(rec.Measurements))
}
if rec.Measurements[0].Type != measurement.TypeOS {
t.Errorf("measurement type = %v, want TypeOS", rec.Measurements[0].Type)
}
}
func TestRecipeBuilder_WithSystemDMeasurement(t *testing.T) {
rb := NewRecipeBuilder().
WithSystemDMeasurement(
ConfigSubtype(map[string]any{"service": "containerd"}),
)
rec := rb.Build()
if len(rec.Measurements) != 1 {
t.Errorf("measurements length = %d, want 1", len(rec.Measurements))
}
if rec.Measurements[0].Type != measurement.TypeSystemD {
t.Errorf("measurement type = %v, want TypeSystemD", rec.Measurements[0].Type)
}
}
func TestRecipeBuilder_Build(t *testing.T) {
rb := NewRecipeBuilder().
WithK8sMeasurement(
ConfigSubtype(map[string]any{"version": "1.28.0"}),
).
WithGPUMeasurement(
SMISubtype(map[string]string{"driver": "580"}),
)
rec := rb.Build()
if rec == nil {
t.Fatal("Build() returned nil")
}
if len(rec.Measurements) != 2 {
t.Errorf("measurements length = %d, want 2", len(rec.Measurements))
}
}
func TestImageSubtype(t *testing.T) {
images := map[string]string{
"gpu-operator": "v25.3.3",
"driver": "580.82.07",
}
subtype := ImageSubtype(images)
if subtype.Name != "image" {
t.Errorf("subtype name = %s, want image", subtype.Name)
}
if len(subtype.Data) != 2 {
t.Errorf("data length = %d, want 2", len(subtype.Data))
}
}
func TestConfigSubtype(t *testing.T) {
configs := map[string]any{
"string_val": "test",
"bool_val": true,
"int_val": 42,
"float_val": 3.14,
}
subtype := ConfigSubtype(configs)
if subtype.Name != "config" {
t.Errorf("subtype name = %s, want config", subtype.Name)
}
if len(subtype.Data) != 4 {
t.Errorf("data length = %d, want 4", len(subtype.Data))
}
// Verify types are preserved
if val, ok := subtype.Data["string_val"]; ok {
if v, ok := val.Any().(string); !ok || v != "test" {
t.Error("string_val should be string type")
}
}
if val, ok := subtype.Data["bool_val"]; ok {
if v, ok := val.Any().(bool); !ok || !v {
t.Error("bool_val should be bool type")
}
}
}
func TestSMISubtype(t *testing.T) {
data := map[string]string{
"driver-version": "580.82.07",
"cuda-version": "13.1",
}
subtype := SMISubtype(data)
if subtype.Name != "smi" {
t.Errorf("subtype name = %s, want smi", subtype.Name)
}
if len(subtype.Data) != 2 {
t.Errorf("data length = %d, want 2", len(subtype.Data))
}
}
func TestTestValidateRecipe(t *testing.T) {
validateFunc := func(r *recipe.Recipe) error {
if r == nil {
return fmt.Errorf("recipe is nil")
}
if len(r.Measurements) == 0 {
return fmt.Errorf("recipe has empty measurements")
}
return nil
}
TestValidateRecipe(t, validateFunc)
}
func TestAssertConfigValue(t *testing.T) {
config := map[string]string{
"key1": "value1",
"key2": "value2",
}
// Should not panic for correct value
AssertConfigValue(t, config, "key1", "value1")
}