github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/tools/vpdbootmanager/main_test.go (about)

     1  // Copyright 2017-2021 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  	"os"
    10  	"path"
    11  	"testing"
    12  
    13  	"github.com/mvdan/u-root-coreutils/pkg/boot/systembooter"
    14  )
    15  
    16  func TestInvalidCommand(t *testing.T) {
    17  	if err := cli([]string{"unknown"}); err.Error() != "Unrecognized action" {
    18  		t.Errorf(`err.Error() = %q, want "Unrecognized action"`, err.Error())
    19  	}
    20  }
    21  
    22  func TestNoEntryType(t *testing.T) {
    23  	if err := cli([]string{"add", "localboot"}); err.Error() != "you need to provide method" {
    24  		t.Errorf(`err.Error() = %q, want "you need to provide method"`, err.Error())
    25  	}
    26  }
    27  
    28  func TestNoAction(t *testing.T) {
    29  	if err := cli([]string{}); err.Error() != "you need to provide action" {
    30  		t.Errorf(`err.Error() = %q, want "you need to provide action"`, err.Error())
    31  	}
    32  }
    33  
    34  func TestAddNetbootEntryFull(t *testing.T) {
    35  	dir := t.TempDir()
    36  	if err := os.MkdirAll(path.Join(dir, "rw"), 0o700); err != nil {
    37  		t.Errorf(`os.MkdirAll(path.Join(%q, "rw"), 0o700) = %v, want nil`, dir, err)
    38  	}
    39  
    40  	args := []string{
    41  		"add",
    42  		"netboot",
    43  		"dhcpv6",
    44  		"aa:bb:cc:dd:ee:ff",
    45  		"-vpd-dir",
    46  		dir,
    47  	}
    48  	if err := cli(args); err != nil {
    49  		t.Errorf(`cli(%v) = %v, want nil`, args, err)
    50  	}
    51  	file, err := os.ReadFile(path.Join(dir, "rw", "Boot0001"))
    52  	if err != nil {
    53  		t.Errorf(`os.ReadFile(path.Join(%q, "rw", "Boot0001")) = %v, want nil`, file, err)
    54  	}
    55  	var out systembooter.NetBooter
    56  	if err := json.Unmarshal([]byte(file), &out); err != nil {
    57  		t.Errorf(`json.Unmarshal([]byte(%q), %v) = %v, want nil`, file, &out, err)
    58  	}
    59  	if out.Method != "dhcpv6" || out.MAC != "aa:bb:cc:dd:ee:ff" {
    60  		t.Errorf(`out.Method, out.Mac = %q, %q, want "dhcpv6", "aa:bb:cc:dd:ee:ff"`, out.Method, out.MAC)
    61  	}
    62  }
    63  
    64  func TestAddLocalbootEntryFull(t *testing.T) {
    65  	dir := t.TempDir()
    66  	if err := os.MkdirAll(path.Join(dir, "rw"), 0o700); err != nil {
    67  		t.Errorf(`os.MkdirAll(path.Join(%q, "rw"), 0o700) = %v, want nil`, dir, err)
    68  	}
    69  
    70  	args := []string{
    71  		"add",
    72  		"localboot",
    73  		"grub",
    74  		"-vpd-dir",
    75  		dir,
    76  	}
    77  	if err := cli(args); err != nil {
    78  		t.Errorf(`cli(%v) = %v, want nil`, args, err)
    79  	}
    80  	file, err := os.ReadFile(path.Join(dir, "rw", "Boot0001"))
    81  	if err != nil {
    82  		t.Errorf(`os.ReadFile(path.Join(%q, "rw", "Boot0001")) = %v, want nil`, file, err)
    83  	}
    84  	var out systembooter.NetBooter
    85  	if err := json.Unmarshal([]byte(file), &out); err != nil {
    86  		t.Errorf(`json.Unmarshal([]byte(%q), %v) = %v, want nil`, file, &out, err)
    87  	}
    88  	if out.Method != "grub" {
    89  		t.Errorf(`out.Method = %q, want "grub"`, out.Method)
    90  	}
    91  }