2017-07-13 03:45:53 +00:00
|
|
|
package filesync
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/moby/buildkit/session"
|
|
|
|
"github.com/moby/buildkit/session/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"golang.org/x/sync/errgroup"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFileSyncIncludePatterns(t *testing.T) {
|
2017-12-09 02:19:08 +00:00
|
|
|
t.Parallel()
|
2017-07-13 03:45:53 +00:00
|
|
|
tmpDir, err := ioutil.TempDir("", "fsynctest")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
destDir, err := ioutil.TempDir("", "fsynctest")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(tmpDir, "foo"), []byte("content1"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filepath.Join(tmpDir, "bar"), []byte("content2"), 0600)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
s, err := session.NewSession("foo", "bar")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m, err := session.NewManager()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-07-20 22:13:00 +00:00
|
|
|
fs := NewFSSyncProvider([]SyncedDir{{Name: "test0", Dir: tmpDir}})
|
2017-07-13 03:45:53 +00:00
|
|
|
s.Allow(fs)
|
|
|
|
|
|
|
|
dialer := session.Dialer(testutil.TestStream(testutil.Handler(m.HandleConn)))
|
|
|
|
|
|
|
|
g, ctx := errgroup.WithContext(context.Background())
|
|
|
|
|
|
|
|
g.Go(func() error {
|
|
|
|
return s.Run(ctx, dialer)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.Go(func() (reterr error) {
|
2017-07-14 18:05:54 +00:00
|
|
|
c, err := m.Get(ctx, s.ID())
|
2017-07-13 03:45:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := FSSync(ctx, c, FSSendRequestOpt{
|
2017-07-20 22:13:00 +00:00
|
|
|
Name: "test0",
|
2017-07-13 03:45:53 +00:00
|
|
|
DestDir: destDir,
|
|
|
|
IncludePatterns: []string{"ba*"},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = ioutil.ReadFile(filepath.Join(destDir, "foo"))
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
|
|
|
dt, err := ioutil.ReadFile(filepath.Join(destDir, "bar"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
assert.Equal(t, "content2", string(dt))
|
|
|
|
return s.Close()
|
|
|
|
})
|
|
|
|
|
|
|
|
err = g.Wait()
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|