github.com/anjalikarhana/fabric@v2.1.1+incompatible/orderer/common/server/util_test.go (about) 1 /* 2 Copyright IBM Corp. 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/hyperledger/fabric/common/metrics/disabled" 15 "github.com/hyperledger/fabric/core/config/configtest" 16 config "github.com/hyperledger/fabric/orderer/common/localconfig" 17 "github.com/stretchr/testify/assert" 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 ledgerDirPrefix string 27 expectPanic bool 28 }{ 29 {"FilewithPathSet", filepath.Join(os.TempDir(), "test-dir"), "", false}, 30 {"FilewithPathUnset", "", "test-prefix", false}, 31 } 32 33 conf, err := config.Load() 34 if err != nil { 35 t.Fatal("failed to load config") 36 } 37 38 for _, tc := range testCases { 39 t.Run(tc.name, func(t *testing.T) { 40 defer func() { 41 r := recover() 42 if tc.expectPanic && r == nil { 43 t.Fatal("Should have panicked") 44 } 45 if !tc.expectPanic && r != nil { 46 t.Fatal("Should not have panicked") 47 } 48 }() 49 50 conf.FileLedger.Location = tc.ledgerDir 51 conf.FileLedger.Prefix = tc.ledgerDirPrefix 52 lf, ld, err := createLedgerFactory(conf, &disabled.Provider{}) 53 assert.NoError(t, err) 54 defer func() { 55 if ld != "" { 56 os.RemoveAll(ld) 57 t.Log("Removed temp dir:", ld) 58 } 59 }() 60 lf.ChannelIDs() 61 }) 62 } 63 }