github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/events/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 events
    18  
    19  import (
    20  	"flag"
    21  	"fmt"
    22  	"runtime"
    23  	"strings"
    24  
    25  	"github.com/op/go-logging"
    26  	"github.com/spf13/viper"
    27  
    28  	"github.com/hyperledger/fabric/bccsp/factory"
    29  	"github.com/hyperledger/fabric/msp"
    30  )
    31  
    32  // Config the config wrapper structure
    33  type Config struct {
    34  }
    35  
    36  // SetupTestLogging setup the logging during test execution
    37  func SetupTestLogging() {
    38  	level, err := logging.LogLevel(viper.GetString("peer.logging.level"))
    39  	if err == nil {
    40  		// No error, use the setting
    41  		logging.SetLevel(level, "main")
    42  		logging.SetLevel(level, "server")
    43  		logging.SetLevel(level, "peer")
    44  	} else {
    45  		logging.SetLevel(logging.ERROR, "main")
    46  		logging.SetLevel(logging.ERROR, "server")
    47  		logging.SetLevel(logging.ERROR, "peer")
    48  	}
    49  }
    50  
    51  // SetupTestConfig setup the config during test execution
    52  func SetupTestConfig() {
    53  	runtime.GOMAXPROCS(runtime.NumCPU())
    54  	flag.Parse()
    55  
    56  	// Now set the configuration file
    57  	viper.SetEnvPrefix("HYPERLEDGER")
    58  	viper.AutomaticEnv()
    59  	replacer := strings.NewReplacer(".", "_")
    60  	viper.SetEnvKeyReplacer(replacer)
    61  	viper.SetConfigName("core")       // name of config file (without extension)
    62  	viper.AddConfigPath("./")         // path to look for the config file in
    63  	viper.AddConfigPath("./../peer/") // path to look for the config file in
    64  	err := viper.ReadInConfig()       // Find and read the config file
    65  	if err != nil {                   // Handle errors reading the config file
    66  		panic(fmt.Errorf("Fatal error config file: %s \n", err))
    67  	}
    68  
    69  	SetupTestLogging()
    70  
    71  	// Init the BCCSP
    72  	var bccspConfig *factory.FactoryOpts
    73  	err = viper.UnmarshalKey("peer.BCCSP", &bccspConfig)
    74  	if err != nil {
    75  		bccspConfig = nil
    76  	}
    77  
    78  	msp.SetupBCCSPKeystoreConfig(bccspConfig, viper.GetString("peer.mspConfigPath")+"/keystore")
    79  
    80  	err = factory.InitFactories(bccspConfig)
    81  	if err != nil {
    82  		panic(fmt.Errorf("Could not initialize BCCSP Factories [%s]", err))
    83  	}
    84  }