Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d586ed4f9 |
No files matched your search
@@ -4,8 +4,10 @@ package snapshot
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"syscall"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/afero"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
)
|
)
|
||||||
@@ -38,3 +40,36 @@ func TestDeviceNumberFromStat(t *testing.T) {
|
|||||||
// /dev is a different filesystem from the temp dir on Linux, macOS and in Docker.
|
// /dev is a different filesystem from the temp dir on Linux, macOS and in Docker.
|
||||||
assert.NotEqual(t, deviceOf(dir), deviceOf("/dev"))
|
assert.NotEqual(t, deviceOf(dir), deviceOf("/dev"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fileInfoWithStat is a file's info whose Sys returns stat.
|
||||||
|
type fileInfoWithStat struct {
|
||||||
|
os.FileInfo
|
||||||
|
|
||||||
|
stat *syscall.Stat_t
|
||||||
|
}
|
||||||
|
|
||||||
|
func (i fileInfoWithStat) Sys() any { return i.stat }
|
||||||
|
|
||||||
|
// The in-memory test filesystem has no device numbers. The pinned afero
|
||||||
|
// gives no stat; its unreleased master gives a stat holding only uid, gid
|
||||||
|
// and size. See https://git.eeqj.de/sneak/vaultik/issues/462.
|
||||||
|
func TestDeviceNumberFromStatInMemory(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
fs := afero.NewMemMapFs()
|
||||||
|
require.NoError(t, afero.WriteFile(fs, "/file.txt", []byte("data"), 0o600))
|
||||||
|
|
||||||
|
info, err := fs.Stat("/file.txt")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, ok := deviceNumberFromStat(info)
|
||||||
|
assert.False(t, ok, "device number read from a file info with no stat")
|
||||||
|
|
||||||
|
zeroed := fileInfoWithStat{
|
||||||
|
FileInfo: info,
|
||||||
|
stat: &syscall.Stat_t{Uid: 1000, Gid: 1000, Size: 4},
|
||||||
|
}
|
||||||
|
|
||||||
|
_, ok = deviceNumberFromStat(zeroed)
|
||||||
|
assert.False(t, ok, "device number read from a stat with no device or inode number")
|
||||||
|
}
|
||||||
@@ -1438,11 +1438,12 @@ func (s *Scanner) deviceNumberAt(path string) (uint64, bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deviceNumberFromStat returns the number of the device that holds the
|
// deviceNumberFromStat returns the number of the device that holds the
|
||||||
// file info describes, and false when info carries no stat, as from the
|
// file info describes, and false when info carries no stat from a real
|
||||||
// in-memory test filesystem.
|
// filesystem. The in-memory test filesystem gives either no stat or one
|
||||||
|
// whose device and inode numbers are both zero, which no real file has.
|
||||||
func deviceNumberFromStat(info os.FileInfo) (uint64, bool) {
|
func deviceNumberFromStat(info os.FileInfo) (uint64, bool) {
|
||||||
stat, ok := info.Sys().(*syscall.Stat_t)
|
stat, ok := info.Sys().(*syscall.Stat_t)
|
||||||
if !ok {
|
if !ok || (stat.Dev == 0 && stat.Ino == 0) {
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -67,12 +67,10 @@ func NewRcloneStorer(ctx context.Context, remote, path string) (*RcloneStorer, e
|
|||||||
// Check for remote not found error
|
// Check for remote not found error
|
||||||
if strings.Contains(err.Error(), "didn't find section in config file") ||
|
if strings.Contains(err.Error(), "didn't find section in config file") ||
|
||||||
strings.Contains(err.Error(), "failed to find remote") {
|
strings.Contains(err.Error(), "failed to find remote") {
|
||||||
return nil, fmt.Errorf("%w: %q (%s)",
|
return nil, fmt.Errorf("%w: %s", ErrRemoteNotFound, remote)
|
||||||
ErrRemoteNotFound, remote, describeRcloneConfigFile())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil, fmt.Errorf("opening rclone remote %q (%s): %w",
|
return nil, fmt.Errorf("creating rclone filesystem: %w", err)
|
||||||
remote, describeRcloneConfigFile(), err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return &RcloneStorer{
|
return &RcloneStorer{
|
||||||
@@ -326,22 +324,6 @@ func (r *RcloneStorer) upload(ctx context.Context, key string, data io.Reader) e
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// describeRcloneConfigFile names the rclone config file for an error
|
|
||||||
// message, and says so when that file does not exist.
|
|
||||||
func describeRcloneConfigFile() string {
|
|
||||||
path := config.GetConfigPath()
|
|
||||||
if path == "" {
|
|
||||||
return "no config file"
|
|
||||||
}
|
|
||||||
|
|
||||||
_, err := os.Stat(path)
|
|
||||||
if errors.Is(err, os.ErrNotExist) {
|
|
||||||
return fmt.Sprintf("config file %q, which does not exist", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
return fmt.Sprintf("config file %q", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// progressReader wraps an io.Reader to track read progress.
|
// progressReader wraps an io.Reader to track read progress.
|
||||||
type progressReader struct {
|
type progressReader struct {
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
|
|||||||
@@ -398,100 +398,24 @@ func TestNewRcloneStorerUnknownRemote(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// setRcloneConfigEnv points RCLONE_CONFIG at path for the rest of the test.
|
// TestNewRcloneStorerReadsRcloneConfigEnv checks that a remote defined only
|
||||||
func setRcloneConfigEnv(t *testing.T, path string) {
|
// in the file RCLONE_CONFIG names is found and written to, as rclone itself
|
||||||
t.Helper()
|
// would.
|
||||||
|
func TestNewRcloneStorerReadsRcloneConfigEnv(t *testing.T) {
|
||||||
// rclone keeps the config file path in a process-global variable, which
|
// rclone keeps the config file path in a process-global variable, which
|
||||||
// NewRcloneStorer leaves pointing at path.
|
// NewRcloneStorer leaves pointing at this test's file.
|
||||||
previous := config.GetConfigPath()
|
previous := config.GetConfigPath()
|
||||||
|
|
||||||
t.Cleanup(func() { _ = config.SetConfigPath(previous) })
|
t.Cleanup(func() { _ = config.SetConfigPath(previous) })
|
||||||
|
|
||||||
t.Setenv("RCLONE_CONFIG", path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// writeRcloneConfigFile writes contents to a new rclone config file and
|
|
||||||
// returns its path.
|
|
||||||
func writeRcloneConfigFile(t *testing.T, contents string) string {
|
|
||||||
t.Helper()
|
|
||||||
|
|
||||||
confFile := filepath.Join(t.TempDir(), "rclone.conf")
|
confFile := filepath.Join(t.TempDir(), "rclone.conf")
|
||||||
|
|
||||||
err := os.WriteFile(confFile, []byte(contents), 0o600)
|
err := os.WriteFile(confFile, []byte("[loc]\ntype = local\n"), 0o600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("writing rclone config: %v", err)
|
t.Fatalf("writing rclone config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return confFile
|
t.Setenv("RCLONE_CONFIG", confFile)
|
||||||
}
|
|
||||||
|
|
||||||
// TestNewRcloneStorerErrorsNameRemoteAndConfigFile checks that each error
|
|
||||||
// from opening a remote names the remote and the rclone config file, and
|
|
||||||
// says when that file does not exist.
|
|
||||||
//
|
|
||||||
//nolint:paralleltest // sets RCLONE_CONFIG and the process-global rclone config
|
|
||||||
func TestNewRcloneStorerErrorsNameRemoteAndConfigFile(t *testing.T) {
|
|
||||||
const doesNotExist = "which does not exist"
|
|
||||||
|
|
||||||
missingFile := filepath.Join(t.TempDir(), "missing.conf")
|
|
||||||
emptyFile := writeRcloneConfigFile(t, "")
|
|
||||||
badRemoteFile := writeRcloneConfigFile(t,
|
|
||||||
"[bad]\ntype = vaultik-no-such-backend\n")
|
|
||||||
|
|
||||||
tests := []struct {
|
|
||||||
name string
|
|
||||||
configFile string
|
|
||||||
remote string
|
|
||||||
wantNotFound bool
|
|
||||||
wantNoSuchFile bool
|
|
||||||
}{
|
|
||||||
{"config file missing", missingFile, "dav", true, true},
|
|
||||||
{"remote not in config file", emptyFile, "dav", true, false},
|
|
||||||
{"remote fails to open", badRemoteFile, "bad", false, false},
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, tt := range tests {
|
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
|
||||||
setRcloneConfigEnv(t, tt.configFile)
|
|
||||||
|
|
||||||
_, err := storage.NewRcloneStorer(
|
|
||||||
context.Background(), tt.remote, "path")
|
|
||||||
if err == nil {
|
|
||||||
t.Fatal("NewRcloneStorer succeeded, want an error")
|
|
||||||
}
|
|
||||||
|
|
||||||
msg := err.Error()
|
|
||||||
|
|
||||||
if errors.Is(err, storage.ErrRemoteNotFound) != tt.wantNotFound {
|
|
||||||
t.Errorf("error = %v, want ErrRemoteNotFound: %v",
|
|
||||||
err, tt.wantNotFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(msg, `"`+tt.remote+`"`) {
|
|
||||||
t.Errorf("error %q does not name remote %q", msg, tt.remote)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(msg, tt.configFile) {
|
|
||||||
t.Errorf("error %q does not name config file %q",
|
|
||||||
msg, tt.configFile)
|
|
||||||
}
|
|
||||||
|
|
||||||
if strings.Contains(msg, doesNotExist) != tt.wantNoSuchFile {
|
|
||||||
t.Errorf("error %q, want it to say the file does not exist: %v",
|
|
||||||
msg, tt.wantNoSuchFile)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestNewRcloneStorerReadsRcloneConfigEnv checks that a remote defined only
|
|
||||||
// in the file RCLONE_CONFIG names is found and written to, as rclone itself
|
|
||||||
// would.
|
|
||||||
//
|
|
||||||
//nolint:paralleltest // sets RCLONE_CONFIG and the process-global rclone config
|
|
||||||
func TestNewRcloneStorerReadsRcloneConfigEnv(t *testing.T) {
|
|
||||||
setRcloneConfigEnv(t, writeRcloneConfigFile(t, "[loc]\ntype = local\n"))
|
|
||||||
|
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
|
|||||||
Reference in new issue
Block a user