Skip to content

Commit

Permalink
Use realistic tar-split data in TestGenerateAndParseManifest
Browse files Browse the repository at this point in the history
We are going to be checking its consistency with the TOC.

Signed-off-by: Miloslav Trmač <mitr@redhat.com>
  • Loading branch information
mtrmac committed Jul 17, 2024
1 parent bc692bb commit cda2c18
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions pkg/chunked/zstdchunked_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import (
"github.com/opencontainers/go-digest"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/vbatts/tar-split/archive/tar"
"github.com/vbatts/tar-split/tar/asm"
"github.com/vbatts/tar-split/tar/storage"
)

type seekable struct {
Expand Down Expand Up @@ -88,13 +91,36 @@ func TestGenerateAndParseManifest(t *testing.T) {
annotations := make(map[string]string)
offsetManifest := uint64(100000)

var tsTarball bytes.Buffer
tsTarW := tar.NewWriter(&tsTarball)
for _, e := range someFiles {
tf, err := typeToTarType(e.Type)
require.NoError(t, err)
err = tsTarW.WriteHeader(&tar.Header{
Typeflag: tf,
Name: e.Name,
Size: e.Size,
Mode: e.Mode,
})
require.NoError(t, err)
data := make([]byte, e.Size)
_, err = tsTarW.Write(data)
require.NoError(t, err)
}
err := tsTarW.Close()
require.NoError(t, err)
var tarSplitUncompressed bytes.Buffer
tsReader, err := asm.NewInputTarStream(&tsTarball, storage.NewJSONPacker(&tarSplitUncompressed), storage.NewDiscardFilePutter())
require.NoError(t, err)
_, err = io.Copy(io.Discard, tsReader)
require.NoError(t, err)

encoder, err := zstd.NewWriter(nil)
if err != nil {
t.Error(err)
}
defer encoder.Close()

tarSplitCompressedData := encoder.EncodeAll([]byte("TAR-SPLIT"), nil)
tarSplitCompressedData := encoder.EncodeAll(tarSplitUncompressed.Bytes(), nil)

ts := internal.TarSplitData{
Data: tarSplitCompressedData,
Expand Down Expand Up @@ -131,9 +157,9 @@ func TestGenerateAndParseManifest(t *testing.T) {
t.Fatal("no manifest written")
}

var tarSplitOffset, tarSplitLength, tarSplitUncompressed uint64
var tarSplitOffset, tarSplitLength, tarSplitUncompressedLength uint64
tarSplitMetadata := annotations[internal.TarSplitInfoKey]
if _, err := fmt.Sscanf(tarSplitMetadata, "%d:%d:%d", &tarSplitOffset, &tarSplitLength, &tarSplitUncompressed); err != nil {
if _, err := fmt.Sscanf(tarSplitMetadata, "%d:%d:%d", &tarSplitOffset, &tarSplitLength, &tarSplitUncompressedLength); err != nil {
t.Error(err)
}

Expand Down

0 comments on commit cda2c18

Please sign in to comment.