diff --git a/assemble_test.go b/assemble_test.go index 3199fc2..75f91ff 100644 --- a/assemble_test.go +++ b/assemble_test.go @@ -5,9 +5,11 @@ import ( "context" "crypto/md5" "crypto/rand" + "github.com/stretchr/testify/require" "io" "io/ioutil" "os" + "path/filepath" "testing" ) @@ -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) +} diff --git a/sequencer.go b/sequencer.go index b56e61f..57d7a07 100644 --- a/sequencer.go +++ b/sequencer.go @@ -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{ @@ -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() { diff --git a/testdata/blob2 b/testdata/blob2 new file mode 120000 index 0000000..777e84a --- /dev/null +++ b/testdata/blob2 @@ -0,0 +1 @@ +../cmd/desync/testdata/blob2 \ No newline at end of file diff --git a/testdata/blob2.caibx b/testdata/blob2.caibx new file mode 120000 index 0000000..292fd5b --- /dev/null +++ b/testdata/blob2.caibx @@ -0,0 +1 @@ +../cmd/desync/testdata/blob2.caibx \ No newline at end of file diff --git a/testdata/blob2.store b/testdata/blob2.store new file mode 120000 index 0000000..7e5d52c --- /dev/null +++ b/testdata/blob2.store @@ -0,0 +1 @@ +../cmd/desync/testdata/blob2.store \ No newline at end of file diff --git a/testdata/blob2_corrupted b/testdata/blob2_corrupted new file mode 120000 index 0000000..f9ecb82 --- /dev/null +++ b/testdata/blob2_corrupted @@ -0,0 +1 @@ +../cmd/desync/testdata/blob2_corrupted \ No newline at end of file diff --git a/testdata/blob2_corrupted.caibx b/testdata/blob2_corrupted.caibx new file mode 120000 index 0000000..6922d5a --- /dev/null +++ b/testdata/blob2_corrupted.caibx @@ -0,0 +1 @@ +../cmd/desync/testdata/blob2_corrupted.caibx \ No newline at end of file