2
2
mirror of https://github.com/octoleo/restic.git synced 2024-05-28 22:50:48 +00:00
restic/internal/restic/testing_test.go

79 lines
1.9 KiB
Go
Raw Normal View History

2016-04-10 14:52:15 +00:00
package restic_test
import (
2017-06-05 21:56:59 +00:00
"context"
2016-04-10 14:52:15 +00:00
"testing"
"time"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/repository"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2016-04-10 14:52:15 +00:00
)
var testSnapshotTime = time.Unix(1460289341, 207401672)
2016-07-31 08:58:09 +00:00
const (
testCreateSnapshots = 3
testDepth = 2
)
2016-04-10 15:25:32 +00:00
2020-12-06 04:22:27 +00:00
// LoadAllSnapshots returns a list of all snapshots in the repo.
// If a snapshot ID is in excludeIDs, it will not be included in the result.
func loadAllSnapshots(ctx context.Context, repo restic.Repository, excludeIDs restic.IDSet) (snapshots restic.Snapshots, err error) {
err = restic.ForAllSnapshots(ctx, repo.Backend(), repo, excludeIDs, func(id restic.ID, sn *restic.Snapshot, err error) error {
2020-12-06 04:22:27 +00:00
if err != nil {
return err
}
snapshots = append(snapshots, sn)
return nil
})
if err != nil {
return nil, err
}
return snapshots, nil
}
2016-04-10 14:52:15 +00:00
func TestCreateSnapshot(t *testing.T) {
repo := repository.TestRepository(t)
2016-04-10 15:25:32 +00:00
for i := 0; i < testCreateSnapshots; i++ {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
2016-04-10 15:25:32 +00:00
}
2016-04-10 14:52:15 +00:00
2020-12-06 04:22:27 +00:00
snapshots, err := loadAllSnapshots(context.TODO(), repo, restic.NewIDSet())
2016-04-10 14:52:15 +00:00
if err != nil {
t.Fatal(err)
}
2016-04-10 15:25:32 +00:00
if len(snapshots) != testCreateSnapshots {
2016-04-10 14:52:15 +00:00
t.Fatalf("got %d snapshots, expected %d", len(snapshots), 1)
}
sn := snapshots[0]
2016-04-10 15:25:32 +00:00
if sn.Time.Before(testSnapshotTime) || sn.Time.After(testSnapshotTime.Add(testCreateSnapshots*time.Second)) {
2016-05-08 11:09:36 +00:00
t.Fatalf("timestamp %v is outside of the allowed time range", sn.Time)
2016-04-10 14:52:15 +00:00
}
if sn.Tree == nil {
t.Fatalf("tree id is nil")
}
if sn.Tree.IsNull() {
t.Fatalf("snapshot has zero tree ID")
}
checker.TestCheckRepo(t, repo)
2016-04-10 14:52:15 +00:00
}
func BenchmarkTestCreateSnapshot(t *testing.B) {
repo := repository.TestRepository(t)
t.ResetTimer()
for i := 0; i < t.N; i++ {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
}
}