This commit is contained in:
Motiejus Jakštys 2021-05-24 00:11:58 +03:00
parent 46aed94077
commit 890d99cd3e
3 changed files with 29 additions and 17 deletions

View File

@ -9,6 +9,7 @@ import (
"go.uber.org/multierr"
)
// CmdRootFS is "rootfs" command
type CmdRootFS struct {
PositionalArgs struct {
Infile goflags.Filename `long:"infile" description:"Input tarball"`
@ -16,6 +17,7 @@ type CmdRootFS struct {
} `positional-args:"yes" required:"yes"`
}
// Execute executes CmdRootFS
func (r *CmdRootFS) Execute(args []string) (err error) {
if len(args) != 0 {
return errors.New("too many args")

View File

@ -10,33 +10,39 @@ import (
)
type (
// Tarrer is an object that can tar itself to an archive
Tarrer interface {
Tar(*tar.Writer) error
}
// Tarball is a list of tarrables
Tarball []Tarrer
// Extractable is an empty interface for comparing extracted outputs in tests.
// Using that just to avoid the ugly `interface{}`.
Extractable interface{}
// Dir is a tarrable directory representation
Dir struct {
Name string
Uid int
UID int
}
// File is a tarrable file representation
File struct {
Name string
Uid int
UID int
Contents *bytes.Buffer
}
// Hardlink is a representation of a hardlink
Hardlink struct {
Name string
Uid int
UID int
}
)
// Buffer returns a byte buffer
func (tb Tarball) Buffer() *bytes.Buffer {
var buf bytes.Buffer
tw := tar.NewWriter(&buf)
@ -47,16 +53,18 @@ func (tb Tarball) Buffer() *bytes.Buffer {
return &buf
}
// Tar tars the Dir
func (d Dir) Tar(tw *tar.Writer) error {
hdr := &tar.Header{
Typeflag: tar.TypeDir,
Name: d.Name,
Mode: 0644,
Uid: d.Uid,
Uid: d.UID,
}
return tw.WriteHeader(hdr)
}
// Tar tars the File
func (f File) Tar(tw *tar.Writer) error {
var contents []byte
if f.Contents != nil {
@ -66,7 +74,7 @@ func (f File) Tar(tw *tar.Writer) error {
Typeflag: tar.TypeReg,
Name: f.Name,
Mode: 0644,
Uid: f.Uid,
Uid: f.UID,
Size: int64(len(contents)),
}
if err := tw.WriteHeader(hdr); err != nil {
@ -78,15 +86,17 @@ func (f File) Tar(tw *tar.Writer) error {
return nil
}
// Tar tars the Hardlink
func (h Hardlink) Tar(tw *tar.Writer) error {
return tw.WriteHeader(&tar.Header{
Typeflag: tar.TypeLink,
Name: h.Name,
Mode: 0644,
Uid: h.Uid,
Uid: h.UID,
})
}
// Extract extracts a tarball to a slice of extractables
func Extract(t *testing.T, r io.Reader) []Extractable {
t.Helper()
ret := []Extractable{}
@ -101,11 +111,11 @@ func Extract(t *testing.T, r io.Reader) []Extractable {
var elem Extractable
switch hdr.Typeflag {
case tar.TypeDir:
elem = Dir{Name: hdr.Name, Uid: hdr.Uid}
elem = Dir{Name: hdr.Name, UID: hdr.Uid}
case tar.TypeLink:
elem = Hardlink{Name: hdr.Name}
case tar.TypeReg:
f := File{Name: hdr.Name, Uid: hdr.Uid}
f := File{Name: hdr.Name, UID: hdr.Uid}
if hdr.Size > 0 {
var buf bytes.Buffer
io.Copy(&buf, tr)

View File

@ -21,16 +21,16 @@ type (
func TestRootFS(t *testing.T) {
layer0 := tarball{
dir{Name: "/", Uid: 0},
file{Name: "/file", Uid: 0, Contents: bytes.NewBufferString("from 0")},
dir{Name: "/", UID: 0},
file{Name: "/file", UID: 0, Contents: bytes.NewBufferString("from 0")},
}
layer1 := tarball{
file{Name: "/file", Uid: 1, Contents: bytes.NewBufferString("from 1")},
file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
}
layer2 := tarball{
dir{Name: "/", Uid: 2},
dir{Name: "/", UID: 2},
}
tests := []struct {
@ -57,8 +57,8 @@ func TestRootFS(t *testing.T) {
manifest{"layer0/layer.tar", "layer1/layer.tar"},
},
want: []extractable{
dir{Name: "/", Uid: 0},
file{Name: "/file", Uid: 1, Contents: bytes.NewBufferString("from 1")},
dir{Name: "/", UID: 0},
file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
},
},
{
@ -85,9 +85,9 @@ func TestRootFS(t *testing.T) {
manifest{"layer0/layer.tar", "layer1/layer.tar", "layer2/layer.tar"},
},
want: []extractable{
dir{Name: "/", Uid: 0},
file{Name: "/file", Uid: 1, Contents: bytes.NewBufferString("from 1")},
dir{Name: "/", Uid: 2},
dir{Name: "/", UID: 0},
file{Name: "/file", UID: 1, Contents: bytes.NewBufferString("from 1")},
dir{Name: "/", UID: 2},
},
},
{