github.com/lzy4123/fabric@v2.1.1+incompatible/cmd/configtxgen/main_test.go (about)

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package main
     8  
     9  import (
    10  	"flag"
    11  	"io/ioutil"
    12  	"os"
    13  	"path/filepath"
    14  	"testing"
    15  
    16  	"github.com/hyperledger/fabric/bccsp/factory"
    17  	"github.com/hyperledger/fabric/core/config/configtest"
    18  	"github.com/hyperledger/fabric/internal/configtxgen/genesisconfig"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  var tmpDir string
    23  
    24  func TestMain(m *testing.M) {
    25  	dir, err := ioutil.TempDir("", "configtxgen")
    26  	if err != nil {
    27  		panic("Error creating temp dir")
    28  	}
    29  	tmpDir = dir
    30  	testResult := m.Run()
    31  	os.RemoveAll(dir)
    32  
    33  	os.Exit(testResult)
    34  }
    35  
    36  func TestInspectMissing(t *testing.T) {
    37  	assert.Error(t, doInspectBlock("NonSenseBlockFileThatDoesn'tActuallyExist"), "Missing block")
    38  }
    39  
    40  func TestInspectBlock(t *testing.T) {
    41  	blockDest := filepath.Join(tmpDir, "block")
    42  
    43  	config := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
    44  
    45  	assert.NoError(t, doOutputBlock(config, "foo", blockDest), "Good block generation request")
    46  	assert.NoError(t, doInspectBlock(blockDest), "Good block inspection request")
    47  }
    48  
    49  func TestMissingOrdererSection(t *testing.T) {
    50  	blockDest := filepath.Join(tmpDir, "block")
    51  
    52  	config := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
    53  	config.Orderer = nil
    54  
    55  	assert.Error(t, doOutputBlock(config, "foo", blockDest), "Missing orderer section")
    56  }
    57  
    58  func TestMissingConsortiumSection(t *testing.T) {
    59  	blockDest := filepath.Join(tmpDir, "block")
    60  
    61  	config := genesisconfig.Load(genesisconfig.SampleInsecureSoloProfile, configtest.GetDevConfigDir())
    62  	config.Consortiums = nil
    63  
    64  	assert.NoError(t, doOutputBlock(config, "foo", blockDest), "Missing consortiums section")
    65  }
    66  
    67  func TestMissingConsortiumValue(t *testing.T) {
    68  	configTxDest := filepath.Join(tmpDir, "configtx")
    69  
    70  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
    71  	config.Consortium = ""
    72  
    73  	assert.Error(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "Missing Consortium value in Application Profile definition")
    74  }
    75  
    76  func TestMissingApplicationValue(t *testing.T) {
    77  	configTxDest := filepath.Join(tmpDir, "configtx")
    78  
    79  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
    80  	config.Application = nil
    81  
    82  	assert.Error(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "Missing Application value in Application Profile definition")
    83  }
    84  
    85  func TestInspectMissingConfigTx(t *testing.T) {
    86  	assert.Error(t, doInspectChannelCreateTx("ChannelCreateTxFileWhichDoesn'tReallyExist"), "Missing channel create tx file")
    87  }
    88  
    89  func TestInspectConfigTx(t *testing.T) {
    90  	configTxDest := filepath.Join(tmpDir, "configtx")
    91  
    92  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
    93  
    94  	assert.NoError(t, doOutputChannelCreateTx(config, nil, "foo", configTxDest), "Good outputChannelCreateTx generation request")
    95  	assert.NoError(t, doInspectChannelCreateTx(configTxDest), "Good configtx inspection request")
    96  }
    97  
    98  func TestGenerateAnchorPeersUpdate(t *testing.T) {
    99  	configTxDest := filepath.Join(tmpDir, "anchorPeerUpdate")
   100  
   101  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
   102  
   103  	assert.NoError(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "Good anchorPeerUpdate request")
   104  }
   105  
   106  func TestBadAnchorPeersUpdates(t *testing.T) {
   107  	configTxDest := filepath.Join(tmpDir, "anchorPeerUpdate")
   108  
   109  	config := genesisconfig.Load(genesisconfig.SampleSingleMSPChannelProfile, configtest.GetDevConfigDir())
   110  
   111  	assert.Error(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, ""), "Bad anchorPeerUpdate request - asOrg empty")
   112  
   113  	backupApplication := config.Application
   114  	config.Application = nil
   115  	assert.Error(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "Bad anchorPeerUpdate request")
   116  	config.Application = backupApplication
   117  
   118  	config.Application.Organizations[0] = &genesisconfig.Organization{Name: "FakeOrg", ID: "FakeOrg"}
   119  	assert.Error(t, doOutputAnchorPeersUpdate(config, "foo", configTxDest, genesisconfig.SampleOrgName), "Bad anchorPeerUpdate request - fake org")
   120  }
   121  
   122  func TestConfigTxFlags(t *testing.T) {
   123  	configTxDest := filepath.Join(tmpDir, "configtx")
   124  	configTxDestAnchorPeers := filepath.Join(tmpDir, "configtxAnchorPeers")
   125  
   126  	oldArgs := os.Args
   127  	defer func() {
   128  		os.Args = oldArgs
   129  		flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
   130  	}()
   131  
   132  	cleanup := configtest.SetDevFabricConfigPath(t)
   133  	defer cleanup()
   134  	devConfigDir := configtest.GetDevConfigDir()
   135  
   136  	os.Args = []string{
   137  		"cmd",
   138  		"-channelID=testchannelid",
   139  		"-outputCreateChannelTx=" + configTxDest,
   140  		"-channelCreateTxBaseProfile=" + genesisconfig.SampleSingleMSPSoloProfile,
   141  		"-profile=" + genesisconfig.SampleSingleMSPChannelProfile,
   142  		"-configPath=" + devConfigDir,
   143  		"-inspectChannelCreateTx=" + configTxDest,
   144  		"-outputAnchorPeersUpdate=" + configTxDestAnchorPeers,
   145  		"-asOrg=" + genesisconfig.SampleOrgName,
   146  	}
   147  
   148  	main()
   149  
   150  	_, err := os.Stat(configTxDest)
   151  	assert.NoError(t, err, "Configtx file is written successfully")
   152  	_, err = os.Stat(configTxDestAnchorPeers)
   153  	assert.NoError(t, err, "Configtx anchor peers file is written successfully")
   154  }
   155  
   156  func TestBlockFlags(t *testing.T) {
   157  	blockDest := filepath.Join(tmpDir, "block")
   158  	oldArgs := os.Args
   159  	defer func() {
   160  		os.Args = oldArgs
   161  		flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
   162  	}()
   163  	os.Args = []string{
   164  		"cmd",
   165  		"-channelID=testchannelid",
   166  		"-profile=" + genesisconfig.SampleSingleMSPSoloProfile,
   167  		"-outputBlock=" + blockDest,
   168  		"-inspectBlock=" + blockDest,
   169  	}
   170  	cleanup := configtest.SetDevFabricConfigPath(t)
   171  	defer cleanup()
   172  
   173  	main()
   174  
   175  	_, err := os.Stat(blockDest)
   176  	assert.NoError(t, err, "Block file is written successfully")
   177  }
   178  
   179  func TestPrintOrg(t *testing.T) {
   180  	factory.InitFactories(nil)
   181  	config := genesisconfig.LoadTopLevel(configtest.GetDevConfigDir())
   182  
   183  	assert.NoError(t, doPrintOrg(config, genesisconfig.SampleOrgName), "Good org to print")
   184  
   185  	err := doPrintOrg(config, genesisconfig.SampleOrgName+".wrong")
   186  	assert.Error(t, err, "Bad org name")
   187  	assert.Regexp(t, "organization [^ ]* not found", err.Error())
   188  
   189  	config.Organizations[0] = &genesisconfig.Organization{Name: "FakeOrg", ID: "FakeOrg"}
   190  	err = doPrintOrg(config, "FakeOrg")
   191  	assert.Error(t, err, "Fake org")
   192  	assert.Regexp(t, "bad org definition", err.Error())
   193  }