diff --git a/cmd/restic/cmd_list_integration_test.go b/cmd/restic/cmd_list_integration_test.go index d9fb2a562..b8b528945 100644 --- a/cmd/restic/cmd_list_integration_test.go +++ b/cmd/restic/cmd_list_integration_test.go @@ -4,10 +4,13 @@ import ( "bufio" "context" "io" + "path/filepath" + "strings" "testing" "github.com/restic/restic/internal/restic" rtest "github.com/restic/restic/internal/test" + "github.com/restic/restic/internal/ui" ) func testRunList(t testing.TB, gopts GlobalOptions, tpe string) restic.IDs { @@ -24,13 +27,24 @@ func parseIDsFromReader(t testing.TB, rd io.Reader) restic.IDs { sc := bufio.NewScanner(rd) for sc.Scan() { - id, err := restic.ParseID(sc.Text()) - if err != nil { - t.Logf("parse id %v: %v", sc.Text(), err) - continue + if len(sc.Text()) == 64 { + id, err := restic.ParseID(sc.Text()) + if err != nil { + t.Logf("parse id %v: %v", sc.Text(), err) + continue + } + IDs = append(IDs, id) + } else { + // 'list blobs' is different because it lists the blobs together with the blob type + // e.g. "tree ac08ce34ba4f8123618661bef2425f7028ffb9ac740578a3ee88684d2523fee8" + parts := strings.Split(sc.Text(), " ") + id, err := restic.ParseID(parts[len(parts)-1]) + if err != nil { + t.Logf("parse id %v: %v", sc.Text(), err) + continue + } + IDs = append(IDs, id) } - - IDs = append(IDs, id) } return IDs @@ -42,3 +56,48 @@ func testListSnapshots(t testing.TB, gopts GlobalOptions, expected int) restic.I rtest.Assert(t, len(snapshotIDs) == expected, "expected %v snapshot, got %v", expected, snapshotIDs) return snapshotIDs } + +// extract blob set from repository index +func testListBlobs(t testing.TB, gopts GlobalOptions) (blobSetFromIndex restic.IDSet) { + err := withTermStatus(t, gopts, func(ctx context.Context, gopts GlobalOptions) error { + printer := ui.NewProgressPrinter(gopts.JSON, gopts.verbosity, gopts.term) + _, repo, unlock, err := openWithReadLock(ctx, gopts, false, printer) + rtest.OK(t, err) + defer unlock() + + // make sure the index is loaded + rtest.OK(t, repo.LoadIndex(ctx, nil)) + + // get blobs from index + blobSetFromIndex = restic.NewIDSet() + rtest.OK(t, repo.ListBlobs(ctx, func(blob restic.PackedBlob) { + blobSetFromIndex.Insert(blob.ID) + })) + return nil + }) + rtest.OK(t, err) + + return blobSetFromIndex +} + +func TestListBlobs(t *testing.T) { + + env, cleanup := withTestEnvironment(t) + defer cleanup() + + testSetupBackupData(t, env) + opts := BackupOptions{} + + // first backup + testRunBackup(t, "", []string{filepath.Join(env.testdata, "0", "0", "9")}, opts, env.gopts) + testListSnapshots(t, env.gopts, 1) + + // run the `list blobs` command + resticIDs := testRunList(t, env.gopts, "blobs") + + // convert to set + testIDSet := restic.NewIDSet(resticIDs...) + blobSetFromIndex := testListBlobs(t, env.gopts) + + rtest.Assert(t, blobSetFromIndex.Equals(testIDSet), "the set of restic.ID s should be equal") +}