github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/migration/step_05_test.go (about)

     1  // Copyright 2024 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package migration_test
     6  
     7  import (
     8  	"context"
     9  	"testing"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/sharky"
    12  	"github.com/ethersphere/bee/v2/pkg/storage"
    13  	"github.com/ethersphere/bee/v2/pkg/storage/leveldbstore"
    14  	chunktest "github.com/ethersphere/bee/v2/pkg/storage/testing"
    15  
    16  	"github.com/ethersphere/bee/v2/pkg/storer/internal"
    17  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    18  	"github.com/ethersphere/bee/v2/pkg/storer/internal/upload"
    19  	localmigration "github.com/ethersphere/bee/v2/pkg/storer/migration"
    20  	"github.com/ethersphere/bee/v2/pkg/swarm"
    21  	"github.com/stretchr/testify/assert"
    22  )
    23  
    24  func Test_Step_05(t *testing.T) {
    25  	t.Parallel()
    26  
    27  	sharkyDir := t.TempDir()
    28  	sharkyStore, err := sharky.New(&dirFS{basedir: sharkyDir}, 1, swarm.SocMaxChunkSize)
    29  	assert.NoError(t, err)
    30  
    31  	lstore, err := leveldbstore.New("", nil)
    32  	assert.NoError(t, err)
    33  
    34  	store := transaction.NewStorage(sharkyStore, lstore)
    35  	t.Cleanup(func() {
    36  		err := store.Close()
    37  		if err != nil {
    38  			t.Fatalf("Close(): unexpected closing storer: %v", err)
    39  		}
    40  	})
    41  
    42  	ctx := context.Background()
    43  
    44  	wantCount := func(t *testing.T, st storage.Reader, want int) {
    45  		t.Helper()
    46  		count := 0
    47  		err := upload.IterateAll(st, func(_ storage.Item) (bool, error) {
    48  			count++
    49  			return false, nil
    50  		})
    51  		if err != nil {
    52  			t.Fatalf("iterate upload items: %v", err)
    53  		}
    54  		if count != want {
    55  			t.Fatalf("expected %d upload items, got %d", want, count)
    56  		}
    57  	}
    58  
    59  	var tag upload.TagItem
    60  	err = store.Run(context.Background(), func(s transaction.Store) error {
    61  		tag, err = upload.NextTag(s.IndexStore())
    62  		return err
    63  	})
    64  	if err != nil {
    65  		t.Fatalf("create tag: %v", err)
    66  	}
    67  
    68  	var putter internal.PutterCloserWithReference
    69  	err = store.Run(context.Background(), func(s transaction.Store) error {
    70  		putter, err = upload.NewPutter(s.IndexStore(), tag.TagID)
    71  		return err
    72  	})
    73  	if err != nil {
    74  		t.Fatalf("create putter: %v", err)
    75  	}
    76  
    77  	chunks := chunktest.GenerateTestRandomChunks(10)
    78  
    79  	err = store.Run(context.Background(), func(s transaction.Store) error {
    80  		for _, ch := range chunks {
    81  			err := putter.Put(ctx, s, ch)
    82  			if err != nil {
    83  				return err
    84  			}
    85  		}
    86  		return nil
    87  	})
    88  	if err != nil {
    89  		t.Fatalf("put chunk: %v", err)
    90  	}
    91  
    92  	err = store.Run(ctx, func(s transaction.Store) error {
    93  		return putter.Close(s.IndexStore(), swarm.RandAddress(t))
    94  	})
    95  	if err != nil {
    96  		t.Fatalf("close putter: %v", err)
    97  	}
    98  
    99  	wantCount(t, store.IndexStore(), 10)
   100  
   101  	err = localmigration.Step_05(store)()
   102  	if err != nil {
   103  		t.Fatalf("step 05: %v", err)
   104  	}
   105  	wantCount(t, store.IndexStore(), 0)
   106  }