shad-go/distbuild/pkg/tarstream/stream_test.go

68 lines
1.6 KiB
Go
Raw Normal View History

package tarstream_test
2020-03-27 22:52:54 +00:00
import (
"bytes"
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/require"
2020-04-11 20:34:14 +00:00
"golang.org/x/sys/unix"
2022-04-17 15:28:33 +00:00
"gitlab.com/slon/shad-go/distbuild/pkg/tarstream"
2020-03-27 22:52:54 +00:00
)
func TestTarStream(t *testing.T) {
tmpDir, err := ioutil.TempDir("", "tarstream")
require.NoError(t, err)
t.Logf("running inside %s", tmpDir)
from := filepath.Join(tmpDir, "from")
to := filepath.Join(tmpDir, "to")
require.NoError(t, os.Mkdir(from, 0777))
require.NoError(t, os.Mkdir(to, 0777))
var buf bytes.Buffer
require.NoError(t, os.Mkdir(filepath.Join(from, "a"), 0777))
require.NoError(t, os.MkdirAll(filepath.Join(from, "b", "c", "d"), 0777))
require.NoError(t, ioutil.WriteFile(filepath.Join(from, "a", "x.bin"), []byte("xxx"), 0777))
require.NoError(t, ioutil.WriteFile(filepath.Join(from, "b", "c", "y.txt"), []byte("yyy"), 0666))
2021-04-15 13:05:58 +00:00
require.NoError(t, tarstream.Send(from, &buf))
2020-03-27 22:52:54 +00:00
2021-04-15 13:05:58 +00:00
require.NoError(t, tarstream.Receive(to, &buf))
2020-03-27 22:52:54 +00:00
checkDir := func(path string) {
st, err := os.Stat(path)
require.NoError(t, err)
require.True(t, st.IsDir())
}
checkDir(filepath.Join(to, "a"))
checkDir(filepath.Join(to, "b", "c", "d"))
checkFile := func(path string, content []byte, mode os.FileMode) {
t.Helper()
st, err := os.Stat(path)
require.NoError(t, err)
require.Equal(t, mode.String(), st.Mode().String())
b, err := ioutil.ReadFile(path)
require.NoError(t, err)
require.Equal(t, content, b)
}
2020-04-14 11:22:08 +00:00
checkFile(filepath.Join(to, "a", "x.bin"), []byte("xxx"), 0755)
checkFile(filepath.Join(to, "b", "c", "y.txt"), []byte("yyy"), 0644)
2020-03-27 22:52:54 +00:00
}
2020-04-11 20:34:14 +00:00
func init() {
unix.Umask(0022)
}