github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/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  	r.Mkdir(context.Background(), r.Fremote)
    30  
    31  	file1 := r.WriteBoth(context.Background(), "file1", "file1 contents", t1)
    32  	file2 := r.WriteFile("subdir/file2", "file2 contents", t2)
    33  	file3 := r.WriteObject(context.Background(), "subdir/subsubdir/file3", "file3 contents", t3)
    34  
    35  	r.CheckLocalItems(t, file1, file2)
    36  	r.CheckRemoteItems(t, file1, file3)
    37  
    38  	in := rc.Params{
    39  		"srcFs": r.LocalName,
    40  		"dstFs": r.FremoteName,
    41  	}
    42  	out, err := call.Fn(context.Background(), in)
    43  	require.NoError(t, err)
    44  	assert.Equal(t, rc.Params(nil), out)
    45  
    46  	r.CheckLocalItems(t, file1, file2)
    47  	r.CheckRemoteItems(t, file1, file2, file3)
    48  }
    49  
    50  // sync/move: move a directory from source remote to destination remote
    51  func TestRcMove(t *testing.T) {
    52  	r, call := rcNewRun(t, "sync/move")
    53  	r.Mkdir(context.Background(), r.Fremote)
    54  
    55  	file1 := r.WriteBoth(context.Background(), "file1", "file1 contents", t1)
    56  	file2 := r.WriteFile("subdir/file2", "file2 contents", t2)
    57  	file3 := r.WriteObject(context.Background(), "subdir/subsubdir/file3", "file3 contents", t3)
    58  
    59  	r.CheckLocalItems(t, file1, file2)
    60  	r.CheckRemoteItems(t, file1, file3)
    61  
    62  	in := rc.Params{
    63  		"srcFs": r.LocalName,
    64  		"dstFs": r.FremoteName,
    65  	}
    66  	out, err := call.Fn(context.Background(), in)
    67  	require.NoError(t, err)
    68  	assert.Equal(t, rc.Params(nil), out)
    69  
    70  	r.CheckLocalItems(t)
    71  	r.CheckRemoteItems(t, file1, file2, file3)
    72  }
    73  
    74  // sync/sync: sync a directory from source remote to destination remote
    75  func TestRcSync(t *testing.T) {
    76  	r, call := rcNewRun(t, "sync/sync")
    77  	r.Mkdir(context.Background(), r.Fremote)
    78  
    79  	file1 := r.WriteBoth(context.Background(), "file1", "file1 contents", t1)
    80  	file2 := r.WriteFile("subdir/file2", "file2 contents", t2)
    81  	file3 := r.WriteObject(context.Background(), "subdir/subsubdir/file3", "file3 contents", t3)
    82  
    83  	r.CheckLocalItems(t, file1, file2)
    84  	r.CheckRemoteItems(t, file1, file3)
    85  
    86  	in := rc.Params{
    87  		"srcFs": r.LocalName,
    88  		"dstFs": r.FremoteName,
    89  	}
    90  	out, err := call.Fn(context.Background(), in)
    91  	require.NoError(t, err)
    92  	assert.Equal(t, rc.Params(nil), out)
    93  
    94  	r.CheckLocalItems(t, file1, file2)
    95  	r.CheckRemoteItems(t, file1, file2)
    96  }