diff --git a/internal/storage/rclone.go b/internal/storage/rclone.go index 39413cb..e8f154b 100644 --- a/internal/storage/rclone.go +++ b/internal/storage/rclone.go @@ -67,10 +67,12 @@ func NewRcloneStorer(ctx context.Context, remote, path string) (*RcloneStorer, e // Check for remote not found error if strings.Contains(err.Error(), "didn't find section in config file") || strings.Contains(err.Error(), "failed to find remote") { - return nil, fmt.Errorf("%w: %s", ErrRemoteNotFound, remote) + return nil, fmt.Errorf("%w: %q (%s)", + ErrRemoteNotFound, remote, describeRcloneConfigFile()) } - return nil, fmt.Errorf("creating rclone filesystem: %w", err) + return nil, fmt.Errorf("opening rclone remote %q (%s): %w", + remote, describeRcloneConfigFile(), err) } return &RcloneStorer{ @@ -324,6 +326,22 @@ func (r *RcloneStorer) upload(ctx context.Context, key string, data io.Reader) e 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. type progressReader struct { reader io.Reader diff --git a/internal/storage/rclone_test.go b/internal/storage/rclone_test.go index 32ac7a2..15ac7c8 100644 --- a/internal/storage/rclone_test.go +++ b/internal/storage/rclone_test.go @@ -398,24 +398,100 @@ func TestNewRcloneStorerUnknownRemote(t *testing.T) { } } -// TestNewRcloneStorerReadsRcloneConfigEnv checks that a remote defined only -// in the file RCLONE_CONFIG names is found and written to, as rclone itself -// would. -func TestNewRcloneStorerReadsRcloneConfigEnv(t *testing.T) { +// setRcloneConfigEnv points RCLONE_CONFIG at path for the rest of the test. +func setRcloneConfigEnv(t *testing.T, path string) { + t.Helper() + // rclone keeps the config file path in a process-global variable, which - // NewRcloneStorer leaves pointing at this test's file. + // NewRcloneStorer leaves pointing at path. previous := config.GetConfigPath() 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") - err := os.WriteFile(confFile, []byte("[loc]\ntype = local\n"), 0o600) + err := os.WriteFile(confFile, []byte(contents), 0o600) if err != nil { t.Fatalf("writing rclone config: %v", err) } - t.Setenv("RCLONE_CONFIG", confFile) + return 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() ctx := context.Background()