github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_create_app_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/v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 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 ) 21 22 var _ = Describe("v3-create-app Command", func() { 23 var ( 24 cmd v3.V3CreateAppCommand 25 testUI *ui.UI 26 fakeConfig *commandfakes.FakeConfig 27 fakeSharedActor *commandfakes.FakeSharedActor 28 fakeActor *v3fakes.FakeV3CreateAppActor 29 binaryName string 30 executeErr error 31 app string 32 ) 33 34 BeforeEach(func() { 35 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 36 fakeConfig = new(commandfakes.FakeConfig) 37 fakeSharedActor = new(commandfakes.FakeSharedActor) 38 fakeActor = new(v3fakes.FakeV3CreateAppActor) 39 40 binaryName = "faceman" 41 fakeConfig.BinaryNameReturns(binaryName) 42 app = "some-app" 43 44 cmd = v3.V3CreateAppCommand{ 45 UI: testUI, 46 Config: fakeConfig, 47 SharedActor: fakeSharedActor, 48 Actor: fakeActor, 49 RequiredArgs: flag.AppName{AppName: app}, 50 } 51 fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3) 52 }) 53 54 JustBeforeEach(func() { 55 executeErr = cmd.Execute(nil) 56 }) 57 58 Context("when the API version is below the minimum", func() { 59 BeforeEach(func() { 60 fakeActor.CloudControllerAPIVersionReturns("0.0.0") 61 }) 62 63 It("returns a MinimumAPIVersionNotMetError", func() { 64 Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{ 65 CurrentVersion: "0.0.0", 66 MinimumVersion: ccversion.MinVersionV3, 67 })) 68 }) 69 70 It("displays the experimental warning", func() { 71 Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice")) 72 }) 73 }) 74 75 Context("when checking target fails", func() { 76 BeforeEach(func() { 77 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 78 }) 79 80 It("returns an error", func() { 81 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 82 83 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 84 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 85 Expect(checkTargetedOrg).To(BeTrue()) 86 Expect(checkTargetedSpace).To(BeTrue()) 87 }) 88 }) 89 90 Context("when the user is logged in", func() { 91 BeforeEach(func() { 92 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 93 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 94 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 95 }) 96 97 Context("when the create is successful", func() { 98 BeforeEach(func() { 99 fakeActor.CreateApplicationInSpaceReturns(v3action.Application{}, v3action.Warnings{"I am a warning", "I am also a warning"}, nil) 100 }) 101 102 It("displays the header and ok", func() { 103 Expect(executeErr).ToNot(HaveOccurred()) 104 105 Expect(testUI.Out).To(Say("Creating V3 app some-app in org some-org / space some-space as banana...")) 106 Expect(testUI.Out).To(Say("OK")) 107 108 Expect(testUI.Err).To(Say("I am a warning")) 109 Expect(testUI.Err).To(Say("I am also a warning")) 110 111 Expect(fakeActor.CreateApplicationInSpaceCallCount()).To(Equal(1)) 112 113 createApp, createSpaceGUID := fakeActor.CreateApplicationInSpaceArgsForCall(0) 114 Expect(createApp).To(Equal(v3action.Application{ 115 Name: app, 116 })) 117 Expect(createSpaceGUID).To(Equal("some-space-guid")) 118 }) 119 120 Context("when app type is specified", func() { 121 BeforeEach(func() { 122 cmd.AppType = "docker" 123 }) 124 125 It("creates an app with specified app type", func() { 126 Expect(executeErr).ToNot(HaveOccurred()) 127 128 Expect(fakeActor.CreateApplicationInSpaceCallCount()).To(Equal(1)) 129 130 createApp, createSpaceGUID := fakeActor.CreateApplicationInSpaceArgsForCall(0) 131 Expect(createApp).To(Equal(v3action.Application{ 132 Name: app, 133 LifecycleType: constant.AppLifecycleTypeDocker, 134 })) 135 Expect(createSpaceGUID).To(Equal("some-space-guid")) 136 }) 137 }) 138 }) 139 140 Context("when the create is unsuccessful", func() { 141 Context("due to an unexpected error", func() { 142 var expectedErr error 143 144 BeforeEach(func() { 145 expectedErr = errors.New("I am an error") 146 fakeActor.CreateApplicationInSpaceReturns(v3action.Application{}, v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr) 147 }) 148 149 It("displays the header and error", func() { 150 Expect(executeErr).To(MatchError(expectedErr)) 151 152 Expect(testUI.Out).To(Say("Creating V3 app some-app in org some-org / space some-space as banana...")) 153 154 Expect(testUI.Err).To(Say("I am a warning")) 155 Expect(testUI.Err).To(Say("I am also a warning")) 156 }) 157 }) 158 159 Context("due to an ApplicationAlreadyExistsError", func() { 160 BeforeEach(func() { 161 fakeActor.CreateApplicationInSpaceReturns(v3action.Application{}, v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.ApplicationAlreadyExistsError{}) 162 }) 163 164 It("displays the header and ok", func() { 165 Expect(executeErr).ToNot(HaveOccurred()) 166 167 Expect(testUI.Out).To(Say("Creating V3 app some-app in org some-org / space some-space as banana...")) 168 Expect(testUI.Out).To(Say("OK")) 169 170 Expect(testUI.Err).To(Say("I am a warning")) 171 Expect(testUI.Err).To(Say("I am also a warning")) 172 Expect(testUI.Err).To(Say("App %s already exists", app)) 173 }) 174 }) 175 }) 176 }) 177 })