Skip to content

Commit

Permalink
refactor(initrd): Consolidate common test variables and methods
Browse files Browse the repository at this point in the history
This commit homogenizes the tests such that they all use the same
reference point.  The expected headers list is common to all
tests.  The source of truth is the `rootfs.Dockerfile` and the
directory contents was generated with:

```bash
docker build -o ./rootfs -f ./rootfs.Dockerfile .
```

The tarball was generated subsequently like so:

```bash
cd rootfs; tar --sort=name -czvf ../rootfs.tar.gz .
```

Signed-off-by: Alexander Jung <alex@unikraft.io>
  • Loading branch information
nderjung committed Jan 13, 2025
1 parent fa2bcbf commit 017347d
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 107 deletions.
45 changes: 2 additions & 43 deletions initrd/directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,6 @@ func TestNewFromDirectory(t *testing.T) {

r := cpio.NewReader(openFile(t, irdPath))

expectHeaders := map[string]cpio.Header{
"./entrypoint.sh": {
Mode: cpio.TypeReg,
Size: 25,
},
"./etc": {
Mode: cpio.TypeDir,
},
"./etc/app.conf": {
Mode: cpio.TypeReg,
Size: 16,
},
"./lib": {
Mode: cpio.TypeDir,
},
"./lib/libtest.so.1": {
Mode: cpio.TypeSymlink,
Linkname: "libtest.so.1.0.0",
},
"./lib/libtest.so.1.0.0": {
Mode: cpio.TypeReg,
Size: 9,
},
}

for {
hdr, _, err := r.Next()
if err == io.EOF {
Expand All @@ -82,25 +57,9 @@ func TestNewFromDirectory(t *testing.T) {
if hdr.Linkname != expectHdr.Linkname {
t.Errorf("file [%s]: got linkname %q, expected %q", hdr.Name, hdr.Linkname, expectHdr.Linkname)
}
if hdr.Size != expectHdr.Size {
// Special exception for the hardlink which has size of 13 on disk.
if hdr.Size != expectHdr.Size && hdr.Name != "./a/b/c/f-hardlink" {
t.Errorf("file [%s]: got size %d, expected %d", hdr.Name, hdr.Size, expectHdr.Size)
}
}
}

// openFile opens a file for reading, and closes it when the test completes.
func openFile(t *testing.T, path string) io.Reader {
t.Helper()

f, err := os.Open(path)
if err != nil {
t.Fatal("Failed to open file for reading:", err)
}
t.Cleanup(func() {
if err := f.Close(); err != nil {
t.Error("Failed to close file:", err)
}
})

return f
}
2 changes: 1 addition & 1 deletion initrd/dockerfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ func (initrd *dockerfile) Build(ctx context.Context) (string, error) {
return "", fmt.Errorf("could not read tar header: %w", err)
}

internal := filepath.Clean(fmt.Sprintf("/%s", tarHeader.Name))
internal := fmt.Sprintf("./%s", filepath.Clean(tarHeader.Name))

cpioHeader := &cpio.Header{
Name: internal,
Expand Down
28 changes: 0 additions & 28 deletions initrd/dockerfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,6 @@ func TestNewFromDockerfile(t *testing.T) {

r := cpio.NewReader(openFile(t, irdPath))

expectHeaders := map[string]cpio.Header{
"/a": {
Mode: cpio.TypeDir,
},
"/a/b": {
Mode: cpio.TypeDir,
},
"/a/b/c": {
Mode: cpio.TypeDir,
},
"/a/b/c/d": {
Mode: cpio.TypeReg,
Size: 13,
},
"/a/b/c/e-symlink": {
Mode: cpio.TypeSymlink,
Linkname: "./d",
},
"/a/b/c/f-hardlink": {
Mode: cpio.TypeReg,
Size: 0,
},
"/a/b/c/g-recursive-symlink": {
Mode: cpio.TypeSymlink,
Linkname: ".",
},
}

var gotFiles []string

for {
Expand Down
59 changes: 59 additions & 0 deletions initrd/initrd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package initrd_test

import (
"io"
"os"
"testing"

"kraftkit.sh/cpio"
)

var expectHeaders = map[string]cpio.Header{
"./a": {
Mode: cpio.TypeDir,
},
"./a/b": {
Mode: cpio.TypeDir,
},
"./a/b/c": {
Mode: cpio.TypeDir,
},
"./a/b/c/d": {
Mode: cpio.TypeReg,
Size: 13,
},
"./a/b/c/e-symlink": {
Mode: cpio.TypeSymlink,
Linkname: "./d",
},
"./a/b/c/f-hardlink": {
Mode: cpio.TypeReg,
Size: 0,
},
"./a/b/c/g-recursive-symlink": {
Mode: cpio.TypeSymlink,
Linkname: ".",
},
}

// openFile opens a file for reading, and closes it when the test completes.
func openFile(t *testing.T, path string) io.Reader {
t.Helper()

f, err := os.Open(path)
if err != nil {
t.Fatal("Failed to open file for reading:", err)
}
t.Cleanup(func() {
if err := f.Close(); err != nil {
t.Error("Failed to close file:", err)
}
})

return f
}
6 changes: 5 additions & 1 deletion initrd/ociimage.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ func (initrd *ociimage) Build(ctx context.Context) (string, error) {
return err
}

internal := filepath.Clean(fmt.Sprintf("/%s", path))
internal := fmt.Sprintf("./%s", path)
if strings.HasPrefix(internal, ".//") {
internal = internal[2:]
internal = fmt.Sprintf(".%s", internal)
}

cpioHeader := &cpio.Header{
Name: internal,
Expand Down
28 changes: 0 additions & 28 deletions initrd/ociimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,6 @@ func TestNewFromOCIImage(t *testing.T) {

r := cpio.NewReader(openFile(t, irdPath))

expectHeaders := map[string]cpio.Header{
"/a": {
Mode: cpio.TypeDir,
},
"/a/b": {
Mode: cpio.TypeDir,
},
"/a/b/c": {
Mode: cpio.TypeDir,
},
"/a/b/c/d": {
Mode: cpio.TypeReg,
Size: 13,
},
"/a/b/c/e-symlink": {
Mode: cpio.TypeSymlink,
Linkname: "./d",
},
"/a/b/c/f-hardlink": {
Mode: cpio.TypeReg,
Size: 0,
},
"/a/b/c/g-recursive-symlink": {
Mode: cpio.TypeSymlink,
Linkname: ".",
},
}

var gotFiles []string

for {
Expand Down
Binary file added initrd/testdata/rootfs.tar.gz
Binary file not shown.
1 change: 1 addition & 0 deletions initrd/testdata/rootfs/a/b/c/d
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World
1 change: 1 addition & 0 deletions initrd/testdata/rootfs/a/b/c/e-symlink
1 change: 1 addition & 0 deletions initrd/testdata/rootfs/a/b/c/f-hardlink
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, World
1 change: 1 addition & 0 deletions initrd/testdata/rootfs/a/b/c/g-recursive-symlink
2 changes: 0 additions & 2 deletions initrd/testdata/rootfs/entrypoint.sh

This file was deleted.

2 changes: 0 additions & 2 deletions initrd/testdata/rootfs/etc/app.conf

This file was deleted.

1 change: 0 additions & 1 deletion initrd/testdata/rootfs/lib/libtest.so.1

This file was deleted.

1 change: 0 additions & 1 deletion initrd/testdata/rootfs/lib/libtest.so.1.0.0

This file was deleted.

0 comments on commit 017347d

Please sign in to comment.