github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/systembooter/bootentry_test.go (about) 1 // Copyright 2017-2019 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 systembooter 6 7 import ( 8 "errors" 9 "testing" 10 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestGetBooterForNetBooter(t *testing.T) { 15 validConfig := BootEntry{ 16 Name: "Boot0000", 17 Config: []byte(`{"type": "netboot", "method": "dhcpv6", "mac": "aa:bb:cc:dd:ee:ff"}`), 18 } 19 booter := GetBooterFor(validConfig) 20 require.NotNil(t, booter) 21 require.Equal(t, booter.TypeName(), "netboot") 22 require.NotNil(t, booter.(*NetBooter)) 23 } 24 25 func TestGetBooterForNullBooter(t *testing.T) { 26 validConfig := BootEntry{ 27 Name: "Boot0000", 28 Config: []byte(`{"type": "null"}`), 29 } 30 booter := GetBooterFor(validConfig) 31 require.NotNil(t, booter) 32 require.Equal(t, booter.TypeName(), "null") 33 require.NotNil(t, booter.(*NullBooter)) 34 require.Nil(t, booter.Boot()) 35 } 36 37 func TestGetBooterForInvalidBooter(t *testing.T) { 38 invalidConfig := BootEntry{ 39 Name: "Boot0000", 40 Config: []byte(`{"type": "invalid"`), 41 } 42 booter := GetBooterFor(invalidConfig) 43 require.NotNil(t, booter) 44 // an invalid config returns always a NullBooter 45 require.Equal(t, booter.TypeName(), "null") 46 require.NotNil(t, booter.(*NullBooter)) 47 require.Nil(t, booter.Boot()) 48 } 49 50 func TestGetBootEntries(t *testing.T) { 51 var ( 52 bootConfig0000 = []byte(`{"type": "netboot", "method": "dhcpv6", "mac": "aa:bb:cc:dd:ee:ff"}`) 53 bootConfig0001 = []byte(`{"type": "localboot", "uuid": "blah-bleh", "kernel": "/path/to/kernel"}`) 54 ) 55 // Override the package-level variable Get so it will use our test getter 56 // instead of VPD 57 Get = func(key string, readOnly bool) ([]byte, error) { 58 switch key { 59 case "Boot0000": 60 return bootConfig0000, nil 61 case "Boot0001": 62 return bootConfig0001, nil 63 default: 64 return nil, errors.New("No such key") 65 } 66 } 67 entries := GetBootEntries() 68 require.Equal(t, len(entries), 2) 69 require.Equal(t, "Boot0000", entries[0].Name) 70 require.Equal(t, bootConfig0000, entries[0].Config) 71 require.Equal(t, "Boot0001", entries[1].Name) 72 require.Equal(t, bootConfig0001, entries[1].Config) 73 } 74 75 func TestGetBootEntriesOnlyRO(t *testing.T) { 76 // Override the package-level variable Get so it will use our test getter 77 // instead of VPD 78 Get = func(key string, readOnly bool) ([]byte, error) { 79 if !readOnly || key != "Boot0000" { 80 return nil, errors.New("No such key") 81 } 82 return []byte(`{"type": "netboot", "method": "dhcpv6", "mac": "aa:bb:cc:dd:ee:ff"}`), nil 83 } 84 entries := GetBootEntries() 85 require.Equal(t, len(entries), 1) 86 }