github.com/suchongming/fabric@v2.1.1+incompatible/core/testutil/config.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package testutil
     8  
     9  import (
    10  	"flag"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"strings"
    14  
    15  	"github.com/hyperledger/fabric/bccsp/factory"
    16  	"github.com/hyperledger/fabric/common/flogging"
    17  	"github.com/hyperledger/fabric/core/config/configtest"
    18  	"github.com/hyperledger/fabric/msp"
    19  	"github.com/spf13/viper"
    20  )
    21  
    22  var configLogger = flogging.MustGetLogger("config")
    23  
    24  // SetupTestConfig setup the config during test execution
    25  func SetupTestConfig() {
    26  	flag.Parse()
    27  
    28  	// Now set the configuration file
    29  	viper.SetEnvPrefix("CORE")
    30  	viper.AutomaticEnv()
    31  	replacer := strings.NewReplacer(".", "_")
    32  	viper.SetEnvKeyReplacer(replacer)
    33  	viper.SetConfigName("core") // name of config file (without extension)
    34  	configtest.AddDevConfigPath(nil)
    35  
    36  	err := viper.ReadInConfig() // Find and read the config file
    37  	if err != nil {             // Handle errors reading the config file
    38  		panic(fmt.Errorf("Fatal error config file: %s \n", err))
    39  	}
    40  
    41  	// Init the BCCSP
    42  	var bccspConfig *factory.FactoryOpts
    43  	err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
    44  	if err != nil {
    45  		bccspConfig = nil
    46  	}
    47  
    48  	tmpKeyStore, err := ioutil.TempDir("/tmp", "msp-keystore")
    49  	if err != nil {
    50  		panic(fmt.Errorf("Could not create temporary directory: %s\n", tmpKeyStore))
    51  	}
    52  
    53  	msp.SetupBCCSPKeystoreConfig(bccspConfig, tmpKeyStore)
    54  
    55  	err = factory.InitFactories(bccspConfig)
    56  	if err != nil {
    57  		panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
    58  	}
    59  }