github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_set_health_check_command_test.go (about)

     1  package v3_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     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/translatableerror"
    13  	"code.cloudfoundry.org/cli/command/v3"
    14  	"code.cloudfoundry.org/cli/command/v3/v3fakes"
    15  	"code.cloudfoundry.org/cli/util/configv3"
    16  	"code.cloudfoundry.org/cli/util/ui"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("v3-set-health-check Command", func() {
    23  	var (
    24  		cmd             v3.V3SetHealthCheckCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v3fakes.FakeV3SetHealthCheckActor
    29  		binaryName      string
    30  		executeErr      error
    31  		app             string
    32  		healthCheckType string
    33  	)
    34  
    35  	BeforeEach(func() {
    36  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    37  		fakeConfig = new(commandfakes.FakeConfig)
    38  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    39  		fakeActor = new(v3fakes.FakeV3SetHealthCheckActor)
    40  
    41  		binaryName = "faceman"
    42  		fakeConfig.BinaryNameReturns(binaryName)
    43  		app = "some-app"
    44  		healthCheckType = "some-health-check-type"
    45  
    46  		cmd = v3.V3SetHealthCheckCommand{
    47  			RequiredArgs:      flag.SetHealthCheckArgs{AppName: app, HealthCheck: flag.HealthCheckType{Type: healthCheckType}},
    48  			HTTPEndpoint:      "some-http-endpoint",
    49  			ProcessType:       "some-process-type",
    50  			InvocationTimeout: flag.PositiveInteger{Value: 42},
    51  
    52  			UI:          testUI,
    53  			Config:      fakeConfig,
    54  			SharedActor: fakeSharedActor,
    55  			Actor:       fakeActor,
    56  		}
    57  
    58  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    59  			Name: "some-org",
    60  			GUID: "some-org-guid",
    61  		})
    62  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    63  			Name: "some-space",
    64  			GUID: "some-space-guid",
    65  		})
    66  
    67  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    68  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3)
    69  	})
    70  
    71  	JustBeforeEach(func() {
    72  		executeErr = cmd.Execute(nil)
    73  	})
    74  
    75  	Context("when the API version is below the minimum", func() {
    76  		BeforeEach(func() {
    77  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    78  		})
    79  
    80  		It("returns a MinimumAPIVersionNotMetError", func() {
    81  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    82  				CurrentVersion: "0.0.0",
    83  				MinimumVersion: ccversion.MinVersionV3,
    84  			}))
    85  		})
    86  
    87  		It("displays the experimental warning", func() {
    88  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    89  		})
    90  	})
    91  
    92  	Context("when checking target fails", func() {
    93  		BeforeEach(func() {
    94  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    95  		})
    96  
    97  		It("returns an error", func() {
    98  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    99  
   100  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   101  
   102  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
   103  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
   104  			Expect(checkTargetedOrg).To(BeTrue())
   105  			Expect(checkTargetedSpace).To(BeTrue())
   106  		})
   107  	})
   108  
   109  	Context("when the user is not logged in", func() {
   110  		var expectedErr error
   111  
   112  		BeforeEach(func() {
   113  			expectedErr = errors.New("some current user error")
   114  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   115  		})
   116  
   117  		It("return an error", func() {
   118  			Expect(executeErr).To(Equal(expectedErr))
   119  		})
   120  	})
   121  
   122  	Context("when updating the application process health check returns an error", func() {
   123  		var expectedErr error
   124  
   125  		BeforeEach(func() {
   126  			expectedErr = actionerror.ApplicationNotFoundError{Name: app}
   127  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(v3action.Application{}, v3action.Warnings{"warning-1", "warning-2"}, expectedErr)
   128  		})
   129  
   130  		It("returns the error and prints warnings", func() {
   131  			Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   132  
   133  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   134  			Expect(testUI.Out).To(Say("Updating health check type for app some-app process some-process-type in org some-org / space some-space as steve\\.\\.\\."))
   135  
   136  			Expect(testUI.Err).To(Say("warning-1"))
   137  			Expect(testUI.Err).To(Say("warning-2"))
   138  		})
   139  	})
   140  
   141  	Context("when application is started", func() {
   142  		BeforeEach(func() {
   143  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(
   144  				v3action.Application{
   145  					State: constant.ApplicationStarted,
   146  				},
   147  				v3action.Warnings{"warning-1", "warning-2"},
   148  				nil)
   149  		})
   150  
   151  		It("displays a message to restart application", func() {
   152  			Expect(executeErr).ToNot(HaveOccurred())
   153  
   154  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   155  			Expect(testUI.Out).To(Say("Updating health check type for app some-app process some-process-type in org some-org / space some-space as steve\\.\\.\\."))
   156  			Expect(testUI.Out).To(Say("TIP: An app restart is required for the change to take effect\\."))
   157  
   158  			Expect(fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceCallCount()).To(Equal(1))
   159  			appName, spaceGUID, healthCheckType, httpEndpoint, processType, invocationTimeout := fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceArgsForCall(0)
   160  			Expect(appName).To(Equal("some-app"))
   161  			Expect(spaceGUID).To(Equal("some-space-guid"))
   162  			Expect(healthCheckType).To(Equal("some-health-check-type"))
   163  			Expect(httpEndpoint).To(Equal("some-http-endpoint"))
   164  			Expect(processType).To(Equal("some-process-type"))
   165  			Expect(invocationTimeout).To(Equal(42))
   166  
   167  			Expect(testUI.Err).To(Say("warning-1"))
   168  			Expect(testUI.Err).To(Say("warning-2"))
   169  		})
   170  	})
   171  
   172  	Context("when app is not started", func() {
   173  		BeforeEach(func() {
   174  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(
   175  				v3action.Application{
   176  					State: constant.ApplicationStopped,
   177  				},
   178  				v3action.Warnings{"warning-1", "warning-2"},
   179  				nil)
   180  		})
   181  
   182  		It("does not display a message to restart application", func() {
   183  			Expect(executeErr).ToNot(HaveOccurred())
   184  
   185  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   186  			Expect(testUI.Out).To(Say("Updating health check type for app some-app process some-process-type in org some-org / space some-space as steve\\.\\.\\."))
   187  			Expect(testUI.Out).NotTo(Say("TIP: An app restart is required for the change to take effect\\."))
   188  
   189  			Expect(testUI.Err).To(Say("warning-1"))
   190  			Expect(testUI.Err).To(Say("warning-2"))
   191  		})
   192  	})
   193  })