github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+incompatible/integration/multiboot_test.go (about)

     1  // Copyright 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 integration
     6  
     7  import (
     8  	"bytes"
     9  	"encoding/json"
    10  	"fmt"
    11  	"reflect"
    12  	"testing"
    13  
    14  	"github.com/u-root/u-root/pkg/multiboot"
    15  )
    16  
    17  func testMultiboot(t *testing.T, kernel string) {
    18  	var serial wc
    19  	q, cleanup := QEMUTest(t, &Options{
    20  		Files: []string{
    21  			fmt.Sprintf("/home/circleci/%v:kernel", kernel),
    22  		},
    23  		Cmds: []string{
    24  			"github.com/u-root/u-root/cmds/init",
    25  			"github.com/u-root/u-root/cmds/kexec",
    26  		},
    27  		Uinit: []string{
    28  			`kexec -l kernel -e -d --module="/kernel foo=bar" --module="/bbin/bb"`,
    29  		},
    30  		SerialOutput: &serial,
    31  	})
    32  	defer cleanup()
    33  
    34  	if err := q.Expect(`"status": "ok"`); err != nil {
    35  		t.Fatalf(`expected '"status": "ok"', got error: %v`, err)
    36  	}
    37  
    38  	output := serial.Bytes()
    39  
    40  	i := bytes.Index(output, []byte(multiboot.DebugPrefix))
    41  	if i == -1 {
    42  		t.Fatalf("%q prefix not found in output", multiboot.DebugPrefix)
    43  	}
    44  	output = output[i+len(multiboot.DebugPrefix):]
    45  	if i = bytes.Index(output, []byte{'\n'}); i == -1 {
    46  		t.Fatalf("Cannot find newline character")
    47  	}
    48  	var want multiboot.Description
    49  	if err := json.Unmarshal(output[:i], &want); err != nil {
    50  		t.Fatalf("Cannot unmarshal multiboot debug information: %v", err)
    51  	}
    52  
    53  	const starting = "Starting multiboot kernel"
    54  	if i = bytes.Index(output, []byte(starting)); i == -1 {
    55  		t.Fatalf("Multiboot kernel was not executed")
    56  	}
    57  	output = output[i+len(starting):]
    58  
    59  	var got multiboot.Description
    60  	if err := json.Unmarshal(output, &got); err != nil {
    61  		t.Fatalf("Cannot unmarshal multiboot information from executed kernel: %v", err)
    62  	}
    63  	if !reflect.DeepEqual(got, want) {
    64  		t.Errorf("kexec failed: got %v, want %v", got, want)
    65  	}
    66  }
    67  
    68  func TestMultiboot(t *testing.T) {
    69  	// TODO: support arm
    70  	if TestArch() != "amd64" {
    71  		t.Skipf("test not supported on %s", TestArch())
    72  	}
    73  
    74  	for _, kernel := range []string{"/kernel", "/kernel.gz"} {
    75  		t.Run(kernel, func(t *testing.T) {
    76  			testMultiboot(t, kernel)
    77  		})
    78  	}
    79  }