restic/internal/restic/testing_test.go

79 lines
1.9 KiB
Go
Raw Normal View History

2016-04-10 16:52:15 +02:00
package restic_test
import (
2017-06-05 23:56:59 +02:00
"context"
2016-04-10 16:52:15 +02:00
"testing"
"time"
2017-07-23 14:21:03 +02:00
"github.com/restic/restic/internal/checker"
"github.com/restic/restic/internal/repository"
2017-07-24 17:42:25 +02:00
"github.com/restic/restic/internal/restic"
2016-04-10 16:52:15 +02:00
)
var testSnapshotTime = time.Unix(1460289341, 207401672)
2016-07-31 10:58:09 +02:00
const (
testCreateSnapshots = 3
testDepth = 2
)
2016-04-10 17:25:32 +02:00
2020-12-06 05:22:27 +01: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, repo, excludeIDs, func(id restic.ID, sn *restic.Snapshot, err error) error {
2020-12-06 05:22:27 +01:00
if err != nil {
return err
}
snapshots = append(snapshots, sn)
return nil
})
if err != nil {
return nil, err
}
return snapshots, nil
}
2016-04-10 16:52:15 +02:00
func TestCreateSnapshot(t *testing.T) {
repo := repository.TestRepository(t)
2016-04-10 17:25:32 +02:00
for i := 0; i < testCreateSnapshots; i++ {
restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth)
2016-04-10 17:25:32 +02:00
}
2016-04-10 16:52:15 +02:00
2020-12-06 05:22:27 +01:00
snapshots, err := loadAllSnapshots(context.TODO(), repo, restic.NewIDSet())
2016-04-10 16:52:15 +02:00
if err != nil {
t.Fatal(err)
}
2016-04-10 17:25:32 +02:00
if len(snapshots) != testCreateSnapshots {
2016-04-10 16:52:15 +02:00
t.Fatalf("got %d snapshots, expected %d", len(snapshots), 1)
}
sn := snapshots[0]
2016-04-10 17:25:32 +02:00
if sn.Time.Before(testSnapshotTime) || sn.Time.After(testSnapshotTime.Add(testCreateSnapshots*time.Second)) {
2016-05-08 13:09:36 +02:00
t.Fatalf("timestamp %v is outside of the allowed time range", sn.Time)
2016-04-10 16:52:15 +02: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 16:52:15 +02: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)
}
}