github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/testutil/config.go (about)

     1  /*
     2  Copyright hechain. 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/hechain20/hechain/bccsp/factory"
    16  	"github.com/hechain20/hechain/core/config/configtest"
    17  	"github.com/hechain20/hechain/msp"
    18  	"github.com/spf13/viper"
    19  )
    20  
    21  // SetupTestConfig setup the config during test execution
    22  func SetupTestConfig() {
    23  	flag.Parse()
    24  
    25  	// Now set the configuration file
    26  	viper.SetEnvPrefix("CORE")
    27  	viper.AutomaticEnv()
    28  	replacer := strings.NewReplacer(".", "_")
    29  	viper.SetEnvKeyReplacer(replacer)
    30  	viper.SetConfigName("core") // name of config file (without extension)
    31  	configtest.AddDevConfigPath(nil)
    32  
    33  	err := viper.ReadInConfig() // Find and read the config file
    34  	if err != nil {             // Handle errors reading the config file
    35  		panic(fmt.Errorf("Fatal error config file: %s \n", err))
    36  	}
    37  
    38  	// Init the BCCSP
    39  	var bccspConfig *factory.FactoryOpts
    40  	err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
    41  	if err != nil {
    42  		bccspConfig = nil
    43  	}
    44  
    45  	tmpKeyStore, err := ioutil.TempDir("/tmp", "msp-keystore")
    46  	if err != nil {
    47  		panic(fmt.Errorf("Could not create temporary directory: %s\n", tmpKeyStore))
    48  	}
    49  
    50  	msp.SetupBCCSPKeystoreConfig(bccspConfig, tmpKeyStore)
    51  
    52  	err = factory.InitFactories(bccspConfig)
    53  	if err != nil {
    54  		panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
    55  	}
    56  }