github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/boot/bls/bls_test.go (about) 1 // Copyright 2020 the u-root Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package bls 6 7 import ( 8 "os" 9 "path/filepath" 10 "strconv" 11 "strings" 12 "testing" 13 14 "github.com/mvdan/u-root-coreutils/pkg/boot/boottest" 15 "github.com/mvdan/u-root-coreutils/pkg/ulog/ulogtest" 16 ) 17 18 var blsEntries = []struct { 19 entry string 20 err string 21 }{ 22 { 23 entry: "entry-1.conf", 24 }, 25 { 26 entry: "entry-2.conf", 27 err: "neither linux, efi, nor multiboot present in BootLoaderSpec config", 28 }, 29 } 30 31 // Enable this temporarily to generate new configs. Double-check them by hand. 32 func DISABLEDTestGenerateConfigs(t *testing.T) { 33 tests, err := filepath.Glob("testdata/*.json") 34 if err != nil { 35 t.Error("Failed to find test config files:", err) 36 } 37 38 for _, test := range tests { 39 configPath := strings.TrimSuffix(test, ".json") 40 t.Run(configPath, func(t *testing.T) { 41 imgs, err := ScanBLSEntries(ulogtest.Logger{t}, configPath, nil) 42 if err != nil { 43 t.Fatalf("Failed to parse %s: %v", test, err) 44 } 45 46 if err := boottest.ToJSONFile(imgs, test); err != nil { 47 t.Errorf("failed to generate file: %v", err) 48 } 49 }) 50 } 51 } 52 53 func TestParseBLSEntries(t *testing.T) { 54 fsRoot := "./testdata/madeup" 55 dir := filepath.Join(fsRoot, "loader/entries") 56 57 for _, tt := range blsEntries { 58 t.Run(tt.entry, func(t *testing.T) { 59 image, err := parseBLSEntry(filepath.Join(dir, tt.entry), fsRoot, nil) 60 if err != nil { 61 if tt.err == "" { 62 t.Fatalf("Got error %v", err) 63 } 64 if !strings.Contains(err.Error(), tt.err) { 65 t.Fatalf("Got error %v, expected error to contain %s", err, tt.err) 66 } 67 return 68 } 69 if tt.err != "" { 70 t.Fatalf("Expected error %s, got no error", tt.err) 71 } 72 t.Logf("Got image: %s", image.String()) 73 }) 74 } 75 } 76 77 func TestScanBLSEntries(t *testing.T) { 78 // find all saved configs 79 tests, err := filepath.Glob("testdata/*.json") 80 if err != nil { 81 t.Error("Failed to find test config files:", err) 82 } 83 84 for _, test := range tests { 85 configPath := strings.TrimSuffix(test, ".json") 86 t.Run(configPath, func(t *testing.T) { 87 want, err := os.ReadFile(test) 88 if err != nil { 89 t.Errorf("Failed to read test json '%v':%v", test, err) 90 } 91 92 imgs, err := ScanBLSEntries(ulogtest.Logger{t}, configPath, nil) 93 if err != nil { 94 t.Fatalf("Failed to parse %s: %v", test, err) 95 } 96 97 if err := boottest.CompareImagesToJSON(imgs, want); err != nil { 98 t.Errorf("ParseLocalConfig(): %v", err) 99 } 100 }) 101 } 102 } 103 104 func TestSetBLSRank(t *testing.T) { 105 fsRoot := "./testdata/madeup" 106 dir := filepath.Join(fsRoot, "loader/entries") 107 testRank := 2 108 originRank := os.Getenv("BLS_BOOT_RANK") 109 os.Setenv("BLS_BOOT_RANK", strconv.Itoa(testRank)) 110 111 for _, tt := range blsEntries { 112 t.Run(tt.entry, func(t *testing.T) { 113 image, err := parseBLSEntry(filepath.Join(dir, tt.entry), fsRoot, nil) 114 if err != nil { 115 if tt.err == "" { 116 t.Fatalf("Got error %v", err) 117 } 118 if !strings.Contains(err.Error(), tt.err) { 119 t.Fatalf("Got error %v, expected error to contain %s", err, tt.err) 120 } 121 return 122 } 123 if tt.err != "" { 124 t.Fatalf("Expected error %s, got no error", tt.err) 125 } 126 127 if image.Rank() != testRank { 128 t.Errorf("Expected rank %d, got %d", testRank, image.Rank()) 129 } 130 }) 131 } 132 133 os.Setenv("BLS_BOOT_RANK", originRank) 134 }