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