Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More u-root build tests #7

Merged
merged 7 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions uroot/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
)

var (
// BusyBox is a shared GBBBuilder instance.
BusyBox = GBBBuilder{}
// Busybox is a shared GBBBuilder instance.
Busybox = GBBBuilder{}
// Binary is a shared BinaryBuilder instance.
Binary = BinaryBuilder{}
)
Expand Down
35 changes: 27 additions & 8 deletions uroot/initramfs/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package initramfs

import (
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -73,7 +74,7 @@
// a record or file, we want to include its children anyway.
sInfo, err := os.Lstat(src)
if err != nil {
return fmt.Errorf("adding %q to archive failed because Lstat failed: %v", src, err)
return fmt.Errorf("adding %q to archive failed because Lstat failed: %w", src, err)

Check warning on line 77 in uroot/initramfs/files.go

View check run for this annotation

Codecov / codecov/patch

uroot/initramfs/files.go#L77

Added line #L77 was not covered by tests
}

// Recursively add children.
Expand All @@ -92,15 +93,23 @@
}

if record, ok := af.Records[dest]; ok {
return fmt.Errorf("record for %q already exists in archive: %v", dest, record)
return &os.PathError{
Op: "add to archive",
Path: dest,
Err: fmt.Errorf("%w: is %v", os.ErrExist, record),
}
}

if srcFile, ok := af.Files[dest]; ok {
// Just a duplicate.
if src == srcFile {
return nil
}
return fmt.Errorf("record for %q already exists in archive (is %q)", dest, src)
return &os.PathError{
Op: "add to archive",
Path: dest,
Err: fmt.Errorf("%w: backed by %q", os.ErrExist, src),
}
}

af.Files[dest] = src
Expand Down Expand Up @@ -129,21 +138,31 @@
return af.addFile(src, dest, false)
}

var errAbsoluteName = errors.New("record name must not be absolute")

