2019-05-23 13:26:16 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2020-11-05 16:18:51 +01:00
|
|
|
"context"
|
2019-05-23 13:26:16 +02:00
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2019-07-28 19:47:38 +02:00
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fstest/mockfs"
|
2019-05-23 13:26:16 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
called = 0
|
|
|
|
errSentinel = errors.New("an error")
|
|
|
|
)
|
|
|
|
|
2022-12-08 13:43:53 +01:00
|
|
|
func mockNewFs(t *testing.T) func(ctx context.Context, path string) (fs.Fs, error) {
|
2019-05-23 13:26:16 +02:00
|
|
|
called = 0
|
2020-11-05 16:18:51 +01:00
|
|
|
create := func(ctx context.Context, path string) (f fs.Fs, err error) {
|
2019-05-23 13:26:16 +02:00
|
|
|
assert.Equal(t, 0, called)
|
|
|
|
called++
|
|
|
|
switch path {
|
2020-05-01 13:49:34 +02:00
|
|
|
case "mock:/":
|
2023-04-28 13:01:04 +02:00
|
|
|
return mockfs.NewFs(ctx, "mock", "/", nil)
|
2020-07-02 11:15:47 +02:00
|
|
|
case "mock:/file.txt", "mock:file.txt":
|
2023-04-28 13:01:04 +02:00
|
|
|
fMock, err := mockfs.NewFs(ctx, "mock", "/", nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return fMock, fs.ErrorIsFile
|
2020-05-01 13:49:34 +02:00
|
|
|
case "mock:/error":
|
2019-05-23 13:26:16 +02:00
|
|
|
return nil, errSentinel
|
|
|
|
}
|
2020-05-01 13:49:34 +02:00
|
|
|
t.Fatalf("Unknown path %q", path)
|
|
|
|
panic("unreachable")
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
2022-12-08 13:43:53 +01:00
|
|
|
t.Cleanup(Clear)
|
|
|
|
return create
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := GetFn(context.Background(), "mock:/", create)
|
2019-05-23 13:26:16 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 1, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f2, err := GetFn(context.Background(), "mock:/", create)
|
2019-05-23 13:26:16 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, f, f2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFile(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := GetFn(context.Background(), "mock:/file.txt", create)
|
2019-05-23 13:26:16 +02:00
|
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
2019-08-09 18:03:57 +02:00
|
|
|
require.NotNil(t, f)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 2, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f2, err := GetFn(context.Background(), "mock:/file.txt", create)
|
2019-05-23 13:26:16 +02:00
|
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
2019-08-09 18:03:57 +02:00
|
|
|
require.NotNil(t, f2)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
|
|
|
assert.Equal(t, f, f2)
|
2020-07-02 11:15:47 +02:00
|
|
|
|
|
|
|
// check parent is there too
|
2020-11-05 16:18:51 +01:00
|
|
|
f2, err = GetFn(context.Background(), "mock:/", create)
|
2020-07-02 11:15:47 +02:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, f2)
|
|
|
|
|
|
|
|
assert.Equal(t, f, f2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetFile2(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2020-07-02 11:15:47 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2020-07-02 11:15:47 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := GetFn(context.Background(), "mock:file.txt", create)
|
2020-07-02 11:15:47 +02:00
|
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
|
|
require.NotNil(t, f)
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 2, Entries())
|
2020-07-02 11:15:47 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f2, err := GetFn(context.Background(), "mock:file.txt", create)
|
2020-07-02 11:15:47 +02:00
|
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
|
|
require.NotNil(t, f2)
|
|
|
|
|
|
|
|
assert.Equal(t, f, f2)
|
|
|
|
|
|
|
|
// check parent is there too
|
2020-11-05 16:18:51 +01:00
|
|
|
f2, err = GetFn(context.Background(), "mock:/", create)
|
2020-07-02 11:15:47 +02:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.NotNil(t, f2)
|
|
|
|
|
|
|
|
assert.Equal(t, f, f2)
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetError(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
f, err := GetFn(context.Background(), "mock:/error", create)
|
2019-05-23 13:26:16 +02:00
|
|
|
require.Equal(t, errSentinel, err)
|
|
|
|
require.Equal(t, nil, f)
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPut(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2023-04-28 13:01:04 +02:00
|
|
|
f, err := mockfs.NewFs(context.Background(), "mock", "/alien", nil)
|
|
|
|
require.NoError(t, err)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-05-01 13:49:34 +02:00
|
|
|
Put("mock:/alien", f)
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 1, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
fNew, err := GetFn(context.Background(), "mock:/alien", create)
|
2019-05-23 13:26:16 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, f, fNew)
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 1, Entries())
|
2020-05-01 13:49:34 +02:00
|
|
|
|
|
|
|
// Check canonicalisation
|
|
|
|
|
|
|
|
Put("mock:/alien/", f)
|
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
fNew, err = GetFn(context.Background(), "mock:/alien/", create)
|
2020-05-01 13:49:34 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, f, fNew)
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 1, Entries())
|
2020-05-01 13:49:34 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPin(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2020-05-01 13:49:34 +02:00
|
|
|
|
2022-08-14 04:56:32 +02:00
|
|
|
// Test pinning and unpinning nonexistent
|
2023-04-28 13:01:04 +02:00
|
|
|
f, err := mockfs.NewFs(context.Background(), "mock", "/alien", nil)
|
|
|
|
require.NoError(t, err)
|
2020-05-01 13:49:34 +02:00
|
|
|
Pin(f)
|
|
|
|
Unpin(f)
|
|
|
|
|
|
|
|
// Now test pinning an existing
|
2020-11-05 16:18:51 +01:00
|
|
|
f2, err := GetFn(context.Background(), "mock:/", create)
|
2020-05-01 13:49:34 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
Pin(f2)
|
|
|
|
Unpin(f2)
|
|
|
|
}
|
|
|
|
|
2020-11-30 12:52:25 +01:00
|
|
|
func TestClearConfig(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2020-11-30 12:52:25 +01:00
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2020-11-30 12:52:25 +01:00
|
|
|
|
|
|
|
_, err := GetFn(context.Background(), "mock:/file.txt", create)
|
|
|
|
require.Equal(t, fs.ErrorIsFile, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 2, Entries()) // file + parent
|
|
|
|
|
|
|
|
assert.Equal(t, 2, ClearConfig("mock"))
|
|
|
|
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
}
|
|
|
|
|
2020-05-01 13:49:34 +02:00
|
|
|
func TestClear(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2020-05-01 13:49:34 +02:00
|
|
|
|
|
|
|
// Create something
|
2020-11-05 16:18:51 +01:00
|
|
|
_, err := GetFn(context.Background(), "mock:/", create)
|
2020-05-01 13:49:34 +02:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 1, Entries())
|
2020-05-01 13:49:34 +02:00
|
|
|
|
|
|
|
Clear()
|
|
|
|
|
2021-03-29 18:18:49 +02:00
|
|
|
assert.Equal(t, 0, Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
2020-11-29 12:25:51 +01:00
|
|
|
|
|
|
|
func TestEntries(t *testing.T) {
|
2022-12-08 13:43:53 +01:00
|
|
|
create := mockNewFs(t)
|
2020-11-29 12:25:51 +01:00
|
|
|
|
|
|
|
assert.Equal(t, 0, Entries())
|
|
|
|
|
|
|
|
// Create something
|
|
|
|
_, err := GetFn(context.Background(), "mock:/", create)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, Entries())
|
|
|
|
}
|