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")
|
|
|
|
)
|
|
|
|
|
2020-11-05 16:18:51 +01:00
|
|
|
func mockNewFs(t *testing.T) (func(), 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:/":
|
2020-11-05 17:00:08 +01:00
|
|
|
return mockfs.NewFs(ctx, "mock", "/"), nil
|
2020-07-02 11:15:47 +02:00
|
|
|
case "mock:/file.txt", "mock:file.txt":
|
2020-11-05 17:00:08 +01:00
|
|
|
return mockfs.NewFs(ctx, "mock", "/"), 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
|
|
|
}
|
2019-07-31 23:19:07 +02:00
|
|
|
cleanup := func() {
|
|
|
|
c.Clear()
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
2019-07-31 23:19:07 +02:00
|
|
|
return cleanup, create
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGet(t *testing.T) {
|
2019-07-31 23:19:07 +02:00
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 0, c.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)
|
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 1, c.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) {
|
2019-07-31 23:19:07 +02:00
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 0, c.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
|
|
|
|
2020-07-02 11:15:47 +02:00
|
|
|
assert.Equal(t, 2, c.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) {
|
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
assert.Equal(t, 0, c.Entries())
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
assert.Equal(t, 2, c.Entries())
|
|
|
|
|
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) {
|
2019-07-31 23:19:07 +02:00
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 0, c.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)
|
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 0, c.Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPut(t *testing.T) {
|
2019-07-31 23:19:07 +02:00
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2020-11-05 17:00:08 +01:00
|
|
|
f := mockfs.NewFs(context.Background(), "mock", "/alien")
|
2019-05-23 13:26:16 +02:00
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 0, c.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
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 1, c.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)
|
|
|
|
|
2019-07-31 23:19:07 +02:00
|
|
|
assert.Equal(t, 1, c.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)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, c.Entries())
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPin(t *testing.T) {
|
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Test pinning and unpinning non existent
|
2020-11-05 17:00:08 +01:00
|
|
|
f := mockfs.NewFs(context.Background(), "mock", "/alien")
|
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) {
|
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
assert.Equal(t, 0, c.Entries())
|
|
|
|
|
|
|
|
_, 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) {
|
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, c.Entries())
|
|
|
|
|
|
|
|
Clear()
|
|
|
|
|
|
|
|
assert.Equal(t, 0, c.Entries())
|
2019-05-23 13:26:16 +02:00
|
|
|
}
|
2020-11-29 12:25:51 +01: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())
|
|
|
|
}
|