github.com/liamawhite/cli-with-i18n@v6.32.1-0.20171122084555-dede0a5c3448+incompatible/command/v2/set_health_check_command_test.go (about)

     1  package v2_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/liamawhite/cli-with-i18n/actor/sharedaction"
     7  	"github.com/liamawhite/cli-with-i18n/actor/v2action"
     8  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccv2"
     9  	"github.com/liamawhite/cli-with-i18n/api/cloudcontroller/ccversion"
    10  	"github.com/liamawhite/cli-with-i18n/command/commandfakes"
    11  	"github.com/liamawhite/cli-with-i18n/command/translatableerror"
    12  	. "github.com/liamawhite/cli-with-i18n/command/v2"
    13  	"github.com/liamawhite/cli-with-i18n/command/v2/v2fakes"
    14  	"github.com/liamawhite/cli-with-i18n/util/configv3"
    15  	"github.com/liamawhite/cli-with-i18n/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  				sharedaction.NotLoggedInError{BinaryName: binaryName})
    68  		})
    69  
    70  		It("returns an error", func() {
    71  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    72  			config, targetedOrganizationRequired, targetedSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    73  			Expect(config).To(Equal(fakeConfig))
    74  			Expect(targetedOrganizationRequired).To(Equal(true))
    75  			Expect(targetedSpaceRequired).To(Equal(true))
    76  
    77  			Expect(executeErr).To(MatchError(translatableerror.NotLoggedInError{BinaryName: binaryName}))
    78  		})
    79  	})
    80  
    81  	Context("when the API version is below 2.47.0", func() {
    82  		BeforeEach(func() {
    83  			fakeActor.CloudControllerAPIVersionReturns("2.46.0")
    84  		})
    85  
    86  		Context("when the health-check-type 'process' is specified", func() {
    87  			BeforeEach(func() {
    88  				cmd.RequiredArgs.HealthCheck.Type = "process"
    89  			})
    90  
    91  			It("returns the UnsupportedHealthCheckTypeError", func() {
    92  				Expect(executeErr).To(MatchError(translatableerror.HealthCheckTypeUnsupportedError{
    93  					SupportedTypes: []string{"port", "none"},
    94  				}))
    95  			})
    96  		})
    97  
    98  		Context("when the health-check-type 'http' is specified", func() {
    99  			BeforeEach(func() {
   100  				cmd.RequiredArgs.HealthCheck.Type = "http"
   101  			})
   102  
   103  			It("returns the UnsupportedHealthCheckTypeError", func() {
   104  				Expect(executeErr).To(MatchError(translatableerror.HealthCheckTypeUnsupportedError{
   105  					SupportedTypes: []string{"port", "none"},
   106  				}))
   107  			})
   108  		})
   109  
   110  		Context("when a valid health-check-type is specified", func() {
   111  			BeforeEach(func() {
   112  				cmd.RequiredArgs.HealthCheck.Type = "port"
   113  			})
   114  
   115  			It("does not error", func() {
   116  				Expect(executeErr).ToNot(HaveOccurred())
   117  			})
   118  		})
   119  	})
   120  
   121  	Context("when the API version is below 2.68.0", func() {
   122  		BeforeEach(func() {
   123  			fakeActor.CloudControllerAPIVersionReturns("2.67.0")
   124  		})
   125  
   126  		Context("when the health-check-type 'http' is specified", func() {
   127  			BeforeEach(func() {
   128  				cmd.RequiredArgs.HealthCheck.Type = "http"
   129  			})
   130  
   131  			It("returns the UnsupportedHealthCheckTypeError", func() {
   132  				Expect(executeErr).To(MatchError(translatableerror.HealthCheckTypeUnsupportedError{
   133  					SupportedTypes: []string{"port", "none", "process"},
   134  				}))
   135  			})
   136  		})
   137  
   138  		Context("when a valid health-check-type is specified", func() {
   139  			BeforeEach(func() {
   140  				cmd.RequiredArgs.HealthCheck.Type = "process"
   141  			})
   142  
   143  			It("does not error", func() {
   144  				Expect(executeErr).ToNot(HaveOccurred())
   145  			})
   146  		})
   147  	})
   148  
   149  	Context("when setting the application health check type returns an error", func() {
   150  		var expectedErr error
   151  
   152  		BeforeEach(func() {
   153  			cmd.RequiredArgs.AppName = "some-app"
   154  			cmd.RequiredArgs.HealthCheck.Type = "some-health-check-type"
   155  
   156  			expectedErr = errors.New("set health check error")
   157  			fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceReturns(
   158  				v2action.Application{}, v2action.Warnings{"warning-1"}, expectedErr)
   159  		})
   160  
   161  		It("displays warnings and returns the error", func() {
   162  			Expect(testUI.Err).To(Say("warning-1"))
   163  			Expect(executeErr).To(MatchError(expectedErr))
   164  		})
   165  	})
   166  
   167  	Context("when setting health check is successful", func() {
   168  		BeforeEach(func() {
   169  			cmd.RequiredArgs.AppName = "some-app"
   170  			cmd.RequiredArgs.HealthCheck.Type = "some-health-check-type"
   171  			cmd.HTTPEndpoint = "/"
   172  
   173  			fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceReturns(
   174  				v2action.Application{}, v2action.Warnings{"warning-1"}, nil)
   175  		})
   176  
   177  		It("informs the user and displays warnings", func() {
   178  			Expect(testUI.Out).To(Say("Updating health check type for app some-app in org some-org / space some-space as some-user..."))
   179  			Expect(testUI.Err).To(Say("warning-1"))
   180  			Expect(testUI.Out).To(Say("OK"))
   181  			Expect(executeErr).ToNot(HaveOccurred())
   182  
   183  			Expect(fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceCallCount()).To(Equal(1))
   184  			name, spaceGUID, healthCheckType, healthCheckHTTPEndpoint := fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceArgsForCall(0)
   185  			Expect(name).To(Equal("some-app"))
   186  			Expect(spaceGUID).To(Equal("some-space-guid"))
   187  			Expect(healthCheckType).To(Equal(v2action.ApplicationHealthCheckType("some-health-check-type")))
   188  			Expect(healthCheckHTTPEndpoint).To(Equal("/"))
   189  		})
   190  	})
   191  
   192  	Context("when the app is started", func() {
   193  		BeforeEach(func() {
   194  			cmd.RequiredArgs.AppName = "some-app"
   195  			cmd.RequiredArgs.HealthCheck.Type = "some-health-check-type"
   196  
   197  			fakeActor.SetApplicationHealthCheckTypeByNameAndSpaceReturns(
   198  				v2action.Application{State: ccv2.ApplicationStarted}, v2action.Warnings{"warning-1"}, nil)
   199  		})
   200  
   201  		It("displays a tip to restart the app", func() {
   202  			Expect(testUI.Out).To(Say("TIP: An app restart is required for the change to take affect."))
   203  		})
   204  	})
   205  
   206  })