Skip to content

Commit f08ffd7

Browse files
committed
Add the integration IO test as an example
This IMHO is superior to what we're doing today. Skip the test if we do not have a u-root command Signed-off-by: Ronald G Minnich <[email protected]>
1 parent 25153e0 commit f08ffd7

File tree

3 files changed

+43
-8
lines changed

3 files changed

+43
-8
lines changed

initramfs/initramfs.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ type Image struct {
5454
}
5555

5656
var images = map[string]Image{
57-
"linux_amd64": Image{Kernel: kernel_linux_amd64, InitRAMFS: linux_amd64, Cmd: []string{"qemu-system-x86_64", "-m", "1G"}},
58-
"linux_arm64": Image{Kernel: kernel_linux_arm64, InitRAMFS: linux_arm64, Cmd: []string{"qemu-system-aarch64", "-machine", "virt", "-cpu", "max", "-m", "1G"}},
59-
"linux_arm": Image{Kernel: kernel_linux_arm, InitRAMFS: linux_arm, Cmd: []string{"qemu-system-arm", "-M", "virt,highmem=off"}},
60-
"linux_riscv64": Image{Kernel: kernel_linux_riscv64, InitRAMFS: linux_riscv64, Cmd: []string{"qemu-system-riscv64", "-M", "virt", "-cpu", "rv64", "-m", "1G"}},
57+
"linux_amd64": {Kernel: kernel_linux_amd64, InitRAMFS: linux_amd64, Cmd: []string{"qemu-system-x86_64", "-m", "1G"}},
58+
"linux_arm64": {Kernel: kernel_linux_arm64, InitRAMFS: linux_arm64, Cmd: []string{"qemu-system-aarch64", "-machine", "virt", "-cpu", "max", "-m", "1G"}},
59+
"linux_arm": {Kernel: kernel_linux_arm, InitRAMFS: linux_arm, Cmd: []string{"qemu-system-arm", "-M", "virt,highmem=off"}},
60+
"linux_riscv64": {Kernel: kernel_linux_riscv64, InitRAMFS: linux_riscv64, Cmd: []string{"qemu-system-riscv64", "-M", "virt", "-cpu", "rv64", "-m", "1G"}},
6161
}
6262

6363
func New(kernel, arch string) (*Image, error) {
@@ -171,6 +171,6 @@ func (i *Image) CPUCommand(arg string, args ...string) (*client.Cmd, error) {
171171
if err := cpu.Dial(); err != nil {
172172
return nil, err
173173
}
174-
cpu.Env = append(cpu.Env, "PWD=/")
174+
cpu.Env = append(cpu.Env, "PATH=/bbin", "PWD=/", "SHELL=/bbin/x")
175175
return cpu, nil
176176
}
Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,16 @@ import (
1111
"errors"
1212
"os"
1313
"path/filepath"
14+
"strings"
1415
"testing"
1516

1617
"github.com/u-root/cpu/client"
1718
"github.com/u-root/cpu/initramfs"
1819
)
1920

20-
func TestCPU(t *testing.T) {
21+
// TestCPUAMD64 tests both general and specific things. The specific parts are the io and cmos commands.
22+
// It being cheaper to use a single generated initramfs, we use the full u-root for several tests.
23+
func TestCPUAMD64(t *testing.T) {
2124
d := t.TempDir()
2225
i, err := initramfs.New("linux", "amd64")
2326
if !errors.Is(err, nil) {
@@ -34,8 +37,9 @@ func TestCPU(t *testing.T) {
3437

3538
n, err := initramfs.Uroot(d)
3639
if err != nil {
37-
t.Fatal(err)
40+
t.Skipf("skipping this test as we have no uroot command")
3841
}
42+
3943
c, err := i.CommandContext(ctx, d, n)
4044
if err != nil {
4145
t.Fatalf("starting VM: got %v, want nil", err)
@@ -56,7 +60,8 @@ func TestCPU(t *testing.T) {
5660
} {
5761
cpu, err := i.CPUCommand(tt.cmd, tt.args...)
5862
if err != nil {
59-
t.Fatalf("CPUCommand: got %v, want nil", err)
63+
t.Errorf("CPUCommand: got %v, want nil", err)
64+
continue
6065
}
6166
client.SetVerbose(t.Logf)
6267

@@ -73,4 +78,34 @@ func TestCPU(t *testing.T) {
7378
if string(b) != "hi" {
7479
t.Fatalf("file b: got %q, want %q", b, "hi")
7580
}
81+
82+
for _, tt := range []struct {
83+
args string
84+
out string
85+
}{
86+
{args: "cw 14 1", out: ""},
87+
{args: "cr 14", out: "0x01\n"},
88+
{args: "cw 14 0", out: ""},
89+
{args: "cr 14", out: "0x00\n"},
90+
} {
91+
cpu, err := i.CPUCommand("/bbin/io", strings.Split(tt.args, " ")...)
92+
if err != nil {
93+
t.Fatalf("CPUCommand: got %v, want nil", err)
94+
}
95+
client.SetVerbose(t.Logf)
96+
97+
b, err := cpu.CombinedOutput()
98+
if err != nil {
99+
t.Errorf("io %s: got %v, want nil", tt.args, err)
100+
}
101+
if string(b) != tt.out {
102+
t.Errorf("io %s: got %v, want %v", tt.args, string(b), tt.out)
103+
}
104+
t.Logf("io %s = %q", tt.args, string(b))
105+
}
106+
107+
// The io integration tests include writing to 3f8. There's no need to do that,
108+
// the cmos write tests all that needs testing, as it uses inb and outb,
109+
// and uart hardware is fickle. The test above is enough.
110+
76111
}

0 commit comments

Comments
 (0)