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

     1  // Copyright 2023 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  	"io/fs"
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/ethersphere/bee/v2/pkg/sharky"
    15  	"github.com/ethersphere/bee/v2/pkg/storage/inmemstore"
    16  	chunktest "github.com/ethersphere/bee/v2/pkg/storage/testing"
    17  	"github.com/ethersphere/bee/v2/pkg/storer/internal/chunkstore"
    18  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    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  type dirFS struct {
    25  	basedir string
    26  }
    27  
    28  func (d *dirFS) Open(path string) (fs.File, error) {
    29  	return os.OpenFile(filepath.Join(d.basedir, path), os.O_RDWR|os.O_CREATE, 0644)
    30  }
    31  
    32  func Test_Step_04(t *testing.T) {
    33  	t.Parallel()
    34  
    35  	sharkyDir := t.TempDir()
    36  	sharkyStore, err := sharky.New(&dirFS{basedir: sharkyDir}, 1, swarm.SocMaxChunkSize)
    37  	assert.NoError(t, err)
    38  	store := inmemstore.New()
    39  	storage := transaction.NewStorage(sharkyStore, store)
    40  
    41  	stepFn := localmigration.Step_04(sharkyDir, 1, storage)
    42  
    43  	chunks := chunktest.GenerateTestRandomChunks(10)
    44  
    45  	for _, ch := range chunks {
    46  		err = storage.Run(context.Background(), func(s transaction.Store) error {
    47  			return s.ChunkStore().Put(context.Background(), ch)
    48  		})
    49  		assert.NoError(t, err)
    50  	}
    51  
    52  	for _, ch := range chunks[:2] {
    53  		err = storage.Run(context.Background(), func(s transaction.Store) error {
    54  			return s.IndexStore().Delete(&chunkstore.RetrievalIndexItem{Address: ch.Address()})
    55  		})
    56  		assert.NoError(t, err)
    57  	}
    58  
    59  	err = storage.Close()
    60  	assert.NoError(t, err)
    61  
    62  	assert.NoError(t, stepFn())
    63  
    64  	sharkyStore, err = sharky.New(&dirFS{basedir: sharkyDir}, 1, swarm.SocMaxChunkSize)
    65  	assert.NoError(t, err)
    66  
    67  	store2 := transaction.NewStorage(sharkyStore, store)
    68  
    69  	// check that the chunks are still there
    70  	for _, ch := range chunks[2:] {
    71  		_, err := store2.ChunkStore().Get(context.Background(), ch.Address())
    72  		assert.NoError(t, err)
    73  	}
    74  
    75  	err = sharkyStore.Close()
    76  	assert.NoError(t, err)
    77  
    78  	// check that the sharky files are there
    79  	f, err := os.Open(filepath.Join(sharkyDir, "free_000"))
    80  	assert.NoError(t, err)
    81  
    82  	buf := make([]byte, 2)
    83  	_, err = f.Read(buf)
    84  	assert.NoError(t, err)
    85  
    86  	for i := 0; i < 10; i++ {
    87  		if i < 2 {
    88  			// if the chunk is deleted, the bit is set to 1
    89  			assert.Greater(t, buf[i/8]&(1<<(i%8)), byte(0))
    90  		} else {
    91  			// if the chunk is not deleted, the bit is 0
    92  			assert.Equal(t, byte(0), buf[i/8]&(1<<(i%8)))
    93  		}
    94  	}
    95  
    96  	assert.NoError(t, f.Close())
    97  }