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