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

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package packaging_test
     8  
     9  import (
    10  	"errors"
    11  
    12  	"github.com/hyperledger/fabric/internal/peer/packaging"
    13  	"github.com/hyperledger/fabric/internal/peer/packaging/mock"
    14  
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  )
    18  
    19  var _ = Describe("Platforms", func() {
    20  	var (
    21  		registry     *packaging.Registry
    22  		fakePlatform *mock.Platform
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		fakePlatform = &mock.Platform{}
    27  		registry = &packaging.Registry{
    28  			Platforms: map[string]packaging.Platform{
    29  				"fakeType": fakePlatform,
    30  			},
    31  		}
    32  	})
    33  
    34  	Describe("pass through functions", func() {
    35  		Describe("ValidateSpec", func() {
    36  			It("returns the result of the underlying platform", func() {
    37  				fakePlatform.ValidatePathReturns(errors.New("fake-error"))
    38  				err := registry.ValidateSpec("fakeType", "cc-path")
    39  				Expect(err).To(MatchError(errors.New("fake-error")))
    40  				Expect(fakePlatform.ValidatePathCallCount()).To(Equal(1))
    41  				Expect(fakePlatform.ValidatePathArgsForCall(0)).To(Equal("cc-path"))
    42  			})
    43  
    44  			Context("when the platform is unknown", func() {
    45  				It("returns an error", func() {
    46  					err := registry.ValidateSpec("badType", "")
    47  					Expect(err).To(MatchError("Unknown chaincodeType: badType"))
    48  				})
    49  			})
    50  		})
    51  
    52  		Describe("ValidateDeploymentSpec", func() {
    53  			It("returns the result of the underlying platform", func() {
    54  				fakePlatform.ValidateCodePackageReturns(errors.New("fake-error"))
    55  				err := registry.ValidateDeploymentSpec("fakeType", []byte("code-package"))
    56  				Expect(err).To(MatchError(errors.New("fake-error")))
    57  				Expect(fakePlatform.ValidateCodePackageCallCount()).To(Equal(1))
    58  				Expect(fakePlatform.ValidateCodePackageArgsForCall(0)).To(Equal([]byte("code-package")))
    59  			})
    60  
    61  			Context("when the code package is empty", func() {
    62  				It("does nothing", func() {
    63  					err := registry.ValidateDeploymentSpec("fakeType", []byte{})
    64  					Expect(err).NotTo(HaveOccurred())
    65  					Expect(fakePlatform.ValidateCodePackageCallCount()).To(Equal(0))
    66  
    67  					err = registry.ValidateDeploymentSpec("fakeType", nil)
    68  					Expect(err).NotTo(HaveOccurred())
    69  					Expect(fakePlatform.ValidateCodePackageCallCount()).To(Equal(0))
    70  				})
    71  			})
    72  
    73  			Context("when the platform is unknown", func() {
    74  				It("returns an error", func() {
    75  					err := registry.ValidateDeploymentSpec("badType", nil)
    76  					Expect(err).To(MatchError("Unknown chaincodeType: badType"))
    77  				})
    78  			})
    79  		})
    80  
    81  		Describe("GetDeploymentPayload", func() {
    82  			It("returns the result of the underlying platform", func() {
    83  				fakePlatform.GetDeploymentPayloadReturns([]byte("payload"), errors.New("fake-error"))
    84  				payload, err := registry.GetDeploymentPayload("fakeType", "cc-path")
    85  				Expect(payload).To(Equal([]byte("payload")))
    86  				Expect(err).To(MatchError(errors.New("fake-error")))
    87  				Expect(fakePlatform.GetDeploymentPayloadCallCount()).To(Equal(1))
    88  				Expect(fakePlatform.GetDeploymentPayloadArgsForCall(0)).To(Equal("cc-path"))
    89  			})
    90  
    91  			Context("when the platform is unknown", func() {
    92  				It("returns an error", func() {
    93  					payload, err := registry.GetDeploymentPayload("badType", "")
    94  					Expect(payload).To(BeNil())
    95  					Expect(err).To(MatchError("Unknown chaincodeType: badType"))
    96  				})
    97  			})
    98  		})
    99  	})
   100  
   101  	Describe("NewRegistry", func() {
   102  		It("initializes with the known platform types and util writer", func() {
   103  			fakePlatformFoo := &mock.Platform{}
   104  			fakePlatformFoo.NameReturns("foo")
   105  			fakePlatformBar := &mock.Platform{}
   106  			fakePlatformBar.NameReturns("bar")
   107  
   108  			registry = packaging.NewRegistry(fakePlatformFoo, fakePlatformBar)
   109  
   110  			Expect(registry.Platforms).To(Equal(map[string]packaging.Platform{
   111  				"foo": fakePlatformFoo,
   112  				"bar": fakePlatformBar,
   113  			}))
   114  		})
   115  
   116  		Context("when two platforms report the same name", func() {
   117  			It("panics", func() {
   118  				fakePlatformFoo1 := &mock.Platform{}
   119  				fakePlatformFoo1.NameReturns("foo")
   120  				fakePlatformFoo2 := &mock.Platform{}
   121  				fakePlatformFoo2.NameReturns("foo")
   122  				Expect(func() { packaging.NewRegistry(fakePlatformFoo1, fakePlatformFoo2) }).To(Panic())
   123  			})
   124  		})
   125  	})
   126  })