github.com/hugelgupf/u-root@v0.0.0-20191023214958-4807c632154c/pkg/bootconfig/manifest_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 bootconfig
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestNewManifest(t *testing.T) {
    15  	m := NewManifest()
    16  	require.NotNil(t, m)
    17  	require.Equal(t, m.Version, CurrentManifestVersion)
    18  }
    19  
    20  func TestManifestFromBytes(t *testing.T) {
    21  	data := []byte(`{
    22  	"version": 1,
    23  	"configs": [
    24  		{
    25  			"name": "some_boot_config",
    26  			"kernel": "/path/to/kernel",
    27  			"initramfs": "/path/to/initramfs",
    28  			"kernel_args": "init=/bin/bash",
    29  			"devicetree": "some data here"
    30  		}
    31  	]
    32  }`)
    33  	m, err := ManifestFromBytes(data)
    34  	require.NoError(t, err)
    35  	require.Equal(t, 1, len(m.Configs))
    36  }
    37  
    38  func TestManifestFromBytesInvalid(t *testing.T) {
    39  	data := []byte(`{
    40  		"nonexisting": "baaah",
    41  		"configs": {
    42  			"broken": true
    43  		}
    44  }`)
    45  	_, err := ManifestFromBytes(data)
    46  	require.Error(t, err)
    47  }
    48  
    49  func TestManifestGetBootConfig(t *testing.T) {
    50  	data := []byte(`{
    51  	"version": 1,
    52  	"configs": [
    53  		{
    54  			"name": "some_boot_config",
    55  			"kernel": "/path/to/kernel"
    56  		}
    57  	]
    58  }`)
    59  	m, err := ManifestFromBytes(data)
    60  	require.NoError(t, err)
    61  	config, err := m.GetBootConfig(0)
    62  	require.NoError(t, err)
    63  	assert.Equal(t, "some_boot_config", config.Name)
    64  	assert.Equal(t, "/path/to/kernel", config.Kernel)
    65  }
    66  
    67  func TestManifestGetBootConfigMissing(t *testing.T) {
    68  	data := []byte(`{
    69  	"version": 1,
    70  	"configs": [
    71  		{
    72  			"name": "some_boot_config",
    73  			"kernel": "/path/to/kernel"
    74  		}
    75  	]
    76  }`)
    77  	m, err := ManifestFromBytes(data)
    78  	require.NoError(t, err)
    79  	_, err = m.GetBootConfig(1)
    80  	require.Error(t, err)
    81  }