github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/esxi/esxi_test.go (about) 1 // Copyright 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 esxi 6 7 import ( 8 "encoding/hex" 9 "fmt" 10 "reflect" 11 "testing" 12 13 "github.com/u-root/u-root/pkg/boot" 14 ) 15 16 func TestParse(t *testing.T) { 17 for _, tt := range []struct { 18 file string 19 want options 20 }{ 21 { 22 file: "testdata/kernel_cmdline_mods.cfg", 23 want: options{ 24 kernel: "testdata/b.b00", 25 args: "zee", 26 modules: []string{ 27 "testdata/b.b00 blabla", 28 "testdata/k.b00", 29 "testdata/m.m00 marg marg2", 30 }, 31 }, 32 }, 33 { 34 file: "testdata/empty_mods.cfg", 35 want: options{ 36 kernel: "testdata/b.b00", 37 args: "zee", 38 }, 39 }, 40 { 41 file: "testdata/no_mods.cfg", 42 want: options{ 43 kernel: "testdata/b.b00", 44 args: "zee", 45 }, 46 }, 47 { 48 file: "testdata/no_cmdline.cfg", 49 want: options{ 50 kernel: "testdata/b.b00", 51 }, 52 }, 53 { 54 file: "testdata/empty_cmdline.cfg", 55 want: options{ 56 kernel: "testdata/b.b00", 57 }, 58 }, 59 { 60 file: "testdata/empty_updated.cfg", 61 want: options{ 62 kernel: "testdata/b.b00", 63 args: "zee", 64 // Explicitly stating this as the wanted value. 65 updated: 0, 66 }, 67 }, 68 { 69 file: "testdata/updated_twice.cfg", 70 want: options{ 71 kernel: "testdata/b.b00", 72 args: "zee", 73 // Explicitly stating this as the wanted value. 74 updated: 0, 75 }, 76 }, 77 { 78 file: "testdata/updated.cfg", 79 want: options{ 80 kernel: "testdata/b.b00", 81 args: "zee", 82 updated: 4, 83 }, 84 }, 85 { 86 file: "testdata/empty_bootstate.cfg", 87 want: options{ 88 kernel: "testdata/b.b00", 89 args: "zee", 90 // Explicitly stating this as the wanted value. 91 bootstate: bootValid, 92 }, 93 }, 94 { 95 file: "testdata/bootstate_twice.cfg", 96 want: options{ 97 kernel: "testdata/b.b00", 98 args: "zee", 99 // Explicitly stating this as the wanted value. 100 bootstate: bootValid, 101 }, 102 }, 103 { 104 file: "testdata/bootstate.cfg", 105 want: options{ 106 kernel: "testdata/b.b00", 107 args: "zee", 108 bootstate: bootDirty, 109 }, 110 }, 111 { 112 file: "testdata/bootstate_invalid.cfg", 113 want: options{ 114 kernel: "testdata/b.b00", 115 args: "zee", 116 bootstate: bootInvalid, 117 }, 118 }, 119 { 120 file: "testdata/no_bootstate.cfg", 121 want: options{ 122 kernel: "testdata/b.b00", 123 args: "zee", 124 bootstate: bootInvalid, 125 }, 126 }, 127 } { 128 got, err := parse(tt.file) 129 if err != nil { 130 t.Fatalf("cannot parse config at %s: %v", tt.file, err) 131 } 132 133 if !reflect.DeepEqual(got, tt.want) { 134 t.Errorf("LoadConfig(%s) = %v want %v", tt.file, got, tt.want) 135 } 136 } 137 } 138 139 // This is in the second block of testdata/dev5 and testdata/dev6. 140 var ( 141 dev5GUID = "aabbccddeeff0011" 142 dev6GUID = "00112233445566aa" 143 uuid5 = hex.EncodeToString([]byte(dev5GUID)) 144 uuid6 = hex.EncodeToString([]byte(dev6GUID)) 145 device = "testdata/dev" 146 ) 147 148 func TestDev5Valid(t *testing.T) { 149 want := []*boot.MultibootImage{ 150 { 151 Path: "testdata/k", 152 Cmdline: fmt.Sprintf(" bootUUID=%s", uuid5), 153 }, 154 } 155 156 opts5 := &options{ 157 kernel: "testdata/k", 158 updated: 1, 159 bootstate: bootValid, 160 } 161 162 // No opts6 at all. 163 imgs, _ := getImages(device, opts5, nil) 164 if !reflect.DeepEqual(imgs, want) { 165 t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opts5, nil, imgs, want) 166 } 167 168 // Invalid opts6. Higher updated, but invalid state. 169 invalidOpts6 := &options{ 170 kernel: "foobar", 171 updated: 2, 172 bootstate: bootInvalid, 173 } 174 imgs, _ = getImages(device, opts5, invalidOpts6) 175 if !reflect.DeepEqual(imgs, want) { 176 t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opts5, invalidOpts6, imgs, want) 177 } 178 } 179 180 func TestDev6Valid(t *testing.T) { 181 want := []*boot.MultibootImage{ 182 { 183 Path: "testdata/k", 184 Cmdline: fmt.Sprintf(" bootUUID=%s", uuid6), 185 }, 186 } 187 188 opts6 := &options{ 189 kernel: "testdata/k", 190 updated: 1, 191 bootstate: bootValid, 192 } 193 194 // No opts5 at all. 195 imgs, _ := getImages(device, nil, opts6) 196 if !reflect.DeepEqual(imgs, want) { 197 t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, nil, opts6, imgs, want) 198 } 199 200 // Invalid opts5. Higher updated, but invalid state. 201 invalidOpts5 := &options{ 202 kernel: "foobar", 203 updated: 2, 204 bootstate: bootInvalid, 205 } 206 imgs, _ = getImages(device, invalidOpts5, opts6) 207 if !reflect.DeepEqual(imgs, want) { 208 t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, invalidOpts5, opts6, imgs, want) 209 } 210 } 211 212 func TestImageOrder(t *testing.T) { 213 getBlockSize = func(dev string) (int, error) { 214 return 512, nil 215 } 216 217 opt5 := &options{ 218 kernel: "foobar", 219 updated: 2, 220 bootstate: bootValid, 221 } 222 want5 := &boot.MultibootImage{ 223 Path: "foobar", 224 Cmdline: fmt.Sprintf(" bootUUID=%s", uuid5), 225 } 226 227 opt6 := &options{ 228 kernel: "testdata/k", 229 updated: 1, 230 bootstate: bootValid, 231 } 232 want6 := &boot.MultibootImage{ 233 Path: "testdata/k", 234 Cmdline: fmt.Sprintf(" bootUUID=%s", uuid6), 235 } 236 237 // Way 1. 238 want := []*boot.MultibootImage{want5, want6} 239 imgs, _ := getImages(device, opt5, opt6) 240 if !reflect.DeepEqual(imgs, want) { 241 t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opt5, opt6, imgs, want) 242 } 243 244 opt5.updated = 1 245 opt6.updated = 2 246 // Vice versa priority. 247 want = []*boot.MultibootImage{want6, want5} 248 imgs, _ = getImages(device, opt5, opt6) 249 if !reflect.DeepEqual(imgs, want) { 250 t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opt5, opt6, imgs, want) 251 } 252 }