github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v7/get_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/util/configv3" 14 "code.cloudfoundry.org/cli/util/ui" 15 . "github.com/onsi/ginkgo" 16 . "github.com/onsi/gomega" 17 . "github.com/onsi/gomega/gbytes" 18 ) 19 20 var _ = Describe("get-health-check Command", func() { 21 var ( 22 cmd GetHealthCheckCommand 23 testUI *ui.UI 24 fakeConfig *commandfakes.FakeConfig 25 fakeSharedActor *commandfakes.FakeSharedActor 26 fakeActor *v7fakes.FakeGetHealthCheckActor 27 binaryName string 28 executeErr error 29 app string 30 ) 31 32 BeforeEach(func() { 33 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 34 fakeConfig = new(commandfakes.FakeConfig) 35 fakeSharedActor = new(commandfakes.FakeSharedActor) 36 fakeActor = new(v7fakes.FakeGetHealthCheckActor) 37 38 binaryName = "faceman" 39 fakeConfig.BinaryNameReturns(binaryName) 40 app = "some-app" 41 42 cmd = GetHealthCheckCommand{ 43 RequiredArgs: flag.AppName{AppName: app}, 44 45 UI: testUI, 46 Config: fakeConfig, 47 SharedActor: fakeSharedActor, 48 Actor: fakeActor, 49 } 50 51 fakeConfig.TargetedOrganizationReturns(configv3.Organization{ 52 Name: "some-org", 53 GUID: "some-org-guid", 54 }) 55 fakeConfig.TargetedSpaceReturns(configv3.Space{ 56 Name: "some-space", 57 GUID: "some-space-guid", 58 }) 59 60 fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil) 61 }) 62 63 JustBeforeEach(func() { 64 executeErr = cmd.Execute(nil) 65 }) 66 67 When("checking target fails", func() { 68 BeforeEach(func() { 69 fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}) 70 }) 71 72 It("returns an error", func() { 73 Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})) 74 75 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 76 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 77 Expect(checkTargetedOrg).To(BeTrue()) 78 Expect(checkTargetedSpace).To(BeTrue()) 79 }) 80 }) 81 82 When("the user is not logged in", func() { 83 var expectedErr error 84 85 BeforeEach(func() { 86 expectedErr = errors.New("some current user error") 87 fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr) 88 }) 89 90 It("return an error", func() { 91 Expect(executeErr).To(Equal(expectedErr)) 92 }) 93 }) 94 95 When("getting the application process health checks returns an error", func() { 96 var expectedErr error 97 98 BeforeEach(func() { 99 expectedErr = actionerror.ApplicationNotFoundError{Name: app} 100 fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(nil, v7action.Warnings{"warning-1", "warning-2"}, expectedErr) 101 }) 102 103 It("returns the error and prints warnings", func() { 104 Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app})) 105 106 Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as steve...")) 107 108 Expect(testUI.Err).To(Say("warning-1")) 109 Expect(testUI.Err).To(Say("warning-2")) 110 }) 111 }) 112 113 When("app has no processes", func() { 114 BeforeEach(func() { 115 fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns( 116 []v7action.ProcessHealthCheck{}, 117 v7action.Warnings{"warning-1", "warning-2"}, 118 nil) 119 }) 120 121 It("displays a message that there are no processes", func() { 122 Expect(executeErr).ToNot(HaveOccurred()) 123 124 Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as steve...")) 125 Expect(testUI.Out).To(Say("App has no processes")) 126 127 Expect(fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceCallCount()).To(Equal(1)) 128 appName, spaceGUID := fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceArgsForCall(0) 129 Expect(appName).To(Equal("some-app")) 130 Expect(spaceGUID).To(Equal("some-space-guid")) 131 }) 132 }) 133 134 When("app has processes", func() { 135 BeforeEach(func() { 136 appProcessHealthChecks := []v7action.ProcessHealthCheck{ 137 {ProcessType: constant.ProcessTypeWeb, HealthCheckType: constant.HTTP, Endpoint: "/foo", InvocationTimeout: 10}, 138 {ProcessType: "queue", HealthCheckType: constant.Port, Endpoint: "", InvocationTimeout: 0}, 139 {ProcessType: "timer", HealthCheckType: constant.Process, Endpoint: "", InvocationTimeout: 5}, 140 } 141 fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(appProcessHealthChecks, v7action.Warnings{"warning-1", "warning-2"}, nil) 142 }) 143 144 It("prints the health check type of each process and warnings", func() { 145 Expect(executeErr).ToNot(HaveOccurred()) 146 147 Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as steve...")) 148 Expect(testUI.Out).To(Say(`process\s+health check\s+endpoint\s+\(for http\)\s+invocation timeout\n`)) 149 Expect(testUI.Out).To(Say(`web\s+http\s+/foo\s+10\n`)) 150 Expect(testUI.Out).To(Say(`queue\s+port\s+1\n`)) 151 Expect(testUI.Out).To(Say(`timer\s+process\s+5\n`)) 152 153 Expect(fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceCallCount()).To(Equal(1)) 154 appName, spaceGUID := fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceArgsForCall(0) 155 Expect(appName).To(Equal("some-app")) 156 Expect(spaceGUID).To(Equal("some-space-guid")) 157 }) 158 }) 159 })