github.com/adnan-c/fabric_e2e_couchdb@v0.6.1-preview.0.20170228180935-21ce6b23cf91/core/scc/lccc/lccc_test.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  package lccc
    17  
    18  import (
    19  	"fmt"
    20  	"testing"
    21  
    22  	"os"
    23  
    24  	"github.com/golang/protobuf/proto"
    25  	"github.com/hyperledger/fabric/core/chaincode/shim"
    26  	"github.com/hyperledger/fabric/core/common/ccprovider"
    27  	"github.com/hyperledger/fabric/core/common/sysccprovider"
    28  	//"github.com/hyperledger/fabric/core/container"
    29  	"archive/tar"
    30  	"bytes"
    31  	"compress/gzip"
    32  
    33  	"github.com/hyperledger/fabric/core/container/util"
    34  	pb "github.com/hyperledger/fabric/protos/peer"
    35  )
    36  
    37  var lccctestpath = "/tmp/lccctest"
    38  
    39  type mocksccProviderFactory struct {
    40  }
    41  
    42  func (c *mocksccProviderFactory) NewSystemChaincodeProvider() sysccprovider.SystemChaincodeProvider {
    43  	return &mocksccProviderImpl{}
    44  }
    45  
    46  type mocksccProviderImpl struct {
    47  }
    48  
    49  func (c *mocksccProviderImpl) IsSysCC(name string) bool {
    50  	return true
    51  }
    52  
    53  func register(stub *shim.MockStub, ccname string) error {
    54  	args := [][]byte{[]byte("register"), []byte(ccname)}
    55  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
    56  		return fmt.Errorf(string(res.Message))
    57  	}
    58  	return nil
    59  }
    60  
    61  func constructDeploymentSpec(name string, path string, version string, initArgs [][]byte, createFS bool) (*pb.ChaincodeDeploymentSpec, error) {
    62  	spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: &pb.ChaincodeID{Name: name, Path: path, Version: version}, Input: &pb.ChaincodeInput{Args: initArgs}}
    63  
    64  	codePackageBytes := bytes.NewBuffer(nil)
    65  	gz := gzip.NewWriter(codePackageBytes)
    66  	tw := tar.NewWriter(gz)
    67  
    68  	err := util.WriteBytesToPackage("src/garbage.go", []byte(name+path+version), tw)
    69  	if err != nil {
    70  		return nil, err
    71  	}
    72  
    73  	tw.Close()
    74  	gz.Close()
    75  
    76  	chaincodeDeploymentSpec := &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec, CodePackage: codePackageBytes.Bytes()}
    77  
    78  	if createFS {
    79  		err := ccprovider.PutChaincodeIntoFS(chaincodeDeploymentSpec)
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  	}
    84  
    85  	return chaincodeDeploymentSpec, nil
    86  }
    87  
    88  //TestDeploy tests the deploy function (stops short of actually running the chaincode)
    89  func TestDeploy(t *testing.T) {
    90  	scc := new(LifeCycleSysCC)
    91  	stub := shim.NewMockStub("lccc", scc)
    92  
    93  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
    94  		fmt.Println("Init failed", string(res.Message))
    95  		t.FailNow()
    96  	}
    97  
    98  	ccname := "example02"
    99  	path := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
   100  	version := "0"
   101  	cds, err := constructDeploymentSpec(ccname, path, version, [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   102  	if err != nil {
   103  		t.FailNow()
   104  	}
   105  	defer os.Remove(lccctestpath + "/example02.0")
   106  	var b []byte
   107  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   108  		t.FailNow()
   109  	}
   110  
   111  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   112  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   113  		t.FailNow()
   114  	}
   115  
   116  	args = [][]byte{[]byte(GETCHAINCODES)}
   117  	res := stub.MockInvoke("1", args)
   118  	if res.Status != shim.OK {
   119  		t.FailNow()
   120  	}
   121  
   122  	cqr := &pb.ChaincodeQueryResponse{}
   123  	err = proto.Unmarshal(res.Payload, cqr)
   124  	if err != nil {
   125  		t.FailNow()
   126  	}
   127  	// deployed one chaincode so query should return an array with one chaincode
   128  	if len(cqr.GetChaincodes()) != 1 {
   129  		t.FailNow()
   130  	}
   131  
   132  	// check that the ChaincodeInfo values match the input values
   133  	if cqr.GetChaincodes()[0].Name != ccname || cqr.GetChaincodes()[0].Version != version || cqr.GetChaincodes()[0].Path != path {
   134  		t.FailNow()
   135  	}
   136  }
   137  
   138  //TestInstall tests the install function
   139  func TestInstall(t *testing.T) {
   140  	scc := new(LifeCycleSysCC)
   141  	stub := shim.NewMockStub("lccc", scc)
   142  
   143  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   144  		fmt.Println("Init failed", string(res.Message))
   145  		t.FailNow()
   146  	}
   147  	ccname := "example02"
   148  	path := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
   149  	version := "0"
   150  	cds, err := constructDeploymentSpec(ccname, path, version, [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, false)
   151  	var b []byte
   152  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   153  		t.FailNow()
   154  	}
   155  
   156  	//constructDeploymentSpec puts the depspec on the FS. This should succeed
   157  	args := [][]byte{[]byte(INSTALL), b}
   158  	defer os.Remove(lccctestpath + "/example02.0")
   159  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   160  		t.FailNow()
   161  	}
   162  
   163  	args = [][]byte{[]byte(GETINSTALLEDCHAINCODES)}
   164  	res := stub.MockInvoke("1", args)
   165  	if res.Status != shim.OK {
   166  		t.FailNow()
   167  	}
   168  
   169  	cqr := &pb.ChaincodeQueryResponse{}
   170  	err = proto.Unmarshal(res.Payload, cqr)
   171  	if err != nil {
   172  		t.FailNow()
   173  	}
   174  
   175  	// installed one chaincode so query should return an array with one chaincode
   176  	if len(cqr.GetChaincodes()) != 1 {
   177  		t.FailNow()
   178  	}
   179  
   180  	// check that the ChaincodeInfo values match the input values
   181  	if cqr.GetChaincodes()[0].Name != ccname || cqr.GetChaincodes()[0].Version != version || cqr.GetChaincodes()[0].Path != path {
   182  		t.FailNow()
   183  	}
   184  }
   185  
   186  //TestReinstall tests the install function
   187  func TestReinstall(t *testing.T) {
   188  	scc := new(LifeCycleSysCC)
   189  	stub := shim.NewMockStub("lccc", scc)
   190  
   191  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   192  		fmt.Println("Init failed", string(res.Message))
   193  		t.FailNow()
   194  	}
   195  
   196  	//note that this puts the code on the filesyste....
   197  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   198  	defer os.Remove(lccctestpath + "/example02.0")
   199  	var b []byte
   200  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   201  		t.FailNow()
   202  	}
   203  
   204  	//constructDeploymentSpec puts the depspec on the FS. This should fail
   205  	args := [][]byte{[]byte(INSTALL), b}
   206  	if res := stub.MockInvoke("1", args); res.Status == shim.OK {
   207  		t.FailNow()
   208  	}
   209  }
   210  
   211  //TestInvalidCodeDeploy tests the deploy function with invalid code package
   212  func TestInvalidCodeDeploy(t *testing.T) {
   213  	scc := new(LifeCycleSysCC)
   214  	stub := shim.NewMockStub("lccc", scc)
   215  
   216  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   217  		fmt.Println("Init failed", string(res.Message))
   218  		t.FailNow()
   219  	}
   220  
   221  	baddepspec := []byte("bad deploy spec")
   222  	args := [][]byte{[]byte(DEPLOY), []byte("test"), baddepspec}
   223  	res := stub.MockInvoke("1", args)
   224  	if res.Status == shim.OK {
   225  		t.Logf("Expected failure")
   226  		t.FailNow()
   227  	}
   228  }
   229  
   230  //TestInvalidChaincodeName tests the deploy function with invalid chaincode name
   231  func TestInvalidChaincodeName(t *testing.T) {
   232  	scc := new(LifeCycleSysCC)
   233  	stub := shim.NewMockStub("lccc", scc)
   234  
   235  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   236  		fmt.Println("Init failed", string(res.Message))
   237  		t.FailNow()
   238  	}
   239  
   240  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   241  	defer os.Remove(lccctestpath + "/example02.0")
   242  	if err != nil {
   243  		t.FailNow()
   244  	}
   245  
   246  	//change name to empty
   247  	cds.ChaincodeSpec.ChaincodeId.Name = ""
   248  
   249  	var b []byte
   250  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   251  		t.FailNow()
   252  	}
   253  
   254  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   255  	res := stub.MockInvoke("1", args)
   256  	if string(res.Message) != InvalidChaincodeNameErr("").Error() {
   257  		t.Logf("Get error: %s", res.Message)
   258  		t.FailNow()
   259  	}
   260  }
   261  
   262  //TestEmptyChaincodeVersion tests the deploy function without a version name
   263  func TestEmptyChaincodeVersion(t *testing.T) {
   264  	scc := new(LifeCycleSysCC)
   265  	stub := shim.NewMockStub("lccc", scc)
   266  
   267  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   268  		fmt.Println("Init failed", string(res.Message))
   269  		t.FailNow()
   270  	}
   271  
   272  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   273  	defer os.Remove(lccctestpath + "/example02.0")
   274  	if err != nil {
   275  		t.FailNow()
   276  	}
   277  
   278  	//change version to empty
   279  	cds.ChaincodeSpec.ChaincodeId.Version = ""
   280  
   281  	var b []byte
   282  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   283  		t.FailNow()
   284  	}
   285  
   286  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   287  	res := stub.MockInvoke("1", args)
   288  	if string(res.Message) != EmptyVersionErr("example02").Error() {
   289  		t.Logf("Get error: %s", res.Message)
   290  		t.FailNow()
   291  	}
   292  }
   293  
   294  //TestRedeploy tests the redeploying will fail function(and fail with "exists" error)
   295  func TestRedeploy(t *testing.T) {
   296  	scc := new(LifeCycleSysCC)
   297  	stub := shim.NewMockStub("lccc", scc)
   298  
   299  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   300  		fmt.Println("Init failed", string(res.Message))
   301  		t.FailNow()
   302  	}
   303  
   304  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   305  	defer os.Remove(lccctestpath + "/example02.0")
   306  	var b []byte
   307  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   308  		t.FailNow()
   309  	}
   310  
   311  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   312  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   313  		t.FailNow()
   314  	}
   315  
   316  	//this should fail with exists error
   317  	args = [][]byte{[]byte(DEPLOY), []byte("test"), b}
   318  	res := stub.MockInvoke("1", args)
   319  	if string(res.Message) != ExistsErr("example02").Error() {
   320  		t.FailNow()
   321  	}
   322  }
   323  
   324  //TestCheckCC invokes the GETCCINFO function to get status of deployed chaincode
   325  func TestCheckCC(t *testing.T) {
   326  	scc := new(LifeCycleSysCC)
   327  	stub := shim.NewMockStub("lccc", scc)
   328  
   329  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   330  		fmt.Println("Init failed", string(res.Message))
   331  		t.FailNow()
   332  	}
   333  
   334  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   335  	defer os.Remove(lccctestpath + "/example02.0")
   336  
   337  	var b []byte
   338  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   339  		t.FailNow()
   340  	}
   341  
   342  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   343  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   344  		t.FailNow()
   345  	}
   346  
   347  	args = [][]byte{[]byte(GETCCINFO), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   348  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   349  		t.FailNow()
   350  	}
   351  }
   352  
   353  //TestMultipleDeploy tests deploying multiple chaincodeschaincodes
   354  func TestMultipleDeploy(t *testing.T) {
   355  	scc := new(LifeCycleSysCC)
   356  	stub := shim.NewMockStub("lccc", scc)
   357  
   358  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   359  		fmt.Println("Init failed", string(res.Message))
   360  		t.FailNow()
   361  	}
   362  
   363  	//deploy 02
   364  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   365  	defer os.Remove(lccctestpath + "/example02.0")
   366  	var b []byte
   367  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   368  		t.FailNow()
   369  	}
   370  
   371  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   372  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   373  		t.FailNow()
   374  	}
   375  
   376  	args = [][]byte{[]byte(GETCCINFO), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   377  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   378  		t.FailNow()
   379  	}
   380  
   381  	//deploy 01
   382  	cds, err = constructDeploymentSpec("example01", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example01", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   383  	defer os.Remove(lccctestpath + "/example01.0")
   384  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   385  		t.FailNow()
   386  	}
   387  
   388  	args = [][]byte{[]byte(DEPLOY), []byte("test"), b}
   389  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   390  		t.FailNow()
   391  	}
   392  
   393  	args = [][]byte{[]byte(GETCCINFO), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   394  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   395  		t.FailNow()
   396  	}
   397  
   398  	args = [][]byte{[]byte(GETCHAINCODES)}
   399  	res := stub.MockInvoke("1", args)
   400  	if res.Status != shim.OK {
   401  		t.FailNow()
   402  	}
   403  
   404  	cqr := &pb.ChaincodeQueryResponse{}
   405  	err = proto.Unmarshal(res.Payload, cqr)
   406  	if err != nil {
   407  		t.FailNow()
   408  	}
   409  
   410  	// deployed two chaincodes so query should return an array with two chaincodes
   411  	if len(cqr.GetChaincodes()) != 2 {
   412  		t.FailNow()
   413  	}
   414  
   415  }
   416  
   417  //TestRetryFailedDeploy tests re-deploying after a failure
   418  func TestRetryFailedDeploy(t *testing.T) {
   419  	scc := new(LifeCycleSysCC)
   420  	stub := shim.NewMockStub("lccc", scc)
   421  
   422  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   423  		fmt.Println("Init failed", string(res.Message))
   424  		t.FailNow()
   425  	}
   426  
   427  	//deploy 02
   428  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   429  	defer os.Remove(lccctestpath + "/example02.0")
   430  	var b []byte
   431  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   432  		t.FailNow()
   433  	}
   434  
   435  	//send invalid chain name name that should fail
   436  	args := [][]byte{[]byte(DEPLOY), []byte(""), b}
   437  	res := stub.MockInvoke("1", args)
   438  	if res.Status == shim.OK {
   439  		//expected error but got success
   440  		t.FailNow()
   441  	}
   442  
   443  	if string(res.Message) != InvalidChainNameErr("").Error() {
   444  		//expected invalid chain name
   445  		t.FailNow()
   446  	}
   447  
   448  	//deploy correctly now
   449  	args = [][]byte{[]byte(DEPLOY), []byte("test"), b}
   450  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   451  		t.FailNow()
   452  	}
   453  
   454  	//get the deploymentspec
   455  	args = [][]byte{[]byte(GETDEPSPEC), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   456  	if res := stub.MockInvoke("1", args); res.Status != shim.OK || res.Payload == nil {
   457  		t.FailNow()
   458  	}
   459  }
   460  
   461  //TestUpgrade tests the upgrade function
   462  func TestUpgrade(t *testing.T) {
   463  	scc := new(LifeCycleSysCC)
   464  	stub := shim.NewMockStub("lccc", scc)
   465  
   466  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   467  		fmt.Println("Init failed", string(res.Message))
   468  		t.FailNow()
   469  	}
   470  
   471  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   472  	defer os.Remove(lccctestpath + "/example02.0")
   473  	var b []byte
   474  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   475  		t.Fatalf("Marshal DeploymentSpec failed")
   476  	}
   477  
   478  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   479  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   480  		t.Fatalf("Deploy chaincode error: %v", err)
   481  	}
   482  
   483  	newCds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "1", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   484  	defer os.Remove(lccctestpath + "/example02.1")
   485  	var newb []byte
   486  	if newb, err = proto.Marshal(newCds); err != nil || newb == nil {
   487  		t.Fatalf("Marshal DeploymentSpec failed")
   488  	}
   489  
   490  	args = [][]byte{[]byte(UPGRADE), []byte("test"), newb}
   491  	res := stub.MockInvoke("1", args)
   492  	if res.Status != shim.OK {
   493  		t.Fatalf("Upgrade chaincode error: %v", err)
   494  	}
   495  
   496  	expectVer := "1"
   497  	newVer := string(res.Payload)
   498  	if newVer != expectVer {
   499  		t.Fatalf("Upgrade chaincode version error, expected %s, got %s", expectVer, newVer)
   500  	}
   501  }
   502  
   503  //TestUpgradeNonExistChaincode tests upgrade non exist chaincode
   504  func TestUpgradeNonExistChaincode(t *testing.T) {
   505  	scc := new(LifeCycleSysCC)
   506  	stub := shim.NewMockStub("lccc", scc)
   507  
   508  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   509  		fmt.Println("Init failed", string(res.Message))
   510  		t.FailNow()
   511  	}
   512  
   513  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   514  	defer os.Remove(lccctestpath + "/example02.0")
   515  	var b []byte
   516  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   517  		t.Fatalf("Marshal DeploymentSpec failed")
   518  	}
   519  
   520  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   521  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   522  		t.Fatalf("Deploy chaincode error: %s", res.Message)
   523  	}
   524  
   525  	newCds, err := constructDeploymentSpec("example03", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "1", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   526  	defer os.Remove(lccctestpath + "/example03.1")
   527  	var newb []byte
   528  	if newb, err = proto.Marshal(newCds); err != nil || newb == nil {
   529  		t.Fatalf("Marshal DeploymentSpec failed")
   530  	}
   531  
   532  	args = [][]byte{[]byte(UPGRADE), []byte("test"), newb}
   533  	res := stub.MockInvoke("1", args)
   534  	if string(res.Message) != NotFoundErr("test").Error() {
   535  		t.FailNow()
   536  	}
   537  }
   538  
   539  //TestGetAPIsWithoutInstall get functions should return the right responses when chaicode is on
   540  //ledger but not on FS
   541  func TestGetAPIsWithoutInstall(t *testing.T) {
   542  	scc := new(LifeCycleSysCC)
   543  	stub := shim.NewMockStub("lccc", scc)
   544  
   545  	if res := stub.MockInit("1", nil); res.Status != shim.OK {
   546  		fmt.Println("Init failed", string(res.Message))
   547  		t.FailNow()
   548  	}
   549  
   550  	cds, err := constructDeploymentSpec("example02", "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02", "0", [][]byte{[]byte("init"), []byte("a"), []byte("100"), []byte("b"), []byte("200")}, true)
   551  
   552  	var b []byte
   553  	if b, err = proto.Marshal(cds); err != nil || b == nil {
   554  		t.FailNow()
   555  	}
   556  
   557  	args := [][]byte{[]byte(DEPLOY), []byte("test"), b}
   558  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   559  		t.FailNow()
   560  	}
   561  
   562  	//Force remove CC
   563  	os.Remove(lccctestpath + "/example02.0")
   564  
   565  	//GETCCINFO should still work
   566  	args = [][]byte{[]byte(GETCCINFO), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   567  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   568  		t.FailNow()
   569  	}
   570  
   571  	//GETCCDATA should still work
   572  	args = [][]byte{[]byte(GETCCDATA), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   573  	if res := stub.MockInvoke("1", args); res.Status != shim.OK {
   574  		t.FailNow()
   575  	}
   576  
   577  	//GETDEPSPEC should not work
   578  	args = [][]byte{[]byte(GETDEPSPEC), []byte("test"), []byte(cds.ChaincodeSpec.ChaincodeId.Name)}
   579  	if res := stub.MockInvoke("1", args); res.Status == shim.OK {
   580  		t.FailNow()
   581  	}
   582  
   583  	// get instantiated chaincodes
   584  	args = [][]byte{[]byte(GETCHAINCODES)}
   585  	res := stub.MockInvoke("1", args)
   586  	if res.Status != shim.OK {
   587  		t.FailNow()
   588  	}
   589  
   590  	cqr := &pb.ChaincodeQueryResponse{}
   591  	err = proto.Unmarshal(res.Payload, cqr)
   592  	if err != nil {
   593  		t.FailNow()
   594  	}
   595  
   596  	// one chaincode instantiated so query should return an array with one
   597  	// chaincode
   598  	if len(cqr.GetChaincodes()) != 1 {
   599  		t.FailNow()
   600  	}
   601  
   602  	// get installed chaincodes
   603  	args = [][]byte{[]byte(GETINSTALLEDCHAINCODES)}
   604  	res = stub.MockInvoke("1", args)
   605  	if res.Status != shim.OK {
   606  		t.FailNow()
   607  	}
   608  
   609  	cqr = &pb.ChaincodeQueryResponse{}
   610  	err = proto.Unmarshal(res.Payload, cqr)
   611  	if err != nil {
   612  		t.FailNow()
   613  	}
   614  
   615  	// no chaincodes installed to FS so query should return an array with zero
   616  	// chaincodes
   617  	if len(cqr.GetChaincodes()) != 0 {
   618  		t.FailNow()
   619  	}
   620  
   621  }
   622  
   623  func TestMain(m *testing.M) {
   624  	ccprovider.SetChaincodesPath(lccctestpath)
   625  	sysccprovider.RegisterSystemChaincodeProviderFactory(&mocksccProviderFactory{})
   626  	os.Exit(m.Run())
   627  }