github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/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  	"io/ioutil"
     9  	"path/filepath"
    10  	"strings"
    11  	"testing"
    12  
    13  	"github.com/u-root/u-root/pkg/boot/boottest"
    14  	"github.com/u-root/u-root/pkg/ulog/ulogtest"
    15  )
    16  
    17  var blsEntries = []struct {
    18  	entry string
    19  	err   string
    20  }{
    21  	{
    22  		entry: "entry-1.conf",
    23  	},
    24  	{
    25  		entry: "entry-2.conf",
    26  		err:   "neither linux, efi, nor multiboot present in BootLoaderSpec config",
    27  	},
    28  }
    29  
    30  func TestParseBLSEntries(t *testing.T) {
    31  	fsRoot := "./testdata/madeup"
    32  	dir := filepath.Join(fsRoot, "loader/entries")
    33  
    34  	for _, tt := range blsEntries {
    35  		t.Run(tt.entry, func(t *testing.T) {
    36  			image, err := parseBLSEntry(filepath.Join(dir, tt.entry), fsRoot)
    37  			if err != nil {
    38  				if tt.err == "" {
    39  					t.Fatalf("Got error %v", err)
    40  				}
    41  				if !strings.Contains(err.Error(), tt.err) {
    42  					t.Fatalf("Got error %v, expected error to contain %s", err, tt.err)
    43  				}
    44  				return
    45  			}
    46  			if tt.err != "" {
    47  				t.Fatalf("Expected error %s, got no error", tt.err)
    48  			}
    49  			t.Logf("Got image: %s", image.String())
    50  		})
    51  	}
    52  }
    53  
    54  func TestScanBLSEntries(t *testing.T) {
    55  	// find all saved configs
    56  	tests, err := filepath.Glob("testdata/*.json")
    57  	if err != nil {
    58  		t.Error("Failed to find test config files:", err)
    59  	}
    60  
    61  	for _, test := range tests {
    62  		configPath := strings.TrimRight(test, ".json")
    63  		t.Run(configPath, func(t *testing.T) {
    64  			want, err := ioutil.ReadFile(test)
    65  			if err != nil {
    66  				t.Errorf("Failed to read test json '%v':%v", test, err)
    67  			}
    68  
    69  			imgs, err := ScanBLSEntries(ulogtest.Logger{t}, configPath)
    70  			if err != nil {
    71  				t.Fatalf("Failed to parse %s: %v", test, err)
    72  			}
    73  
    74  			if err := boottest.CompareImagesToJSON(imgs, want); err != nil {
    75  				t.Errorf("ParseLocalConfig(): %v", err)
    76  			}
    77  		})
    78  	}
    79  }
    80  
    81  // Enable this temporarily to generate new configs. Double-check them by hand.
    82  func DISABLEDTestGenerateConfigs(t *testing.T) {
    83  	tests, err := filepath.Glob("testdata/*.json")
    84  	if err != nil {
    85  		t.Error("Failed to find test config files:", err)
    86  	}
    87  
    88  	for _, test := range tests {
    89  		configPath := strings.TrimRight(test, ".json")
    90  		t.Run(configPath, func(t *testing.T) {
    91  			imgs, err := ScanBLSEntries(ulogtest.Logger{t}, configPath)
    92  			if err != nil {
    93  				t.Fatalf("Failed to parse %s: %v", test, err)
    94  			}
    95  
    96  			if err := boottest.ToJSONFile(imgs, test); err != nil {
    97  				t.Errorf("failed to generate file: %v", err)
    98  			}
    99  		})
   100  	}
   101  }