github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/pkg/boot/jsonboot/bootconfig_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 jsonboot
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestNewBootConfig(t *testing.T) {
    14  	data := []byte(`{
    15  	"name": "some_conf",
    16  	"kernel": "/path/to/kernel",
    17  	"initramfs": "/path/to/initramfs",
    18  	"kernel_args": "init=/bin/bash",
    19  	"devicetree": "some data here"
    20  }`)
    21  	c, err := NewBootConfig(data)
    22  	require.NoError(t, err)
    23  	require.Equal(t, "some_conf", c.Name)
    24  	require.Equal(t, "/path/to/kernel", c.Kernel)
    25  	require.Equal(t, "/path/to/initramfs", c.Initramfs)
    26  	require.Equal(t, "init=/bin/bash", c.KernelArgs)
    27  	require.Equal(t, "some data here", c.DeviceTree)
    28  	require.Equal(t, true, c.IsValid())
    29  }
    30  
    31  func TestNewBootConfigInvalidJSON(t *testing.T) {
    32  	data := []byte(`{
    33  	"name": "broken
    34  }`)
    35  	_, err := NewBootConfig(data)
    36  	require.Error(t, err)
    37  }
    38  
    39  func TestNewBootConfigMissingKernel(t *testing.T) {
    40  	data := []byte(`{
    41  	"name": "some_conf",
    42  	"kernel_is_missing": "/path/to/kernel",
    43  	"initramfs": "/path/to/initramfs",
    44  	"kernel_args": "init=/bin/bash",
    45  	"devicetree": "some data here"
    46  }`)
    47  	c, err := NewBootConfig(data)
    48  	require.NoError(t, err)
    49  	require.Equal(t, false, c.IsValid())
    50  }
    51  
    52  func TestID(t *testing.T) {
    53  	bc := BootConfig{
    54  		Name: "Slash and space should not \\ appear /here",
    55  	}
    56  	id := bc.ID()
    57  	t.Log(id)
    58  }