From 44585ede613b87189c38f5cd456a109e653cdf64 Mon Sep 17 00:00:00 2001 From: garethgeorge Date: Mon, 26 Aug 2024 19:59:27 -0700 Subject: [PATCH] fix: test fixes for windows file restore --- internal/orchestrator/repo/repo.go | 19 ++++++++++++------- internal/orchestrator/repo/repo_test.go | 22 +++++++++++++++------- 2 files changed, 27 insertions(+), 14 deletions(-) diff --git a/internal/orchestrator/repo/repo.go b/internal/orchestrator/repo/repo.go index 9e23d3c6..63297343 100644 --- a/internal/orchestrator/repo/repo.go +++ b/internal/orchestrator/repo/repo.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "path" + "runtime" "slices" "sort" "strings" @@ -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)) + } } } diff --git a/internal/orchestrator/repo/repo_test.go b/internal/orchestrator/repo/repo_test.go index 8a2fa7ef..65cd519b 100644 --- a/internal/orchestrator/repo/repo_test.go +++ b/internal/orchestrator/repo/repo_test.go @@ -5,6 +5,7 @@ import ( "context" "io/ioutil" "os" + "path" "runtime" "slices" "strings" @@ -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) } @@ -124,19 +125,26 @@ 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 runtime.GOOS == "windows" { + return + } + 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) } // 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) } @@ -144,7 +152,7 @@ func TestRestore(t *testing.T) { 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) } }