github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/test/tools/LTE/experiments/init_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 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 experiments
    18  
    19  import (
    20  	"flag"
    21  	"os"
    22  	"regexp"
    23  	"testing"
    24  
    25  	"github.com/davecgh/go-spew/spew"
    26  	"github.com/hyperledger/fabric/core/ledger/testutil"
    27  
    28  	"fmt"
    29  )
    30  
    31  const chaincodeName = "testChaincode"
    32  
    33  var conf *configuration
    34  
    35  // TestMain loads the yaml file and parses the test parameters
    36  // the configration constructed from test parameters is stored in
    37  // package level variable and is accessible to an expriment
    38  // The test params should be passed in the following format in the golang benchmark test command
    39  // "-testParams=-DataDir="myDir", -NumChains=10, ..."
    40  // This is necessary to parse in the TestMain function because otherwise, the golang test framework
    41  // parses this and does not recognized this flag (-testParams)
    42  func TestMain(m *testing.M) {
    43  	testutil.SetupCoreYAMLConfig()
    44  	testParams := parseTestParams()
    45  	conf = confFromTestParams(testParams)
    46  	logger.Infof("Running experiment with configuration: %s\n", spew.Sdump(conf))
    47  	disableLogging()
    48  	os.Exit(m.Run())
    49  }
    50  
    51  func parseTestParams() []string {
    52  	testParams := flag.String("testParams", "", "Test specific parameters")
    53  	flag.Parse()
    54  	regex, err := regexp.Compile(",(\\s+)?")
    55  	if err != nil {
    56  		panic(fmt.Errorf("Error: %s", err))
    57  	}
    58  	paramsArray := regex.Split(*testParams, -1)
    59  	return paramsArray
    60  }