undocker/internal/tartest/tartest_test.go

49 lines
1.2 KiB
Go
Raw Normal View History

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
"reflect"
2021-05-24 00:11:58 +03:00
"testing"
)
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"},
}
2021-05-24 00:11:58 +03:00
if !reflect.DeepEqual(want, got) {
t.Errorf("tarball mismatch. want: %+v, got: %+v", want, got)
}
2021-05-24 00:11:58 +03:00
}
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())
2021-05-24 00:11:58 +03:00
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
2021-05-24 00:11:58 +03:00
var uncompressed bytes.Buffer
2021-05-24 00:11:58 +03:00
if _, err := io.Copy(&uncompressed, tgz); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !bytes.Equal(tbuf.Bytes(), uncompressed.Bytes()) {
t.Errorf("tbuf and uncompressed bytes mismatch")
}
2021-05-24 00:11:58 +03:00
}