github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/core/container/build_registry_test.go (about)

     1  /*
     2  Copyright hechain. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package container_test
     8  
     9  import (
    10  	"fmt"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  
    15  	"github.com/hechain20/hechain/core/container"
    16  )
    17  
    18  var _ = Describe("BuildRegistry", func() {
    19  	var br *container.BuildRegistry
    20  
    21  	BeforeEach(func() {
    22  		br = &container.BuildRegistry{}
    23  	})
    24  
    25  	It("returns a new build status when one does not exist", func() {
    26  		bs, ok := br.BuildStatus("ccid")
    27  		Expect(ok).To(BeFalse())
    28  		Expect(bs).NotTo(BeNil())
    29  		Expect(bs.Done()).NotTo(BeClosed())
    30  	})
    31  
    32  	When("the ccid is already building", func() {
    33  		var initialBS *container.BuildStatus
    34  
    35  		BeforeEach(func() {
    36  			var ok bool
    37  			initialBS, ok = br.BuildStatus("ccid")
    38  			Expect(ok).To(BeFalse())
    39  		})
    40  
    41  		It("returns the existing build status", func() {
    42  			newBS, ok := br.BuildStatus("ccid")
    43  			Expect(ok).To(BeTrue())
    44  			Expect(newBS).To(Equal(initialBS))
    45  		})
    46  	})
    47  
    48  	When("a previous build status had an error", func() {
    49  		BeforeEach(func() {
    50  			bs, ok := br.BuildStatus("ccid")
    51  			Expect(ok).To(BeFalse())
    52  			Expect(bs).NotTo(BeNil())
    53  			bs.Notify(fmt.Errorf("fake-error"))
    54  			Expect(bs.Done()).To(BeClosed())
    55  			Expect(bs.Err()).To(MatchError(fmt.Errorf("fake-error")))
    56  		})
    57  
    58  		It("can be reset", func() {
    59  			bs := br.ResetBuildStatus("ccid")
    60  			Expect(bs).NotTo(BeNil())
    61  			Expect(bs.Done()).NotTo(BeClosed())
    62  			Expect(bs.Err()).To(BeNil())
    63  		})
    64  	})
    65  })
    66  
    67  var _ = Describe("BuildStatus", func() {
    68  	var bs *container.BuildStatus
    69  
    70  	BeforeEach(func() {
    71  		bs = container.NewBuildStatus()
    72  	})
    73  
    74  	It("has a blocking done channel", func() {
    75  		Expect(bs.Done()).NotTo(BeClosed())
    76  	})
    77  
    78  	When("notify is called with nil", func() {
    79  		BeforeEach(func() {
    80  			bs.Notify(nil)
    81  		})
    82  
    83  		It("closes the blocking done channel", func() {
    84  			Expect(bs.Done()).To(BeClosed())
    85  		})
    86  
    87  		It("leaves err set to nil", func() {
    88  			Expect(bs.Err()).To(BeNil())
    89  		})
    90  	})
    91  
    92  	When("notify is called with an error", func() {
    93  		BeforeEach(func() {
    94  			bs.Notify(fmt.Errorf("fake-error"))
    95  		})
    96  
    97  		It("closes the blocking done channel", func() {
    98  			Expect(bs.Done()).To(BeClosed())
    99  		})
   100  
   101  		It("sets err to the error", func() {
   102  			Expect(bs.Err()).To(MatchError(fmt.Errorf("fake-error")))
   103  		})
   104  	})
   105  })