github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/chaincode/instantiate_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package chaincode
     8  
     9  import (
    10  	"testing"
    11  
    12  	"github.com/hyperledger/fabric/bccsp/sw"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestInstantiateCmd(t *testing.T) {
    17  	mockCF, err := getMockChaincodeCmdFactory()
    18  	assert.NoError(t, err, "Error getting mock chaincode command factory")
    19  	cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore())
    20  	assert.NoError(t, err)
    21  
    22  	// basic function tests
    23  	var tests = []struct {
    24  		name          string
    25  		args          []string
    26  		errorExpected bool
    27  		errMsg        string
    28  	}{
    29  		{
    30  			name:          "successful",
    31  			args:          []string{"-n", "example02", "-v", "anotherversion", "-C", "mychannel", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    32  			errorExpected: false,
    33  			errMsg:        "Run chaincode instantiate cmd error",
    34  		},
    35  		{
    36  			name:          "no option",
    37  			args:          []string{},
    38  			errorExpected: true,
    39  			errMsg:        "Expected error executing instantiate command without required options",
    40  		},
    41  		{
    42  			name:          "missing version",
    43  			args:          []string{"-n", "example02", "-C", "mychannel", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    44  			errorExpected: true,
    45  			errMsg:        "Expected error executing instantiate command without the -v option",
    46  		},
    47  		{
    48  			name:          "missing name",
    49  			args:          []string{"-v", "anotherversion", "-C", "mychannel", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    50  			errorExpected: true,
    51  			errMsg:        "Expected error executing instantiate command without the -n option",
    52  		},
    53  		{
    54  			name:          "missing channelID",
    55  			args:          []string{"-n", "example02", "-v", "anotherversion", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    56  			errorExpected: true,
    57  			errMsg:        "Expected error executing instantiate command without the -C option",
    58  		},
    59  		{
    60  			name:          "missing ctor",
    61  			args:          []string{"-n", "example02", "-C", "mychannel", "-v", "anotherversion"},
    62  			errorExpected: true,
    63  			errMsg:        "Expected error executing instantiate command without the -c option",
    64  		},
    65  		{
    66  			name:          "successful with policy",
    67  			args:          []string{"-P", "OR('MSP.member', 'MSP.WITH.DOTS.member', 'MSP-WITH-DASHES.member')", "-n", "example02", "-v", "anotherversion", "-C", "mychannel", "-c", "{\"Args\": [\"init\",\"a\",\"100\",\"b\",\"200\"]}"},
    68  			errorExpected: false,
    69  			errMsg:        "Run chaincode instantiate cmd error",
    70  		},
    71  	}
    72  	for _, test := range tests {
    73  		t.Run(test.name, func(t *testing.T) {
    74  			resetFlags()
    75  			cmd := instantiateCmd(mockCF, cryptoProvider)
    76  			addFlags(cmd)
    77  			cmd.SetArgs(test.args)
    78  			err = cmd.Execute()
    79  			checkError(t, err, test.errorExpected, test.errMsg)
    80  		})
    81  	}
    82  }
    83  
    84  func checkError(t *testing.T, err error, expectedError bool, msg string) {
    85  	if expectedError {
    86  		assert.Error(t, err, msg)
    87  	} else {
    88  		assert.NoError(t, err, msg)
    89  	}
    90  }