github.com/lzy4123/fabric@v2.1.1+incompatible/core/chaincode/lifecycle/custodian_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package lifecycle_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	"github.com/hyperledger/fabric/core/chaincode/lifecycle"
    16  	"github.com/hyperledger/fabric/core/chaincode/lifecycle/mock"
    17  	"github.com/hyperledger/fabric/core/container"
    18  )
    19  
    20  var _ = Describe("Custodian", func() {
    21  	var (
    22  		cc            *lifecycle.ChaincodeCustodian
    23  		fakeBuilder   *mock.ChaincodeBuilder
    24  		fakeLauncher  *mock.ChaincodeLauncher
    25  		buildRegistry *container.BuildRegistry
    26  		doneC         chan struct{}
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		fakeBuilder = &mock.ChaincodeBuilder{}
    31  		fakeBuilder.BuildReturnsOnCall(1, fmt.Errorf("fake-build-error"))
    32  		fakeLauncher = &mock.ChaincodeLauncher{}
    33  		buildRegistry = &container.BuildRegistry{}
    34  		cc = lifecycle.NewChaincodeCustodian()
    35  		doneC = make(chan struct{})
    36  		go func() {
    37  			cc.Work(buildRegistry, fakeBuilder, fakeLauncher)
    38  			close(doneC)
    39  		}()
    40  	})
    41  
    42  	AfterEach(func() {
    43  		cc.Close()
    44  		Eventually(doneC).Should(BeClosed())
    45  	})
    46  
    47  	It("builds chaincodes", func() {
    48  		cc.NotifyInstalled("ccid1")
    49  		cc.NotifyInstalled("ccid2")
    50  		cc.NotifyInstalled("ccid3")
    51  		Eventually(fakeBuilder.BuildCallCount).Should(Equal(3))
    52  		Expect(fakeBuilder.BuildArgsForCall(0)).To(Equal("ccid1"))
    53  		Expect(fakeBuilder.BuildArgsForCall(1)).To(Equal("ccid2"))
    54  		Expect(fakeBuilder.BuildArgsForCall(2)).To(Equal("ccid3"))
    55  
    56  		Expect(fakeLauncher.LaunchCallCount()).To(Equal(0))
    57  	})
    58  
    59  	It("notifies on the build status", func() {
    60  		cc.NotifyInstalled("ccid1")
    61  		cc.NotifyInstalled("ccid2")
    62  		Eventually(fakeBuilder.BuildCallCount).Should(Equal(2))
    63  
    64  		buildStatus, ok := buildRegistry.BuildStatus("ccid1")
    65  		Expect(ok).To(BeTrue())
    66  		Eventually(buildStatus.Done()).Should(BeClosed())
    67  		Expect(buildStatus.Err()).To(BeNil())
    68  
    69  		buildStatus, ok = buildRegistry.BuildStatus("ccid2")
    70  		Expect(ok).To(BeTrue())
    71  		Eventually(buildStatus.Done()).Should(BeClosed())
    72  		Expect(buildStatus.Err()).To(MatchError("fake-build-error"))
    73  	})
    74  
    75  	When("the chaincode is being built already", func() {
    76  		BeforeEach(func() {
    77  			buildStatus, ok := buildRegistry.BuildStatus("ccid1")
    78  			Expect(ok).To(BeFalse())
    79  			buildStatus.Notify(nil)
    80  		})
    81  
    82  		It("skips the build", func() {
    83  			cc.NotifyInstalled("ccid1")
    84  			Consistently(fakeBuilder.BuildCallCount).Should(Equal(0))
    85  		})
    86  	})
    87  
    88  	It("launches chaincodes", func() {
    89  		cc.NotifyInstalledAndRunnable("ccid1")
    90  		cc.NotifyInstalledAndRunnable("ccid2")
    91  		cc.NotifyInstalledAndRunnable("ccid3")
    92  		Eventually(fakeLauncher.LaunchCallCount).Should(Equal(3))
    93  		ccid := fakeLauncher.LaunchArgsForCall(0)
    94  		Expect(ccid).To(Equal("ccid1"))
    95  		ccid = fakeLauncher.LaunchArgsForCall(1)
    96  		Expect(ccid).To(Equal("ccid2"))
    97  		ccid = fakeLauncher.LaunchArgsForCall(2)
    98  		Expect(ccid).To(Equal("ccid3"))
    99  
   100  		Expect(fakeBuilder.BuildCallCount()).To(Equal(0))
   101  	})
   102  
   103  	It("stops chaincodes", func() {
   104  		cc.NotifyStoppable("ccid1")
   105  		cc.NotifyStoppable("ccid2")
   106  		cc.NotifyStoppable("ccid3")
   107  		Eventually(fakeLauncher.StopCallCount).Should(Equal(3))
   108  		ccid := fakeLauncher.StopArgsForCall(0)
   109  		Expect(ccid).To(Equal("ccid1"))
   110  		ccid = fakeLauncher.StopArgsForCall(1)
   111  		Expect(ccid).To(Equal("ccid2"))
   112  		ccid = fakeLauncher.StopArgsForCall(2)
   113  		Expect(ccid).To(Equal("ccid3"))
   114  
   115  		Expect(fakeBuilder.BuildCallCount()).To(Equal(0))
   116  		Expect(fakeLauncher.LaunchCallCount()).To(Equal(0))
   117  	})
   118  })