github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/tools/vpdbootmanager/main_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  	"io/ioutil"
    10  	"log"
    11  	"os"
    12  	"path"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/require"
    16  	"github.com/u-root/u-root/pkg/boot/systembooter"
    17  )
    18  
    19  func TestInvalidCommand(t *testing.T) {
    20  	err := cli([]string{"unknown"})
    21  	require.Equal(t, "Unrecognized action", err.Error())
    22  }
    23  
    24  func TestNoEntryType(t *testing.T) {
    25  	err := cli([]string{"add", "localboot"})
    26  	require.Equal(t, "You need to provide method", err.Error())
    27  }
    28  
    29  func TestNoAction(t *testing.T) {
    30  	err := cli([]string{})
    31  	require.Equal(t, "You need to provide action", err.Error())
    32  }
    33  
    34  func TestAddNetbootEntryFull(t *testing.T) {
    35  	dir, err := ioutil.TempDir("", "vpdbootmanager")
    36  	if err != nil {
    37  		log.Fatal(err)
    38  	}
    39  	os.MkdirAll(path.Join(dir, "rw"), 0700)
    40  	defer os.RemoveAll(dir)
    41  	err = cli([]string{
    42  		"add",
    43  		"netboot",
    44  		"dhcpv6",
    45  		"aa:bb:cc:dd:ee:ff",
    46  		"-vpd-dir",
    47  		dir,
    48  	})
    49  	require.NoError(t, err)
    50  	file, err := ioutil.ReadFile(path.Join(dir, "rw", "Boot0001"))
    51  	require.NoError(t, err)
    52  	var out systembooter.NetBooter
    53  	err = json.Unmarshal([]byte(file), &out)
    54  	require.NoError(t, err)
    55  	require.Equal(t, "dhcpv6", out.Method)
    56  	require.Equal(t, "aa:bb:cc:dd:ee:ff", out.MAC)
    57  }
    58  
    59  func TestAddLocalbootEntryFull(t *testing.T) {
    60  	dir, err := ioutil.TempDir("", "vpdbootmanager")
    61  	if err != nil {
    62  		log.Fatal(err)
    63  	}
    64  	os.MkdirAll(path.Join(dir, "rw"), 0700)
    65  	defer os.RemoveAll(dir)
    66  	err = cli([]string{
    67  		"add",
    68  		"localboot",
    69  		"grub",
    70  		"-vpd-dir",
    71  		dir,
    72  	})
    73  	require.NoError(t, err)
    74  	file, err := ioutil.ReadFile(path.Join(dir, "rw", "Boot0001"))
    75  	require.NoError(t, err)
    76  	var out systembooter.NetBooter
    77  	err = json.Unmarshal([]byte(file), &out)
    78  	require.NoError(t, err)
    79  	require.Equal(t, "grub", out.Method)
    80  }