github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_push_command_test.go (about) 1 package v3_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/v3action" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command/commandfakes" 11 "code.cloudfoundry.org/cli/command/flag" 12 "code.cloudfoundry.org/cli/command/translatableerror" 13 "code.cloudfoundry.org/cli/command/v3" 14 "code.cloudfoundry.org/cli/command/v3/v3fakes" 15 "code.cloudfoundry.org/cli/util/configv3" 16 "code.cloudfoundry.org/cli/util/ui" 17 . "github.com/onsi/ginkgo" 18 . "github.com/onsi/gomega" 19 . "github.com/onsi/gomega/gbytes" 20 . "github.com/onsi/gomega/gstruct" 21 ) 22 23 type Step struct { 24 Error error 25 Event pushaction.Event 26 Warnings pushaction.Warnings 27 } 28 29 func FillInValues(tuples []Step, state pushaction.PushState) func(state pushaction.PushState, progressBar pushaction.ProgressBar) (<-chan pushaction.PushState, <-chan pushaction.Event, <-chan pushaction.Warnings, <-chan error) { 30 return func(state pushaction.PushState, progressBar pushaction.ProgressBar) (<-chan pushaction.PushState, <-chan pushaction.Event, <-chan pushaction.Warnings, <-chan error) { 31 stateStream := make(chan pushaction.PushState) 32 eventStream := make(chan pushaction.Event) 33 warningsStream := make(chan pushaction.Warnings) 34 errorStream := make(chan error) 35 36 go func() { 37 defer close(stateStream) 38 defer close(eventStream) 39 defer close(warningsStream) 40 defer close(errorStream) 41 42 for _, tuple := range tuples { 43 warningsStream <- tuple.Warnings 44 if tuple.Error != nil { 45 errorStream <- tuple.Error 46 return 47 } else { 48 eventStream <- tuple.Event 49 } 50 } 51 52 eventStream <- pushaction.Complete 53 stateStream <- state 54 }() 55 56 return stateStream, eventStream, warningsStream, errorStream 57 } 58 } 59 60 var _ = Describe("v3-push Command", func() { 61 var ( 62 cmd v3.V3PushCommand 63 testUI *ui.UI 64 fakeConfig *commandfakes.FakeConfig 65 fakeSharedActor *commandfakes.FakeSharedActor 66 fakeActor *v3fakes.FakeV3PushActor 67 fakeVersionActor *v3fakes.FakeV3PushVersionActor 68 binaryName string 69 executeErr error 70 71 userName string 72 spaceName string 73 orgName string 74 ) 75 76 BeforeEach(func() { 77 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 78 fakeConfig = new(commandfakes.FakeConfig) 79 fakeSharedActor = new(commandfakes.FakeSharedActor) 80 fakeActor = new(v3fakes.FakeV3PushActor) 81 fakeVersionActor = new(v3fakes.FakeV3PushVersionActor) 82 83 binaryName = "faceman" 84 fakeConfig.BinaryNameReturns(binaryName) 85 fakeConfig.ExperimentalReturns(true) // TODO: Delete once we remove the experimental flag 86 87 cmd = v3.V3PushCommand{ 88 RequiredArgs: flag.AppName{AppName: "some-app"}, 89 UI: testUI, 90 Config: fakeConfig, 91 Actor: fakeActor, 92 VersionActor: fakeVersionActor, 93 SharedActor: fakeSharedActor, 94 } 95 96 userName = "banana" 97 spaceName = "some-space" 98 orgName = "some-org" 99 }) 100 101 JustBeforeEach(func() { 102 executeErr = cmd.Execute(nil) 103 }) 104 105 Context("when the API version is below the minimum", func() { 106 BeforeEach(func() { 107 fakeVersionActor.CloudControllerAPIVersionReturns("0.0.0") 108 }) 109 110 It("returns a MinimumAPIVersionNotMetError", func() { 111 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 112 CurrentVersion: "0.0.0", 113 MinimumVersion: ccversion.MinVersionV3, 114 })) 115 }) 116 }) 117 118 Context("when the API version is met", func() { 119 BeforeEach(func() { 120 fakeVersionActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3) 121 }) 122 123 Context("when checking target fails", func() { 124 BeforeEach(func() { 125 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 126 }) 127 128 It("returns an error", func() { 129 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 130 131 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 132 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 133 Expect(checkTargetedOrg).To(BeTrue()) 134 Expect(checkTargetedSpace).To(BeTrue()) 135 }) 136 }) 137 138 Context("when checking target fails because the user is not logged in", func() { 139 BeforeEach(func() { 140 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 141 }) 142 143 It("returns an error", func() { 144 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 145 146 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 147 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 148 Expect(checkTargetedOrg).To(BeTrue()) 149 Expect(checkTargetedSpace).To(BeTrue()) 150 }) 151 }) 152 153 Context("when the user is logged in", func() { 154 BeforeEach(func() { 155 fakeConfig.CurrentUserReturns(configv3.User{Name: userName}, nil) 156 157 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 158 Name: orgName, 159 GUID: "some-org-guid", 160 }) 161 fakeConfig.TargetedSpaceReturns(configv3.Space{ 162 Name: spaceName, 163 GUID: "some-space-guid", 164 }) 165 }) 166 167 It("displays the experimental warning", func() { 168 Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice")) 169 }) 170 171 Context("when getting app settings is successful", func() { 172 BeforeEach(func() { 173 fakeActor.ConceptualizeReturns( 174 []pushaction.PushState{ 175 { 176 Application: v3action.Application{Name: "some-app"}, 177 }, 178 }, 179 pushaction.Warnings{"some-warning-1"}, nil) 180 }) 181 182 Context("when the app is successfully actualized", func() { 183 BeforeEach(func() { 184 fakeActor.ActualizeStub = FillInValues([]Step{ 185 { 186 Event: pushaction.SkipingApplicationCreation, 187 Warnings: pushaction.Warnings{"skipping app creation warnings"}, 188 }, 189 { 190 Event: pushaction.CreatedApplication, 191 Warnings: pushaction.Warnings{"app creation warnings"}, 192 }, 193 }, pushaction.PushState{}) 194 }) 195 196 It("generates a push state with the specified app path", func() { 197 Expect(executeErr).ToNot(HaveOccurred()) 198 Expect(testUI.Out).To(Say("Getting app info...")) 199 Expect(testUI.Err).To(Say("some-warning-1")) 200 201 Expect(fakeActor.ConceptualizeCallCount()).To(Equal(1)) 202 settings, spaceGUID := fakeActor.ConceptualizeArgsForCall(0) 203 Expect(settings).To(MatchFields(IgnoreExtras, Fields{ 204 "Name": Equal("some-app"), 205 })) 206 Expect(spaceGUID).To(Equal("some-space-guid")) 207 }) 208 209 It("actualizes the application and displays events/warnings", func() { 210 Expect(executeErr).ToNot(HaveOccurred()) 211 212 Expect(testUI.Out).To(Say("Updating app some-app...")) 213 Expect(testUI.Err).To(Say("skipping app creation warnings")) 214 215 Expect(testUI.Out).To(Say("Creating app some-app...")) 216 Expect(testUI.Err).To(Say("app creation warnings")) 217 }) 218 }) 219 }) 220 221 Context("when getting app settings returns an error", func() { 222 var expectedErr error 223 224 BeforeEach(func() { 225 expectedErr = errors.New("fasdflkjdkjinenwnnondsokekm foo") 226 fakeActor.ConceptualizeReturns(nil, pushaction.Warnings{"some-warning-1"}, expectedErr) 227 }) 228 229 It("generates a push state with the specified app path", func() { 230 Expect(executeErr).To(MatchError(expectedErr)) 231 Expect(testUI.Err).To(Say("some-warning-1")) 232 }) 233 }) 234 235 Context("when app path is specified", func() { 236 BeforeEach(func() { 237 cmd.AppPath = "some/app/path" 238 }) 239 240 It("generates a push state with the specified app path", func() { 241 Expect(fakeActor.ConceptualizeCallCount()).To(Equal(1)) 242 settings, spaceGUID := fakeActor.ConceptualizeArgsForCall(0) 243 Expect(settings).To(MatchFields(IgnoreExtras, Fields{ 244 "Name": Equal("some-app"), 245 "ProvidedAppPath": Equal("some/app/path"), 246 })) 247 Expect(spaceGUID).To(Equal("some-space-guid")) 248 }) 249 }) 250 251 Context("when buildpack is specified", func() { 252 BeforeEach(func() { 253 cmd.Buildpacks = []string{"some-buildpack-1", "some-buildpack-2"} 254 }) 255 256 It("generates a push state with the specified buildpacks", func() { 257 Expect(fakeActor.ConceptualizeCallCount()).To(Equal(1)) 258 settings, spaceGUID := fakeActor.ConceptualizeArgsForCall(0) 259 Expect(settings).To(MatchFields(IgnoreExtras, Fields{ 260 "Name": Equal("some-app"), 261 "Buildpacks": Equal([]string{"some-buildpack-1", "some-buildpack-2"}), 262 })) 263 Expect(spaceGUID).To(Equal("some-space-guid")) 264 }) 265 }) 266 }) 267 }) 268 })