aboutsummaryrefslogtreecommitdiffhomepage
path: root/internal/pkg/tar_test.go
diff options
context:
space:
mode:
authorOphestra <cat@gensokyo.uk>2026-01-04 01:08:59 +0900
committerOphestra <cat@gensokyo.uk>2026-01-04 01:34:30 +0900
commitd76b9d04b813a361fc8e226480e3b66ca0208d41 (patch)
tree5489463bedf7ec016ffad12b2c2c228afff83dbc /internal/pkg/tar_test.go
parentfa93476896700f990e303cf3f60caa77f30df625 (diff)
internal/pkg: implement tar artifact
This is useful for unpacking tarballs downloaded from the internet. Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/tar_test.go')
-rw-r--r--internal/pkg/tar_test.go119
1 files changed, 119 insertions, 0 deletions
diff --git a/internal/pkg/tar_test.go b/internal/pkg/tar_test.go
new file mode 100644
index 00000000..a629c9ee
--- /dev/null
+++ b/internal/pkg/tar_test.go
@@ -0,0 +1,119 @@
+package pkg_test
+
+import (
+ "archive/tar"
+ "bytes"
+ "compress/gzip"
+ "crypto/sha512"
+ "io/fs"
+ "net/http"
+ "testing"
+ "testing/fstest"
+
+ "hakurei.app/container/check"
+ "hakurei.app/internal/pkg"
+)
+
+func TestTar(t *testing.T) {
+ t.Parallel()
+
+ testdataFsys := fstest.MapFS{
+ ".": {Mode: fs.ModeDir | 0700},
+
+ "checksum": {Mode: fs.ModeDir | 0700},
+ "checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP": {Mode: fs.ModeDir | 0700},
+ "checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/check": {Mode: 0400, Data: []byte{0, 0}},
+ "checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib": {Mode: fs.ModeDir | 0700},
+ "checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/pkgconfig": {Mode: fs.ModeDir | 0700},
+ "checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP/lib/libedac.so": {Mode: 01000000777, Data: []byte("/proc/nonexistent/libedac.so")},
+
+ "identifier": {Mode: fs.ModeDir | 0700},
+ "identifier/HnySzeLQvSBZuTUcvfmLEX_OmH4yJWWH788NxuLuv7kVn8_uPM6Ks4rqFWM2NZJY": {Mode: 01000000777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
+ "identifier/Zx5ZG9BAwegNT3zQwCySuI2ktCXxNgxirkGLFjW4FW06PtojYVaCdtEw8yuntPLa": {Mode: 01000000777, Data: []byte("../checksum/1TL00Qb8dcqayX7wTO8WNaraHvY6b-KCsctLDTrb64QBCmxj_-byK1HdIUwMaFEP")},
+
+ "work": {Mode: fs.ModeDir | 0700},
+ }
+
+ var testdata string
+ {
+ var buf bytes.Buffer
+ w := tar.NewWriter(&buf)
+ if err := w.AddFS(testdataFsys); err != nil {
+ t.Fatalf("AddFS: error = %v", err)
+ }
+ if err := w.Close(); err != nil {
+ t.Fatalf("Close: error = %v", err)
+ }
+
+ var zbuf bytes.Buffer
+ gw := gzip.NewWriter(&zbuf)
+ if _, err := gw.Write(buf.Bytes()); err != nil {
+ t.Fatalf("Write: error = %v", err)
+ }
+ if err := gw.Close(); err != nil {
+ t.Fatalf("Close: error = %v", err)
+ }
+ testdata = zbuf.String()
+ }
+
+ testdataChecksum := func() pkg.Checksum {
+ h := sha512.New384()
+ h.Write([]byte(testdata))
+ return (pkg.Checksum)(h.Sum(nil))
+ }()
+
+ var transport http.Transport
+ client := http.Client{Transport: &transport}
+ transport.RegisterProtocol("file", http.NewFileTransportFS(fstest.MapFS{
+ "testdata": {Data: []byte(testdata), Mode: 0400},
+ }))
+
+ checkWithCache(t, []cacheTestCase{
+ {"http", nil, func(t *testing.T, base *check.Absolute, c *pkg.Cache) {
+ wantIdent := func() pkg.ID {
+ h := sha512.New384()
+ h.Write([]byte{byte(pkg.KindTar), 0, 0, 0, 0, 0, 0, 0})
+ h.Write([]byte{pkg.TarGzip, 0, 0, 0, 0, 0, 0, 0})
+ h.Write([]byte{byte(pkg.KindHTTP), 0, 0, 0, 0, 0, 0, 0})
+ h.Write(testdataChecksum[:])
+ return pkg.ID(h.Sum(nil))
+ }()
+
+ a, err := c.NewHTTPGetTar(
+ t.Context(),
+ &client,
+ "file:///testdata",
+ testdataChecksum,
+ pkg.TarGzip,
+ )
+
+ if err != nil {
+ t.Fatalf("NewHTTPGetTar: error = %v", err)
+ } else if id := a.ID(); id != wantIdent {
+ t.Fatalf("ID: %s, want %s", pkg.Encode(id), pkg.Encode(wantIdent))
+ }
+
+ var pathname *check.Absolute
+ wantPathname := base.Append(
+ "identifier",
+ pkg.Encode(wantIdent),
+ )
+ if pathname, err = a.Pathname(); err != nil {
+ t.Fatalf("Pathname: error = %v", err)
+ } else if !pathname.Is(wantPathname) {
+ t.Fatalf("Pathname: %q, want %q", pathname, wantPathname)
+ }
+
+ var checksum pkg.Checksum
+ wantChecksum := pkg.MustDecode("yJlSb2A3jxaMLuKqwp1GwHOguAHddS9MjygF9ICEeegKfRvgLPdPmNh8mva47f8o")
+ if checksum, err = a.Hash(); err != nil {
+ t.Fatalf("Hash: error = %v", err)
+ } else if checksum != wantChecksum {
+ t.Fatalf("Hash: %v", &pkg.ChecksumMismatchError{
+ Got: checksum,
+ Want: wantChecksum,
+ })
+ }
+ }, pkg.MustDecode("p1HdTOQhIeWzpXdZ45xo00H9CFeXNIvazxOhBAfExlhFO64zt7TUbxoLJ2eAL5oc")},
+ })
+}