Skip to content

Commit

Permalink
Test a seed that changes while an extraction is in progress (#221)
Browse files Browse the repository at this point in the history
Signed-off-by: Ludovico de Nittis <ludovico.denittis@collabora.com>
  • Loading branch information
RyuzakiKK authored Feb 8, 2024
1 parent 29df629 commit 5b0c1a2
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 0 deletions.
47 changes: 47 additions & 0 deletions assemble_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"context"
"crypto/md5"
"crypto/rand"
"github.com/stretchr/testify/require"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"
)

Expand Down Expand Up @@ -295,3 +297,48 @@ func join(slices ...[]byte) []byte {
}
return out
}

func readCaibxFile(t *testing.T, indexLocation string) (idx Index) {
is, err := NewLocalIndexStore(filepath.Dir(indexLocation))
require.NoError(t, err)
defer is.Close()
indexName := filepath.Base(indexLocation)
idx, err = is.GetIndex(indexName)
require.NoError(t, err)
return idx
}

func TestExtractWithNonStaticSeeds(t *testing.T) {
n := 10
outDir := t.TempDir()
out := filepath.Join(outDir, "out")

// Test a seed that is initially valid, but becomes corrupted halfway through
// the extraction operation
MockValidate = true

store, err := NewLocalStore("testdata/blob2.store", StoreOptions{})
require.NoError(t, err)
defer store.Close()

index := readCaibxFile(t, "testdata/blob2.caibx")

var seeds []Seed
srcIndex := readCaibxFile(t, "testdata/blob2_corrupted.caibx")
seed, err := NewIndexSeed(out, "testdata/blob2_corrupted", srcIndex)
seeds = append(seeds, seed)

// Test that the MockValidate works as expected
seq := NewSeedSequencer(index, seeds...)
plan := seq.Plan()
err = plan.Validate(context.Background(), n, NullProgressBar{})
require.NoError(t, err)

options := AssembleOptions{n, InvalidSeedActionRegenerate}
_, err = AssembleFile(context.Background(), out, index, store, seeds, options)
require.NoError(t, err)

//Test the output
err = VerifyIndex(context.Background(), out, index, n, NullProgressBar{})
require.NoError(t, err)
}
6 changes: 6 additions & 0 deletions sequencer.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ type SeedSegmentCandidate struct {

type Plan []SeedSegmentCandidate

var MockValidate = false

// NewSeedSequencer initializes a new sequencer from a number of seeds.
func NewSeedSequencer(idx Index, src ...Seed) *SeedSequencer {
return &SeedSequencer{
Expand Down Expand Up @@ -108,6 +110,10 @@ func (p Plan) Validate(ctx context.Context, n int, pb ProgressBar) (err error) {
in = make(chan Job)
fileMap = make(map[string]*os.File)
)
if MockValidate {
// This is used in the automated tests to mock a plan that is valid
return nil
}
length := 0
for _, s := range p {
if !s.isFileSeed() {
Expand Down
1 change: 1 addition & 0 deletions testdata/blob2
1 change: 1 addition & 0 deletions testdata/blob2.caibx
1 change: 1 addition & 0 deletions testdata/blob2.store
1 change: 1 addition & 0 deletions testdata/blob2_corrupted
1 change: 1 addition & 0 deletions testdata/blob2_corrupted.caibx

0 comments on commit 5b0c1a2

Please sign in to comment.