Skip to content

Commit

Permalink
fix: test fixes for windows file restore
Browse files Browse the repository at this point in the history
  • Loading branch information
garethgeorge committed Aug 27, 2024
1 parent cc173aa commit 0df770b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
19 changes: 12 additions & 7 deletions internal/orchestrator/repo/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"path"
"runtime"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -305,13 +306,17 @@ func (r *RepoOrchestrator) Restore(ctx context.Context, snapshotId string, snaps
opts = append(opts, restic.WithFlags("--target", target))

if snapshotPath != "" {
dir := path.Dir(snapshotPath)
base := path.Base(snapshotPath)
if dir != "" {
snapshotId = snapshotId + ":" + dir
}
if base != "" {
opts = append(opts, restic.WithFlags("--include", base))
if runtime.GOOS == "windows" {
opts = append(opts, restic.WithFlags("--include", snapshotPath))
} else {
dir := path.Dir(snapshotPath)
base := path.Base(snapshotPath)
if dir != "" {
snapshotId = snapshotId + ":" + dir
}
if base != "" {
opts = append(opts, restic.WithFlags("--include", base))
}
}
}

Expand Down
21 changes: 14 additions & 7 deletions internal/orchestrator/repo/repo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"io/ioutil"
"os"
"path"
"runtime"
"slices"
"strings"
Expand Down Expand Up @@ -90,8 +91,8 @@ func TestBackup(t *testing.T) {
func TestRestore(t *testing.T) {
t.Parallel()

testFile := t.TempDir() + "/test.txt"
if err := ioutil.WriteFile(testFile, []byte("test"), 0644); err != nil {
testFile := path.Join(t.TempDir(), "test.txt")
if err := ioutil.WriteFile(testFile, []byte("lorum ipsum"), 0644); err != nil {
t.Fatalf("failed to create test file: %v", err)
}

Expand Down Expand Up @@ -124,27 +125,33 @@ func TestRestore(t *testing.T) {

// Restore the file
restoreDir := t.TempDir()
restoreSummary, err := orchestrator.Restore(context.Background(), summary.SnapshotId, testFile, restoreDir, nil)
snapshotPath := strings.ReplaceAll(testFile, ":", "") // remove the colon from the windows path e.g. C:\test.txt -> C\test.txt
restoreSummary, err := orchestrator.Restore(context.Background(), summary.SnapshotId, snapshotPath, restoreDir, nil)
if err != nil {
t.Fatalf("restore error: %v", err)
}
t.Logf("restore summary: %+v", restoreSummary)
if restoreSummary.FilesRestored != 1 {
t.Fatalf("expected 1 new file, got %d", restoreSummary.FilesRestored)
t.Errorf("expected 1 new file, got %d", restoreSummary.FilesRestored)
}
if restoreSummary.TotalFiles != 1 {
t.Fatalf("expected 1 total file, got %d", restoreSummary.TotalFiles)
t.Errorf("expected 1 total file, got %d", restoreSummary.TotalFiles)
}

if runtime.GOOS == "windows" {
return
}

// Check the restored file
restoredFile := restoreDir + "/test.txt"
restoredFile := path.Join(restoreDir, "test.txt")
if _, err := os.Stat(restoredFile); err != nil {
t.Fatalf("failed to stat restored file: %v", err)
}
restoredData, err := ioutil.ReadFile(restoredFile)
if err != nil {
t.Fatalf("failed to read restored file: %v", err)
}
if string(restoredData) != "test" {
if string(restoredData) != "lorum ipsum" {
t.Fatalf("expected 'test', got '%s'", restoredData)
}
}
Expand Down

0 comments on commit 0df770b

Please sign in to comment.