github.com/10XDev/rclone@v1.52.3-0.20200626220027-16af9ab76b2a/fs/sync/rc_test.go (about)

     1  package sync
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/rclone/rclone/fs/cache"
     8  	"github.com/rclone/rclone/fs/rc"
     9  	"github.com/rclone/rclone/fstest"
    10  	"github.com/stretchr/testify/assert"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func rcNewRun(t *testing.T, method string) (*fstest.Run, *rc.Call) {
    15  	if *fstest.RemoteName != "" {
    16  		t.Skip("Skipping test on non local remote")
    17  	}
    18  	r := fstest.NewRun(t)
    19  	call := rc.Calls.Get(method)
    20  	assert.NotNil(t, call)
    21  	cache.Put(r.LocalName, r.Flocal)
    22  	cache.Put(r.FremoteName, r.Fremote)
    23  	return r, call
    24  }
    25  
    26  // sync/copy: copy a directory from source remote to destination remote
    27  func TestRcCopy(t *testing.T) {
    28  	r, call := rcNewRun(t, "sync/copy")
    29  	defer r.Finalise()
    30  	r.Mkdir(context.Background(), r.Fremote)
    31  
    32  	file1 := r.WriteBoth(context.Background(), "file1", "file1 contents", t1)
    33  	file2 := r.WriteFile("subdir/file2", "file2 contents", t2)
    34  	file3 := r.WriteObject(context.Background(), "subdir/subsubdir/file3", "file3 contents", t3)
    35  
    36  	fstest.CheckItems(t, r.Flocal, file1, file2)
    37  	fstest.CheckItems(t, r.Fremote, file1, file3)
    38  
    39  	in := rc.Params{
    40  		"srcFs": r.LocalName,
    41  		"dstFs": r.FremoteName,
    42  	}
    43  	out, err := call.Fn(context.Background(), in)
    44  	require.NoError(t, err)
    45  	assert.Equal(t, rc.Params(nil), out)
    46  
    47  	fstest.CheckItems(t, r.Flocal, file1, file2)
    48  	fstest.CheckItems(t, r.Fremote, file1, file2, file3)
    49  }
    50  
    51  // sync/move: move a directory from source remote to destination remote
    52  func TestRcMove(t *testing.T) {
    53  	r, call := rcNewRun(t, "sync/move")
    54  	defer r.Finalise()
    55  	r.Mkdir(context.Background(), r.Fremote)
    56  
    57  	file1 := r.WriteBoth(context.Background(), "file1", "file1 contents", t1)
    58  	file2 := r.WriteFile("subdir/file2", "file2 contents", t2)
    59  	file3 := r.WriteObject(context.Background(), "subdir/subsubdir/file3", "file3 contents", t3)
    60  
    61  	fstest.CheckItems(t, r.Flocal, file1, file2)
    62  	fstest.CheckItems(t, r.Fremote, file1, file3)
    63  
    64  	in := rc.Params{
    65  		"srcFs": r.LocalName,
    66  		"dstFs": r.FremoteName,
    67  	}
    68  	out, err := call.Fn(context.Background(), in)
    69  	require.NoError(t, err)
    70  	assert.Equal(t, rc.Params(nil), out)
    71  
    72  	fstest.CheckItems(t, r.Flocal)
    73  	fstest.CheckItems(t, r.Fremote, file1, file2, file3)
    74  }
    75  
    76  // sync/sync: sync a directory from source remote to destination remote
    77  func TestRcSync(t *testing.T) {
    78  	r, call := rcNewRun(t, "sync/sync")
    79  	defer r.Finalise()
    80  	r.Mkdir(context.Background(), r.Fremote)
    81  
    82  	file1 := r.WriteBoth(context.Background(), "file1", "file1 contents", t1)
    83  	file2 := r.WriteFile("subdir/file2", "file2 contents", t2)
    84  	file3 := r.WriteObject(context.Background(), "subdir/subsubdir/file3", "file3 contents", t3)
    85  
    86  	fstest.CheckItems(t, r.Flocal, file1, file2)
    87  	fstest.CheckItems(t, r.Fremote, file1, file3)
    88  
    89  	in := rc.Params{
    90  		"srcFs": r.LocalName,
    91  		"dstFs": r.FremoteName,
    92  	}
    93  	out, err := call.Fn(context.Background(), in)
    94  	require.NoError(t, err)
    95  	assert.Equal(t, rc.Params(nil), out)
    96  
    97  	fstest.CheckItems(t, r.Flocal, file1, file2)
    98  	fstest.CheckItems(t, r.Fremote, file1, file2)
    99  }