-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrcd_test.go
More file actions
779 lines (660 loc) · 25.6 KB
/
rcd_test.go
File metadata and controls
779 lines (660 loc) · 25.6 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
package rcd
import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"
"github.com/open-edge-platform/os-image-composer/internal/chroot"
"github.com/open-edge-platform/os-image-composer/internal/config"
"github.com/open-edge-platform/os-image-composer/internal/ospackage/rpmutils"
"github.com/open-edge-platform/os-image-composer/internal/provider"
"github.com/open-edge-platform/os-image-composer/internal/utils/system"
)
// mockChrootEnv is a simple mock implementation of ChrootEnvInterface for testing
type mockChrootEnv struct{}
// Ensure mockChrootEnv implements ChrootEnvInterface
var _ chroot.ChrootEnvInterface = (*mockChrootEnv)(nil)
func (m *mockChrootEnv) GetChrootEnvRoot() string { return "/tmp/test-chroot" }
func (m *mockChrootEnv) GetChrootImageBuildDir() string { return "/tmp/test-build" }
func (m *mockChrootEnv) GetTargetOsPkgType() string { return "rpm" }
func (m *mockChrootEnv) GetTargetOsConfigDir() string { return "/tmp/test-config" }
func (m *mockChrootEnv) GetTargetOsReleaseVersion() string { return "10.0" }
func (m *mockChrootEnv) GetChrootPkgCacheDir() string { return "/tmp/test-cache" }
func (m *mockChrootEnv) GetChrootEnvEssentialPackageList() ([]string, error) {
return []string{"base-files"}, nil
}
func (m *mockChrootEnv) GetChrootEnvHostPath(chrootPath string) (string, error) {
return chrootPath, nil
}
func (m *mockChrootEnv) GetChrootEnvPath(hostPath string) (string, error) { return hostPath, nil }
func (m *mockChrootEnv) MountChrootSysfs(chrootPath string) error { return nil }
func (m *mockChrootEnv) UmountChrootSysfs(chrootPath string) error { return nil }
func (m *mockChrootEnv) MountChrootPath(hostFullPath, chrootPath, mountFlags string) error {
return nil
}
func (m *mockChrootEnv) UmountChrootPath(chrootPath string) error { return nil }
func (m *mockChrootEnv) CopyFileFromHostToChroot(hostFilePath, chrootPath string) error { return nil }
func (m *mockChrootEnv) CopyFileFromChrootToHost(hostFilePath, chrootPath string) error { return nil }
func (m *mockChrootEnv) UpdateChrootLocalRepoMetadata(chrootRepoDir string, targetArch string, sudo bool) error {
return nil
}
func (m *mockChrootEnv) RefreshLocalCacheRepo() error { return nil }
func (m *mockChrootEnv) InitChrootEnv(targetOs, targetDist, targetArch string) error {
return nil
}
func (m *mockChrootEnv) CleanupChrootEnv(targetOs, targetDist, targetArch string) error { return nil }
func (m *mockChrootEnv) TdnfInstallPackage(packageName, installRoot string, repositoryIDList []string) error {
return nil
}
func (m *mockChrootEnv) AptInstallPackage(packageName, installRoot string, repoSrcList []string) error {
return nil
}
func (m *mockChrootEnv) UpdateSystemPkgs(template *config.ImageTemplate) error { return nil }
// Helper function to create a test ImageTemplate
func createTestImageTemplate() *config.ImageTemplate {
return &config.ImageTemplate{
Image: config.ImageInfo{
Name: "test-rcd-image",
Version: "1.0.0",
},
Target: config.TargetInfo{
OS: "redhat-compatible-distro",
Dist: "rcd10",
Arch: "x86_64",
ImageType: "raw",
},
SystemConfig: config.SystemConfig{
Name: "test-rcd-system",
Description: "Test RCD system configuration",
Packages: []string{"curl", "wget", "vim"},
},
}
}
// TestRCDProviderInterface tests that RCD implements Provider interface
func TestRCDProviderInterface(t *testing.T) {
var _ provider.Provider = (*RCD)(nil) // Compile-time interface check
}
// TestRCDProviderName tests the Name method
func TestRCDProviderName(t *testing.T) {
rcd := &RCD{}
name := rcd.Name("rcd10", "x86_64")
expected := "redhat-compatible-distro-rcd10-x86_64"
if name != expected {
t.Errorf("Expected name %s, got %s", expected, name)
}
}
// TestGetProviderId tests the GetProviderId function
func TestGetProviderId(t *testing.T) {
testCases := []struct {
dist string
arch string
expected string
}{
{"rcd10", "x86_64", "redhat-compatible-distro-rcd10-x86_64"},
{"rcd10", "aarch64", "redhat-compatible-distro-rcd10-aarch64"},
{"rcd11", "x86_64", "redhat-compatible-distro-rcd11-x86_64"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s-%s", tc.dist, tc.arch), func(t *testing.T) {
result := system.GetProviderId(OsName, tc.dist, tc.arch)
if result != tc.expected {
t.Errorf("Expected %s, got %s", tc.expected, result)
}
})
}
}
// TestRCDCentralizedConfig tests the centralized configuration loading
func TestRCDCentralizedConfig(t *testing.T) {
// Change to project root for tests that need config files
originalDir, _ := os.Getwd()
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Failed to change back to original directory: %v", err)
}
}()
// Navigate to project root (3 levels up from internal/provider/rcd)
if err := os.Chdir("../../../"); err != nil {
t.Skipf("Cannot change to project root: %v", err)
return
}
// Test loading repo config
cfg, err := loadRepoConfigFromYAML("rcd10", "x86_64")
if err != nil {
t.Skipf("loadRepoConfig failed (expected in test environment): %v", err)
return
}
// If we successfully load config, verify the values
if cfg.Name == "" {
t.Error("Expected config name to be set")
}
if cfg.Section == "" {
t.Error("Expected config section to be set")
}
// Verify URL is set properly
if cfg.URL == "" {
t.Error("Expected config URL to be set")
}
t.Logf("Successfully loaded repo config: %s", cfg.Name)
t.Logf("Config details: %+v", cfg)
}
// TestRCDProviderFallback tests the fallback to centralized config when HTTP fails
func TestRCDProviderFallback(t *testing.T) {
// Change to project root for tests that need config files
originalDir, _ := os.Getwd()
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Failed to change back to original directory: %v", err)
}
}()
// Navigate to project root (3 levels up from internal/provider/rcd)
if err := os.Chdir("../../../"); err != nil {
t.Skipf("Cannot change to project root: %v", err)
return
}
rcd := &RCD{
chrootEnv: &mockChrootEnv{},
}
// Test initialization which should use centralized config
err := rcd.Init("rcd10", "x86_64")
if err != nil {
t.Logf("Init failed as expected in test environment: %v", err)
} else {
// If it succeeds, verify the configuration was set up from YAML
if rcd.repoCfg.Name == "" {
t.Error("Expected repoCfg.Name to be set after successful init")
}
t.Logf("Successfully tested initialization with centralized config")
t.Logf("Config name: %s", rcd.repoCfg.Name)
t.Logf("Config URL: %s", rcd.repoCfg.URL)
}
}
// TestRCDProviderInit tests the Init method with centralized configuration
func TestRCDProviderInit(t *testing.T) {
// Change to project root for tests that need config files
originalDir, _ := os.Getwd()
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Failed to change back to original directory: %v", err)
}
}()
// Navigate to project root (3 levels up from internal/provider/rcd)
if err := os.Chdir("../../../"); err != nil {
t.Skipf("Cannot change to project root: %v", err)
return
}
rcd := &RCD{
chrootEnv: &mockChrootEnv{},
}
// Test with x86_64 architecture - now uses centralized config
err := rcd.Init("rcd10", "x86_64")
if err != nil {
t.Skipf("Init failed (expected in test environment): %v", err)
return
}
// If it succeeds, verify the configuration was set up
if rcd.repoCfg.Name == "" {
t.Error("Expected repoCfg.Name to be set after successful Init")
}
t.Logf("Successfully initialized with centralized config")
t.Logf("Config name: %s", rcd.repoCfg.Name)
t.Logf("Config URL: %s", rcd.repoCfg.URL)
t.Logf("Primary href: %s", rcd.gzHref)
}
// TestLoadRepoConfigFromYAML tests the centralized YAML configuration loading
func TestLoadRepoConfigFromYAML(t *testing.T) {
// Change to project root for tests that need config files
originalDir, _ := os.Getwd()
defer func() {
if err := os.Chdir(originalDir); err != nil {
t.Logf("Failed to change back to original directory: %v", err)
}
}()
// Navigate to project root (3 levels up from internal/provider/rcd)
if err := os.Chdir("../../../"); err != nil {
t.Skipf("Cannot change to project root: %v", err)
return
}
// Test loading repo config
cfg, err := loadRepoConfigFromYAML("rcd10", "x86_64")
if err != nil {
t.Skipf("loadRepoConfigFromYAML failed (expected in test environment): %v", err)
return
}
// If we successfully load config, verify the values
if cfg.Name == "" {
t.Error("Expected config name to be set")
}
if cfg.Section == "" {
t.Error("Expected config section to be set")
}
t.Logf("Successfully loaded repo config from YAML: %s", cfg.Name)
t.Logf("Config details: %+v", cfg)
}
// TestFetchPrimaryURL tests the fetchPrimaryURL function with mock server
func TestFetchPrimaryURL(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
repomdXML := `<?xml version="1.0" encoding="UTF-8"?>
<repomd xmlns="http://linux.duke.edu/metadata/repo">
<data type="primary">
<location href="repodata/primary.xml.gz"/>
<checksum type="sha256">abcd1234</checksum>
</data>
<data type="filelists">
<location href="repodata/filelists.xml.gz"/>
<checksum type="sha256">efgh5678</checksum>
</data>
</repomd>`
fmt.Fprint(w, repomdXML)
}))
defer server.Close()
href, err := rpmutils.FetchPrimaryURL(server.URL)
if err != nil {
t.Fatalf("fetchPrimaryURL failed: %v", err)
}
expected := "repodata/primary.xml.gz"
if href != expected {
t.Errorf("Expected href '%s', got '%s'", expected, href)
}
}
// TestFetchPrimaryURLNoPrimary tests fetchPrimaryURL when no primary data exists
func TestFetchPrimaryURLNoPrimary(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
repomdXML := `<?xml version="1.0" encoding="UTF-8"?>
<repomd xmlns="http://linux.duke.edu/metadata/repo">
<data type="filelists">
<location href="repodata/filelists.xml.gz"/>
<checksum type="sha256">efgh5678</checksum>
</data>
</repomd>`
fmt.Fprint(w, repomdXML)
}))
defer server.Close()
_, err := rpmutils.FetchPrimaryURL(server.URL)
if err == nil {
t.Error("Expected error when primary location not found")
}
expectedError := "primary location not found"
if !strings.Contains(err.Error(), expectedError) {
t.Errorf("Expected error containing '%s', got '%s'", expectedError, err.Error())
}
}
// TestFetchPrimaryURLInvalidXML tests fetchPrimaryURL with invalid XML
func TestFetchPrimaryURLInvalidXML(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "invalid xml content")
}))
defer server.Close()
_, err := rpmutils.FetchPrimaryURL(server.URL)
if err == nil {
t.Error("Expected error when XML is invalid")
}
}
// TestRCDProviderPreProcess tests PreProcess method with mocked dependencies
func TestRCDProviderPreProcess(t *testing.T) {
t.Skip("PreProcess test requires full chroot environment and system dependencies - skipping in unit tests")
}
// TestRCDProviderBuildImage tests BuildImage method
func TestRCDProviderBuildImage(t *testing.T) {
t.Skip("BuildImage test requires full system dependencies and image builders - skipping in unit tests")
}
// TestRCDProviderBuildImageISO tests BuildImage method with ISO type
func TestRCDProviderBuildImageISO(t *testing.T) {
t.Skip("BuildImage ISO test requires full system dependencies and image builders - skipping in unit tests")
}
// TestRCDProviderPostProcess tests PostProcess method
func TestRCDProviderPostProcess(t *testing.T) {
t.Skip("PostProcess test requires full chroot environment - skipping in unit tests")
}
// TestRCDProviderInstallHostDependency tests installHostDependency method
func TestRCDProviderInstallHostDependency(t *testing.T) {
t.Skip("installHostDependency test requires host package manager and system dependencies - skipping in unit tests")
}
// TestRCDProviderInstallHostDependencyCommands tests expected host dependencies
func TestRCDProviderInstallHostDependencyCommands(t *testing.T) {
// Test the expected dependencies mapping by accessing the internal map
// This verifies what packages the RCD provider expects to install
expectedDeps := map[string]string{
"rpm": "rpm", // For the chroot env build RPM pkg installation
"mkfs.fat": "dosfstools", // For the FAT32 boot partition creation
"qemu-img": "qemu-utils", // For image file format conversion
"mformat": "mtools", // For writing files to FAT32 partition
"xorriso": "xorriso", // For ISO image creation
"grub-mkimage": "grub-common", // For ISO image UEFI Grub binary creation
"sbsign": "sbsigntool", // For the UKI image creation
}
t.Logf("Expected host dependencies for RCD provider: %v", expectedDeps)
// Verify that each expected dependency has a mapping
for cmd, pkg := range expectedDeps {
if cmd == "" || pkg == "" {
t.Errorf("Empty dependency mapping: cmd='%s', pkg='%s'", cmd, pkg)
}
}
}
// TestRCDProviderRegister tests the Register function
func TestRCDProviderRegister(t *testing.T) {
t.Skip("Register test requires chroot environment initialization - skipping in unit tests")
}
// TestRCDProviderWorkflow tests a complete RCD provider workflow
func TestRCDProviderWorkflow(t *testing.T) {
// This is an integration-style test showing how an RCD provider
// would be used in a complete workflow
rcd := &RCD{}
// Test provider name generation
name := rcd.Name("rcd10", "x86_64")
expectedName := "redhat-compatible-distro-rcd10-x86_64"
if name != expectedName {
t.Errorf("Expected name %s, got %s", expectedName, name)
}
// Test Init (will likely fail due to network dependencies)
if err := rcd.Init("rcd10", "x86_64"); err != nil {
t.Logf("Skipping Init test to avoid config file errors in unit test environment")
} else {
t.Log("Init succeeded - repo config loaded")
if rcd.repoCfg.Name != "" {
t.Logf("Repo config loaded: %s", rcd.repoCfg.Name)
}
}
// Skip PreProcess, BuildImage and PostProcess tests to avoid system-level dependencies
t.Log("Skipping PreProcess, BuildImage and PostProcess tests to avoid system-level dependencies")
t.Log("Complete workflow test finished - core methods exist and are callable")
}
// TestRCDConfigurationStructure tests the internal configuration structure
func TestRCDConfigurationStructure(t *testing.T) {
rcd := &RCD{
repoCfg: rpmutils.RepoConfig{
Section: "rcd-base",
Name: "Red Hat Compatible Distro 10.0 Base Repository",
URL: "https://example.com/rcd/10.0/base/x86_64",
GPGCheck: true,
RepoGPGCheck: true,
Enabled: true,
GPGKey: "https://example.com/keys/rcd.asc",
},
gzHref: "repodata/primary.xml.gz",
}
// Verify internal structure is properly set up
if rcd.repoCfg.Section == "" {
t.Error("Expected repo config section to be set")
}
if rcd.gzHref == "" {
t.Error("Expected gzHref to be set")
}
// Test configuration structure without relying on constants that may not exist
t.Logf("Skipping config loading test to avoid file system errors in unit test environment")
}
// TestRCDArchitectureHandling tests architecture-specific URL construction
func TestRCDArchitectureHandling(t *testing.T) {
testCases := []struct {
inputArch string
expectedName string
}{
{"x86_64", "redhat-compatible-distro-rcd10-x86_64"},
{"aarch64", "redhat-compatible-distro-rcd10-aarch64"},
{"armv7hl", "redhat-compatible-distro-rcd10-armv7hl"},
}
for _, tc := range testCases {
t.Run(tc.inputArch, func(t *testing.T) {
rcd := &RCD{}
name := rcd.Name("rcd10", tc.inputArch)
if name != tc.expectedName {
t.Errorf("For arch %s, expected name %s, got %s", tc.inputArch, tc.expectedName, name)
}
})
}
}
// TestRCDBuildImageNilTemplate tests BuildImage with nil template
func TestRCDBuildImageNilTemplate(t *testing.T) {
rcd := &RCD{}
err := rcd.BuildImage(nil)
if err == nil {
t.Error("Expected error when template is nil")
}
expectedError := "template cannot be nil"
if err.Error() != expectedError {
t.Errorf("Expected error '%s', got '%s'", expectedError, err.Error())
}
}
// TestRCDBuildImageUnsupportedType tests BuildImage with unsupported image type
func TestRCDBuildImageUnsupportedType(t *testing.T) {
rcd := &RCD{}
template := createTestImageTemplate()
template.Target.ImageType = "unsupported"
err := rcd.BuildImage(template)
if err == nil {
t.Error("Expected error for unsupported image type")
}
expectedError := "unsupported image type: unsupported"
if err.Error() != expectedError {
t.Errorf("Expected error '%s', got '%s'", expectedError, err.Error())
}
}
// TestRCDBuildImageValidTypes tests BuildImage error handling for valid image types
func TestRCDBuildImageValidTypes(t *testing.T) {
rcd := &RCD{}
validTypes := []string{"raw", "img", "iso"}
for _, imageType := range validTypes {
t.Run(imageType, func(t *testing.T) {
template := createTestImageTemplate()
template.Target.ImageType = imageType
// These will fail due to missing chrootEnv, but we can verify
// that the code path is reached and the error is expected
err := rcd.BuildImage(template)
if err == nil {
t.Errorf("Expected error for image type %s (missing dependencies)", imageType)
} else {
t.Logf("Image type %s correctly failed with: %v", imageType, err)
// Verify the error is related to missing dependencies, not invalid type
if err.Error() == "unsupported image type: "+imageType {
t.Errorf("Image type %s should be supported but got unsupported error", imageType)
}
}
})
}
}
// TestRCDPostProcessWithNilChroot tests PostProcess with nil chrootEnv
func TestRCDPostProcessWithNilChroot(t *testing.T) {
rcd := &RCD{}
template := createTestImageTemplate()
// Test that PostProcess panics with nil chrootEnv (current behavior)
// We use defer/recover to catch the panic and validate it
defer func() {
if r := recover(); r != nil {
t.Logf("PostProcess correctly panicked with nil chrootEnv: %v", r)
} else {
t.Error("Expected PostProcess to panic with nil chrootEnv")
}
}()
// This will panic due to nil chrootEnv
_ = rcd.PostProcess(template, nil)
}
// TestRCDPostProcessErrorHandling tests PostProcess error handling logic
func TestRCDPostProcessErrorHandling(t *testing.T) {
// Test that PostProcess method exists and has correct signature
rcd := &RCD{}
inputError := fmt.Errorf("build failed")
// Verify the method signature is correct by assigning it to a function variable
var postProcessFunc func(*config.ImageTemplate, error) error = rcd.PostProcess
t.Logf("PostProcess method has correct signature: %T", postProcessFunc)
t.Logf("Input error for testing: %v", inputError)
// Test passes if we can assign the method to the correct function type
}
// TestRCDStructInitialization tests RCD struct initialization
func TestRCDStructInitialization(t *testing.T) {
// Test zero value initialization
rcd := &RCD{}
if rcd.repoCfg.Name != "" {
t.Error("Expected empty repoCfg.Name in uninitialized RCD")
}
if rcd.gzHref != "" {
t.Error("Expected empty gzHref in uninitialized RCD")
}
if rcd.chrootEnv != nil {
t.Error("Expected nil chrootEnv in uninitialized RCD")
}
}
// TestRCDStructWithData tests RCD struct with initialized data
func TestRCDStructWithData(t *testing.T) {
cfg := rpmutils.RepoConfig{
Name: "Test RCD Repo",
URL: "https://test.rcd.example.com",
Section: "test-section",
Enabled: true,
}
rcd := &RCD{
repoCfg: cfg,
gzHref: "test/primary.xml.gz",
}
if rcd.repoCfg.Name != "Test RCD Repo" {
t.Errorf("Expected repoCfg.Name 'Test RCD Repo', got '%s'", rcd.repoCfg.Name)
}
if rcd.repoCfg.URL != "https://test.rcd.example.com" {
t.Errorf("Expected repoCfg.URL 'https://test.rcd.example.com', got '%s'", rcd.repoCfg.URL)
}
if rcd.gzHref != "test/primary.xml.gz" {
t.Errorf("Expected gzHref 'test/primary.xml.gz', got '%s'", rcd.gzHref)
}
}
// TestRCDConstants tests RCD provider constants
func TestRCDConstants(t *testing.T) {
// Test OsName constant
if OsName != "redhat-compatible-distro" {
t.Errorf("Expected OsName 'redhat-compatible-distro', got '%s'", OsName)
}
}
// TestRCDNameWithVariousInputs tests Name method with different dist and arch combinations
func TestRCDNameWithVariousInputs(t *testing.T) {
rcd := &RCD{}
testCases := []struct {
dist string
arch string
expected string
}{
{"rcd10", "x86_64", "redhat-compatible-distro-rcd10-x86_64"},
{"rcd10", "aarch64", "redhat-compatible-distro-rcd10-aarch64"},
{"rcd11", "x86_64", "redhat-compatible-distro-rcd11-x86_64"},
{"", "", "redhat-compatible-distro--"},
{"test", "test", "redhat-compatible-distro-test-test"},
}
for _, tc := range testCases {
t.Run(fmt.Sprintf("%s-%s", tc.dist, tc.arch), func(t *testing.T) {
result := rcd.Name(tc.dist, tc.arch)
if result != tc.expected {
t.Errorf("Expected '%s', got '%s'", tc.expected, result)
}
})
}
}
// TestRCDMethodSignatures tests that all interface methods have correct signatures
func TestRCDMethodSignatures(t *testing.T) {
rcd := &RCD{}
// Test that all methods can be assigned to their expected function types
var nameFunc func(string, string) string = rcd.Name
var initFunc func(string, string) error = rcd.Init
var preProcessFunc func(*config.ImageTemplate) error = rcd.PreProcess
var buildImageFunc func(*config.ImageTemplate) error = rcd.BuildImage
var postProcessFunc func(*config.ImageTemplate, error) error = rcd.PostProcess
t.Logf("Name method signature: %T", nameFunc)
t.Logf("Init method signature: %T", initFunc)
t.Logf("PreProcess method signature: %T", preProcessFunc)
t.Logf("BuildImage method signature: %T", buildImageFunc)
t.Logf("PostProcess method signature: %T", postProcessFunc)
}
// TestRCDRegister tests the Register function
func TestRCDRegister(t *testing.T) {
// Test Register function with valid parameters
targetOs := "rcd"
targetDist := "rcd10"
targetArch := "x86_64"
// Register should fail in unit test environment due to missing dependencies
// but we can test that it doesn't panic and has correct signature
err := Register(targetOs, targetDist, targetArch)
// We expect an error in unit test environment
if err == nil {
t.Log("Unexpected success - RCD registration succeeded in test environment")
} else {
// This is expected in unit test environment due to missing config
t.Logf("Expected error in test environment: %v", err)
}
// Test with invalid parameters
err = Register("", "", "")
if err == nil {
t.Error("Expected error with empty parameters")
}
t.Log("Successfully tested Register function behavior")
}
// TestRCDPreProcess tests the PreProcess function
func TestRCDPreProcess(t *testing.T) {
// Skip this test as PreProcess requires proper initialization with chrootEnv
// and calls downloadImagePkgs which doesn't handle nil chrootEnv gracefully
t.Skip("PreProcess requires proper RCD initialization with chrootEnv - function exists and is callable")
}
// TestRCDInstallHostDependency tests the installHostDependency function
func TestRCDInstallHostDependency(t *testing.T) {
rcd := &RCD{}
// Test that the function exists and can be called
err := rcd.installHostDependency()
// In test environment, we expect an error due to missing system dependencies
// but the function should not panic
if err == nil {
t.Log("installHostDependency succeeded - host dependencies available in test environment")
} else {
t.Logf("installHostDependency failed as expected in test environment: %v", err)
}
t.Log("installHostDependency function signature and execution test completed")
}
// TestRCDDownloadImagePkgs tests the downloadImagePkgs function
func TestRCDDownloadImagePkgs(t *testing.T) {
// Skip this test as downloadImagePkgs requires proper initialization with chrootEnv
// and doesn't handle nil chrootEnv gracefully
t.Skip("downloadImagePkgs requires proper RCD initialization with chrootEnv - function exists and is callable")
}
// TestRCDBuildRawImage tests buildRawImage method error handling
func TestRCDBuildRawImage(t *testing.T) {
rcd := &RCD{}
template := createTestImageTemplate()
// Test that buildRawImage fails gracefully without proper initialization
err := rcd.buildRawImage(template)
if err == nil {
t.Error("Expected error when building raw image without proper initialization")
} else {
t.Logf("buildRawImage correctly failed with: %v", err)
}
}
// TestRCDBuildInitrdImage tests buildInitrdImage method error handling
func TestRCDBuildInitrdImage(t *testing.T) {
rcd := &RCD{}
template := createTestImageTemplate()
// Test that buildInitrdImage fails gracefully without proper initialization
err := rcd.buildInitrdImage(template)
if err == nil {
t.Error("Expected error when building initrd image without proper initialization")
} else {
t.Logf("buildInitrdImage correctly failed with: %v", err)
}
}
// TestRCDBuildIsoImage tests buildIsoImage method error handling
func TestRCDBuildIsoImage(t *testing.T) {
rcd := &RCD{}
template := createTestImageTemplate()
// Test that buildIsoImage fails gracefully without proper initialization
err := rcd.buildIsoImage(template)
if err == nil {
t.Error("Expected error when building ISO image without proper initialization")
} else {
t.Logf("buildIsoImage correctly failed with: %v", err)
}
}
// TestRCDDisplayImageArtifacts tests displayImageArtifacts function
func TestRCDDisplayImageArtifacts(t *testing.T) {
// Test that the displayImageArtifacts function exists and is callable
// This function doesn't return anything so we just test that it doesn't panic
defer func() {
if r := recover(); r != nil {
t.Errorf("displayImageArtifacts panicked: %v", r)
}
}()
displayImageArtifacts("/tmp/test", "TEST")
t.Log("displayImageArtifacts function executed without panic")
}