github.com/mvdan/u-root-coreutils@v0.0.0-20230122170626-c2eef2898555/pkg/boot/jsonboot/bootconfig_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 jsonboot
     6  
     7  import (
     8  	"testing"
     9  )
    10  
    11  func TestNewBootConfig(t *testing.T) {
    12  	data := []byte(`{
    13  	"name": "some_conf",
    14  	"kernel": "/path/to/kernel",
    15  	"initramfs": "/path/to/initramfs",
    16  	"kernel_args": "init=/bin/bash",
    17  	"devicetree": "some data here"
    18  }`)
    19  	c, err := NewBootConfig(data)
    20  	if c.Name != "some_conf" || err != nil {
    21  		t.Errorf(`NewBootConfig(data).Name = %q, %v, want "some_conf", nil`, c.Name, err)
    22  	}
    23  	if c.Kernel != "/path/to/kernel" {
    24  		t.Errorf(`NewBootConfig(data).Kernel = %q, %v, want "/path/to/kernel", nil`, c.Kernel, err)
    25  	}
    26  	if c.Initramfs != "/path/to/initramfs" {
    27  		t.Errorf(`NewBootConfig(data).Initramfs = %q, %v, want "/path/to/initramfs", nil`, c.Initramfs, err)
    28  	}
    29  	if c.KernelArgs != "init=/bin/bash" {
    30  		t.Errorf(`NewBootConfig(data).KernelArgs = %q, %v, want "init=/bin/bash", nil`, c.KernelArgs, err)
    31  	}
    32  	if c.DeviceTree != "some data here" {
    33  		t.Errorf(`NewBootConfig(data).DeviceTree = %q, %v, want "some data here", nil`, c.DeviceTree, err)
    34  	}
    35  	if !c.IsValid() {
    36  		t.Errorf(`NewBootConfig(data).IsValid() = %t, %v, want "true", nil`, c.IsValid(), err)
    37  	}
    38  }
    39  
    40  func TestNewBootConfigInvalidJSON(t *testing.T) {
    41  	data := []byte(`{
    42  	"name": "broken
    43  }`)
    44  	c, err := NewBootConfig(data)
    45  	if err == nil {
    46  		t.Errorf(`NewBootConfig(data) = %q, %v, want "nil", error`, c, err)
    47  	}
    48  }
    49  
    50  func TestNewBootConfigMissingKernel(t *testing.T) {
    51  	data := []byte(`{
    52  	"name": "some_conf",
    53  	"kernel_is_missing": "/path/to/kernel",
    54  	"initramfs": "/path/to/initramfs",
    55  	"kernel_args": "init=/bin/bash",
    56  	"devicetree": "some data here"
    57  }`)
    58  	c, err := NewBootConfig(data)
    59  	if c.IsValid() != false || err != nil {
    60  		t.Errorf(`NewBootConfig(data).IsValid() = %t, %v, want "false", nil`, c.IsValid(), err)
    61  	}
    62  }
    63  
    64  func TestID(t *testing.T) {
    65  	bc := BootConfig{
    66  		Name: "Slash and space should not \\ appear /here",
    67  	}
    68  	id := bc.ID()
    69  	t.Log(id)
    70  }