github.com/jenspinney/cli@v6.42.1-0.20190207184520-7450c600020e+incompatible/command/v6/get_health_check_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v2action"
     8  	"code.cloudfoundry.org/cli/command/commandfakes"
     9  	. "code.cloudfoundry.org/cli/command/v6"
    10  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    11  	"code.cloudfoundry.org/cli/util/configv3"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("get-health-check Command", func() {
    19  	var (
    20  		cmd             GetHealthCheckCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v6fakes.FakeGetHealthCheckActor
    25  		binaryName      string
    26  		executeErr      error
    27  	)
    28  
    29  	BeforeEach(func() {
    30  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    31  		fakeConfig = new(commandfakes.FakeConfig)
    32  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    33  		fakeActor = new(v6fakes.FakeGetHealthCheckActor)
    34  
    35  		cmd = GetHealthCheckCommand{
    36  			UI:          testUI,
    37  			Config:      fakeConfig,
    38  			SharedActor: fakeSharedActor,
    39  			Actor:       fakeActor,
    40  		}
    41  
    42  		binaryName = "faceman"
    43  		fakeConfig.BinaryNameReturns(binaryName)
    44  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    45  			Name: "some-org",
    46  		})
    47  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    48  			GUID: "some-space-guid",
    49  			Name: "some-space",
    50  		})
    51  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    52  	})
    53  
    54  	JustBeforeEach(func() {
    55  		executeErr = cmd.Execute(nil)
    56  	})
    57  
    58  	When("checking the target fails", func() {
    59  		BeforeEach(func() {
    60  			fakeSharedActor.CheckTargetReturns(
    61  				actionerror.NotLoggedInError{BinaryName: binaryName})
    62  		})
    63  
    64  		It("returns a wrapped error", func() {
    65  			Expect(executeErr).To(MatchError(
    66  				actionerror.NotLoggedInError{BinaryName: binaryName}))
    67  		})
    68  	})
    69  
    70  	When("getting the user returns an error", func() {
    71  		var expectedErr error
    72  
    73  		BeforeEach(func() {
    74  			expectedErr = errors.New("current user error")
    75  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    76  		})
    77  
    78  		It("returns a wrapped error", func() {
    79  			Expect(executeErr).To(MatchError(expectedErr))
    80  		})
    81  	})
    82  
    83  	When("getting the application returns an error", func() {
    84  		var expectedErr error
    85  
    86  		BeforeEach(func() {
    87  			cmd.RequiredArgs.AppName = "some-app"
    88  
    89  			expectedErr = errors.New("get health check error")
    90  			fakeActor.GetApplicationByNameAndSpaceReturns(
    91  				v2action.Application{}, v2action.Warnings{"warning-1"}, expectedErr)
    92  		})
    93  
    94  		It("displays warnings and returns the error", func() {
    95  			Expect(testUI.Err).To(Say("warning-1"))
    96  			Expect(executeErr).To(MatchError(expectedErr))
    97  		})
    98  	})
    99  
   100  	When("getting the application is successful", func() {
   101  		When("the health check type is not http", func() {
   102  			BeforeEach(func() {
   103  				cmd.RequiredArgs.AppName = "some-app"
   104  
   105  				fakeActor.GetApplicationByNameAndSpaceReturns(
   106  					v2action.Application{
   107  						HealthCheckType:         "some-health-check-type",
   108  						HealthCheckHTTPEndpoint: "/some-endpoint",
   109  					}, v2action.Warnings{"warning-1"}, nil)
   110  			})
   111  
   112  			It("show a blank endpoint and displays warnings", func() {
   113  				Expect(executeErr).ToNot(HaveOccurred())
   114  
   115  				Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as some-user..."))
   116  				Expect(testUI.Out).To(Say("\n\n"))
   117  				Expect(testUI.Out).To(Say("health check type:          some-health-check-type"))
   118  				Expect(testUI.Out).To(Say(`endpoint \(for http type\):   \n`))
   119  
   120  				Expect(testUI.Err).To(Say("warning-1"))
   121  
   122  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   123  				targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
   124  				Expect(targetedOrganizationRequired).To(Equal(true))
   125  				Expect(targetedSpaceRequired).To(Equal(true))
   126  
   127  				Expect(fakeConfig.CurrentUserCallCount()).To(Equal(1))
   128  
   129  				Expect(fakeActor.GetApplicationByNameAndSpaceCallCount()).To(Equal(1))
   130  				name, spaceGUID := fakeActor.GetApplicationByNameAndSpaceArgsForCall(0)
   131  				Expect(name).To(Equal("some-app"))
   132  				Expect(spaceGUID).To(Equal("some-space-guid"))
   133  			})
   134  		})
   135  
   136  		When("the health check type is http", func() {
   137  			BeforeEach(func() {
   138  				cmd.RequiredArgs.AppName = "some-app"
   139  
   140  				fakeActor.GetApplicationByNameAndSpaceReturns(
   141  					v2action.Application{
   142  						HealthCheckType:         "http",
   143  						HealthCheckHTTPEndpoint: "/some-endpoint",
   144  					}, v2action.Warnings{"warning-1"}, nil)
   145  			})
   146  
   147  			It("shows the endpoint", func() {
   148  				Expect(executeErr).ToNot(HaveOccurred())
   149  
   150  				Expect(testUI.Out).To(Say("Getting health check type for app some-app in org some-org / space some-space as some-user..."))
   151  				Expect(testUI.Out).To(Say("\n\n"))
   152  				Expect(testUI.Out).To(Say("health check type:          http"))
   153  				Expect(testUI.Out).To(Say(`endpoint \(for http type\):   /some-endpoint`))
   154  			})
   155  		})
   156  	})
   157  })