github.com/andrewsun2898/u-root@v6.0.1-0.20200616011413-4b2895c1b815+incompatible/pkg/boot/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  	"github.com/u-root/u-root/pkg/boot/multiboot"
    15  	"github.com/u-root/u-root/pkg/uio"
    16  )
    17  
    18  func TestParse(t *testing.T) {
    19  	for _, tt := range []struct {
    20  		file string
    21  		want options
    22  	}{
    23  		{
    24  			file: "testdata/kernel_cmdline_mods.cfg",
    25  			want: options{
    26  				kernel: "testdata/b.b00",
    27  				args:   "b.b00 zee",
    28  				modules: []module{
    29  					{
    30  						path:    "testdata/b.b00",
    31  						cmdline: "b.b00 blabla",
    32  					},
    33  					{
    34  						path:    "testdata/k.b00",
    35  						cmdline: "k.b00",
    36  					},
    37  					{
    38  						path:    "testdata/m.m00",
    39  						cmdline: "m.m00 marg marg2",
    40  					},
    41  				},
    42  			},
    43  		},
    44  		{
    45  			file: "testdata/kernelopt_first.cfg",
    46  			want: options{
    47  				kernel: "testdata/b.b00",
    48  				args:   "b.b00 zee",
    49  			},
    50  		},
    51  		{
    52  			file: "testdata/empty_mods.cfg",
    53  			want: options{
    54  				kernel: "testdata/b.b00",
    55  				args:   "b.b00 zee",
    56  			},
    57  		},
    58  		{
    59  			file: "testdata/no_mods.cfg",
    60  			want: options{
    61  				kernel: "testdata/b.b00",
    62  				args:   "b.b00 zee",
    63  			},
    64  		},
    65  		{
    66  			file: "testdata/no_cmdline.cfg",
    67  			want: options{
    68  				kernel: "testdata/b.b00",
    69  				args:   "b.b00 ",
    70  			},
    71  		},
    72  		{
    73  			file: "testdata/empty_cmdline.cfg",
    74  			want: options{
    75  				kernel: "testdata/b.b00",
    76  				args:   "b.b00 ",
    77  			},
    78  		},
    79  		{
    80  			file: "testdata/empty_updated.cfg",
    81  			want: options{
    82  				kernel: "testdata/b.b00",
    83  				args:   "b.b00 zee",
    84  				// Explicitly stating this as the wanted value.
    85  				updated: 0,
    86  			},
    87  		},
    88  		{
    89  			file: "testdata/updated_twice.cfg",
    90  			want: options{
    91  				kernel: "testdata/b.b00",
    92  				args:   "b.b00 zee",
    93  				// Explicitly stating this as the wanted value.
    94  				updated: 0,
    95  			},
    96  		},
    97  		{
    98  			file: "testdata/updated.cfg",
    99  			want: options{
   100  				kernel:  "testdata/b.b00",
   101  				args:    "b.b00 zee",
   102  				updated: 4,
   103  			},
   104  		},
   105  		{
   106  			file: "testdata/empty_bootstate.cfg",
   107  			want: options{
   108  				kernel: "testdata/b.b00",
   109  				args:   "b.b00 zee",
   110  				// Explicitly stating this as the wanted value.
   111  				bootstate: bootValid,
   112  			},
   113  		},
   114  		{
   115  			file: "testdata/bootstate_twice.cfg",
   116  			want: options{
   117  				kernel: "testdata/b.b00",
   118  				args:   "b.b00 zee",
   119  				// Explicitly stating this as the wanted value.
   120  				bootstate: bootValid,
   121  			},
   122  		},
   123  		{
   124  			file: "testdata/bootstate.cfg",
   125  			want: options{
   126  				kernel:    "testdata/b.b00",
   127  				args:      "b.b00 zee",
   128  				bootstate: bootDirty,
   129  			},
   130  		},
   131  		{
   132  			file: "testdata/bootstate_invalid.cfg",
   133  			want: options{
   134  				kernel:    "testdata/b.b00",
   135  				args:      "b.b00 zee",
   136  				bootstate: bootInvalid,
   137  			},
   138  		},
   139  		{
   140  			file: "testdata/no_bootstate.cfg",
   141  			want: options{
   142  				kernel:    "testdata/b.b00",
   143  				args:      "b.b00 zee",
   144  				bootstate: bootInvalid,
   145  			},
   146  		},
   147  	} {
   148  		got, err := parse(tt.file)
   149  		if err != nil {
   150  			t.Fatalf("cannot parse config at %s: %v", tt.file, err)
   151  		}
   152  
   153  		if !reflect.DeepEqual(got, tt.want) {
   154  			t.Errorf("LoadConfig(%s) = %#v want %#v", tt.file, got, tt.want)
   155  		}
   156  	}
   157  }
   158  
   159  // This is in the second block of testdata/dev5 and testdata/dev6.
   160  var (
   161  	dev5GUID = "aabbccddeeff0011"
   162  	dev6GUID = "00112233445566aa"
   163  	uuid5    = hex.EncodeToString([]byte(dev5GUID))
   164  	uuid6    = hex.EncodeToString([]byte(dev6GUID))
   165  	device   = "testdata/dev"
   166  )
   167  
   168  // Poor man's equal.
   169  //
   170  // the Kernel and Modules fields will be full of uio.NewLazyFiles. We just want
   171  // them to be pointing to the same file name; we can't compare the function
   172  // pointers obviously. Lazy files will always print their name.
   173  func multibootEqual(a, b []*boot.MultibootImage) bool {
   174  	return fmt.Sprintf("%v", a) == fmt.Sprintf("%v", b)
   175  }
   176  
   177  func TestDev5Valid(t *testing.T) {
   178  	want := []*boot.MultibootImage{
   179  		{
   180  			Name:    "VMware ESXi from testdata/dev5",
   181  			Kernel:  uio.NewLazyFile("testdata/k"),
   182  			Cmdline: fmt.Sprintf(" bootUUID=%s", uuid5),
   183  			Modules: []multiboot.Module{},
   184  		},
   185  	}
   186  
   187  	opts5 := &options{
   188  		kernel:    "testdata/k",
   189  		updated:   1,
   190  		bootstate: bootValid,
   191  	}
   192  
   193  	// No opts6 at all.
   194  	imgs, _ := getImages(device, opts5, nil)
   195  	if !multibootEqual(imgs, want) {
   196  		t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opts5, nil, imgs, want)
   197  	}
   198  
   199  	// Invalid opts6. Higher updated, but invalid state.
   200  	invalidOpts6 := &options{
   201  		kernel:    "foobar",
   202  		updated:   2,
   203  		bootstate: bootInvalid,
   204  	}
   205  	imgs, _ = getImages(device, opts5, invalidOpts6)
   206  	if !multibootEqual(imgs, want) {
   207  		t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opts5, invalidOpts6, imgs, want)
   208  	}
   209  }
   210  
   211  func TestDev6Valid(t *testing.T) {
   212  	want := []*boot.MultibootImage{
   213  		{
   214  			Name:    "VMware ESXi from testdata/dev6",
   215  			Kernel:  uio.NewLazyFile("testdata/k"),
   216  			Cmdline: fmt.Sprintf(" bootUUID=%s", uuid6),
   217  			Modules: []multiboot.Module{},
   218  		},
   219  	}
   220  
   221  	opts6 := &options{
   222  		kernel:    "testdata/k",
   223  		updated:   1,
   224  		bootstate: bootValid,
   225  	}
   226  
   227  	// No opts5 at all.
   228  	imgs, _ := getImages(device, nil, opts6)
   229  	if !multibootEqual(imgs, want) {
   230  		t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, nil, opts6, imgs, want)
   231  	}
   232  
   233  	// Invalid opts5. Higher updated, but invalid state.
   234  	invalidOpts5 := &options{
   235  		kernel:    "foobar",
   236  		updated:   2,
   237  		bootstate: bootInvalid,
   238  	}
   239  	imgs, _ = getImages(device, invalidOpts5, opts6)
   240  	if !multibootEqual(imgs, want) {
   241  		t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, invalidOpts5, opts6, imgs, want)
   242  	}
   243  }
   244  
   245  func TestImageOrder(t *testing.T) {
   246  	prevGetBlockSize := getBlockSize
   247  	defer func() {
   248  		getBlockSize = prevGetBlockSize
   249  	}()
   250  	getBlockSize = func(dev string) (int, error) {
   251  		return 512, nil
   252  	}
   253  
   254  	opt5 := &options{
   255  		kernel:    "foobar",
   256  		updated:   2,
   257  		bootstate: bootValid,
   258  	}
   259  	want5 := &boot.MultibootImage{
   260  		Name:    "VMware ESXi from testdata/dev5",
   261  		Kernel:  uio.NewLazyFile("foobar"),
   262  		Cmdline: fmt.Sprintf(" bootUUID=%s", uuid5),
   263  		Modules: []multiboot.Module{},
   264  	}
   265  
   266  	opt6 := &options{
   267  		kernel:    "testdata/k",
   268  		updated:   1,
   269  		bootstate: bootValid,
   270  	}
   271  	want6 := &boot.MultibootImage{
   272  		Name:    "VMware ESXi from testdata/dev6",
   273  		Kernel:  uio.NewLazyFile("testdata/k"),
   274  		Cmdline: fmt.Sprintf(" bootUUID=%s", uuid6),
   275  		Modules: []multiboot.Module{},
   276  	}
   277  
   278  	// Way 1.
   279  	want := []*boot.MultibootImage{want5, want6}
   280  	imgs, _ := getImages(device, opt5, opt6)
   281  	if !multibootEqual(imgs, want) {
   282  		t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opt5, opt6, imgs, want)
   283  	}
   284  
   285  	opt5.updated = 1
   286  	opt6.updated = 2
   287  	// Vice versa priority.
   288  	want = []*boot.MultibootImage{want6, want5}
   289  	imgs, _ = getImages(device, opt5, opt6)
   290  	if !multibootEqual(imgs, want) {
   291  		t.Fatalf("getImages(%s, %v, %v) = %v, want %v", device, opt5, opt6, imgs, want)
   292  	}
   293  }