github.com/true-sqn/fabric@v2.1.1+incompatible/core/ledger/kvledger/upgrade_dbs_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package kvledger
     8  
     9  import (
    10  	"fmt"
    11  	"os"
    12  	"path/filepath"
    13  	"testing"
    14  
    15  	"github.com/hyperledger/fabric/common/ledger/dataformat"
    16  	"github.com/hyperledger/fabric/common/ledger/testutil"
    17  	"github.com/hyperledger/fabric/common/ledger/util/leveldbhelper"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  func TestUpgradeDBs(t *testing.T) {
    22  	conf, cleanup := testConfig(t)
    23  	defer cleanup()
    24  	provider := testutilNewProvider(conf, t)
    25  
    26  	// upgrade should fail when provider is still open
    27  	err := UpgradeDBs(conf.RootFSPath)
    28  	require.Error(t, err, "as another peer node command is executing, wait for that command to complete its execution or terminate it before retrying")
    29  	provider.Close()
    30  
    31  	// load v11 ledger data for upgrade
    32  	rootFSPath := conf.RootFSPath
    33  	require.NoError(t, testutil.Unzip("tests/testdata/v11/sample_ledgers/ledgersData.zip", rootFSPath, false))
    34  	v11LedgerIDs := getLedgerIDs(t, rootFSPath)
    35  	require.NoError(t, UpgradeIDStoreFormat(rootFSPath))
    36  
    37  	err = UpgradeDBs(rootFSPath)
    38  	require.NoError(t, err)
    39  
    40  	// verify idStore has formatKey and metadata entries
    41  	idStore, err := openIDStore(LedgerProviderPath(conf.RootFSPath))
    42  	require.NoError(t, err)
    43  	formatVersion, err := idStore.db.Get(formatKey)
    44  	require.NoError(t, err)
    45  	require.Equal(t, []byte(dataformat.Version20), formatVersion)
    46  	metadataLedgerIDs, err := idStore.getActiveLedgerIDs()
    47  	require.NoError(t, err)
    48  	require.ElementsMatch(t, v11LedgerIDs, metadataLedgerIDs)
    49  	idStore.close()
    50  
    51  	// verify blockstoreIndex, configHistory, history, state, bookkeeper dbs are deleted
    52  	_, err = os.Stat(filepath.Join(BlockStorePath(rootFSPath), "index"))
    53  	require.Equal(t, os.IsNotExist(err), true)
    54  	_, err = os.Stat(ConfigHistoryDBPath(rootFSPath))
    55  	require.Equal(t, os.IsNotExist(err), true)
    56  	_, err = os.Stat(HistoryDBPath(rootFSPath))
    57  	require.Equal(t, os.IsNotExist(err), true)
    58  	_, err = os.Stat(StateDBPath(rootFSPath))
    59  	require.Equal(t, os.IsNotExist(err), true)
    60  	_, err = os.Stat(BookkeeperDBPath(rootFSPath))
    61  	require.Equal(t, os.IsNotExist(err), true)
    62  
    63  	// upgrade again should be successful
    64  	err = UpgradeDBs(rootFSPath)
    65  	require.NoError(t, err)
    66  }
    67  
    68  func TestUpgradeIDStoreWrongFormat(t *testing.T) {
    69  	conf, cleanup := testConfig(t)
    70  	conf.HistoryDBConfig.Enabled = false
    71  	defer cleanup()
    72  	provider := testutilNewProvider(conf, t)
    73  
    74  	// change format to a wrong value
    75  	err := provider.idStore.db.Put(formatKey, []byte("x.0"), true)
    76  	provider.Close()
    77  	require.NoError(t, err)
    78  
    79  	err = UpgradeIDStoreFormat(conf.RootFSPath)
    80  	expectedErr := &dataformat.ErrVersionMismatch{
    81  		ExpectedVersion: "",
    82  		Version:         "x.0",
    83  		DBInfo:          fmt.Sprintf("leveldb for channel-IDs at [%s]", LedgerProviderPath(conf.RootFSPath)),
    84  	}
    85  	require.EqualError(t, err, expectedErr.Error())
    86  }
    87  
    88  // getLedgerIDs returns ledger ids using ledgerKeyPrefix (available in both old format and new format)
    89  func getLedgerIDs(t *testing.T, rootFSPath string) []string {
    90  	dbPath := LedgerProviderPath(rootFSPath)
    91  	db := leveldbhelper.CreateDB(&leveldbhelper.Conf{DBPath: dbPath})
    92  	db.Open()
    93  	idStore := &idStore{db, dbPath}
    94  	defer db.Close()
    95  	itr := db.GetIterator(ledgerKeyPrefix, ledgerKeyStop)
    96  	defer itr.Release()
    97  	var ledgerIDs []string
    98  	for itr.Next() {
    99  		require.NoError(t, itr.Error())
   100  		ledgerIDs = append(ledgerIDs, idStore.decodeLedgerID(itr.Key(), ledgerKeyPrefix))
   101  	}
   102  	return ledgerIDs
   103  }