2021-05-24 00:11:57 +03:00
|
|
|
package rootfs
|
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2021-05-24 00:11:57 +03:00
|
|
|
"io"
|
2021-05-24 00:11:57 +03:00
|
|
|
"testing"
|
2021-05-24 00:11:57 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-05-24 00:11:57 +03:00
|
|
|
)
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func TestRootFS(t *testing.T) {
|
|
|
|
layer0 := tarball{
|
|
|
|
dir{name: "/", uid: 0},
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "/file", uid: 0, contents: bytes.NewBufferString("from 0")},
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
layer1 := tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "/file", uid: 1, contents: bytes.NewBufferString("from 1")},
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
layer2 := tarball{
|
|
|
|
dir{name: "/", uid: 2},
|
|
|
|
}
|
|
|
|
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
image tarball
|
|
|
|
want []extractable
|
|
|
|
wantErr string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "empty tarball",
|
|
|
|
image: tarball{manifest{}},
|
|
|
|
want: []extractable{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "missing layer",
|
|
|
|
image: tarball{manifest{"layer0/layer.tar"}},
|
|
|
|
wantErr: "bad or missing manifest.json",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "basic file overwrite, layer order mixed",
|
|
|
|
image: tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "layer1/layer.tar", contents: layer1},
|
|
|
|
file{name: "layer0/layer.tar", contents: layer0},
|
2021-05-24 00:11:58 +03:00
|
|
|
manifest{"layer0/layer.tar", "layer1/layer.tar"},
|
|
|
|
},
|
|
|
|
want: []extractable{
|
|
|
|
dir{name: "/", uid: 0},
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "/file", uid: 1, contents: bytes.NewBufferString("from 1")},
|
2021-05-24 00:11:58 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "directory overwrite retains original dir",
|
|
|
|
image: tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "layer2/layer.tar", contents: layer2},
|
|
|
|
file{name: "layer0/layer.tar", contents: layer0},
|
|
|
|
file{name: "layer1/layer.tar", contents: layer1},
|
2021-05-24 00:11:58 +03:00
|
|
|
manifest{"layer0/layer.tar", "layer1/layer.tar", "layer2/layer.tar"},
|
|
|
|
},
|
|
|
|
want: []extractable{
|
|
|
|
dir{name: "/", uid: 0},
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "/file", uid: 1, contents: bytes.NewBufferString("from 1")},
|
2021-05-24 00:11:58 +03:00
|
|
|
dir{name: "/", uid: 2},
|
|
|
|
},
|
|
|
|
},
|
2021-05-24 00:11:58 +03:00
|
|
|
{
|
|
|
|
name: "simple whiteout",
|
|
|
|
image: tarball{
|
|
|
|
file{name: "layer0/layer.tar", contents: tarball{
|
|
|
|
file{name: "filea"},
|
|
|
|
file{name: "fileb"},
|
|
|
|
dir{name: "dira"},
|
|
|
|
dir{name: "dirb"},
|
|
|
|
}},
|
|
|
|
file{name: "layer1/layer.tar", contents: tarball{
|
|
|
|
hardlink{name: ".wh.filea"},
|
|
|
|
hardlink{name: ".wh.dira"},
|
|
|
|
}},
|
|
|
|
manifest{"layer0/layer.tar", "layer1/layer.tar"},
|
|
|
|
},
|
|
|
|
want: []extractable{
|
|
|
|
file{name: "fileb"},
|
|
|
|
dir{name: "dirb"},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "whiteout with override",
|
|
|
|
image: tarball{
|
|
|
|
file{name: "layer0/layer.tar", contents: tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "file", contents: bytes.NewBufferString("from 0")},
|
2021-05-24 00:11:58 +03:00
|
|
|
}},
|
|
|
|
file{name: "layer1/layer.tar", contents: tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
hardlink{name: ".wh.file"},
|
2021-05-24 00:11:58 +03:00
|
|
|
}},
|
|
|
|
file{name: "layer2/layer.tar", contents: tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "file", contents: bytes.NewBufferString("from 3")},
|
2021-05-24 00:11:58 +03:00
|
|
|
}},
|
|
|
|
manifest{
|
|
|
|
"layer0/layer.tar",
|
|
|
|
"layer1/layer.tar",
|
|
|
|
"layer2/layer.tar",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
want: []extractable{
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: "file", contents: bytes.NewBufferString("from 3")},
|
2021-05-24 00:11:58 +03:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "files and directories do not whiteout",
|
|
|
|
image: tarball{
|
|
|
|
file{name: "layer0/layer.tar", contents: tarball{
|
|
|
|
dir{name: "dir"},
|
|
|
|
file{name: "file"},
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: ".wh..wh..opq", uid: 0},
|
2021-05-24 00:11:58 +03:00
|
|
|
}},
|
|
|
|
file{name: "layer1/layer.tar", contents: tarball{
|
|
|
|
dir{name: ".wh.dir"},
|
|
|
|
file{name: ".wh.file"},
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: ".wh..wh..opq", uid: 1},
|
2021-05-24 00:11:58 +03:00
|
|
|
}},
|
2021-05-24 00:11:58 +03:00
|
|
|
manifest{"layer0/layer.tar", "layer1/layer.tar"},
|
2021-05-24 00:11:58 +03:00
|
|
|
},
|
|
|
|
want: []extractable{
|
|
|
|
dir{name: "dir"},
|
|
|
|
dir{name: ".wh.dir"},
|
|
|
|
file{name: "file"},
|
|
|
|
file{name: ".wh.file"},
|
2021-05-24 00:11:58 +03:00
|
|
|
file{name: ".wh..wh..opq", uid: 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "simple readdir whiteout",
|
|
|
|
image: tarball{
|
|
|
|
file{name: "layer0/layer.tar", contents: tarball{
|
|
|
|
dir{name: "a"},
|
|
|
|
file{name: "a/filea"},
|
|
|
|
}},
|
|
|
|
file{name: "layer1/layer.tar", contents: tarball{
|
|
|
|
file{name: "a/fileb"},
|
|
|
|
hardlink{name: "a/.wh..wh..opq"},
|
|
|
|
}},
|
|
|
|
manifest{"layer0/layer.tar", "layer1/layer.tar"},
|
|
|
|
},
|
|
|
|
want: []extractable{
|
|
|
|
dir{name: "a"},
|
|
|
|
file{name: "a/fileb"},
|
2021-05-24 00:11:58 +03:00
|
|
|
},
|
|
|
|
},
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2021-05-24 00:11:58 +03:00
|
|
|
in := bytes.NewReader(tt.image.Bytes())
|
2021-05-24 00:11:58 +03:00
|
|
|
out := bytes.Buffer{}
|
|
|
|
|
|
|
|
err := RootFS(in, &out)
|
|
|
|
if tt.wantErr != "" {
|
|
|
|
assert.EqualError(t, err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
got := extract(t, &out)
|
2021-05-24 00:11:58 +03:00
|
|
|
assert.Equal(t, tt.want, got)
|
2021-05-24 00:11:58 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Helpers
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
type tarrer interface {
|
|
|
|
tar(*tar.Writer)
|
|
|
|
}
|
|
|
|
|
|
|
|
type byter interface {
|
|
|
|
Bytes() []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
type tarball []tarrer
|
|
|
|
|
|
|
|
func (tb tarball) Bytes() []byte {
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
tw := tar.NewWriter(&buf)
|
|
|
|
for _, member := range tb {
|
|
|
|
member.tar(tw)
|
|
|
|
}
|
|
|
|
tw.Close()
|
|
|
|
return buf.Bytes()
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
2021-05-24 00:11:57 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// extractable is an empty interface for comparing extracted outputs in tests.
|
|
|
|
// Using that just to avoid the ugly `interface{}`.
|
|
|
|
type extractable interface{}
|
|
|
|
|
2021-05-24 00:11:57 +03:00
|
|
|
type dir struct {
|
|
|
|
name string
|
|
|
|
uid int
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func (d dir) tar(tw *tar.Writer) {
|
2021-05-24 00:11:57 +03:00
|
|
|
hdr := &tar.Header{
|
|
|
|
Typeflag: tar.TypeDir,
|
|
|
|
Name: d.name,
|
2021-05-24 00:11:58 +03:00
|
|
|
Mode: 0644,
|
2021-05-24 00:11:57 +03:00
|
|
|
Uid: d.uid,
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
tw.WriteHeader(hdr)
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type file struct {
|
|
|
|
name string
|
|
|
|
uid int
|
2021-05-24 00:11:58 +03:00
|
|
|
contents byter
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func (f file) tar(tw *tar.Writer) {
|
|
|
|
var contentbytes []byte
|
|
|
|
if f.contents != nil {
|
|
|
|
contentbytes = f.contents.Bytes()
|
|
|
|
}
|
2021-05-24 00:11:57 +03:00
|
|
|
hdr := &tar.Header{
|
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
Name: f.name,
|
2021-05-24 00:11:58 +03:00
|
|
|
Mode: 0644,
|
2021-05-24 00:11:57 +03:00
|
|
|
Uid: f.uid,
|
2021-05-24 00:11:58 +03:00
|
|
|
Size: int64(len(contentbytes)),
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
tw.WriteHeader(hdr)
|
|
|
|
tw.Write(contentbytes)
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:57 +03:00
|
|
|
type manifest []string
|
2021-05-24 00:11:57 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func (m manifest) tar(tw *tar.Writer) {
|
2021-05-24 00:11:57 +03:00
|
|
|
b, err := json.Marshal(dockerManifestJSON{{Layers: m}})
|
2021-05-24 00:11:58 +03:00
|
|
|
if err != nil {
|
|
|
|
panic("testerr")
|
|
|
|
}
|
|
|
|
file{
|
|
|
|
name: "manifest.json",
|
|
|
|
uid: 0,
|
|
|
|
contents: bytes.NewBuffer(b),
|
|
|
|
}.tar(tw)
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
type hardlink struct {
|
|
|
|
name string
|
|
|
|
uid int
|
|
|
|
}
|
2021-05-24 00:11:57 +03:00
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func (h hardlink) tar(tw *tar.Writer) {
|
|
|
|
tw.WriteHeader(&tar.Header{
|
|
|
|
Typeflag: tar.TypeLink,
|
|
|
|
Name: h.name,
|
|
|
|
Mode: 0644,
|
|
|
|
Uid: h.uid,
|
|
|
|
})
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
func extract(t *testing.T, r io.Reader) []extractable {
|
2021-05-24 00:11:57 +03:00
|
|
|
t.Helper()
|
2021-05-24 00:11:58 +03:00
|
|
|
ret := []extractable{}
|
2021-05-24 00:11:58 +03:00
|
|
|
tr := tar.NewReader(r)
|
2021-05-24 00:11:57 +03:00
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
var elem extractable
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeDir:
|
|
|
|
elem = dir{name: hdr.Name, uid: hdr.Uid}
|
|
|
|
case tar.TypeReg:
|
2021-05-24 00:11:58 +03:00
|
|
|
f := file{name: hdr.Name, uid: hdr.Uid}
|
|
|
|
if hdr.Size > 0 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, tr)
|
|
|
|
f.contents = &buf
|
|
|
|
}
|
|
|
|
elem = f
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
2021-05-24 00:11:57 +03:00
|
|
|
ret = append(ret, elem)
|
2021-05-24 00:11:57 +03:00
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|