github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/tools/vpdbootmanager/add_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 main
     6  
     7  import (
     8  	"encoding/json"
     9  	"fmt"
    10  	"io/ioutil"
    11  	"log"
    12  	"os"
    13  	"path"
    14  	"testing"
    15  
    16  	"github.com/stretchr/testify/require"
    17  	"github.com/u-root/u-root/pkg/boot/systembooter"
    18  	"github.com/u-root/u-root/pkg/vpd"
    19  )
    20  
    21  func TestParseNetboot(t *testing.T) {
    22  	b, err := parseNetbootFlags("dhcpv4", "aa:bb:cc:dd:ee:ff", []string{})
    23  	require.NoError(t, err)
    24  	require.Equal(t, "netboot", b.Type)
    25  	require.Equal(t, "dhcpv4", b.Method)
    26  	require.Equal(t, "aa:bb:cc:dd:ee:ff", b.MAC)
    27  	require.Nil(t, b.OverrideURL)
    28  	require.Nil(t, b.Retries)
    29  }
    30  
    31  func TestParseNetbootWithFlags(t *testing.T) {
    32  	b, err := parseNetbootFlags("dhcpv4", "aa:bb:cc:dd:ee:ff", []string{
    33  		"-override-url",
    34  		"http://url",
    35  		"-retries",
    36  		"1",
    37  		"-vpd-dir",
    38  		"test",
    39  	})
    40  	require.NoError(t, err)
    41  	require.Equal(t, "http://url", *b.OverrideURL)
    42  	require.Equal(t, 1, *b.Retries)
    43  	require.Equal(t, "test", vpd.VpdDir)
    44  }
    45  
    46  func TestParseLocalboot(t *testing.T) {
    47  	b, err := parseLocalbootFlags("grub", []string{})
    48  	require.NoError(t, err)
    49  	require.Equal(t, "grub", b.Method)
    50  
    51  	b, err = parseLocalbootFlags("path", []string{
    52  		"device",
    53  		"path",
    54  	})
    55  	require.NoError(t, err)
    56  	require.Equal(t, "path", b.Method)
    57  	require.Equal(t, "device", b.DeviceGUID)
    58  	require.Equal(t, "path", b.Kernel)
    59  }
    60  
    61  func TestParseLocalbootWithFlags(t *testing.T) {
    62  	b, err := parseLocalbootFlags("grub", []string{
    63  		"-kernel-args",
    64  		"kernel-argument-test",
    65  		"-ramfs",
    66  		"ramfs-test",
    67  		"-vpd-dir",
    68  		"test",
    69  	})
    70  	require.NoError(t, err)
    71  	require.Equal(t, "grub", b.Method)
    72  	require.Equal(t, "kernel-argument-test", b.KernelArgs)
    73  	require.Equal(t, "ramfs-test", b.Initramfs)
    74  	require.Equal(t, "test", vpd.VpdDir)
    75  
    76  	b, err = parseLocalbootFlags("path", []string{
    77  		"device",
    78  		"path",
    79  		"-kernel-args",
    80  		"kernel-argument-test",
    81  		"-ramfs",
    82  		"ramfs-test",
    83  		"-vpd-dir",
    84  		"test",
    85  	})
    86  	require.NoError(t, err)
    87  	require.Equal(t, "path", b.Method)
    88  	require.Equal(t, "device", b.DeviceGUID)
    89  	require.Equal(t, "path", b.Kernel)
    90  	require.Equal(t, "kernel-argument-test", b.KernelArgs)
    91  	require.Equal(t, "ramfs-test", b.Initramfs)
    92  	require.Equal(t, "test", vpd.VpdDir)
    93  }
    94  
    95  func TestFailGracefullyMissingArg(t *testing.T) {
    96  	err := add("localboot", []string{})
    97  	require.Equal(t, "You need to provide method", err.Error())
    98  
    99  	err = add("localboot", []string{"path"})
   100  	require.Equal(t, "You need to pass DeviceGUID and Kernel path", err.Error())
   101  
   102  	err = add("localboot", []string{"path", "device"})
   103  	require.Equal(t, "You need to pass DeviceGUID and Kernel path", err.Error())
   104  
   105  	err = add("netboot", []string{})
   106  	require.Equal(t, "You need to pass method and MAC address", err.Error())
   107  
   108  	err = add("netboot", []string{"dhcpv6"})
   109  	require.Equal(t, "You need to pass method and MAC address", err.Error())
   110  }
   111  
   112  func TestFailGracefullyBadMACAddress(t *testing.T) {
   113  	err := add("netboot", []string{"dhcpv6", "test"})
   114  	require.Equal(t, "address test: invalid MAC address", err.Error())
   115  }
   116  
   117  func TestFailGracefullyBadNetworkType(t *testing.T) {
   118  	err := add("netboot", []string{"not-valid", "test"})
   119  	require.Equal(t, "Method needs to be either dhcpv4 or dhcpv6", err.Error())
   120  }
   121  
   122  func TestFailGracefullyBadLocalbootType(t *testing.T) {
   123  	err := add("localboot", []string{"not-valid"})
   124  	require.Equal(t, "Method needs to be grub or path", err.Error())
   125  }
   126  
   127  func TestFailGracefullyUnknownEntryType(t *testing.T) {
   128  	err := add("test", []string{})
   129  	require.Equal(t, "Unknown entry type", err.Error())
   130  }
   131  
   132  func TestAddBootEntry(t *testing.T) {
   133  	dir, err := ioutil.TempDir("", "vpdbootmanager")
   134  	if err != nil {
   135  		log.Fatal(err)
   136  	}
   137  	os.MkdirAll(path.Join(dir, "rw"), 0700)
   138  	defer os.RemoveAll(dir)
   139  	vpd.VpdDir = dir
   140  	err = addBootEntry(&systembooter.LocalBooter{
   141  		Method: "grub",
   142  	})
   143  	require.NoError(t, err)
   144  	file, err := ioutil.ReadFile(path.Join(dir, "rw", "Boot0001"))
   145  	require.NoError(t, err)
   146  	var out systembooter.LocalBooter
   147  	err = json.Unmarshal([]byte(file), &out)
   148  	require.NoError(t, err)
   149  	require.Equal(t, "grub", out.Method)
   150  }
   151  
   152  func TestAddBootEntryMultiple(t *testing.T) {
   153  	dir, err := ioutil.TempDir("", "vpdbootmanager")
   154  	if err != nil {
   155  		log.Fatal(err)
   156  	}
   157  	os.MkdirAll(path.Join(dir, "rw"), 0700)
   158  	defer os.RemoveAll(dir)
   159  	vpd.VpdDir = dir
   160  	for i := 1; i < 5; i++ {
   161  		err = addBootEntry(&systembooter.LocalBooter{
   162  			Method: "grub",
   163  		})
   164  		require.NoError(t, err)
   165  		file, err := ioutil.ReadFile(path.Join(dir, "rw", fmt.Sprintf("Boot%04d", i)))
   166  		require.NoError(t, err)
   167  		var out systembooter.LocalBooter
   168  		err = json.Unmarshal([]byte(file), &out)
   169  		require.NoError(t, err)
   170  		require.Equal(t, "grub", out.Method)
   171  	}
   172  }