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
2023-10-03 17:25:41 +00:00
"gitlab.com/manytask/itmo-go/public/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")
2023-10-02 19:32:41 +00:00
require.NoError(t, os.Mkdir(from, 0o777))
require.NoError(t, os.Mkdir(to, 0o777))
2020-03-27 22:52:54 +00:00
var buf bytes.Buffer
2023-10-02 19:32:41 +00:00
require.NoError(t, os.Mkdir(filepath.Join(from, "a"), 0o777))
require.NoError(t, os.MkdirAll(filepath.Join(from, "b", "c", "d"), 0o777))
require.NoError(t, ioutil.WriteFile(filepath.Join(from, "a", "x.bin"), []byte("xxx"), 0o777))
require.NoError(t, ioutil.WriteFile(filepath.Join(from, "b", "c", "y.txt"), []byte("yyy"), 0o666))
2020-03-27 22:52:54 +00:00
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)
}
2023-10-02 19:32:41 +00:00
checkFile(filepath.Join(to, "a", "x.bin"), []byte("xxx"), 0o755)
checkFile(filepath.Join(to, "b", "c", "y.txt"), []byte("yyy"), 0o644)
2020-03-27 22:52:54 +00:00
}
2020-04-11 20:34:14 +00:00
func init() {
2023-10-02 19:32:41 +00:00
unix.Umask(0o022)
2020-04-11 20:34:14 +00:00
}