github.com/leonlxy/hyperledger@v1.0.0-alpha.0.20170427033203-34922035d248/core/testutil/config.go (about)

     1  /*
     2  Copyright IBM Corp. 2016 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8  		 http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package testutil
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"io/ioutil"
    23  	"runtime"
    24  	"strings"
    25  
    26  	"github.com/op/go-logging"
    27  	"github.com/spf13/viper"
    28  
    29  	"github.com/hyperledger/fabric/bccsp/factory"
    30  	"github.com/hyperledger/fabric/core/config"
    31  	"github.com/hyperledger/fabric/msp"
    32  
    33  	"github.com/hyperledger/fabric/common/flogging"
    34  )
    35  
    36  // Config the config wrapper structure
    37  type Config struct {
    38  }
    39  
    40  var configLogger = flogging.MustGetLogger("config")
    41  
    42  func init() {
    43  
    44  }
    45  
    46  // SetupTestLogging setup the logging during test execution
    47  func SetupTestLogging() {
    48  	level, err := logging.LogLevel(viper.GetString("logging.peer"))
    49  	if err == nil {
    50  		// No error, use the setting
    51  		logging.SetLevel(level, "main")
    52  		logging.SetLevel(level, "server")
    53  		logging.SetLevel(level, "peer")
    54  	} else {
    55  		configLogger.Warningf("Log level not recognized '%s', defaulting to %s: %s", viper.GetString("logging.peer"), logging.ERROR, err)
    56  		logging.SetLevel(logging.ERROR, "main")
    57  		logging.SetLevel(logging.ERROR, "server")
    58  		logging.SetLevel(logging.ERROR, "peer")
    59  	}
    60  }
    61  
    62  // SetupTestConfig setup the config during test execution
    63  func SetupTestConfig() {
    64  	flag.Parse()
    65  
    66  	// Now set the configuration file
    67  	viper.SetEnvPrefix("HYPERLEDGER")
    68  	viper.AutomaticEnv()
    69  	replacer := strings.NewReplacer(".", "_")
    70  	viper.SetEnvKeyReplacer(replacer)
    71  	viper.SetConfigName("core") // name of config file (without extension)
    72  	err := config.AddDevConfigPath(nil)
    73  	if err != nil {
    74  		panic(fmt.Errorf("Fatal error adding DevConfigPath: %s \n", err))
    75  	}
    76  
    77  	err = viper.ReadInConfig() // Find and read the config file
    78  	if err != nil {            // Handle errors reading the config file
    79  		panic(fmt.Errorf("Fatal error config file: %s \n", err))
    80  	}
    81  
    82  	SetupTestLogging()
    83  
    84  	// Set the number of maxprocs
    85  	var numProcsDesired = viper.GetInt("peer.gomaxprocs")
    86  	configLogger.Debugf("setting Number of procs to %d, was %d\n", numProcsDesired, runtime.GOMAXPROCS(2))
    87  
    88  	// Init the BCCSP
    89  	var bccspConfig *factory.FactoryOpts
    90  	err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
    91  	if err != nil {
    92  		bccspConfig = nil
    93  	}
    94  
    95  	tmpKeyStore, err := ioutil.TempDir("/tmp", "msp-keystore")
    96  	if err != nil {
    97  		panic(fmt.Errorf("Could not create temporary directory: %s\n", tmpKeyStore))
    98  	}
    99  
   100  	msp.SetupBCCSPKeystoreConfig(bccspConfig, tmpKeyStore)
   101  
   102  	err = factory.InitFactories(bccspConfig)
   103  	if err != nil {
   104  		panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
   105  	}
   106  }