github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7pushaction/actualize_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/actor/sharedaction"
     8  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
     9  	"code.cloudfoundry.org/cli/actor/v7pushaction/v7pushactionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    11  	"code.cloudfoundry.org/cli/resources"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  func streamsDrainedAndClosed(eventStream <-chan *PushEvent) bool {
    17  	for range eventStream {
    18  	}
    19  	return true
    20  }
    21  
    22  // TODO: for refactor: We can use the following style of code to validate that
    23  // each event is received in a specific order
    24  
    25  // Expect(nextEvent()).Should(Equal(SettingUpApplication))
    26  // Expect(nextEvent()).Should(Equal(CreatingApplication))
    27  // Expect(nextEvent()).Should(Equal(...))
    28  // Expect(nextEvent()).Should(Equal(...))
    29  // Expect(nextEvent()).Should(Equal(...))
    30  
    31  func buildV3Resource(name string) sharedaction.V3Resource {
    32  	return sharedaction.V3Resource{
    33  		FilePath:    name,
    34  		Checksum:    ccv3.Checksum{Value: fmt.Sprintf("checksum-%s", name)},
    35  		SizeInBytes: 6,
    36  	}
    37  }
    38  
    39  var _ = Describe("Actualize", func() {
    40  	var (
    41  		actor *Actor
    42  
    43  		plan            PushPlan
    44  		fakeProgressBar *v7pushactionfakes.FakeProgressBar
    45  
    46  		successfulChangeAppFuncCallCount int
    47  		warningChangeAppFuncCallCount    int
    48  		errorChangeAppFuncCallCount      int
    49  
    50  		eventStream <-chan *PushEvent
    51  
    52  		expectedPlan PushPlan
    53  	)
    54  
    55  	successfulChangeAppFunc := func(pushPlan PushPlan, eStream chan<- *PushEvent, progressBar ProgressBar) (PushPlan, Warnings, error) {
    56  		defer GinkgoRecover()
    57  
    58  		Expect(pushPlan).To(Equal(plan))
    59  		Expect(eStream).ToNot(BeNil())
    60  		Expect(progressBar).To(Equal(fakeProgressBar))
    61  
    62  		pushPlan.Application.GUID = "successful-app-guid"
    63  		successfulChangeAppFuncCallCount++
    64  		return pushPlan, nil, nil
    65  	}
    66  
    67  	warningChangeAppFunc := func(pushPlan PushPlan, eventStream chan<- *PushEvent, progressBar ProgressBar) (PushPlan, Warnings, error) {
    68  		pushPlan.Application.GUID = "warning-app-guid"
    69  		warningChangeAppFuncCallCount++
    70  		return pushPlan, Warnings{"warning-1", "warning-2"}, nil
    71  	}
    72  
    73  	errorChangeAppFunc := func(pushPlan PushPlan, eventStream chan<- *PushEvent, progressBar ProgressBar) (PushPlan, Warnings, error) {
    74  		pushPlan.Application.GUID = "error-app-guid"
    75  		errorChangeAppFuncCallCount++
    76  		return pushPlan, nil, errors.New("some error")
    77  	}
    78  
    79  	BeforeEach(func() {
    80  		actor, _, _ = getTestPushActor()
    81  
    82  		successfulChangeAppFuncCallCount = 0
    83  		warningChangeAppFuncCallCount = 0
    84  		errorChangeAppFuncCallCount = 0
    85  
    86  		fakeProgressBar = new(v7pushactionfakes.FakeProgressBar)
    87  		plan = PushPlan{
    88  			Application: resources.Application{
    89  				Name: "some-app",
    90  				GUID: "some-app-guid",
    91  			},
    92  			SpaceGUID: "some-space-guid",
    93  		}
    94  
    95  		expectedPlan = plan
    96  	})
    97  
    98  	AfterEach(func() {
    99  		Eventually(streamsDrainedAndClosed(eventStream)).Should(BeTrue())
   100  	})
   101  
   102  	JustBeforeEach(func() {
   103  		eventStream = actor.Actualize(plan, fakeProgressBar)
   104  	})
   105  
   106  	Describe("ChangeApplicationSequence", func() {
   107  		When("none of the ChangeApplicationSequence return errors", func() {
   108  			BeforeEach(func() {
   109  				actor.ChangeApplicationSequence = func(plan PushPlan) []ChangeApplicationFunc {
   110  					return []ChangeApplicationFunc{
   111  						successfulChangeAppFunc,
   112  						warningChangeAppFunc,
   113  					}
   114  				}
   115  			})
   116  
   117  			It("iterates over the actor's ChangeApplicationSequence", func() {
   118  				expectedPlan.Application.GUID = "successful-app-guid"
   119  				Eventually(eventStream).Should(Receive(Equal(&PushEvent{Plan: expectedPlan})))
   120  
   121  				expectedPlan.Application.GUID = "warning-app-guid"
   122  				Eventually(eventStream).Should(Receive(Equal(&PushEvent{Plan: expectedPlan, Warnings: Warnings{"warning-1", "warning-2"}})))
   123  
   124  				Expect(successfulChangeAppFuncCallCount).To(Equal(1))
   125  				Expect(warningChangeAppFuncCallCount).To(Equal(1))
   126  				Expect(errorChangeAppFuncCallCount).To(Equal(0))
   127  			})
   128  		})
   129  
   130  		When("the ChangeApplicationSequence return errors", func() {
   131  			BeforeEach(func() {
   132  				actor.ChangeApplicationSequence = func(plan PushPlan) []ChangeApplicationFunc {
   133  					return []ChangeApplicationFunc{
   134  						errorChangeAppFunc,
   135  						successfulChangeAppFunc,
   136  					}
   137  				}
   138  			})
   139  
   140  			It("iterates over the actor's ChangeApplicationSequence", func() {
   141  				expectedPlan.Application.GUID = "error-app-guid"
   142  				Eventually(eventStream).Should(Receive(Equal(&PushEvent{Plan: expectedPlan, Err: errors.New("some error")})))
   143  
   144  				Expect(successfulChangeAppFuncCallCount).To(Equal(0))
   145  				Expect(warningChangeAppFuncCallCount).To(Equal(0))
   146  				Expect(errorChangeAppFuncCallCount).To(Equal(1))
   147  			})
   148  		})
   149  	})
   150  })