gitee.com/mirrors_u-root/u-root@v7.0.0+incompatible/pkg/boot/linux_test.go (about)

     1  // Copyright 2017-2020 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 boot
     6  
     7  import (
     8  	"fmt"
     9  	"io/ioutil"
    10  	"net/url"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/u-root/u-root/pkg/curl"
    16  	"github.com/u-root/u-root/pkg/uio"
    17  	"github.com/u-root/u-root/pkg/vfile"
    18  )
    19  
    20  func TestLinuxLabel(t *testing.T) {
    21  	dir, err := ioutil.TempDir("", "foo")
    22  	if err != nil {
    23  		t.Fatal(err)
    24  	}
    25  	defer os.RemoveAll(dir)
    26  
    27  	osKernel, err := os.Create(filepath.Join(dir, "kernel"))
    28  	if err != nil {
    29  		t.Fatal(err)
    30  	}
    31  
    32  	osInitrd, err := os.Create(filepath.Join(dir, "initrd"))
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  
    37  	k, _ := url.Parse("http://127.0.0.1/kernel")
    38  	i1, _ := url.Parse("http://127.0.0.1/initrd1")
    39  	i2, _ := url.Parse("http://127.0.0.1/initrd2")
    40  	httpKernel, _ := curl.LazyFetch(k)
    41  	httpInitrd1, _ := curl.LazyFetch(i1)
    42  	httpInitrd2, _ := curl.LazyFetch(i2)
    43  
    44  	for _, tt := range []struct {
    45  		desc string
    46  		img  *LinuxImage
    47  		want string
    48  	}{
    49  		{
    50  			desc: "os.File",
    51  			img: &LinuxImage{
    52  				Kernel: osKernel,
    53  				Initrd: osInitrd,
    54  			},
    55  			want: fmt.Sprintf("Linux(kernel=%s/kernel initrd=%s/initrd)", dir, dir),
    56  		},
    57  		{
    58  			desc: "lazy file",
    59  			img: &LinuxImage{
    60  				Kernel: uio.NewLazyFile(filepath.Join(dir, "kernel")),
    61  				Initrd: uio.NewLazyFile(filepath.Join(dir, "initrd")),
    62  			},
    63  			want: fmt.Sprintf("Linux(kernel=%s/kernel initrd=%s/initrd)", dir, dir),
    64  		},
    65  		{
    66  			desc: "concat lazy file",
    67  			img: &LinuxImage{
    68  				Kernel: uio.NewLazyFile(filepath.Join(dir, "kernel")),
    69  				Initrd: CatInitrds(
    70  					uio.NewLazyFile(filepath.Join(dir, "initrd")),
    71  					uio.NewLazyFile(filepath.Join(dir, "initrd")),
    72  				),
    73  			},
    74  			want: fmt.Sprintf("Linux(kernel=%s/kernel initrd=%s/initrd,%s/initrd)", dir, dir, dir),
    75  		},
    76  		{
    77  			desc: "curl lazy file",
    78  			img: &LinuxImage{
    79  				Kernel: httpKernel,
    80  				Initrd: CatInitrds(
    81  					httpInitrd1,
    82  					httpInitrd2,
    83  				),
    84  			},
    85  			want: "Linux(kernel=http://127.0.0.1/kernel initrd=http://127.0.0.1/initrd1,http://127.0.0.1/initrd2)",
    86  		},
    87  		{
    88  			desc: "verified file",
    89  			img: &LinuxImage{
    90  				Kernel: &vfile.File{Reader: nil, FileName: "/boot/foobar"},
    91  				Initrd: CatInitrds(
    92  					&vfile.File{Reader: nil, FileName: "/boot/initrd1"},
    93  					&vfile.File{Reader: nil, FileName: "/boot/initrd2"},
    94  				),
    95  			},
    96  			want: "Linux(kernel=/boot/foobar initrd=/boot/initrd1,/boot/initrd2)",
    97  		},
    98  	} {
    99  		t.Run(tt.desc, func(t *testing.T) {
   100  			got := tt.img.Label()
   101  			if got != tt.want {
   102  				t.Errorf("Label() = %s, want %s", got, tt.want)
   103  			}
   104  		})
   105  	}
   106  }