2021-05-24 00:11:58 +03:00
|
|
|
package tartest
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2021-05-24 00:11:58 +03:00
|
|
|
"compress/gzip"
|
|
|
|
"io"
|
2021-05-24 00:11:58 +03:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-05-24 00:11:58 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-05-24 00:11:58 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestTarball(t *testing.T) {
|
|
|
|
bk := Tarball{File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("hello")}}
|
|
|
|
img := Tarball{
|
2021-05-24 00:11:58 +03:00
|
|
|
File{Name: "backup.tar", Contents: bk.Buffer()},
|
2021-05-24 00:11:58 +03:00
|
|
|
File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("bye")},
|
|
|
|
Dir{Name: "bin"},
|
|
|
|
Hardlink{Name: "entrypoint2"},
|
|
|
|
}
|
|
|
|
|
2021-05-24 00:11:58 +03:00
|
|
|
got := Extract(t, img.Buffer())
|
2021-05-24 00:11:58 +03:00
|
|
|
want := []Extractable{
|
2021-05-24 00:11:58 +03:00
|
|
|
File{Name: "backup.tar", Contents: bk.Buffer()},
|
2021-05-24 00:11:58 +03:00
|
|
|
File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("bye")},
|
|
|
|
Dir{Name: "bin"},
|
|
|
|
Hardlink{Name: "entrypoint2"},
|
|
|
|
}
|
|
|
|
assert.Equal(t, want, got)
|
|
|
|
}
|
2021-05-24 00:11:58 +03:00
|
|
|
|
|
|
|
func TestGzip(t *testing.T) {
|
|
|
|
tb := Tarball{File{Name: "entrypoint.sh", Contents: bytes.NewBufferString("hello")}}
|
|
|
|
tbuf := tb.Buffer()
|
|
|
|
|
|
|
|
tgz, err := gzip.NewReader(tb.Gzip())
|
|
|
|
require.NoError(t, err)
|
|
|
|
var uncompressed bytes.Buffer
|
|
|
|
_, err = io.Copy(&uncompressed, tgz)
|
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, tbuf.Bytes(), uncompressed.Bytes())
|
|
|
|
|
|
|
|
}
|