github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/actor/pushaction/application_test.go (about) 1 package pushaction_test 2 3 import ( 4 "errors" 5 6 "github.com/liamawhite/cli-with-i18n/actor/actionerror" 7 . "github.com/liamawhite/cli-with-i18n/actor/pushaction" 8 "github.com/liamawhite/cli-with-i18n/actor/pushaction/pushactionfakes" 9 "github.com/liamawhite/cli-with-i18n/actor/v2action" 10 "github.com/liamawhite/cli-with-i18n/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 update errors", func() { 127 var expectedErr error 128 BeforeEach(func() { 129 expectedErr = errors.New("oh my") 130 fakeV2Actor.UpdateApplicationReturns(v2action.Application{}, v2action.Warnings{"update-warning"}, expectedErr) 131 }) 132 133 It("returns warnings and error and stops", func() { 134 Expect(executeErr).To(MatchError(expectedErr)) 135 Expect(warnings).To(ConsistOf("update-warning")) 136 }) 137 }) 138 }) 139 140 Context("when the app does not exist", func() { 141 Context("when the creation is successful", func() { 142 BeforeEach(func() { 143 fakeV2Actor.CreateApplicationReturns(v2action.Application{ 144 Name: "some-app-name", 145 GUID: "some-app-guid", 146 SpaceGUID: "some-space-guid", 147 Buildpack: types.FilteredString{Value: "ruby", IsSet: true}, 148 }, v2action.Warnings{"create-warning"}, nil) 149 }) 150 151 It("creates the application", func() { 152 Expect(executeErr).ToNot(HaveOccurred()) 153 Expect(warnings).To(ConsistOf("create-warning")) 154 Expect(event).To(Equal(CreatedApplication)) 155 156 Expect(returnedConfig.DesiredApplication).To(Equal(Application{ 157 Application: v2action.Application{ 158 Name: "some-app-name", 159 GUID: "some-app-guid", 160 SpaceGUID: "some-space-guid", 161 Buildpack: types.FilteredString{Value: "ruby", IsSet: true}, 162 }})) 163 Expect(returnedConfig.CurrentApplication).To(Equal(returnedConfig.DesiredApplication)) 164 165 Expect(fakeV2Actor.CreateApplicationCallCount()).To(Equal(1)) 166 Expect(fakeV2Actor.CreateApplicationArgsForCall(0)).To(Equal(v2action.Application{ 167 Name: "some-app-name", 168 SpaceGUID: "some-space-guid", 169 })) 170 }) 171 }) 172 173 Context("when the creation errors", func() { 174 var expectedErr error 175 176 BeforeEach(func() { 177 expectedErr = errors.New("oh my") 178 fakeV2Actor.CreateApplicationReturns(v2action.Application{}, v2action.Warnings{"create-warning"}, expectedErr) 179 }) 180 181 It("sends the warnings and errors and returns true", func() { 182 Expect(executeErr).To(MatchError(expectedErr)) 183 Expect(warnings).To(ConsistOf("create-warning")) 184 }) 185 }) 186 }) 187 }) 188 189 Describe("FindOrReturnPartialApp", func() { 190 var expectedStack v2action.Stack 191 var expectedApp v2action.Application 192 193 Context("when the app exists", func() { 194 Context("when retrieving the stack is successful", func() { 195 BeforeEach(func() { 196 expectedStack = v2action.Stack{ 197 Name: "some-stack", 198 GUID: "some-stack-guid", 199 } 200 fakeV2Actor.GetStackReturns(expectedStack, v2action.Warnings{"stack-warnings"}, nil) 201 202 expectedApp = v2action.Application{ 203 GUID: "some-app-guid", 204 Name: "some-app", 205 StackGUID: expectedStack.GUID, 206 } 207 fakeV2Actor.GetApplicationByNameAndSpaceReturns(expectedApp, v2action.Warnings{"app-warnings"}, nil) 208 }) 209 210 It("fills in the stack", func() { 211 found, app, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid") 212 Expect(err).ToNot(HaveOccurred()) 213 Expect(warnings).To(ConsistOf("app-warnings", "stack-warnings")) 214 Expect(found).To(BeTrue()) 215 Expect(app).To(Equal(Application{ 216 Application: expectedApp, 217 Stack: expectedStack, 218 })) 219 }) 220 }) 221 222 Context("when retrieving the stack errors", func() { 223 var expectedErr error 224 225 BeforeEach(func() { 226 expectedErr = errors.New("stack stack stack em up") 227 fakeV2Actor.GetStackReturns(v2action.Stack{}, v2action.Warnings{"stack-warnings"}, expectedErr) 228 229 expectedApp = v2action.Application{ 230 GUID: "some-app-guid", 231 Name: "some-app", 232 StackGUID: "some-stack-guid", 233 } 234 fakeV2Actor.GetApplicationByNameAndSpaceReturns(expectedApp, v2action.Warnings{"app-warnings"}, nil) 235 }) 236 237 It("returns error and warnings", func() { 238 found, _, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid") 239 Expect(err).To(MatchError(expectedErr)) 240 Expect(warnings).To(ConsistOf("app-warnings", "stack-warnings")) 241 Expect(found).To(BeFalse()) 242 }) 243 }) 244 }) 245 246 Context("when the app does not exist", func() { 247 BeforeEach(func() { 248 fakeV2Actor.GetApplicationByNameAndSpaceReturns(v2action.Application{}, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, actionerror.ApplicationNotFoundError{}) 249 }) 250 251 It("returns a partial app and warnings", func() { 252 found, app, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid") 253 Expect(err).ToNot(HaveOccurred()) 254 Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2")) 255 Expect(found).To(BeFalse()) 256 Expect(app).To(Equal(Application{ 257 Application: v2action.Application{ 258 Name: "some-app", 259 SpaceGUID: "some-space-guid", 260 }, 261 })) 262 }) 263 }) 264 265 Context("when retrieving the app errors", func() { 266 var expectedErr error 267 BeforeEach(func() { 268 expectedErr = errors.New("dios mio") 269 fakeV2Actor.GetApplicationByNameAndSpaceReturns(v2action.Application{}, v2action.Warnings{"some-app-warning-1", "some-app-warning-2"}, expectedErr) 270 }) 271 272 It("returns a errors and warnings", func() { 273 found, _, warnings, err := actor.FindOrReturnPartialApp("some-app", "some-space-guid") 274 Expect(err).To(MatchError(expectedErr)) 275 Expect(warnings).To(ConsistOf("some-app-warning-1", "some-app-warning-2")) 276 Expect(found).To(BeFalse()) 277 }) 278 }) 279 }) 280 })