From 017347dd689bdf76cd49f3dd1e34011836be8737 Mon Sep 17 00:00:00 2001 From: Alexander Jung Date: Sun, 12 Jan 2025 23:30:23 +0100 Subject: [PATCH] refactor(initrd): Consolidate common test variables and methods 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 --- initrd/directory_test.go | 45 +------------ initrd/dockerfile.go | 2 +- initrd/dockerfile_test.go | 28 --------- initrd/initrd_test.go | 59 ++++++++++++++++++ initrd/ociimage.go | 6 +- initrd/ociimage_test.go | 28 --------- initrd/testdata/rootfs.tar.gz | Bin 0 -> 292 bytes initrd/testdata/rootfs/a/b/c/d | 1 + initrd/testdata/rootfs/a/b/c/e-symlink | 1 + initrd/testdata/rootfs/a/b/c/f-hardlink | 1 + .../testdata/rootfs/a/b/c/g-recursive-symlink | 1 + initrd/testdata/rootfs/entrypoint.sh | 2 - initrd/testdata/rootfs/etc/app.conf | 2 - initrd/testdata/rootfs/lib/libtest.so.1 | 1 - initrd/testdata/rootfs/lib/libtest.so.1.0.0 | 1 - 15 files changed, 71 insertions(+), 107 deletions(-) create mode 100644 initrd/initrd_test.go create mode 100644 initrd/testdata/rootfs.tar.gz create mode 100644 initrd/testdata/rootfs/a/b/c/d create mode 120000 initrd/testdata/rootfs/a/b/c/e-symlink create mode 100644 initrd/testdata/rootfs/a/b/c/f-hardlink create mode 120000 initrd/testdata/rootfs/a/b/c/g-recursive-symlink delete mode 100755 initrd/testdata/rootfs/entrypoint.sh delete mode 100644 initrd/testdata/rootfs/etc/app.conf delete mode 120000 initrd/testdata/rootfs/lib/libtest.so.1 delete mode 100644 initrd/testdata/rootfs/lib/libtest.so.1.0.0 diff --git a/initrd/directory_test.go b/initrd/directory_test.go index 7398710b5..9fcc69928 100644 --- a/initrd/directory_test.go +++ b/initrd/directory_test.go @@ -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 { @@ -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 -} diff --git a/initrd/dockerfile.go b/initrd/dockerfile.go index e7d69a533..489749ebf 100644 --- a/initrd/dockerfile.go +++ b/initrd/dockerfile.go @@ -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, diff --git a/initrd/dockerfile_test.go b/initrd/dockerfile_test.go index 8bbe65220..c24e60671 100644 --- a/initrd/dockerfile_test.go +++ b/initrd/dockerfile_test.go @@ -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 { diff --git a/initrd/initrd_test.go b/initrd/initrd_test.go new file mode 100644 index 000000000..2e0c2e737 --- /dev/null +++ b/initrd/initrd_test.go @@ -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 +} diff --git a/initrd/ociimage.go b/initrd/ociimage.go index 0ffd9db45..51f4b865f 100644 --- a/initrd/ociimage.go +++ b/initrd/ociimage.go @@ -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, diff --git a/initrd/ociimage_test.go b/initrd/ociimage_test.go index 4df2943e2..b59c77172 100644 --- a/initrd/ociimage_test.go +++ b/initrd/ociimage_test.go @@ -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 { diff --git a/initrd/testdata/rootfs.tar.gz b/initrd/testdata/rootfs.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..c9a9e2af339c6bd0b83998b51b8510d8d04bb01f GIT binary patch literal 292 zcmV+<0o(o`iwFP!000001MSw|PJ=KM2Jl|{6ny|;r~hBUH}IMb)J2q}Y$m?_3JYGi zOtNLCM)`g>B|y@KujiC6Ch{lml$MIU(U?~_xCr7HGNrl{tr#tcK7nJi*0@P+lzthruuJ+{`0NfzP~ce zaasRQeM0GAQvcT_Yg;;6c>PcLKNT-kta&!TZ2vzw|I?8E|5c8|3Hgto__>J9W*__Q q$L%}8<@Il!rlJ0CwuJK*o<9Hp0000000000{7(nG6kSjNC;$Koj+e6l literal 0 HcmV?d00001 diff --git a/initrd/testdata/rootfs/a/b/c/d b/initrd/testdata/rootfs/a/b/c/d new file mode 100644 index 000000000..3fa0d4b98 --- /dev/null +++ b/initrd/testdata/rootfs/a/b/c/d @@ -0,0 +1 @@ +Hello, World diff --git a/initrd/testdata/rootfs/a/b/c/e-symlink b/initrd/testdata/rootfs/a/b/c/e-symlink new file mode 120000 index 000000000..cd1426d19 --- /dev/null +++ b/initrd/testdata/rootfs/a/b/c/e-symlink @@ -0,0 +1 @@ +./d \ No newline at end of file diff --git a/initrd/testdata/rootfs/a/b/c/f-hardlink b/initrd/testdata/rootfs/a/b/c/f-hardlink new file mode 100644 index 000000000..3fa0d4b98 --- /dev/null +++ b/initrd/testdata/rootfs/a/b/c/f-hardlink @@ -0,0 +1 @@ +Hello, World diff --git a/initrd/testdata/rootfs/a/b/c/g-recursive-symlink b/initrd/testdata/rootfs/a/b/c/g-recursive-symlink new file mode 120000 index 000000000..945c9b46d --- /dev/null +++ b/initrd/testdata/rootfs/a/b/c/g-recursive-symlink @@ -0,0 +1 @@ +. \ No newline at end of file diff --git a/initrd/testdata/rootfs/entrypoint.sh b/initrd/testdata/rootfs/entrypoint.sh deleted file mode 100755 index ec5b9e5f3..000000000 --- a/initrd/testdata/rootfs/entrypoint.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/usr/bin/env sh -exit 0 diff --git a/initrd/testdata/rootfs/etc/app.conf b/initrd/testdata/rootfs/etc/app.conf deleted file mode 100644 index 97f6c3fe5..000000000 --- a/initrd/testdata/rootfs/etc/app.conf +++ /dev/null @@ -1,2 +0,0 @@ -[app] -key=value diff --git a/initrd/testdata/rootfs/lib/libtest.so.1 b/initrd/testdata/rootfs/lib/libtest.so.1 deleted file mode 120000 index b02b0339c..000000000 --- a/initrd/testdata/rootfs/lib/libtest.so.1 +++ /dev/null @@ -1 +0,0 @@ -libtest.so.1.0.0 \ No newline at end of file diff --git a/initrd/testdata/rootfs/lib/libtest.so.1.0.0 b/initrd/testdata/rootfs/lib/libtest.so.1.0.0 deleted file mode 100644 index 7bd0e3d13..000000000 --- a/initrd/testdata/rootfs/lib/libtest.so.1.0.0 +++ /dev/null @@ -1 +0,0 @@ -#fakelib