github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/common/configtx/tool/configtxgen/main_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 main
    18  
    19  import (
    20  	"flag"
    21  	"io/ioutil"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/hyperledger/fabric/bccsp/factory"
    26  	genesisconfig "github.com/hyperledger/fabric/common/configtx/tool/localconfig"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  )
    30  
    31  var tmpDir string
    32  
    33  func TestMain(m *testing.M) {
    34  	dir, err := ioutil.TempDir("", "configtxgen")
    35  	if err != nil {
    36  		panic("Error creating temp dir")
    37  	}
    38  	tmpDir = dir
    39  	testResult := m.Run()
    40  	os.RemoveAll(dir)
    41  
    42  	os.Exit(testResult)
    43  }
    44  
    45  func TestInspectMissing(t *testing.T) {
    46  	assert.Error(t, doInspectBlock("NonSenseBlockFileThatDoesn'tActuallyExist"), "Missing block")
    47  }
    48  
    49  func TestInspectBlock(t *testing.T) {
    50  	blockDest := tmpDir + string(os.PathSeparator) + "block"
    51  
    52  	factory.InitFactories(nil)
    53  	config := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
    54  
    55  	assert.NoError(t, doOutputBlock(config, "foo", blockDest), "Good block generation request")
    56  	assert.NoError(t, doInspectBlock(blockDest), "Good block inspection request")
    57  }
    58  
    59  func TestMissingOrdererSection(t *testing.T) {
    60  	blockDest := tmpDir + string(os.PathSeparator) + "block"
    61  
    62  	factory.InitFactories(nil)
    63  	config := genesisconfig.Load(genesisconfig.SampleInsecureProfile)
    64  	config.Orderer = nil
    65  
    66  	assert.Error(t, doOutputBlock(config, "foo", blockDest), "Missing orderer section")
    67  }
    68  
    69  func TestMissingConsortiumValue(t *testing.T) {
    70  	configTxDest := tmpDir + string(os.PathSeparator) + "configtx"
    71  
    72  	factory.InitFactories(nil)
    73  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile)
    74  	config.Consortium = ""
    75  
    76  	assert.Error(t, doOutputChannelCreateTx(config, "foo", configTxDest), "Missing Consortium value in Application Profile definition")
    77  }
    78  
    79  func TestInspectMissingConfigTx(t *testing.T) {
    80  	assert.Error(t, doInspectChannelCreateTx("ChannelCreateTxFileWhichDoesn'tReallyExist"), "Missing channel create tx file")
    81  }
    82  
    83  func TestInspectConfigTx(t *testing.T) {
    84  	configTxDest := tmpDir + string(os.PathSeparator) + "configtx"
    85  
    86  	factory.InitFactories(nil)
    87  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile)
    88  
    89  	assert.NoError(t, doOutputChannelCreateTx(config, "foo", configTxDest), "Good outputChannelCreateTx generation request")
    90  	assert.NoError(t, doInspectChannelCreateTx(configTxDest), "Good configtx inspection request")
    91  }
    92  
    93  func TestGenerateAnchorPeersUpdate(t *testing.T) {
    94  	configTxDest := tmpDir + string(os.PathSeparator) + "anchorPeerUpdate"
    95  
    96  	factory.InitFactories(nil)
    97  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile)
    98  
    99  	assert.NoError(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "Good anchorPeerUpdate request")
   100  }
   101  
   102  func TestConfigTxFlags(t *testing.T) {
   103  	configTxDest := tmpDir + string(os.PathSeparator) + "configtx"
   104  	configTxDestAnchorPeers := tmpDir + string(os.PathSeparator) + "configtxAnchorPeers"
   105  	oldArgs := os.Args
   106  	defer func() {
   107  		os.Args = oldArgs
   108  		flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
   109  	}()
   110  	os.Args = []string{
   111  		"cmd",
   112  		"-outputCreateChannelTx=" + configTxDest,
   113  		"-profile=" + genesisconfig.SampleSingleMSPChannelProfile,
   114  		"-inspectChannelCreateTx=" + configTxDest,
   115  		"-outputAnchorPeersUpdate=" + configTxDestAnchorPeers,
   116  		"-asOrg=" + genesisconfig.SampleOrgName,
   117  	}
   118  	main()
   119  
   120  	_, err := os.Stat(configTxDest)
   121  	assert.NoError(t, err, "Configtx file is written successfully")
   122  	_, err = os.Stat(configTxDestAnchorPeers)
   123  	assert.NoError(t, err, "Configtx anchor peers file is written successfully")
   124  }
   125  
   126  func TestBlockFlags(t *testing.T) {
   127  	blockDest := tmpDir + string(os.PathSeparator) + "block"
   128  	oldArgs := os.Args
   129  	defer func() {
   130  		os.Args = oldArgs
   131  		flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
   132  	}()
   133  	os.Args = []string{
   134  		"cmd",
   135  		"-profile=" + genesisconfig.SampleSingleMSPSoloProfile,
   136  		"-outputBlock=" + blockDest,
   137  		"-inspectBlock=" + blockDest,
   138  	}
   139  	main()
   140  
   141  	_, err := os.Stat(blockDest)
   142  	assert.NoError(t, err, "Block file is written successfully")
   143  }