github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/actor/v7pushaction/actualize_test.go (about)

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