Skip to content

Commit

Permalink
Test for missing paths
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Koch <[email protected]>
  • Loading branch information
hugelgupf committed Feb 12, 2024
1 parent 57b2564 commit e9f9e2a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 5 deletions.
6 changes: 6 additions & 0 deletions uroot/initramfs/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,17 @@
package initramfs

import (
"errors"
"io"

"github.com/u-root/mkuimage/cpio"
)

// Possible errors.
var (
ErrNoPath = errors.New("invalid argument: must specify path")
)

// ReadOpener opens a cpio.RecordReader.
type ReadOpener interface {
OpenReader() (cpio.RecordReader, error)
Expand Down
4 changes: 2 additions & 2 deletions uroot/initramfs/cpio.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _ WriteOpener = &CPIOFile{}
// OpenWriter opens c.Path for writing.
func (c *CPIOFile) OpenWriter() (Writer, error) {
if len(c.Path) == 0 {
return nil, fmt.Errorf("path is required")
return nil, fmt.Errorf("failed to write to CPIO: %w", ErrNoPath)
}
f, err := os.OpenFile(c.Path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o644)
if err != nil {
Expand All @@ -34,7 +34,7 @@ func (c *CPIOFile) OpenWriter() (Writer, error) {
// OpenReader opens c.Path for reading.
func (c *CPIOFile) OpenReader() (cpio.RecordReader, error) {
if len(c.Path) == 0 {
return nil, fmt.Errorf("path is required")
return nil, fmt.Errorf("failed to read from CPIO: %w", ErrNoPath)
}
f, err := os.Open(c.Path)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion uroot/initramfs/dir.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var _ WriteOpener = &Dir{}
// OpenWriter implements Archiver.OpenWriter.
func (d *Dir) OpenWriter() (Writer, error) {
if len(d.Path) == 0 {
return nil, fmt.Errorf("path is required")
return nil, fmt.Errorf("failed to use directory as output: %w", ErrNoPath)
}
if err := os.MkdirAll(d.Path, 0o755); err != nil && !errors.Is(err, os.ErrExist) {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion uroot/uroot.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func CreateInitramfs(logger ulog.Logger, opts Opts) error {

// Finally, write the archive.
if err := initramfs.Write(archive); err != nil {
return fmt.Errorf("error archiving: %v", err)
return fmt.Errorf("error archiving: %w", err)
}
return nil
}
Expand Down
49 changes: 48 additions & 1 deletion uroot/uroot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,10 +405,57 @@ func TestCreateInitramfs(t *testing.T) {
itest.IsEmpty{},
},
},
{
name: "cpio no path given",
opts: Opts{
TempDir: dir,
InitCmd: "/bin/systemd",
OutputFile: &initramfs.CPIOFile{},
},
errs: []error{initramfs.ErrNoPath},
},
{
name: "dir no path given",
opts: Opts{
TempDir: dir,
InitCmd: "/bin/systemd",
OutputFile: &initramfs.Dir{},
},
errs: []error{initramfs.ErrNoPath},
},
{
name: "dir failed to create",
opts: Opts{
TempDir: dir,
InitCmd: "/bin/systemd",
OutputFile: &initramfs.Dir{Path: filepath.Join(tmp400, "foobar")},
},
errs: []error{os.ErrPermission},
},
{
name: "cpio failed to create",
opts: Opts{
TempDir: dir,
InitCmd: "/bin/systemd",
OutputFile: &initramfs.CPIOFile{Path: filepath.Join(tmp400, "foobar")},
},
errs: []error{os.ErrPermission},
},
{
name: "cpio basefile no path given",
opts: Opts{
TempDir: dir,
InitCmd: "/bin/systemd",
BaseArchive: &initramfs.CPIOFile{},
},
errs: []error{initramfs.ErrNoPath},
},
} {
t.Run(fmt.Sprintf("Test %d [%s]", i, tt.name), func(t *testing.T) {
archive := cpio.InMemArchive()
tt.opts.OutputFile = &initramfs.Archive{Archive: archive}
if tt.opts.OutputFile == nil {
tt.opts.OutputFile = &initramfs.Archive{Archive: archive}
}
err := CreateInitramfs(l, tt.opts)
for _, want := range tt.errs {
if !errors.Is(err, want) {
Expand Down

0 comments on commit e9f9e2a

Please sign in to comment.