-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathresolver_test.go
More file actions
544 lines (505 loc) · 14.3 KB
/
resolver_test.go
File metadata and controls
544 lines (505 loc) · 14.3 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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
package rpmutils
import (
"bytes"
"compress/gzip"
"fmt"
"net/http"
"net/http/httptest"
"os"
"path/filepath"
"strings"
"testing"
"github.com/open-edge-platform/os-image-composer/internal/config"
"github.com/open-edge-platform/os-image-composer/internal/ospackage"
"github.com/open-edge-platform/os-image-composer/internal/ospackage/resolvertest"
)
func TestRPMResolver(t *testing.T) {
resolvertest.RunResolverTestsFunc(
t,
"rpmutils",
ResolveDependencies, // directly passing your function
)
}
func TestExtractBaseRequirement(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "simple requirement",
input: "bash",
expected: "bash",
},
{
name: "requirement with version",
input: "glibc >= 2.30",
expected: "glibc",
},
{
name: "complex requirement with parentheses",
input: "(libssl.so.1.1 >= 1.1.0)",
expected: "libssl.so.1.1",
},
{
name: "requirement with 64bit suffix",
input: "libpthread.so.0()(64bit)",
expected: "libpthread.so.0",
},
{
name: "complex requirement with multiple conditions",
input: "(gcc-c++ and make)",
expected: "gcc-c++",
},
{
name: "empty string",
input: "",
expected: "",
},
{
name: "whitespace only",
input: " ",
expected: "",
},
{
name: "requirement with complex versioning",
input: "python3-devel >= 3.8.0",
expected: "python3-devel",
},
{
name: "parentheses with spaces",
input: "( openssl-libs )",
expected: "openssl-libs",
},
{
name: "file path requirement",
input: "/bin/sh",
expected: "/bin/sh",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := extractBaseRequirement(tt.input)
if result != tt.expected {
t.Errorf("extractBaseRequirement(%q) = %q, want %q", tt.input, result, tt.expected)
}
})
}
}
func TestGenerateDot(t *testing.T) {
// Create temporary directory for test files
tmpDir, err := os.MkdirTemp("", "dot_test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(tmpDir)
tests := []struct {
name string
packages []ospackage.PackageInfo
filename string
pkgSources map[string]config.PackageSource
expectError bool
}{
{
name: "simple package with dependencies",
packages: []ospackage.PackageInfo{
{
Name: "bash",
Requires: []string{"glibc", "ncurses"},
},
{
Name: "glibc",
Requires: []string{},
},
{
Name: "ncurses",
Requires: []string{"glibc"},
},
},
filename: filepath.Join(tmpDir, "simple.dot"),
expectError: false,
},
{
name: "empty package list",
packages: []ospackage.PackageInfo{},
filename: filepath.Join(tmpDir, "empty.dot"),
expectError: false,
},
{
name: "package with no dependencies",
packages: []ospackage.PackageInfo{
{
Name: "standalone",
Requires: []string{},
},
},
filename: filepath.Join(tmpDir, "standalone.dot"),
expectError: false,
},
{
name: "packages with special characters in names",
packages: []ospackage.PackageInfo{
{
Name: "package-with-dashes",
Requires: []string{"lib.so.1"},
},
{
Name: "lib.so.1",
Requires: []string{},
},
},
filename: filepath.Join(tmpDir, "special_chars.dot"),
expectError: false,
},
{
name: "with package source colors",
packages: []ospackage.PackageInfo{
{Name: "kernel", Requires: []string{}},
{Name: "boot", Requires: []string{}},
},
filename: filepath.Join(tmpDir, "sources.dot"),
pkgSources: map[string]config.PackageSource{
"kernel": config.PackageSourceKernel,
"boot": config.PackageSourceBootloader,
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := GenerateDot(tt.packages, tt.filename, tt.pkgSources)
if tt.expectError {
if err == nil {
t.Error("Expected error, but got none")
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
// Verify the file was created and has expected content
content, err := os.ReadFile(tt.filename)
if err != nil {
t.Fatalf("Failed to read generated file: %v", err)
}
contentStr := string(content)
// Check basic DOT structure
if !strings.Contains(contentStr, "digraph G {") {
t.Error("DOT file should start with 'digraph G {'")
}
if !strings.Contains(contentStr, "rankdir=LR;") {
t.Error("DOT file should contain 'rankdir=LR;'")
}
if !strings.Contains(contentStr, "}") {
t.Error("DOT file should end with '}'")
}
// Check that all packages are represented
for _, pkg := range tt.packages {
nodePrefix := fmt.Sprintf("\"%s\" [label=\"%s\"", pkg.Name, pkg.Name)
if !strings.Contains(contentStr, nodePrefix) {
t.Errorf("DOT file should contain node definition for %s", pkg.Name)
}
// Check dependencies
for _, dep := range pkg.Requires {
expectedEdge := fmt.Sprintf("\"%s\" -> \"%s\";", pkg.Name, dep)
if !strings.Contains(contentStr, expectedEdge) {
t.Errorf("DOT file should contain edge: %s", expectedEdge)
}
}
}
if tt.pkgSources != nil {
if !strings.Contains(contentStr, "legend_kernel") {
t.Errorf("legend for kernel packages not found")
}
if !strings.Contains(contentStr, "\"kernel\" [label=\"kernel\", fillcolor=\"#d6eaf8\", color=\"#1f618d\"];") {
t.Errorf("expected kernel node styling")
}
if !strings.Contains(contentStr, "\"boot\" [label=\"boot\", fillcolor=\"#fdebd0\", color=\"#d35400\"];") {
t.Errorf("expected bootloader node styling")
}
}
})
}
}
func TestParsePrimary(t *testing.T) {
tests := []struct {
name string
xmlContent string
filename string
expectedError bool
expectedCount int
expectedNames []string
}{
{
name: "simple gzipped XML",
xmlContent: `<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://linux.duke.edu/metadata/common" xmlns:rpm="http://linux.duke.edu/metadata/rpm" packages="2"><package type="rpm"><name>bash</name><arch>x86_64</arch><location href="bash-5.1-8.el9.x86_64.rpm"/><format><rpm:license>GPLv3+</rpm:license><rpm:vendor>Red Hat, Inc.</rpm:vendor><rpm:provides><rpm:entry name="bash"/></rpm:provides><rpm:requires><rpm:entry name="glibc"/></rpm:requires></format></package><package type="rpm"><name>glibc</name><arch>x86_64</arch><location href="glibc-2.32-1.el9.x86_64.rpm"/><format><rpm:license>LGPLv2+</rpm:license><rpm:vendor>Red Hat, Inc.</rpm:vendor><rpm:provides><rpm:entry name="glibc"/></rpm:provides></format></package></metadata>`,
filename: "primary.xml.gz",
expectedError: false,
expectedCount: 2,
expectedNames: []string{"bash", "glibc"},
},
{
name: "empty metadata",
xmlContent: `<?xml version="1.0" encoding="UTF-8"?><metadata xmlns="http://linux.duke.edu/metadata/common" packages="0"></metadata>`,
filename: "empty.xml.gz",
expectedError: false,
expectedCount: 0,
expectedNames: []string{},
},
{
name: "invalid compression",
xmlContent: "dummy content",
filename: "primary.xml.bz2",
expectedError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Create a test server that serves the XML content
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Set appropriate content type and serve compressed content
if strings.HasSuffix(tt.filename, ".gz") {
w.Header().Set("Content-Type", "application/gzip")
// Compress the content properly
content := compressGzip(t, tt.xmlContent)
_, _ = w.Write(content)
} else {
w.Header().Set("Content-Type", "text/xml")
_, _ = w.Write([]byte(tt.xmlContent))
}
}))
defer server.Close()
// Test ParseRepositoryMetadata
packages, err := ParseRepositoryMetadata(server.URL+"/", tt.filename)
if tt.expectedError {
if err == nil {
t.Error("Expected error, but got none")
}
return
}
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if len(packages) != tt.expectedCount {
t.Errorf("Expected %d packages, got %d", tt.expectedCount, len(packages))
}
// Check that expected packages are present
foundNames := make(map[string]bool)
for _, pkg := range packages {
foundNames[pkg.Name] = true
// Verify the package has the expected fields
if pkg.Type != "rpm" {
t.Errorf("Package %s should have type 'rpm', got %s", pkg.Name, pkg.Type)
}
if pkg.URL == "" {
t.Errorf("Package %s should have URL set", pkg.Name)
}
}
for _, expectedName := range tt.expectedNames {
if !foundNames[expectedName] {
t.Errorf("Expected package %s not found", expectedName)
}
}
// Additional checks for the first test case
if tt.name == "simple gzipped XML" && len(packages) >= 2 {
// Check bash package details
var bashPkg *ospackage.PackageInfo
for _, pkg := range packages {
if pkg.Name == "bash" {
bashPkg = &pkg
break
}
}
if bashPkg == nil {
t.Fatal("bash package not found")
}
if bashPkg.License != "GPLv3+" {
t.Errorf("bash license should be 'GPLv3+', got %s", bashPkg.License)
}
if bashPkg.Origin != "Red Hat, Inc." {
t.Errorf("bash origin should be 'Red Hat, Inc.', got %s", bashPkg.Origin)
}
}
})
}
}
// Helper function to compress content with gzip
func compressGzip(t *testing.T, content string) []byte {
t.Helper()
var buf bytes.Buffer
writer := gzip.NewWriter(&buf)
_, err := writer.Write([]byte(content))
if err != nil {
t.Fatal(err)
}
err = writer.Close()
if err != nil {
t.Fatal(err)
}
return buf.Bytes()
}
// TestMatchRequestedAdvanced tests advanced scenarios for MatchRequested function
func TestMatchRequestedAdvanced(t *testing.T) {
testPackages := []ospackage.PackageInfo{
{
Name: "curl",
Version: "8.8.0-2.azl3",
Arch: "x86_64",
URL: "https://repo.example.com/curl-8.8.0-2.azl3.x86_64.rpm",
},
{
Name: "curl-devel",
Version: "8.8.0-2.azl3",
Arch: "x86_64",
URL: "https://repo.example.com/curl-devel-8.8.0-2.azl3.x86_64.rpm",
},
{
Name: "curl",
Version: "7.8.0-1.azl3",
Arch: "x86_64",
URL: "https://repo.example.com/curl-7.8.0-1.azl3.x86_64.rpm",
},
{
Name: "libcurl",
Version: "8.8.0-2.azl3",
Arch: "x86_64",
URL: "https://repo.example.com/libcurl-8.8.0-2.azl3.x86_64.rpm",
},
{
Name: "python3-curl",
Version: "1.0-1.azl3",
Arch: "noarch",
URL: "https://repo.example.com/python3-curl-1.0-1.azl3.noarch.rpm",
},
{
Name: "package-with-src",
Version: "1.0-1",
Arch: "src",
URL: "https://repo.example.com/package-with-src-1.0-1.src.rpm",
},
}
tests := []struct {
name string
requests []string
expectError bool
expectedCount int
expectedNames []string
expectedArch string
}{
{
name: "Multiple package requests",
requests: []string{"curl", "libcurl"},
expectError: false,
expectedCount: 2,
expectedNames: []string{"curl", "libcurl"},
},
{
name: "Request with devel package",
requests: []string{"curl-devel"},
expectError: false,
expectedCount: 1,
expectedNames: []string{"curl-devel"},
},
{
name: "Request latest version (should pick 8.8.0)",
requests: []string{"curl"},
expectError: false,
expectedCount: 1,
expectedNames: []string{"curl"},
},
{
name: "Request nonexistent package",
requests: []string{"nonexistent-package"},
expectError: true,
expectedCount: 0,
},
{
name: "Request package that exists only as src",
requests: []string{"package-with-src"},
expectError: false,
expectedCount: 1,
expectedNames: []string{"package-with-src"},
expectedArch: "src", // Should still find src packages
},
{
name: "Mixed existing and nonexistent",
requests: []string{"curl", "nonexistent"},
expectError: true,
expectedCount: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := MatchRequested(tt.requests, testPackages)
if tt.expectError {
if err == nil {
t.Error("Expected error but got none")
}
return
}
if err != nil {
t.Errorf("Unexpected error: %v", err)
return
}
if len(result) != tt.expectedCount {
t.Errorf("Expected %d packages, got %d", tt.expectedCount, len(result))
return
}
for i, expectedName := range tt.expectedNames {
if i < len(result) {
if !strings.Contains(result[i].Name, expectedName) {
t.Errorf("Expected package name to contain %q, got %q", expectedName, result[i].Name)
}
}
}
if tt.expectedArch != "" && len(result) > 0 {
if result[0].Arch != tt.expectedArch {
t.Errorf("Expected arch %q, got %q", tt.expectedArch, result[0].Arch)
}
}
})
}
}
// TestGetRepoMetaDataURL tests URL construction for repository metadata
func TestGetRepoMetaDataURL(t *testing.T) {
tests := []struct {
name string
baseURL string
repoMetaXmlPath string
expected string
}{
{
name: "Standard repository URL",
baseURL: "https://repo.example.com/rpm/",
repoMetaXmlPath: "repodata/repomd.xml",
expected: "https://repo.example.com/rpm/repodata/repomd.xml",
},
{
name: "Base URL without trailing slash",
baseURL: "https://repo.example.com/rpm",
repoMetaXmlPath: "repodata/repomd.xml",
expected: "https://repo.example.com/rpm/repodata/repomd.xml",
},
{
name: "Empty base URL",
baseURL: "",
repoMetaXmlPath: "repodata/repomd.xml",
expected: "", // Function returns empty string for non-http URLs
},
{
name: "Path with leading slash",
baseURL: "https://repo.example.com/rpm/",
repoMetaXmlPath: "/repodata/repomd.xml",
expected: "https://repo.example.com/rpm//repodata/repomd.xml", // Function creates double slash
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := GetRepoMetaDataURL(tt.baseURL, tt.repoMetaXmlPath)
if result != tt.expected {
t.Errorf("GetRepoMetaDataURL(%q, %q) = %q, want %q",
tt.baseURL, tt.repoMetaXmlPath, result, tt.expected)
}
})
}
}