// AddRecord adds a cpio.Record into the archive at `r.Name`.
func (af *Files) AddRecord(r cpio.Record) error {
r.Name = path.Clean(r.Name)
if filepath.IsAbs(r.Name) {
return fmt.Errorf("record name %q must not be absolute", r.Name)
return fmt.Errorf("%w: %q", errAbsoluteName, r.Name)
}

if src, ok := af.Files[r.Name]; ok {
return fmt.Errorf("record for %q already exists in archive: file %q", r.Name, src)
return &os.PathError{
Op: "add to archive",
Path: r.Name,
Err: fmt.Errorf("%w: backed by %q", os.ErrExist, src),
}
}
if rr, ok := af.Records[r.Name]; ok {
if rr.Info == r.Info {
if record, ok := af.Records[r.Name]; ok {
if record.Info == r.Info {
return nil
}
return fmt.Errorf("record for %q already exists in archive: %v", r.Name, rr)
return &os.PathError{
Op: "add to archive",
Path: r.Name,
Err: fmt.Errorf("%w: is %v", os.ErrExist, record),
}
}

af.Records[r.Name] = r
Expand Down
86 changes: 37 additions & 49 deletions uroot/initramfs/files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
package initramfs

import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/u-root/mkuimage/cpio"
Expand All @@ -34,12 +34,12 @@ func TestFilesAddFileNoFollow(t *testing.T) {
}

for i, tt := range []struct {
name string
af *Files
src string
dest string
result *Files
errContains string
name string
af *Files
src string
dest string
result *Files
err error
}{
{
name: "just add a file",
Expand Down Expand Up @@ -70,11 +70,8 @@ func TestFilesAddFileNoFollow(t *testing.T) {
} {
t.Run(fmt.Sprintf("Test %02d: %s", i, tt.name), func(t *testing.T) {
err := tt.af.AddFileNoFollow(tt.src, tt.dest)
if err != nil && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("Error is %v, does not contain %v", err, tt.errContains)
}
if err == nil && len(tt.errContains) > 0 {
t.Errorf("Got no error, want %v", tt.errContains)
if !errors.Is(err, tt.err) {
t.Errorf("AddFileNoFollow = %v, want %v", err, tt.err)
}

if tt.result != nil && !reflect.DeepEqual(tt.af, tt.result) {
Expand All @@ -97,25 +94,22 @@ func TestFilesAddFile(t *testing.T) {

symlinkToDir3 := filepath.Join(dir3, "fooSymDir/")
fooDir := filepath.Join(dir3, "fooDir")
//nolint:errcheck
{
os.Create(filepath.Join(dir, "foo"))
os.Create(filepath.Join(dir, "foo2"))
os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3"))
_ = os.WriteFile(filepath.Join(dir, "foo"), nil, 0o777)
_ = os.WriteFile(filepath.Join(dir, "foo2"), nil, 0o777)
_ = os.Symlink(filepath.Join(dir, "foo2"), filepath.Join(dir2, "foo3"))

os.Mkdir(fooDir, os.ModePerm)
os.Symlink(fooDir, symlinkToDir3)
os.Create(filepath.Join(fooDir, "foo"))
os.Create(filepath.Join(fooDir, "bar"))
}
_ = os.Mkdir(fooDir, os.ModePerm)
_ = os.Symlink(fooDir, symlinkToDir3)
_ = os.WriteFile(filepath.Join(fooDir, "foo"), nil, 0o777)
_ = os.WriteFile(filepath.Join(fooDir, "bar"), nil, 0o777)

for i, tt := range []struct {
name string
af *Files
src string
dest string
result *Files
errContains string
name string
af *Files
src string
dest string
result *Files
err error
}{
{
name: "just add a file",
Expand Down Expand Up @@ -171,7 +165,7 @@ func TestFilesAddFile(t *testing.T) {
"bar/foo": "/some/other/place",
},
},
errContains: "already exists in archive",
err: os.ErrExist,
},
{
name: "add a file that exists in Records",
Expand All @@ -187,7 +181,7 @@ func TestFilesAddFile(t *testing.T) {
"bar/foo": cpio.Symlink("bar/foo", "/some/other/place"),
},
},
errContains: "already exists in archive",
err: os.ErrExist,
},
{
name: "add a file that already exists in Files, but is the same one",
Expand Down Expand Up @@ -261,18 +255,15 @@ func TestFilesAddFile(t *testing.T) {
"bar/foo/foo2": "/some/place/real/zed",
},
},
src: dir,
dest: "bar/foo",
errContains: "already exists in archive",
src: dir,
dest: "bar/foo",
err: os.ErrExist,
},
} {
t.Run(fmt.Sprintf("Test %02d: %s", i, tt.name), func(t *testing.T) {
err := tt.af.AddFile(tt.src, tt.dest)
if err != nil && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("Error is %v, does not contain %v", err, tt.errContains)
}
if err == nil && len(tt.errContains) > 0 {
t.Errorf("Got no error, want %v", tt.errContains)
if !errors.Is(err, tt.err) {
t.Errorf("AddFile = %v, want %v", err, tt.err)
}

if tt.result != nil && !reflect.DeepEqual(tt.af, tt.result) {
Expand All @@ -287,8 +278,8 @@ func TestFilesAddRecord(t *testing.T) {
af *Files
record cpio.Record

result *Files
errContains string
result *Files
err error
}{
{
af: NewFiles(),
Expand All @@ -312,7 +303,7 @@ func TestFilesAddRecord(t *testing.T) {
"bar/foo": "/some/other/place",
},
},
errContains: "already exists in archive",
err: os.ErrExist,
},
{
af: &Files{
Expand All @@ -326,7 +317,7 @@ func TestFilesAddRecord(t *testing.T) {
"bar/foo": cpio.Symlink("bar/foo", "/some/other/place"),
},
},
errContains: "already exists in archive",
err: os.ErrExist,
},
{
af: &Files{
Expand All @@ -342,17 +333,14 @@ func TestFilesAddRecord(t *testing.T) {
},
},
{
record: cpio.Symlink("/bar/foo", ""),
errContains: "must not be absolute",
record: cpio.Symlink("/bar/foo", ""),
err: errAbsoluteName,
},
} {
t.Run(fmt.Sprintf("Test %02d", i), func(t *testing.T) {
err := tt.af.AddRecord(tt.record)
if err != nil && !strings.Contains(err.Error(), tt.errContains) {
t.Errorf("Error is %v, does not contain %v", err, tt.errContains)
}
if err == nil && len(tt.errContains) > 0 {
t.Errorf("Got no error, want %v", tt.errContains)
if !errors.Is(err, tt.err) {
t.Errorf("AddRecord = %v, want %v", err, tt.err)
}

if !reflect.DeepEqual(tt.af, tt.result) {
Expand Down
11 changes: 0 additions & 11 deletions uroot/initramfs/test/pkg_test.go

This file was deleted.

15 changes: 15 additions & 0 deletions uroot/initramfs/test/ramfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@
return fmt.Errorf("archive does not contain %s, but should", hf.Path)
}

type HasDir struct {
Path string
}

func (h HasDir) Validate(a *cpio.Archive) error {
r, ok := a.Get(h.Path)
if !ok {
return fmt.Errorf("archive does not contain %s, but should", h.Path)

Check warning on line 53 in uroot/initramfs/test/ramfs.go

View check run for this annotation

Codecov / codecov/patch

uroot/initramfs/test/ramfs.go#L53

Added line #L53 was not covered by tests
}
if r.Mode&cpio.S_IFDIR == 0 {
return fmt.Errorf("file %v should be directory, but isn't", h.Path)

Check warning on line 56 in uroot/initramfs/test/ramfs.go

View check run for this annotation

Codecov / codecov/patch

uroot/initramfs/test/ramfs.go#L56

Added line #L56 was not covered by tests
}
return nil
}

type HasContent struct {
Path string
Content string
Expand Down
Loading
Loading