diff options
| author | Ophestra <cat@gensokyo.uk> | 2026-01-07 22:15:55 +0900 |
|---|---|---|
| committer | Ophestra <cat@gensokyo.uk> | 2026-01-07 22:16:26 +0900 |
| commit | c86ff02d8d794ef31d8f646ae85e776b714c7ef9 (patch) | |
| tree | 6c8fce143e3c0b22d094b8f9c73bacb92c4d1f0d /internal/pkg/tar_test.go | |
| parent | e8dda70c417a78647ae31930acc0a98ecf5ad097 (diff) | |
internal/pkg: tar optional file
This allows tar to take a single-file directory Artifact as input.
Signed-off-by: Ophestra <cat@gensokyo.uk>
Diffstat (limited to 'internal/pkg/tar_test.go')
| -rw-r--r-- | internal/pkg/tar_test.go | 103 |
1 files changed, 88 insertions, 15 deletions
diff --git a/internal/pkg/tar_test.go b/internal/pkg/tar_test.go index c4257929..6c22d316 100644 --- a/internal/pkg/tar_test.go +++ b/internal/pkg/tar_test.go @@ -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)}, + }) } |
