github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/orderer/common/server/util_test.go (about)

     1  /*
     2  Copyright hechain. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package server
     8  
     9  import (
    10  	"os"
    11  	"path/filepath"
    12  	"testing"
    13  
    14  	"github.com/hechain20/hechain/common/metrics/disabled"
    15  	"github.com/hechain20/hechain/core/config/configtest"
    16  	config "github.com/hechain20/hechain/orderer/common/localconfig"
    17  	"github.com/stretchr/testify/require"
    18  )
    19  
    20  func TestCreateLedgerFactory(t *testing.T) {
    21  	cleanup := configtest.SetDevFabricConfigPath(t)
    22  	defer cleanup()
    23  	testCases := []struct {
    24  		name        string
    25  		ledgerDir   string
    26  		expectPanic bool
    27  	}{
    28  		{
    29  			name:        "PathSet",
    30  			ledgerDir:   filepath.Join(os.TempDir(), "test-dir"),
    31  			expectPanic: false,
    32  		},
    33  		{
    34  			name:        "PathUnset",
    35  			ledgerDir:   "",
    36  			expectPanic: true,
    37  		},
    38  	}
    39  
    40  	conf, err := config.Load()
    41  	if err != nil {
    42  		t.Fatal("failed to load config")
    43  	}
    44  
    45  	for _, tc := range testCases {
    46  		t.Run(tc.name, func(t *testing.T) {
    47  			conf.FileLedger.Location = tc.ledgerDir
    48  			if tc.expectPanic {
    49  				require.PanicsWithValue(t, "Orderer.FileLedger.Location must be set", func() { createLedgerFactory(conf, &disabled.Provider{}) })
    50  			} else {
    51  				lf, err := createLedgerFactory(conf, &disabled.Provider{})
    52  				require.NoError(t, err)
    53  				defer os.RemoveAll(tc.ledgerDir)
    54  				require.Equal(t, []string{}, lf.ChannelIDs())
    55  			}
    56  		})
    57  	}
    58  }