internal/pkg: tar optional file
All checks were successful
Test / Create distribution (push) Successful in 47s
Test / Sandbox (push) Successful in 2m49s
Test / ShareFS (push) Successful in 4m46s
Test / Sandbox (race detector) (push) Successful in 5m17s
Test / Hpkg (push) Successful in 5m19s
Test / Hakurei (push) Successful in 5m31s
Test / Hakurei (race detector) (push) Successful in 7m27s
Test / Flake checks (push) Successful in 1m39s

This allows tar to take a single-file directory Artifact as input.

Signed-off-by: Ophestra <cat@gensokyo.uk>
This commit is contained in:
2026-01-07 22:15:55 +09:00
parent e8dda70c41
commit c86ff02d8d
5 changed files with 187 additions and 55 deletions

View File

@@ -5,12 +5,15 @@ import (
"bytes"
"compress/gzip"
"crypto/sha512"
"errors"
"io/fs"
"net/http"
"os"
"testing"
"testing/fstest"
"hakurei.app/container/check"
"hakurei.app/container/stub"
"hakurei.app/internal/pkg"
)
@@ -37,7 +40,7 @@ func TestTar(t *testing.T) {
}, pkg.MustDecode(
"cTw0h3AmYe7XudSoyEMByduYXqGi-N5ZkTZ0t9K5elsu3i_jNIVF5T08KR1roBFM",
))
}, pkg.MustDecode("PrNWkHqtSmdtVecctlI9Xf63dQPIyyLBIMCtRAP2-VqF9u1oQ8ydV7-WPbzEvMkG")},
}, pkg.MustDecode("sxbgyX-bPoezbha214n2lbQhiVfTUBkhZ0EX6zI7mmkMdrCdwuMwhMBJphLQsy94")},
{"http expand", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
checkTarHTTP(t, base, c, fstest.MapFS{
@@ -48,7 +51,7 @@ func TestTar(t *testing.T) {
}, pkg.MustDecode(
"CH3AiUrCCcVOjOYLaMKKK1Da78989JtfHeIsxMzWOQFiN4mrCLDYpoDxLWqJWCUN",
))
}, pkg.MustDecode("YZUoGdMwmfW5sQWto9hQgMKah648rHKck8Ds_GGnqgCBpTAiZKOefpHCKnvktfYh")},
}, pkg.MustDecode("4I8wx_h7NSJTlG5lbuz-GGEXrOg0GYC3M_503LYEBhv5XGWXfNIdIY9Q3eVSYldX")},
})
}
@@ -115,18 +118,88 @@ func checkTarHTTP(
t.Fatalf("Ident: %s, want %s", pkg.Encode(id), pkg.Encode(wantIdent))
}
wantPathname := base.Append(
"identifier",
pkg.Encode(wantIdent),
)
if pathname, checksum, err := c.Cure(a); err != nil {
t.Fatalf("Cure: error = %v", err)
} else if !pathname.Is(wantPathname) {
t.Fatalf("Cure: %q, want %q", pathname, wantPathname)
} else if checksum != wantChecksum {
t.Fatalf("Cure: %v", &pkg.ChecksumMismatchError{
Got: checksum,
Want: wantChecksum,
})
tarDir := stubArtifact{
kind: pkg.KindExec,
params: []byte("directory containing a single regular file"),
cure: func(c *pkg.CureContext) error {
work := c.GetWorkDir()
if err := os.MkdirAll(work.String(), 0700); err != nil {
return err
}
return os.WriteFile(
work.Append("sample.tar.gz").String(),
[]byte(testdata),
0400,
)
},
}
tarDirMulti := stubArtifact{
kind: pkg.KindExec,
params: []byte("directory containing a multiple entries"),
cure: func(c *pkg.CureContext) error {
work := c.GetWorkDir()
if err := os.MkdirAll(work.Append(
"garbage",
).String(), 0700); err != nil {
return err
}
return os.WriteFile(
work.Append("sample.tar.gz").String(),
[]byte(testdata),
0400,
)
},
}
tarDirType := stubArtifact{
kind: pkg.KindExec,
params: []byte("directory containing a symbolic link"),
cure: func(c *pkg.CureContext) error {
work := c.GetWorkDir()
if err := os.MkdirAll(work.String(), 0700); err != nil {
return err
}
return os.Symlink(
work.String(),
work.Append("sample.tar.gz").String(),
)
},
}
// destroy these to avoid including it in flatten test case
defer newDestroyArtifactFunc(tarDir)(t, base, c)
defer newDestroyArtifactFunc(tarDirMulti)(t, base, c)
defer newDestroyArtifactFunc(tarDirType)(t, base, c)
cureMany(t, c, []cureStep{
{"file", a, base.Append(
"identifier",
pkg.Encode(wantIdent),
), wantChecksum, nil},
{"directory", pkg.NewTar(
tarDir,
pkg.TarGzip,
), ignorePathname, wantChecksum, nil},
{"multiple entries", pkg.NewTar(
tarDirMulti,
pkg.TarGzip,
), nil, pkg.Checksum{}, errors.New(
"input directory does not contain a single regular file",
)},
{"bad type", pkg.NewTar(
tarDirType,
pkg.TarGzip,
), nil, pkg.Checksum{}, errors.New(
"input directory does not contain a single regular file",
)},
{"error passthrough", pkg.NewTar(stubArtifact{
kind: pkg.KindExec,
params: []byte("doomed artifact"),
cure: func(c *pkg.CureContext) error {
return stub.UniqueError(0xcafe)
},
}, pkg.TarGzip), nil, pkg.Checksum{}, stub.UniqueError(0xcafe)},
})
}