2019-05-23 11:26:16 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"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")
|
|
|
|
)
|
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
func mockNewFs(t *testing.T) (func(), func(path string) (fs.Fs, error)) {
|
2019-05-23 11:26:16 +00:00
|
|
|
called = 0
|
2020-05-01 11:49:34 +00:00
|
|
|
create := func(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:/":
|
|
|
|
return mockfs.NewFs("mock", "/"), nil
|
|
|
|
case "mock:/file.txt":
|
|
|
|
return mockfs.NewFs("mock", "/"), fs.ErrorIsFile
|
|
|
|
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() {
|
|
|
|
c.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
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 0, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
f, err := GetFn("mock:/", create)
|
2019-05-23 11:26:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 1, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
f2, err := GetFn("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
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 0, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
f, err := GetFn("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
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 1, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
f2, err := GetFn("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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 0, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
f, err := GetFn("mock:/error", create)
|
2019-05-23 11:26:16 +00:00
|
|
|
require.Equal(t, errSentinel, err)
|
|
|
|
require.Equal(t, nil, f)
|
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 0, c.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-05-01 11:49:34 +00:00
|
|
|
f := mockfs.NewFs("mock", "/alien")
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 0, c.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
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 1, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
|
2020-05-01 11:49:34 +00:00
|
|
|
fNew, err := GetFn("mock:/alien", create)
|
2019-05-23 11:26:16 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, f, fNew)
|
|
|
|
|
2019-07-31 21:19:07 +00:00
|
|
|
assert.Equal(t, 1, c.Entries())
|
2020-05-01 11:49:34 +00:00
|
|
|
|
|
|
|
// Check canonicalisation
|
|
|
|
|
|
|
|
Put("mock:/alien/", f)
|
|
|
|
|
|
|
|
fNew, err = GetFn("mock:/alien/", create)
|
|
|
|
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
|
|
|
|
f := mockfs.NewFs("mock", "/alien")
|
|
|
|
Pin(f)
|
|
|
|
Unpin(f)
|
|
|
|
|
|
|
|
// Now test pinning an existing
|
|
|
|
f2, err := GetFn("mock:/", create)
|
|
|
|
require.NoError(t, err)
|
|
|
|
Pin(f2)
|
|
|
|
Unpin(f2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClear(t *testing.T) {
|
|
|
|
cleanup, create := mockNewFs(t)
|
|
|
|
defer cleanup()
|
|
|
|
|
|
|
|
// Create something
|
|
|
|
_, err := GetFn("mock:/", create)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, c.Entries())
|
|
|
|
|
|
|
|
Clear()
|
|
|
|
|
|
|
|
assert.Equal(t, 0, c.Entries())
|
2019-05-23 11:26:16 +00:00
|
|
|
}
|