github.com/arunkumar7540/cli@v6.45.0+incompatible/actor/v7pushaction/prepare_space_test.go (about)

     1  package v7pushaction_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  
    10  	. "code.cloudfoundry.org/cli/actor/v7pushaction"
    11  	"code.cloudfoundry.org/cli/actor/v7pushaction/v7pushactionfakes"
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  	log "github.com/sirupsen/logrus"
    15  )
    16  
    17  func PrepareSpaceStreamsDrainedAndClosed(
    18  	pushPlansStream <-chan []PushPlan,
    19  	eventStream <-chan Event,
    20  	warningsStream <-chan Warnings,
    21  	errorStream <-chan error,
    22  ) bool {
    23  	var configStreamClosed, eventStreamClosed, warningsStreamClosed, errorStreamClosed bool
    24  	for {
    25  		select {
    26  		case _, ok := <-pushPlansStream:
    27  			if !ok {
    28  				configStreamClosed = true
    29  			}
    30  		case _, ok := <-eventStream:
    31  			if !ok {
    32  				eventStreamClosed = true
    33  			}
    34  		case _, ok := <-warningsStream:
    35  			if !ok {
    36  				warningsStreamClosed = true
    37  			}
    38  		case _, ok := <-errorStream:
    39  			if !ok {
    40  				errorStreamClosed = true
    41  			}
    42  		}
    43  		if configStreamClosed && eventStreamClosed && warningsStreamClosed && errorStreamClosed {
    44  			break
    45  		}
    46  	}
    47  	return true
    48  }
    49  
    50  func getPrepareNextEvent(c <-chan []PushPlan, e <-chan Event, w <-chan Warnings) func() Event {
    51  	timeOut := time.Tick(500 * time.Millisecond)
    52  
    53  	return func() Event {
    54  		for {
    55  			select {
    56  			case <-c:
    57  			case event, ok := <-e:
    58  				if ok {
    59  					log.WithField("event", event).Debug("getNextEvent")
    60  					return event
    61  				}
    62  				return ""
    63  			case <-w:
    64  			case <-timeOut:
    65  				return ""
    66  			}
    67  		}
    68  	}
    69  }
    70  
    71  var _ = Describe("PrepareSpace", func() {
    72  	var (
    73  		actor       *Actor
    74  		fakeV7Actor *v7pushactionfakes.FakeV7Actor
    75  
    76  		pushPlans          []PushPlan
    77  		fakeManifestParser *v7pushactionfakes.FakeManifestParser
    78  
    79  		spaceGUID string
    80  
    81  		pushPlansStream <-chan []PushPlan
    82  		eventStream     <-chan Event
    83  		warningsStream  <-chan Warnings
    84  		errorStream     <-chan error
    85  	)
    86  
    87  	BeforeEach(func() {
    88  		actor, _, fakeV7Actor, _ = getTestPushActor()
    89  
    90  		spaceGUID = "space"
    91  
    92  		fakeManifestParser = new(v7pushactionfakes.FakeManifestParser)
    93  	})
    94  
    95  	AfterEach(func() {
    96  		Eventually(PrepareSpaceStreamsDrainedAndClosed(pushPlansStream, eventStream, warningsStream, errorStream)).Should(BeTrue())
    97  	})
    98  
    99  	JustBeforeEach(func() {
   100  		pushPlansStream, eventStream, warningsStream, errorStream = actor.PrepareSpace(pushPlans, fakeManifestParser)
   101  	})
   102  
   103  	When("there is a single push state and no manifest", func() {
   104  		var appName = "app-name"
   105  
   106  		When("Creating the app succeeds", func() {
   107  			BeforeEach(func() {
   108  				pushPlans = []PushPlan{{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName}}}
   109  				fakeV7Actor.CreateApplicationInSpaceReturns(
   110  					v7action.Application{Name: appName},
   111  					v7action.Warnings{"create-app-warning"},
   112  					nil,
   113  				)
   114  			})
   115  
   116  			It("creates the app using the API", func() {
   117  				Consistently(fakeV7Actor.SetSpaceManifestCallCount).Should(Equal(0))
   118  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(CreatingApplication))
   119  				Eventually(fakeV7Actor.CreateApplicationInSpaceCallCount).Should(Equal(1))
   120  				actualApp, actualSpaceGUID := fakeV7Actor.CreateApplicationInSpaceArgsForCall(0)
   121  				Expect(actualApp).To(Equal(v7action.Application{Name: appName}))
   122  				Expect(actualSpaceGUID).To(Equal(spaceGUID))
   123  				Eventually(warningsStream).Should(Receive(Equal(Warnings{"create-app-warning"})))
   124  				Eventually(pushPlansStream).Should(Receive(ConsistOf(PushPlan{
   125  					SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName},
   126  				})))
   127  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(CreatedApplication))
   128  			})
   129  		})
   130  
   131  		When("the app already exists", func() {
   132  			BeforeEach(func() {
   133  				pushPlans = []PushPlan{{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName}}}
   134  				fakeV7Actor.CreateApplicationInSpaceReturns(
   135  					v7action.Application{},
   136  					v7action.Warnings{"create-app-warning"},
   137  					actionerror.ApplicationAlreadyExistsError{},
   138  				)
   139  			})
   140  			It("Sends already exists events", func() {
   141  				Consistently(fakeV7Actor.SetSpaceManifestCallCount).Should(Equal(0))
   142  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(SkippingApplicationCreation))
   143  				Eventually(fakeV7Actor.CreateApplicationInSpaceCallCount).Should(Equal(1))
   144  				actualApp, actualSpaceGUID := fakeV7Actor.CreateApplicationInSpaceArgsForCall(0)
   145  				Expect(actualApp).To(Equal(v7action.Application{Name: appName}))
   146  				Expect(actualSpaceGUID).To(Equal(spaceGUID))
   147  				Eventually(warningsStream).Should(Receive(Equal(Warnings{"create-app-warning"})))
   148  				Eventually(pushPlansStream).Should(Receive(ConsistOf(PushPlan{
   149  					SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName},
   150  				})))
   151  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(ApplicationAlreadyExists))
   152  			})
   153  		})
   154  
   155  		When("creating the app fails", func() {
   156  			BeforeEach(func() {
   157  				pushPlans = []PushPlan{{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName}}}
   158  				fakeV7Actor.CreateApplicationInSpaceReturns(
   159  					v7action.Application{},
   160  					v7action.Warnings{"create-app-warning"},
   161  					errors.New("some-create-error"),
   162  				)
   163  			})
   164  
   165  			It("Returns the error", func() {
   166  				Consistently(fakeV7Actor.SetSpaceManifestCallCount).Should(Equal(0))
   167  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(CreatingApplication))
   168  				Eventually(fakeV7Actor.CreateApplicationInSpaceCallCount).Should(Equal(1))
   169  				actualApp, actualSpaceGuid := fakeV7Actor.CreateApplicationInSpaceArgsForCall(0)
   170  				Expect(actualApp.Name).To(Equal(appName))
   171  				Expect(actualSpaceGuid).To(Equal(spaceGUID))
   172  				Eventually(warningsStream).Should(Receive(Equal(Warnings{"create-app-warning"})))
   173  				Eventually(errorStream).Should(Receive(Equal(errors.New("some-create-error"))))
   174  				Consistently(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).ShouldNot(Equal(ApplicationAlreadyExists))
   175  			})
   176  		})
   177  	})
   178  
   179  	When("There is a a manifest", func() {
   180  		var (
   181  			manifest = []byte("app manifest")
   182  			appName1 = "app-name1"
   183  			appName2 = "app-name2"
   184  		)
   185  		When("applying the manifest fails", func() {
   186  			BeforeEach(func() {
   187  				pushPlans = []PushPlan{{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName1}}}
   188  				fakeManifestParser.ContainsManifestReturns(true)
   189  				fakeManifestParser.RawAppManifestReturns(manifest, nil)
   190  				fakeV7Actor.SetSpaceManifestReturns(v7action.Warnings{"apply-manifest-warnings"}, errors.New("some-error"))
   191  			})
   192  
   193  			It("returns the error and exits", func() {
   194  				Consistently(fakeV7Actor.CreateApplicationInSpaceCallCount).Should(Equal(0))
   195  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(ApplyManifest))
   196  				Eventually(fakeV7Actor.SetSpaceManifestCallCount).Should(Equal(1))
   197  				actualSpaceGuid, actualManifest, _ := fakeV7Actor.SetSpaceManifestArgsForCall(0)
   198  				Expect(actualSpaceGuid).To(Equal(spaceGUID))
   199  				Expect(actualManifest).To(Equal(manifest))
   200  				Eventually(warningsStream).Should(Receive(Equal(Warnings{"apply-manifest-warnings"})))
   201  				Eventually(errorStream).Should(Receive(Equal(errors.New("some-error"))))
   202  				Consistently(pushPlansStream).ShouldNot(Receive(ConsistOf(PushPlan{
   203  					SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName1},
   204  				})))
   205  				Consistently(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).ShouldNot(Equal(ApplyManifestComplete))
   206  			})
   207  		})
   208  
   209  		When("There is a single pushPlan", func() {
   210  
   211  			BeforeEach(func() {
   212  				pushPlans = []PushPlan{{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName1}, NoRouteFlag: true}}
   213  				fakeManifestParser.ContainsManifestReturns(true)
   214  				fakeManifestParser.RawAppManifestReturns(manifest, nil)
   215  				fakeV7Actor.SetSpaceManifestReturns(v7action.Warnings{"apply-manifest-warnings"}, nil)
   216  			})
   217  
   218  			It("applies the app specific manifest", func() {
   219  				Consistently(fakeV7Actor.CreateApplicationInSpaceCallCount).Should(Equal(0))
   220  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(ApplyManifest))
   221  				Eventually(fakeManifestParser.RawAppManifestCallCount).Should(Equal(1))
   222  				actualAppName := fakeManifestParser.RawAppManifestArgsForCall(0)
   223  				Expect(actualAppName).To(Equal(appName1))
   224  				Eventually(fakeV7Actor.SetSpaceManifestCallCount).Should(Equal(1))
   225  				actualSpaceGUID, actualManifest, actualNoRoute := fakeV7Actor.SetSpaceManifestArgsForCall(0)
   226  				Expect(actualManifest).To(Equal(manifest))
   227  				Expect(actualSpaceGUID).To(Equal(spaceGUID))
   228  				Expect(actualNoRoute).To(BeTrue())
   229  				Eventually(warningsStream).Should(Receive(Equal(Warnings{"apply-manifest-warnings"})))
   230  				Eventually(pushPlansStream).Should(Receive(ConsistOf(PushPlan{
   231  					SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName1}, NoRouteFlag: true,
   232  				})))
   233  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(ApplyManifestComplete))
   234  			})
   235  		})
   236  
   237  		When("There are multiple push states", func() {
   238  			BeforeEach(func() {
   239  				pushPlans = []PushPlan{
   240  					{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName1}},
   241  					{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName2}},
   242  				}
   243  				fakeManifestParser.ContainsManifestReturns(true)
   244  				fakeManifestParser.FullRawManifestReturns(manifest)
   245  				fakeV7Actor.SetSpaceManifestReturns(v7action.Warnings{"apply-manifest-warnings"}, nil)
   246  			})
   247  
   248  			It("Applies the entire manifest", func() {
   249  				Consistently(fakeV7Actor.CreateApplicationInSpaceCallCount).Should(Equal(0))
   250  				Consistently(fakeManifestParser.RawAppManifestCallCount).Should(Equal(0))
   251  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(ApplyManifest))
   252  				Eventually(fakeManifestParser.FullRawManifestCallCount).Should(Equal(1))
   253  				Eventually(fakeV7Actor.SetSpaceManifestCallCount).Should(Equal(1))
   254  				actualSpaceGUID, actualManifest, actualNoRoute := fakeV7Actor.SetSpaceManifestArgsForCall(0)
   255  				Expect(actualManifest).To(Equal(manifest))
   256  				Expect(actualSpaceGUID).To(Equal(spaceGUID))
   257  				Expect(actualNoRoute).To(BeFalse())
   258  				Eventually(warningsStream).Should(Receive(Equal(Warnings{"apply-manifest-warnings"})))
   259  				Eventually(pushPlansStream).Should(Receive(ConsistOf(
   260  					PushPlan{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName1}},
   261  					PushPlan{SpaceGUID: spaceGUID, Application: v7action.Application{Name: appName2}},
   262  				)))
   263  				Eventually(getPrepareNextEvent(pushPlansStream, eventStream, warningsStream)).Should(Equal(ApplyManifestComplete))
   264  			})
   265  		})
   266  	})
   267  })