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