github.com/ablease/cli@v6.37.1-0.20180613014814-3adbb7d7fb19+incompatible/command/v2/set_health_check_command_test.go (about)

     1  package v2_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/api/cloudcontroller/ccv2/constant"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccversion"
    10  	"code.cloudfoundry.org/cli/command/commandfakes"
    11  	"code.cloudfoundry.org/cli/command/translatableerror"
    12  	. "code.cloudfoundry.org/cli/command/v2"
    13  	"code.cloudfoundry.org/cli/command/v2/v2fakes"
    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       *v2fakes.FakeSetHealthCheckActor
    28  		binaryName      string
    29  		executeErr      error
    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(v2fakes.FakeSetHealthCheckActor)
    37  
    38  		cmd = SetHealthCheckCommand{
    39  			UI:          testUI,
    40  			Config:      fakeConfig,
    41  			SharedActor: fakeSharedActor,
    42  			Actor:       fakeActor,
    43  		}
    44  
    45  		binaryName = "faceman"
    46  		fakeConfig.BinaryNameReturns(binaryName)
    47  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    48  			Name: "some-org",
    49  		})
    50  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    51  			GUID: "some-space-guid",
    52  			Name: "some-space",
    53  		})
    54  
    55  		fakeConfig.CurrentUserReturns(configv3.User{Name: "some-user"}, nil)
    56  
    57  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionHTTPEndpointHealthCheckV2)
    58  	})
    59  
    60  	JustBeforeEach(func() {
    61  		executeErr = cmd.Execute(nil)
    62  	})
    63  
    64  	Context("when checking the target fails", func() {
    65  		BeforeEach(func() {
    66  			fakeSharedActor.CheckTargetReturns(
    67  				actionerror.NotLoggedInError{BinaryName: binaryName})
    68  		})
    69  
    70  		It("returns an error", func() {
    71  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    72  			targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    73  			Expect(targetedOrganizationRequired).To(Equal(true))
    74  			Expect(targetedSpaceRequired).To(Equal(true))
    75  
    76  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    77  		})
    78  	})
    79  
    80  	Context("when the API version is below 2.47.0", func() {
    81  		BeforeEach(func() {
    82  			fakeActor.CloudControllerAPIVersionReturns("2.46.0")
    83  		})
    84  
    85  		Context("when the health-check-type 'process' is specified", func() {
    86  			BeforeEach(func() {
    87  				cmd.RequiredArgs.HealthCheck.Type = "process"
    88  			})
    89  
    90  			It("returns the UnsupportedHealthCheckTypeError", func() {
    91  				Expect(executeErr).To(MatchError(translatableerror.HealthCheckTypeUnsupportedError{
    92  					SupportedTypes: []string{"port", "none"},
    93  				}))
    94  			})
    95  		})
    96  
    97  		Context("when the health-check-type 'http' is specified", func() {
    98  			BeforeEach(func() {
    99  				cmd.RequiredArgs.HealthCheck.Type = "http"
   100  			})
   101  
   102  			It("returns the UnsupportedHealthCheckTypeError", func() {
   103  				Expect(executeErr).To(MatchError(translatableerror.HealthCheckTypeUnsupportedError{
   104  					SupportedTypes: []string{"port", "none"},
   105  				}))
   106  			})
   107  		})
   108  
   109  		Context("when a valid health-check-type is specified", func() {
   110  			BeforeEach(func() {
   111  				cmd.RequiredArgs.HealthCheck.Type = "port"
   112  			})
   113  
   114  			It("does not error", func() {
   115  				Expect(executeErr).ToNot(HaveOccurred())
   116  			})
   117  		})
   118  	})
   119  
   120  	Context("when the API version is below 2.68.0", func() {
   121  		BeforeEach(func() {
   122  			fakeActor.CloudControllerAPIVersionReturns("2.67.0")
   123  		})
   124  
   125  		Context("when the health-check-type 'http' is specified", func() {
   126  			BeforeEach(func() {
   127  				cmd.RequiredArgs.HealthCheck.Type = "http"
   128  			})
   129  
   130  			It("returns the UnsupportedHealthCheckTypeError", func() {
   131  				Expect(executeErr).To(MatchError(translatableerror.HealthCheckTypeUnsupportedError{
   132  					SupportedTypes: []string{"port", "none", "process"},
   133  				}))
   134  			})
   135  		})
   136  
   137  		Context("when a valid health-check-type is specified", func() {
   138  			BeforeEach(func() {
   139  				cmd.RequiredArgs.HealthCheck.Type = "process"
   140  			})
   141  
   142  			It("does not error", func() {
   143  				Expect(executeErr).ToNot(HaveOccurred())
   144  			})
   145  		})
   146  	})
   147  
   148  	Context("when setting the application health check type returns an error", func() {
   149  		var expectedErr error
   150  
   151  		BeforeEach(func() {
   152  			cmd.RequiredArgs.AppName = "some-app"
   153  			cmd.RequiredArgs.HealthCheck.Type = "some-health-check-type"
   154  
   155  			expectedErr = errors.New("set health check error")
   156  			fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceReturns(
   157  				v2action.Application{}, v2action.Warnings{"warning-1"}, expectedErr)
   158  		})
   159  
   160  		It("displays warnings and returns the error", func() {
   161  			Expect(testUI.Err).To(Say("warning-1"))
   162  			Expect(executeErr).To(MatchError(expectedErr))
   163  		})
   164  	})
   165  
   166  	Context("when setting health check is successful", func() {
   167  		BeforeEach(func() {
   168  			cmd.RequiredArgs.AppName = "some-app"
   169  			cmd.RequiredArgs.HealthCheck.Type = "some-health-check-type"
   170  			cmd.HTTPEndpoint = "/"
   171  
   172  			fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceReturns(
   173  				v2action.Application{}, v2action.Warnings{"warning-1"}, nil)
   174  		})
   175  
   176  		It("informs the user and displays warnings", func() {
   177  			Expect(testUI.Out).To(Say("Updating health check type for app some-app in org some-org / space some-space as some-user..."))
   178  			Expect(testUI.Err).To(Say("warning-1"))
   179  			Expect(testUI.Out).To(Say("OK"))
   180  			Expect(executeErr).ToNot(HaveOccurred())
   181  
   182  			Expect(fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceCallCount()).To(Equal(1))
   183  			name, spaceGUID, healthCheckType, healthCheckHTTPEndpoint := fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceArgsForCall(0)
   184  			Expect(name).To(Equal("some-app"))
   185  			Expect(spaceGUID).To(Equal("some-space-guid"))
   186  			Expect(healthCheckType).To(Equal(constant.ApplicationHealthCheckType("some-health-check-type")))
   187  			Expect(healthCheckHTTPEndpoint).To(Equal("/"))
   188  		})
   189  	})
   190  
   191  	Context("when the app is started", func() {
   192  		BeforeEach(func() {
   193  			cmd.RequiredArgs.AppName = "some-app"
   194  			cmd.RequiredArgs.HealthCheck.Type = "some-health-check-type"
   195  
   196  			fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceReturns(
   197  				v2action.Application{State: constant.ApplicationStarted}, v2action.Warnings{"warning-1"}, nil)
   198  		})
   199  
   200  		It("displays a tip to restart the app", func() {
   201  			Expect(testUI.Out).To(Say("TIP: An app restart is required for the change to take effect."))
   202  		})
   203  	})
   204  
   205  })