/
same_image.go
92 lines (77 loc) · 2.52 KB
/
same_image.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright 2020 the u-root Authors. All rights reserved
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package boottest
import (
"fmt"
"io"
"github.com/u-root/u-root/pkg/boot"
"github.com/u-root/u-root/pkg/uio"
)
func mustReadAll(r io.ReaderAt) string {
if r == nil {
return ""
}
b, err := uio.ReadAll(r)
if err != nil {
return fmt.Sprintf("read error: %s", err)
}
return string(b)
}
// SameBootImage compares the contents of given boot images, but not the
// underlying URLs.
//
// Works for Linux and Multiboot images.
func SameBootImage(got, want boot.OSImage) error {
if got.Label() != want.Label() {
return fmt.Errorf("got image label %s, want %s", got.Label(), want.Label())
}
if gotLinux, ok := got.(*boot.LinuxImage); ok {
wantLinux, ok := want.(*boot.LinuxImage)
if !ok {
return fmt.Errorf("got image %s is Linux image, but %s is not", got, want)
}
// Same kernel?
if !uio.ReaderAtEqual(gotLinux.Kernel, wantLinux.Kernel) {
return fmt.Errorf("got kernel %s, want %s", mustReadAll(gotLinux.Kernel), mustReadAll(wantLinux.Kernel))
}
// Same initrd?
if !uio.ReaderAtEqual(gotLinux.Initrd, wantLinux.Initrd) {
return fmt.Errorf("got initrd %s, want %s", mustReadAll(gotLinux.Initrd), mustReadAll(wantLinux.Initrd))
}
// Same cmdline?
if gotLinux.Cmdline != wantLinux.Cmdline {
return fmt.Errorf("got cmdline %s, want %s", gotLinux.Cmdline, wantLinux.Cmdline)
}
return nil
}
if gotMB, ok := got.(*boot.MultibootImage); ok {
wantMB, ok := want.(*boot.MultibootImage)
if !ok {
return fmt.Errorf("got image %s is Multiboot image, but %s is not", got, want)
}
// Same kernel?
if !uio.ReaderAtEqual(gotMB.Kernel, wantMB.Kernel) {
return fmt.Errorf("got kernel %s, want %s", mustReadAll(gotMB.Kernel), mustReadAll(wantMB.Kernel))
}
// Same cmdline?
if gotMB.Cmdline != wantMB.Cmdline {
return fmt.Errorf("got cmdline %s, want %s", gotMB.Cmdline, wantMB.Cmdline)
}
if len(gotMB.Modules) != len(wantMB.Modules) {
return fmt.Errorf("got %d modules, want %d modules", len(gotMB.Modules), len(wantMB.Modules))
}
for i := range gotMB.Modules {
g := gotMB.Modules[i]
w := wantMB.Modules[i]
if g.Cmdline != w.Cmdline {
return fmt.Errorf("module %d got name %s, want %s", i, g.Cmdline, w.Cmdline)
}
if !uio.ReaderAtEqual(g.Module, w.Module) {
return fmt.Errorf("got kernel %s, want %s", mustReadAll(g.Module), mustReadAll(w.Module))
}
}
return nil
}
return fmt.Errorf("image not supported")
}