2021-05-24 00:11:58 +03:00
|
|
|
package tartest
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/tar"
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
2021-05-24 00:11:58 +03:00
|
|
|
// Tarrer is an object that can tar itself to an archive
|
2021-05-24 00:11:58 +03:00
|
|
|
Tarrer interface {
|
2021-05-24 00:11:58 +03:00
|
|
|
Tar(*tar.Writer) error
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Tarball is a list of tarrables
|
2021-05-24 00:11:58 +03:00
|
|
|
Tarball []Tarrer
|
|
|
|
|
|
|
|
// Extractable is an empty interface for comparing extracted outputs in tests.
|
|
|
|
// Using that just to avoid the ugly `interface{}`.
|
|
|
|
Extractable interface{}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Dir is a tarrable directory representation
|
2021-05-24 00:11:58 +03:00
|
|
|
Dir struct {
|
|
|
|
Name string
|
2021-05-24 00:11:58 +03:00
|
|
|
UID int
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// File is a tarrable file representation
|
2021-05-24 00:11:58 +03:00
|
|
|
File struct {
|
|
|
|
Name string
|
2021-05-24 00:11:58 +03:00
|
|
|
UID int
|
2021-05-24 00:11:58 +03:00
|
|
|
Contents *bytes.Buffer
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Hardlink is a representation of a hardlink
|
2021-05-24 00:11:58 +03:00
|
|
|
Hardlink struct {
|
|
|
|
Name string
|
2021-05-24 00:11:58 +03:00
|
|
|
UID int
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Buffer returns a byte buffer
|
2021-05-24 00:11:58 +03:00
|
|
|
func (tb Tarball) Buffer() *bytes.Buffer {
|
|
|
|
var buf bytes.Buffer
|
2021-05-24 00:11:58 +03:00
|
|
|
tw := tar.NewWriter(&buf)
|
|
|
|
for _, member := range tb {
|
|
|
|
member.Tar(tw)
|
|
|
|
}
|
|
|
|
tw.Close()
|
2021-05-24 00:11:58 +03:00
|
|
|
return &buf
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Tar tars the Dir
|
2021-05-24 00:11:58 +03:00
|
|
|
func (d Dir) Tar(tw *tar.Writer) error {
|
2021-05-24 00:11:58 +03:00
|
|
|
hdr := &tar.Header{
|
|
|
|
Typeflag: tar.TypeDir,
|
|
|
|
Name: d.Name,
|
|
|
|
Mode: 0644,
|
2021-05-24 00:11:58 +03:00
|
|
|
Uid: d.UID,
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
return tw.WriteHeader(hdr)
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Tar tars the File
|
2021-05-24 00:11:58 +03:00
|
|
|
func (f File) Tar(tw *tar.Writer) error {
|
|
|
|
var contents []byte
|
2021-05-24 00:11:58 +03:00
|
|
|
if f.Contents != nil {
|
2021-05-24 00:11:58 +03:00
|
|
|
contents = f.Contents.Bytes()
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
hdr := &tar.Header{
|
|
|
|
Typeflag: tar.TypeReg,
|
|
|
|
Name: f.Name,
|
|
|
|
Mode: 0644,
|
2021-05-24 00:11:58 +03:00
|
|
|
Uid: f.UID,
|
2021-05-24 00:11:58 +03:00
|
|
|
Size: int64(len(contents)),
|
|
|
|
}
|
|
|
|
if err := tw.WriteHeader(hdr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := tw.Write(contents); err != nil {
|
|
|
|
return err
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
return nil
|
2021-05-24 00:11:58 +03:00
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Tar tars the Hardlink
|
2021-05-24 00:11:58 +03:00
|
|
|
func (h Hardlink) Tar(tw *tar.Writer) error {
|
|
|
|
return tw.WriteHeader(&tar.Header{
|
2021-05-24 00:11:58 +03:00
|
|
|
Typeflag: tar.TypeLink,
|
|
|
|
Name: h.Name,
|
|
|
|
Mode: 0644,
|
2021-05-24 00:11:58 +03:00
|
|
|
Uid: h.UID,
|
2021-05-24 00:11:58 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
// Extract extracts a tarball to a slice of extractables
|
2021-05-24 00:11:58 +03:00
|
|
|
func Extract(t *testing.T, r io.Reader) []Extractable {
|
|
|
|
t.Helper()
|
|
|
|
ret := []Extractable{}
|
|
|
|
tr := tar.NewReader(r)
|
|
|
|
for {
|
|
|
|
hdr, err := tr.Next()
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var elem Extractable
|
|
|
|
switch hdr.Typeflag {
|
|
|
|
case tar.TypeDir:
|
2021-05-24 00:11:58 +03:00
|
|
|
elem = Dir{Name: hdr.Name, UID: hdr.Uid}
|
2021-05-24 00:11:58 +03:00
|
|
|
case tar.TypeLink:
|
|
|
|
elem = Hardlink{Name: hdr.Name}
|
|
|
|
case tar.TypeReg:
|
2021-05-24 00:11:58 +03:00
|
|
|
f := File{Name: hdr.Name, UID: hdr.Uid}
|
2021-05-24 00:11:58 +03:00
|
|
|
if hdr.Size > 0 {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
io.Copy(&buf, tr)
|
|
|
|
f.Contents = &buf
|
|
|
|
}
|
|
|
|
elem = f
|
|
|
|
}
|
|
|
|
ret = append(ret, elem)
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|