github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v7/create_app_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 8 9 "code.cloudfoundry.org/cli/actor/actionerror" 10 "code.cloudfoundry.org/cli/actor/v7action" 11 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 12 "code.cloudfoundry.org/cli/command/commandfakes" 13 "code.cloudfoundry.org/cli/command/flag" 14 v7 "code.cloudfoundry.org/cli/command/v7" 15 "code.cloudfoundry.org/cli/command/v7/v7fakes" 16 "code.cloudfoundry.org/cli/util/configv3" 17 "code.cloudfoundry.org/cli/util/ui" 18 . "github.com/onsi/ginkgo" 19 . "github.com/onsi/gomega" 20 . "github.com/onsi/gomega/gbytes" 21 ) 22 23 var _ = Describe("create-app Command", func() { 24 var ( 25 cmd v7.CreateAppCommand 26 testUI *ui.UI 27 fakeConfig *commandfakes.FakeConfig 28 fakeSharedActor *commandfakes.FakeSharedActor 29 fakeActor *v7fakes.FakeCreateAppActor 30 binaryName string 31 executeErr error 32 app string 33 ) 34 35 BeforeEach(func() { 36 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 37 fakeConfig = new(commandfakes.FakeConfig) 38 fakeSharedActor = new(commandfakes.FakeSharedActor) 39 fakeActor = new(v7fakes.FakeCreateAppActor) 40 41 binaryName = "faceman" 42 fakeConfig.BinaryNameReturns(binaryName) 43 app = "some-app" 44 45 cmd = v7.CreateAppCommand{ 46 UI: testUI, 47 Config: fakeConfig, 48 SharedActor: fakeSharedActor, 49 Actor: fakeActor, 50 RequiredArgs: flag.AppName{AppName: app}, 51 } 52 }) 53 54 JustBeforeEach(func() { 55 executeErr = cmd.Execute(nil) 56 }) 57 58 It("displays the experimental warning", func() { 59 Expect(testUI.Err).NotTo(Say("This command is in EXPERIMENTAL stage and may change without notice")) 60 }) 61 62 When("checking target fails", func() { 63 BeforeEach(func() { 64 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 65 }) 66 67 It("returns an error", func() { 68 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName})) 69 70 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 71 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 72 Expect(checkTargetedOrg).To(BeTrue()) 73 Expect(checkTargetedSpace).To(BeTrue()) 74 }) 75 }) 76 77 When("the user is logged in", func() { 78 BeforeEach(func() { 79 fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil) 80 fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"}) 81 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 82 }) 83 84 When("the create is successful", func() { 85 BeforeEach(func() { 86 fakeActor.CreateApplicationInSpaceReturns(v7action.Application{}, v7action.Warnings{"I am a warning", "I am also a warning"}, nil) 87 }) 88 89 It("displays the header and ok", func() { 90 Expect(executeErr).ToNot(HaveOccurred()) 91 92 Expect(testUI.Out).To(Say("Creating app some-app in org some-org / space some-space as banana...")) 93 Expect(testUI.Out).To(Say("OK")) 94 95 Expect(testUI.Err).To(Say("I am a warning")) 96 Expect(testUI.Err).To(Say("I am also a warning")) 97 98 Expect(fakeActor.CreateApplicationInSpaceCallCount()).To(Equal(1)) 99 100 createApp, createSpaceGUID := fakeActor.CreateApplicationInSpaceArgsForCall(0) 101 Expect(createApp).To(Equal(v7action.Application{ 102 Name: app, 103 })) 104 Expect(createSpaceGUID).To(Equal("some-space-guid")) 105 }) 106 107 When("app type is specified", func() { 108 BeforeEach(func() { 109 cmd.AppType = "docker" 110 }) 111 112 It("creates an app with specified app type", func() { 113 Expect(executeErr).ToNot(HaveOccurred()) 114 115 Expect(fakeActor.CreateApplicationInSpaceCallCount()).To(Equal(1)) 116 117 createApp, createSpaceGUID := fakeActor.CreateApplicationInSpaceArgsForCall(0) 118 Expect(createApp).To(Equal(v7action.Application{ 119 Name: app, 120 LifecycleType: constant.AppLifecycleTypeDocker, 121 })) 122 Expect(createSpaceGUID).To(Equal("some-space-guid")) 123 }) 124 }) 125 }) 126 127 When("the create is unsuccessful", func() { 128 Context("due to an unexpected error", func() { 129 var expectedErr error 130 131 BeforeEach(func() { 132 expectedErr = errors.New("I am an error") 133 fakeActor.CreateApplicationInSpaceReturns(v7action.Application{}, v7action.Warnings{"I am a warning", "I am also a warning"}, expectedErr) 134 }) 135 136 It("displays the header and error", func() { 137 Expect(executeErr).To(MatchError(expectedErr)) 138 139 Expect(testUI.Out).To(Say("Creating app some-app in org some-org / space some-space as banana...")) 140 141 Expect(testUI.Err).To(Say("I am a warning")) 142 Expect(testUI.Err).To(Say("I am also a warning")) 143 }) 144 }) 145 146 Context("due to NameNotUniqueInSpaceError{}", func() { 147 BeforeEach(func() { 148 fakeActor.CreateApplicationInSpaceReturns( 149 v7action.Application{}, 150 v7action.Warnings{"I am a warning", "I am also a warning"}, 151 ccerror.NameNotUniqueInSpaceError{ 152 UnprocessableEntityError: ccerror.UnprocessableEntityError{ 153 Message: fmt.Sprintf("Application '%s' already exists.", app), 154 }, 155 }) 156 }) 157 158 It("displays the header and ok", func() { 159 Expect(executeErr).ToNot(HaveOccurred()) 160 161 Expect(testUI.Out).To(Say("Creating app some-app in org some-org / space some-space as banana...")) 162 Expect(testUI.Out).To(Say("Application '%s' already exists.", app)) 163 Expect(testUI.Out).To(Say("OK")) 164 165 Expect(testUI.Err).To(Say("I am a warning")) 166 Expect(testUI.Err).To(Say("I am also a warning")) 167 }) 168 }) 169 }) 170 }) 171 })