github.com/ethersphere/bee/v2@v2.2.0/pkg/storer/migration/step_02_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  	"crypto/rand"
    10  	"testing"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  
    14  	storage "github.com/ethersphere/bee/v2/pkg/storage"
    15  	"github.com/ethersphere/bee/v2/pkg/storer/internal"
    16  	"github.com/ethersphere/bee/v2/pkg/storer/internal/cache"
    17  	"github.com/ethersphere/bee/v2/pkg/storer/internal/transaction"
    18  	localmigration "github.com/ethersphere/bee/v2/pkg/storer/migration"
    19  	"github.com/ethersphere/bee/v2/pkg/swarm"
    20  )
    21  
    22  type testEntry struct {
    23  	address swarm.Address
    24  }
    25  
    26  func (e *testEntry) Namespace() string { return "cacheEntry" }
    27  
    28  func (e *testEntry) ID() string { return e.address.ByteString() }
    29  
    30  func (e *testEntry) Marshal() ([]byte, error) {
    31  	buf := make([]byte, 32*3)
    32  	_, _ = rand.Read(buf)
    33  	return buf, nil
    34  }
    35  
    36  func (e *testEntry) Unmarshal(buf []byte) error {
    37  	return nil
    38  }
    39  
    40  func (e *testEntry) Clone() storage.Item {
    41  	return &testEntry{
    42  		address: e.address,
    43  	}
    44  }
    45  
    46  func (e testEntry) String() string {
    47  	return "testEntry"
    48  }
    49  
    50  func Test_Step_02(t *testing.T) {
    51  	t.Parallel()
    52  
    53  	stepFn := localmigration.Step_02
    54  	store := internal.NewInmemStorage()
    55  
    56  	// simulate old cacheEntryItem with some random bytes.
    57  	var addrs []*testEntry
    58  	for i := 0; i < 10; i++ {
    59  		entry := &testEntry{address: swarm.RandAddress(t)}
    60  		addrs = append(addrs, entry)
    61  		err := store.Run(context.Background(), func(s transaction.Store) error {
    62  			return s.IndexStore().Put(entry)
    63  		})
    64  		assert.NoError(t, err)
    65  	}
    66  
    67  	assert.NoError(t, stepFn(store)())
    68  
    69  	// check if all entries are migrated.
    70  	for _, entry := range addrs {
    71  		cEntry := &cache.CacheEntryItem{Address: entry.address}
    72  		err := store.IndexStore().Get(cEntry)
    73  		assert.NoError(t, err)
    74  		assert.Equal(t, entry.address, cEntry.Address)
    75  		assert.Greater(t, cEntry.AccessTimestamp, int64(0))
    76  	}
    77  }