github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/actor/pushaction/application_test.go (about)

     1  package pushaction_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/pushaction"
     8  	"code.cloudfoundry.org/cli/actor/pushaction/pushactionfakes"
     9  	"code.cloudfoundry.org/cli/actor/v2action"
    10  	"code.cloudfoundry.org/cli/types"
    11  
    12  	. "github.com/onsi/ginkgo"
    13  	. "github.com/onsi/gomega"
    14  )
    15  
    16  var _ = Describe("Applications", func() {
    17  	var (
    18  		actor       *Actor
    19  		fakeV2Actor *pushactionfakes.FakeV2Actor
    20  	)
    21  
    22  	BeforeEach(func() {
    23  		fakeV2Actor = new(pushactionfakes.FakeV2Actor)
    24  		actor = NewActor(fakeV2Actor, nil)
    25  	})
    26  
    27  	Describe("CreateOrUpdateApp", func() {
    28  		var (
    29  			config ApplicationConfig
    30  
    31  			returnedConfig ApplicationConfig
    32  			event          Event
    33  			warnings       Warnings
    34  			executeErr     error
    35  		)
    36  
    37  		BeforeEach(func() {
    38  			config = ApplicationConfig{
    39  				DesiredApplication: Application{
    40  					Application: v2action.Application{
    41  						Name:      "some-app-name",
    42  						SpaceGUID: "some-space-guid",
    43  					},
    44  				},
    45  				Path: "some-path",
    46  			}
    47  		})
    48  
    49  		JustBeforeEach(func() {
    50  			returnedConfig, event, warnings, executeErr = actor.CreateOrUpdateApp(config)
    51  		})
    52  
    53  		Context("when the app exists", func() {
    54  			BeforeEach(func() {
    55  				config.CurrentApplication = Application{
    56  					Application: v2action.Application{
    57  						Name:      "some-app-name",
    58  						GUID:      "some-app-guid",
    59  						SpaceGUID: "some-space-guid",
    60  						Buildpack: types.FilteredString{Value: "java", IsSet: true},
    61  					},
    62  				}
    63  				config.DesiredApplication = Application{
    64  					Application: v2action.Application{
    65  						Name:      "some-app-name",
    66  						GUID:      "some-app-guid",
    67  						SpaceGUID: "some-space-guid",
    68  						Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
    69  					},
    70  				}
    71  			})
    72  
    73  			Context("when the update is successful", func() {
    74  				BeforeEach(func() {
    75  					fakeV2Actor.UpdateApplicationReturns(v2action.Application{
    76  						Name:      "some-app-name",
    77  						GUID:      "some-app-guid",
    78  						SpaceGUID: "some-space-guid",
    79  						Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
    80  					}, v2action.Warnings{"update-warning"}, nil)
    81  				})
    82  
    83  				It("updates the application", func() {
    84  					Expect(executeErr).ToNot(HaveOccurred())
    85  					Expect(warnings).To(ConsistOf("update-warning"))
    86  					Expect(event).To(Equal(UpdatedApplication))
    87  
    88  					Expect(returnedConfig.DesiredApplication).To(Equal(Application{
    89  						Application: v2action.Application{
    90  							Name:      "some-app-name",
    91  							GUID:      "some-app-guid",
    92  							SpaceGUID: "some-space-guid",
    93  							Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
    94  						}}))
    95  					Expect(returnedConfig.CurrentApplication).To(Equal(returnedConfig.DesiredApplication))
    96  
    97  					Expect(fakeV2Actor.UpdateApplicationCallCount()).To(Equal(1))
    98  					Expect(fakeV2Actor.UpdateApplicationArgsForCall(0)).To(Equal(v2action.Application{
    99  						Name:      "some-app-name",
   100  						GUID:      "some-app-guid",
   101  						SpaceGUID: "some-space-guid",
   102  						Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
   103  					}))
   104  				})
   105  
   106  				Context("when the stack guid is not being updated", func() {
   107  					BeforeEach(func() {
   108  						config.CurrentApplication.StackGUID = "some-stack-guid"
   109  						config.DesiredApplication.StackGUID = "some-stack-guid"
   110  					})
   111  
   112  					It("does not send the stack guid on update", func() {
   113  						Expect(executeErr).ToNot(HaveOccurred())
   114  
   115  						Expect(fakeV2Actor.UpdateApplicationCallCount()).To(Equal(1))
   116  						Expect(fakeV2Actor.UpdateApplicationArgsForCall(0)).To(Equal(v2action.Application{
   117  							Name:      "some-app-name",
   118  							GUID:      "some-app-guid",
   119  							SpaceGUID: "some-space-guid",
   120  							Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
   121  						}))
   122  					})
   123  				})
   124  			})
   125  
   126  			Context("when the state is not being updated", func() {
   127  				BeforeEach(func() {
   128  					config.CurrentApplication.State = "some-state"
   129  					config.DesiredApplication.State = "some-state"
   130  				})
   131  
   132  				It("does not send the state on update", func() {
   133  					Expect(executeErr).ToNot(HaveOccurred())
   134  
   135  					Expect(fakeV2Actor.UpdateApplicationCallCount()).To(Equal(1))
   136  					Expect(fakeV2Actor.UpdateApplicationArgsForCall(0)).To(Equal(v2action.Application{
   137  						Name:      "some-app-name",
   138  						GUID:      "some-app-guid",
   139  						SpaceGUID: "some-space-guid",
   140  						Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
   141  					}))
   142  				})
   143  			})
   144  
   145  			Context("when the update errors", func() {
   146  				var expectedErr error
   147  				BeforeEach(func() {
   148  					expectedErr = errors.New("oh my")
   149  					fakeV2Actor.UpdateApplicationReturns(v2action.Application{}, v2action.Warnings{"update-warning"}, expectedErr)
   150  				})
   151  
   152  				It("returns warnings and error and stops", func() {
   153  					Expect(executeErr).To(MatchError(expectedErr))
   154  					Expect(warnings).To(ConsistOf("update-warning"))
   155  				})
   156  			})
   157  		})
   158  
   159  		Context("when the app does not exist", func() {
   160  			Context("when the creation is successful", func() {
   161  				BeforeEach(func() {
   162  					fakeV2Actor.CreateApplicationReturns(v2action.Application{
   163  						Name:      "some-app-name",
   164  						GUID:      "some-app-guid",
   165  						SpaceGUID: "some-space-guid",
   166  						Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
   167  					}, v2action.Warnings{"create-warning"}, nil)
   168  				})
   169  
   170  				It("creates the application", func() {
   171  					Expect(executeErr).ToNot(HaveOccurred())
   172  					Expect(warnings).To(ConsistOf("create-warning"))
   173  					Expect(event).To(Equal(CreatedApplication))
   174  
   175  					Expect(returnedConfig.DesiredApplication).To(Equal(Application{
   176  						Application: v2action.Application{
   177  							Name:      "some-app-name",
   178  							GUID:      "some-app-guid",
   179  							SpaceGUID: "some-space-guid",
   180  							Buildpack: types.FilteredString{Value: "ruby", IsSet: true},
   181  						}}))
   182  					Expect(returnedConfig.CurrentApplication).To(Equal(returnedConfig.DesiredApplication))
   183  
   184  					Expect(fakeV2Actor.CreateApplicationCallCount()).To(Equal(1))
   185  					Expect(fakeV2Actor.CreateApplicationArgsForCall(0)).To(Equal(v2action.Application{
   186  						Name:      "some-app-name",
   187  						SpaceGUID: "some-space-guid",
   188  					}))
   189  				})
   190  			})
   191  
   192  			Context("when the creation errors", func() {
   193  				var expectedErr error
   194  
   195  				BeforeEach(func() {
   196  					expectedErr = errors.New("oh my")
   197  					fakeV2Actor.CreateApplicationReturns(v2action.Application{}, v2action.Warnings{"create-warning"}, expectedErr)
   198  				})
   199  
   200  				It("sends the warnings and errors and returns true", func() {
   201  					Expect(executeErr).To(MatchError(expectedErr))
   202  					Expect(warnings).To(ConsistOf("create-warning"))
   203  				})
   204  			})
   205  		})
   206  	})
   207  
   208  	Describe("FindOrReturnPartialApp", func() {
   209  		var expectedStack v2action.Stack
   210  		var expectedApp v2action.Application
   211  
   212  		Context("when the app exists", func() {
   213  			Context("when retrieving the stack is successful", func() {
   214  				BeforeEach(func() {
   215  					expectedStack = v2action.Stack{
   216  						Name: "some-stack",
   217  						GUID: "some-stack-guid",
   218  					}
   219  					fakeV2Actor.GetStackReturns(expectedStack, v2action.Warnings{"stack-warnings"}, nil)
   220  
   221  					expectedApp = v2action.Application{
   222  						GUID:      "some-app-guid",
   223  						Name:      "some-app",
   224  						StackGUID: expectedStack.GUID,
   225  					}
   226  					fakeV2Actor.GetApplicationByNameAndSpaceReturns(expectedApp, v2action.Warnings{"app-warnings"}, nil)
   227  				})
   228  
   229  				It("fills in the stack", func() {
   230  					found, app, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid")
   231  					Expect(err).ToNot(HaveOccurred())
   232  					Expect(warnings).To(ConsistOf("app-warnings", "stack-warnings"))
   233  					Expect(found).To(BeTrue())
   234  					Expect(app).To(Equal(Application{
   235  						Application: expectedApp,
   236  						Stack:       expectedStack,
   237  					}))
   238  				})
   239  			})
   240  
   241  			Context("when retrieving the stack errors", func() {
   242  				var expectedErr error
   243  
   244  				BeforeEach(func() {
   245  					expectedErr = errors.New("stack stack stack em up")
   246  					fakeV2Actor.GetStackReturns(v2action.Stack{}, v2action.Warnings{"stack-warnings"}, expectedErr)
   247  
   248  					expectedApp = v2action.Application{
   249  						GUID:      "some-app-guid",
   250  						Name:      "some-app",
   251  						StackGUID: "some-stack-guid",
   252  					}
   253  					fakeV2Actor.GetApplicationByNameAndSpaceReturns(expectedApp, v2action.Warnings{"app-warnings"}, nil)
   254  				})
   255  
   256  				It("returns error and warnings", func() {
   257  					found, _, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid")
   258  					Expect(err).To(MatchError(expectedErr))
   259  					Expect(warnings).To(ConsistOf("app-warnings", "stack-warnings"))
   260  					Expect(found).To(BeFalse())
   261  				})
   262  			})
   263  		})
   264  
   265  		Context("when the app does not exist", func() {
   266  			BeforeEach(func() {
   267  				fakeV2Actor.GetApplicationByNameAndSpaceReturns(v2action.Application{}, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, actionerror.ApplicationNotFoundError{})
   268  			})
   269  
   270  			It("returns a partial app and warnings", func() {
   271  				found, app, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid")
   272  				Expect(err).ToNot(HaveOccurred())
   273  				Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2"))
   274  				Expect(found).To(BeFalse())
   275  				Expect(app).To(Equal(Application{
   276  					Application: v2action.Application{
   277  						Name:      "some-app",
   278  						SpaceGUID: "some-space-guid",
   279  					},
   280  				}))
   281  			})
   282  		})
   283  
   284  		Context("when retrieving the app errors", func() {
   285  			var expectedErr error
   286  			BeforeEach(func() {
   287  				expectedErr = errors.New("dios mio")
   288  				fakeV2Actor.GetApplicationByNameAndSpaceReturns(v2action.Application{}, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, expectedErr)
   289  			})
   290  
   291  			It("returns a errors and warnings", func() {
   292  				found, _, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid")
   293  				Expect(err).To(MatchError(expectedErr))
   294  				Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2"))
   295  				Expect(found).To(BeFalse())
   296  			})
   297  		})
   298  	})
   299  })