github.com/loafoe/cli@v7.1.0+incompatible/command/v7/set_health_check_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 6 "code.cloudfoundry.org/cli/actor/actionerror" 7 "code.cloudfoundry.org/cli/actor/v7action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant" 9 "code.cloudfoundry.org/cli/command/commandfakes" 10 "code.cloudfoundry.org/cli/command/flag" 11 . "code.cloudfoundry.org/cli/command/v7" 12 "code.cloudfoundry.org/cli/command/v7/v7fakes" 13 "code.cloudfoundry.org/cli/resources" 14 "code.cloudfoundry.org/cli/util/configv3" 15 "code.cloudfoundry.org/cli/util/ui" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 . "github.com/onsi/gomega/gbytes" 19 ) 20 21 var _ = Describe("set-health-check Command", func() { 22 var ( 23 cmd SetHealthCheckCommand 24 testUI *ui.UI 25 fakeConfig *commandfakes.FakeConfig 26 fakeSharedActor *commandfakes.FakeSharedActor 27 fakeActor *v7fakes.FakeActor 28 binaryName string 29 executeErr error 30 app string 31 healthCheckType constant.HealthCheckType 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(v7fakes.FakeActor) 39 40 binaryName = "faceman" 41 fakeConfig.BinaryNameReturns(binaryName) 42 app = "some-app" 43 healthCheckType = "some-health-check-type" 44 45 cmd = SetHealthCheckCommand{ 46 RequiredArgs: flag.SetHealthCheckArgs{AppName: app, HealthCheck: flag.HealthCheckType{Type: healthCheckType}}, 47 HTTPEndpoint: "some-http-endpoint", 48 ProcessType: "some-process-type", 49 InvocationTimeout: flag.PositiveInteger{Value: 42}, 50 51 BaseCommand: BaseCommand{ 52 UI: testUI, 53 Config: fakeConfig, 54 SharedActor: fakeSharedActor, 55 Actor: fakeActor, 56 }, 57 } 58 59 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 60 Name: "some-org", 61 GUID: "some-org-guid", 62 }) 63 fakeConfig.TargetedSpaceReturns(configv3.Space{ 64 Name: "some-space", 65 GUID: "some-space-guid", 66 }) 67 68 fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil) 69 }) 70 71 JustBeforeEach(func() { 72 executeErr = cmd.Execute(nil) 73 }) 74 75 When("checking target fails", func() { 76 BeforeEach(func() { 77 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 78 }) 79 80 It("returns an error", func() { 81 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{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 When("the user is not logged in", func() { 91 var expectedErr error 92 93 BeforeEach(func() { 94 expectedErr = errors.New("some current user error") 95 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 96 }) 97 98 It("return an error", func() { 99 Expect(executeErr).To(Equal(expectedErr)) 100 }) 101 }) 102 103 When("updating the application process health check returns an error", func() { 104 var expectedErr error 105 106 BeforeEach(func() { 107 expectedErr = actionerror.ApplicationNotFoundError{Name: app} 108 fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(resources.Application{}, v7action.Warnings{"warning-1", "warning-2"}, expectedErr) 109 }) 110 111 It("returns the error and prints warnings", func() { 112 Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app})) 113 114 Expect(testUI.Out).To(Say(`Updating health check type for app some-app process some-process-type in org some-org / space some-space as steve\.\.\.`)) 115 116 Expect(testUI.Err).To(Say("warning-1")) 117 Expect(testUI.Err).To(Say("warning-2")) 118 }) 119 }) 120 121 When("application is started", func() { 122 BeforeEach(func() { 123 fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns( 124 resources.Application{ 125 State: constant.ApplicationStarted, 126 }, 127 v7action.Warnings{"warning-1", "warning-2"}, 128 nil) 129 }) 130 131 It("displays a message to restart application", func() { 132 Expect(executeErr).ToNot(HaveOccurred()) 133 134 Expect(testUI.Out).To(Say(`Updating health check type for app some-app process some-process-type in org some-org / space some-space as steve\.\.\.`)) 135 Expect(testUI.Out).To(Say(`TIP: An app restart is required for the change to take effect\.`)) 136 137 Expect(fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceCallCount()).To(Equal(1)) 138 appName, spaceGUID, healthCheckType, httpEndpoint, processType, invocationTimeout := fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceArgsForCall(0) 139 Expect(appName).To(Equal("some-app")) 140 Expect(spaceGUID).To(Equal("some-space-guid")) 141 Expect(healthCheckType).To(Equal(constant.HealthCheckType("some-health-check-type"))) 142 Expect(httpEndpoint).To(Equal("some-http-endpoint")) 143 Expect(processType).To(Equal("some-process-type")) 144 Expect(invocationTimeout).To(BeEquivalentTo(42)) 145 146 Expect(testUI.Err).To(Say("warning-1")) 147 Expect(testUI.Err).To(Say("warning-2")) 148 }) 149 }) 150 151 When("app is not started", func() { 152 BeforeEach(func() { 153 fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns( 154 resources.Application{ 155 State: constant.ApplicationStopped, 156 }, 157 v7action.Warnings{"warning-1", "warning-2"}, 158 nil) 159 }) 160 161 It("does not display a message to restart application", func() { 162 Expect(executeErr).ToNot(HaveOccurred()) 163 Expect(testUI.Out).To(Say(`Updating health check type for app some-app process some-process-type in org some-org / space some-space as steve\.\.\.`)) 164 Expect(testUI.Out).NotTo(Say(`TIP: An app restart is required for the change to take effect\.`)) 165 166 Expect(testUI.Err).To(Say("warning-1")) 167 Expect(testUI.Err).To(Say("warning-2")) 168 }) 169 }) 170 })