github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/fs/rc/cache_test.go (about) 1 package rc 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 8 "github.com/rclone/rclone/fs" 9 "github.com/rclone/rclone/fs/cache" 10 "github.com/rclone/rclone/fs/filter" 11 "github.com/rclone/rclone/fstest/mockfs" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func mockNewFs(t *testing.T) func() { 17 ctx := context.Background() 18 f, err := mockfs.NewFs(ctx, "/", "", nil) 19 require.NoError(t, err) 20 cache.Put("/", f) 21 f, err = mockfs.NewFs(ctx, "mock", "/", nil) 22 require.NoError(t, err) 23 cache.Put("mock:/", f) 24 cache.Put(":mock:/", f) 25 f, err = mockfs.NewFs(ctx, "mock", "dir/file.txt", nil) 26 require.NoError(t, err) 27 cache.PutErr("mock:dir/file.txt", f, fs.ErrorIsFile) 28 return func() { 29 cache.Clear() 30 } 31 } 32 33 func TestGetFsNamed(t *testing.T) { 34 defer mockNewFs(t)() 35 36 in := Params{ 37 "potato": "/", 38 } 39 f, err := GetFsNamed(context.Background(), in, "potato") 40 require.NoError(t, err) 41 assert.NotNil(t, f) 42 43 in = Params{ 44 "sausage": "/", 45 } 46 f, err = GetFsNamed(context.Background(), in, "potato") 47 require.Error(t, err) 48 assert.Nil(t, f) 49 } 50 51 func TestGetFsNamedStruct(t *testing.T) { 52 defer mockNewFs(t)() 53 54 in := Params{ 55 "potato": Params{ 56 "type": "mock", 57 "_root": "/", 58 }, 59 } 60 f, err := GetFsNamed(context.Background(), in, "potato") 61 require.NoError(t, err) 62 assert.NotNil(t, f) 63 64 in = Params{ 65 "potato": Params{ 66 "_name": "mock", 67 "_root": "/", 68 }, 69 } 70 f, err = GetFsNamed(context.Background(), in, "potato") 71 require.NoError(t, err) 72 assert.NotNil(t, f) 73 } 74 75 func TestGetFsNamedFileOK(t *testing.T) { 76 defer mockNewFs(t)() 77 ctx := context.Background() 78 79 in := Params{ 80 "potato": "/", 81 } 82 newCtx, f, err := GetFsNamedFileOK(ctx, in, "potato") 83 require.NoError(t, err) 84 assert.NotNil(t, f) 85 assert.Equal(t, ctx, newCtx) 86 87 in = Params{ 88 "sausage": "/", 89 } 90 newCtx, f, err = GetFsNamedFileOK(ctx, in, "potato") 91 require.Error(t, err) 92 assert.Nil(t, f) 93 assert.Equal(t, ctx, newCtx) 94 95 in = Params{ 96 "potato": "mock:dir/file.txt", 97 } 98 newCtx, f, err = GetFsNamedFileOK(ctx, in, "potato") 99 assert.Nil(t, err) 100 assert.NotNil(t, f) 101 assert.NotEqual(t, ctx, newCtx) 102 103 fi := filter.GetConfig(newCtx) 104 assert.False(t, fi.InActive()) 105 assert.True(t, fi.IncludeRemote("file.txt")) 106 assert.False(t, fi.IncludeRemote("other.txt")) 107 } 108 109 func TestGetConfigMap(t *testing.T) { 110 for _, test := range []struct { 111 in Params 112 fsName string 113 wantFsString string 114 wantErr string 115 }{ 116 { 117 in: Params{ 118 "Fs": Params{}, 119 }, 120 fsName: "Fs", 121 wantErr: `couldn't find "type" or "_name" in JSON config definition`, 122 }, 123 { 124 in: Params{ 125 "Fs": Params{ 126 "notastring": true, 127 }, 128 }, 129 fsName: "Fs", 130 wantErr: `cannot unmarshal bool`, 131 }, 132 { 133 in: Params{ 134 "Fs": Params{ 135 "_name": "potato", 136 }, 137 }, 138 fsName: "Fs", 139 wantFsString: "potato:", 140 }, 141 { 142 in: Params{ 143 "Fs": Params{ 144 "type": "potato", 145 }, 146 }, 147 fsName: "Fs", 148 wantFsString: ":potato:", 149 }, 150 { 151 in: Params{ 152 "Fs": Params{ 153 "type": "sftp", 154 "_name": "potato", 155 "parameter": "42", 156 "parameter2": "true", 157 "_root": "/path/to/somewhere", 158 }, 159 }, 160 fsName: "Fs", 161 wantFsString: "potato,parameter='42',parameter2='true':/path/to/somewhere", 162 }, 163 } { 164 gotFsString, gotErr := getConfigMap(test.in, test.fsName) 165 what := fmt.Sprintf("%+v", test.in) 166 assert.Equal(t, test.wantFsString, gotFsString, what) 167 if test.wantErr == "" { 168 assert.NoError(t, gotErr) 169 } else { 170 require.Error(t, gotErr) 171 assert.Contains(t, gotErr.Error(), test.wantErr) 172 173 } 174 } 175 } 176 177 func TestGetFs(t *testing.T) { 178 defer mockNewFs(t)() 179 180 in := Params{ 181 "fs": "/", 182 } 183 f, err := GetFs(context.Background(), in) 184 require.NoError(t, err) 185 assert.NotNil(t, f) 186 } 187 188 func TestGetFsAndRemoteNamed(t *testing.T) { 189 defer mockNewFs(t)() 190 191 in := Params{ 192 "fs": "/", 193 "remote": "hello", 194 } 195 f, remote, err := GetFsAndRemoteNamed(context.Background(), in, "fs", "remote") 196 require.NoError(t, err) 197 assert.NotNil(t, f) 198 assert.Equal(t, "hello", remote) 199 200 f, _, err = GetFsAndRemoteNamed(context.Background(), in, "fsX", "remote") 201 require.Error(t, err) 202 assert.Nil(t, f) 203 204 f, _, err = GetFsAndRemoteNamed(context.Background(), in, "fs", "remoteX") 205 require.Error(t, err) 206 assert.Nil(t, f) 207 208 } 209 210 func TestGetFsAndRemote(t *testing.T) { 211 defer mockNewFs(t)() 212 213 in := Params{ 214 "fs": "/", 215 "remote": "hello", 216 } 217 f, remote, err := GetFsAndRemote(context.Background(), in) 218 require.NoError(t, err) 219 assert.NotNil(t, f) 220 assert.Equal(t, "hello", remote) 221 222 t.Run("RcFscache", func(t *testing.T) { 223 getEntries := func() int { 224 call := Calls.Get("fscache/entries") 225 require.NotNil(t, call) 226 227 in := Params{} 228 out, err := call.Fn(context.Background(), in) 229 require.NoError(t, err) 230 require.NotNil(t, out) 231 return out["entries"].(int) 232 } 233 234 t.Run("Entries", func(t *testing.T) { 235 assert.NotEqual(t, 0, getEntries()) 236 }) 237 238 t.Run("Clear", func(t *testing.T) { 239 call := Calls.Get("fscache/clear") 240 require.NotNil(t, call) 241 242 in := Params{} 243 out, err := call.Fn(context.Background(), in) 244 require.NoError(t, err) 245 require.Nil(t, out) 246 }) 247 248 t.Run("Entries2", func(t *testing.T) { 249 assert.Equal(t, 0, getEntries()) 250 }) 251 }) 252 }