From 04614c752735cc2964d34583dea9e1ea072ea9a6 Mon Sep 17 00:00:00 2001 From: Alexander Neumann Date: Sun, 25 Oct 2015 15:35:33 +0100 Subject: [PATCH] Add test for packs in duplicate indexes --- repository/repository_test.go | 68 +++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/repository/repository_test.go b/repository/repository_test.go index 93daece63..3028bd838 100644 --- a/repository/repository_test.go +++ b/repository/repository_test.go @@ -6,6 +6,7 @@ import ( "crypto/sha256" "encoding/json" "io" + mrand "math/rand" "path/filepath" "testing" @@ -220,3 +221,70 @@ func BenchmarkLoadIndex(b *testing.B) { } }) } + +// saveRandomDataBlobs generates random data blobs and saves them to the repository. +func saveRandomDataBlobs(t testing.TB, repo *repository.Repository, num int, sizeMax int) { + for i := 0; i < num; i++ { + size := mrand.Int() % sizeMax + + buf := make([]byte, size) + _, err := io.ReadFull(rand.Reader, buf) + OK(t, err) + + _, err = repo.SaveAndEncrypt(pack.Data, buf, nil) + OK(t, err) + } +} + +func TestRepositoryIncrementalIndex(t *testing.T) { + repo := SetupRepo() + defer TeardownRepo(repo) + + repository.IndexFull = func(*repository.Index) bool { return true } + + // add 15 packs + for j := 0; j < 5; j++ { + // add 3 packs, write intermediate index + for i := 0; i < 3; i++ { + saveRandomDataBlobs(t, repo, 5, 1<<15) + OK(t, repo.Flush()) + } + + OK(t, repo.SaveFullIndex()) + } + + // add another 5 packs + for i := 0; i < 5; i++ { + saveRandomDataBlobs(t, repo, 5, 1<<15) + OK(t, repo.Flush()) + } + + // save final index + OK(t, repo.SaveIndex()) + + type packEntry struct { + id backend.ID + indexes []*repository.Index + } + + packEntries := make(map[backend.ID]map[backend.ID]struct{}) + + for id := range repo.List(backend.Index, nil) { + idx, err := repository.LoadIndex(repo, id.String()) + OK(t, err) + + for pb := range idx.Each(nil) { + if _, ok := packEntries[pb.PackID]; !ok { + packEntries[pb.PackID] = make(map[backend.ID]struct{}) + } + + packEntries[pb.PackID][id] = struct{}{} + } + } + + for packID, ids := range packEntries { + if len(ids) > 1 { + t.Errorf("pack %v listed in %d indexes\n", packID, len(ids)) + } + } +}