github.com/mckael/restic@v0.8.3/internal/restic/testing_test.go (about)

     1  package restic_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/restic/restic/internal/checker"
     9  	"github.com/restic/restic/internal/repository"
    10  	"github.com/restic/restic/internal/restic"
    11  )
    12  
    13  var testSnapshotTime = time.Unix(1460289341, 207401672)
    14  
    15  const (
    16  	testCreateSnapshots = 3
    17  	testDepth           = 2
    18  )
    19  
    20  func TestCreateSnapshot(t *testing.T) {
    21  	repo, cleanup := repository.TestRepository(t)
    22  	defer cleanup()
    23  
    24  	for i := 0; i < testCreateSnapshots; i++ {
    25  		restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
    26  	}
    27  
    28  	snapshots, err := restic.LoadAllSnapshots(context.TODO(), repo)
    29  	if err != nil {
    30  		t.Fatal(err)
    31  	}
    32  
    33  	if len(snapshots) != testCreateSnapshots {
    34  		t.Fatalf("got %d snapshots, expected %d", len(snapshots), 1)
    35  	}
    36  
    37  	sn := snapshots[0]
    38  	if sn.Time.Before(testSnapshotTime) || sn.Time.After(testSnapshotTime.Add(testCreateSnapshots*time.Second)) {
    39  		t.Fatalf("timestamp %v is outside of the allowed time range", sn.Time)
    40  	}
    41  
    42  	if sn.Tree == nil {
    43  		t.Fatalf("tree id is nil")
    44  	}
    45  
    46  	if sn.Tree.IsNull() {
    47  		t.Fatalf("snapshot has zero tree ID")
    48  	}
    49  
    50  	checker.TestCheckRepo(t, repo)
    51  }
    52  
    53  func BenchmarkTestCreateSnapshot(t *testing.B) {
    54  	repo, cleanup := repository.TestRepository(t)
    55  	defer cleanup()
    56  
    57  	t.ResetTimer()
    58  
    59  	for i := 0; i < t.N; i++ {
    60  		restic.TestCreateSnapshot(t, repo, testSnapshotTime.Add(time.Duration(i)*time.Second), testDepth, 0)
    61  	}
    62  }