use .Buffer() for layer contents

This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent c3be4bdb35
commit 81db130c94
3 changed files with 43 additions and 42 deletions

View File

@ -55,8 +55,8 @@ func TestRootFS(t *testing.T) {
{
name: "basic file overwrite, layer order mixed",
image: tarball{
file{Name: "layer1/layer.tar", Contents: layer1},
file{Name: "layer0/layer.tar", Contents: layer0},
file{Name: "layer1/layer.tar", Contents: layer1.Buffer()},
file{Name: "layer0/layer.tar", Contents: layer0.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
@ -69,10 +69,10 @@ func TestRootFS(t *testing.T) {
image: tarball{
file{Name: "layer0/layer.tar", Contents: tarball{
file{Name: "a"},
}},
}.Buffer()},
file{Name: "layer1/layer.tar", Contents: tarball{
hardlink{Name: "a"},
}},
}.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
@ -82,9 +82,9 @@ func TestRootFS(t *testing.T) {
{
name: "directory overwrite retains original dir",
image: tarball{
file{Name: "layer2/layer.tar", Contents: layer2},
file{Name: "layer0/layer.tar", Contents: layer0},
file{Name: "layer1/layer.tar", Contents: layer1},
file{Name: "layer2/layer.tar", Contents: layer2.Buffer()},
file{Name: "layer0/layer.tar", Contents: layer0.Buffer()},
file{Name: "layer1/layer.tar", Contents: layer1.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar", "layer2/layer.tar"},
},
want: []extractable{
@ -101,11 +101,11 @@ func TestRootFS(t *testing.T) {
file{Name: "fileb"},
dir{Name: "dira"},
dir{Name: "dirb"},
}},
}.Buffer()},
file{Name: "layer1/layer.tar", Contents: tarball{
hardlink{Name: ".wh.filea"},
hardlink{Name: ".wh.dira"},
}},
}.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
@ -118,13 +118,13 @@ func TestRootFS(t *testing.T) {
image: tarball{
file{Name: "layer0/layer.tar", Contents: tarball{
file{Name: "file", Contents: bytes.NewBufferString("from 0")},
}},
}.Buffer()},
file{Name: "layer1/layer.tar", Contents: tarball{
hardlink{Name: ".wh.file"},
}},
}.Buffer()},
file{Name: "layer2/layer.tar", Contents: tarball{
file{Name: "file", Contents: bytes.NewBufferString("from 3")},
}},
}.Buffer()},
manifest{
"layer0/layer.tar",
"layer1/layer.tar",
@ -140,10 +140,10 @@ func TestRootFS(t *testing.T) {
image: tarball{
file{Name: "layer0/layer.tar", Contents: tarball{
dir{Name: "dir"},
}},
}.Buffer()},
file{Name: "layer1/layer.tar", Contents: tarball{
dir{Name: ".wh.dir"},
}},
}.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
@ -157,12 +157,12 @@ func TestRootFS(t *testing.T) {
file{Name: "layer0/layer.tar", Contents: tarball{
dir{Name: "a"},
file{Name: "a/filea"},
}},
}.Buffer()},
file{Name: "layer1/layer.tar", Contents: tarball{
dir{Name: "a"},
file{Name: "a/fileb"},
hardlink{Name: "a/.wh..wh..opq"},
}},
}.Buffer()},
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
@ -174,7 +174,7 @@ func TestRootFS(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
in := bytes.NewReader(tt.image.Bytes())
in := bytes.NewReader(tt.image.Buffer().Bytes())
out := bytes.Buffer{}
err := New(in).WriteTo(&out)

View File

@ -12,11 +12,7 @@ import (
type (
Tarrer interface {
Tar(*tar.Writer)
}
Byter interface {
Bytes() []byte
Tar(*tar.Writer) error
}
Tarball []Tarrer
@ -33,7 +29,7 @@ type (
File struct {
Name string
Uid int
Contents Byter
Contents *bytes.Buffer
}
Manifest []string
@ -48,55 +44,60 @@ type (
}
)
func (tb Tarball) Bytes() []byte {
buf := bytes.Buffer{}
func (tb Tarball) Buffer() *bytes.Buffer {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
for _, member := range tb {
member.Tar(tw)
}
tw.Close()
return buf.Bytes()
return &buf
}
func (d Dir) Tar(tw *tar.Writer) {
func (d Dir) Tar(tw *tar.Writer) error {
hdr := &tar.Header{
Typeflag: tar.TypeDir,
Name: d.Name,
Mode: 0644,
Uid: d.Uid,
}
tw.WriteHeader(hdr)
return tw.WriteHeader(hdr)
}
func (f File) Tar(tw *tar.Writer) {
var contentbytes []byte
func (f File) Tar(tw *tar.Writer) error {
var contents []byte
if f.Contents != nil {
contentbytes = f.Contents.Bytes()
contents = f.Contents.Bytes()
}
hdr := &tar.Header{
Typeflag: tar.TypeReg,
Name: f.Name,
Mode: 0644,
Uid: f.Uid,
Size: int64(len(contentbytes)),
Size: int64(len(contents)),
}
tw.WriteHeader(hdr)
tw.Write(contentbytes)
if err := tw.WriteHeader(hdr); err != nil {
return err
}
if _, err := tw.Write(contents); err != nil {
return err
}
return nil
}
func (m Manifest) Tar(tw *tar.Writer) {
func (m Manifest) Tar(tw *tar.Writer) error {
b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
if err != nil {
panic("testerr")
return err
}
File{
return File{
Name: "manifest.json",
Contents: bytes.NewBuffer(b),
}.Tar(tw)
}
func (h Hardlink) Tar(tw *tar.Writer) {
tw.WriteHeader(&tar.Header{
func (h Hardlink) Tar(tw *tar.Writer) error {
return tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeLink,
Name: h.Name,
Mode: 0644,

View File

@ -10,15 +10,15 @@ import (
func TestTarball(t *testing.T) {
bk := Tarball{File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("hello")}}
img := Tarball{
File{Name: "backup.tar", Contents: bk},
File{Name: "backup.tar", Contents: bk.Buffer()},
File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("bye")},
Dir{Name: "bin"},
Hardlink{Name: "entrypoint2"},
}
got := Extract(t, bytes.NewBuffer(img.Bytes()))
got := Extract(t, img.Buffer())
want := []Extractable{
File{Name: "backup.tar", Contents: bytes.NewBuffer(bk.Bytes())},
File{Name: "backup.tar", Contents: bk.Buffer()},
File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("bye")},
Dir{Name: "bin"},
Hardlink{Name: "entrypoint2"},