github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+incompatible/command/v7/set_health_check_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	. "code.cloudfoundry.org/cli/command/v7"
    12  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("set-health-check Command", func() {
    21  	var (
    22  		cmd             SetHealthCheckCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v7fakes.FakeSetHealthCheckActor
    27  		binaryName      string
    28  		executeErr      error
    29  		app             string
    30  		healthCheckType constant.HealthCheckType
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    35  		fakeConfig = new(commandfakes.FakeConfig)
    36  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    37  		fakeActor = new(v7fakes.FakeSetHealthCheckActor)
    38  
    39  		binaryName = "faceman"
    40  		fakeConfig.BinaryNameReturns(binaryName)
    41  		app = "some-app"
    42  		healthCheckType = "some-health-check-type"
    43  
    44  		cmd = SetHealthCheckCommand{
    45  			RequiredArgs:      flag.SetHealthCheckArgs{AppName: app, HealthCheck: flag.HealthCheckType{Type: healthCheckType}},
    46  			HTTPEndpoint:      "some-http-endpoint",
    47  			ProcessType:       "some-process-type",
    48  			InvocationTimeout: flag.PositiveInteger{Value: 42},
    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  	})
    67  
    68  	JustBeforeEach(func() {
    69  		executeErr = cmd.Execute(nil)
    70  	})
    71  
    72  	When("checking target fails", func() {
    73  		BeforeEach(func() {
    74  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    75  		})
    76  
    77  		It("returns an error", func() {
    78  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    79  
    80  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    81  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    82  			Expect(checkTargetedOrg).To(BeTrue())
    83  			Expect(checkTargetedSpace).To(BeTrue())
    84  		})
    85  	})
    86  
    87  	When("the user is not logged in", func() {
    88  		var expectedErr error
    89  
    90  		BeforeEach(func() {
    91  			expectedErr = errors.New("some current user error")
    92  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    93  		})
    94  
    95  		It("return an error", func() {
    96  			Expect(executeErr).To(Equal(expectedErr))
    97  		})
    98  	})
    99  
   100  	When("updating the application process health check returns an error", func() {
   101  		var expectedErr error
   102  
   103  		BeforeEach(func() {
   104  			expectedErr = actionerror.ApplicationNotFoundError{Name: app}
   105  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(v7action.Application{}, v7action.Warnings{"warning-1", "warning-2"}, expectedErr)
   106  		})
   107  
   108  		It("returns the error and prints warnings", func() {
   109  			Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   110  
   111  			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\.\.\.`))
   112  
   113  			Expect(testUI.Err).To(Say("warning-1"))
   114  			Expect(testUI.Err).To(Say("warning-2"))
   115  		})
   116  	})
   117  
   118  	When("application is started", func() {
   119  		BeforeEach(func() {
   120  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(
   121  				v7action.Application{
   122  					State: constant.ApplicationStarted,
   123  				},
   124  				v7action.Warnings{"warning-1", "warning-2"},
   125  				nil)
   126  		})
   127  
   128  		It("displays a message to restart application", func() {
   129  			Expect(executeErr).ToNot(HaveOccurred())
   130  
   131  			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\.\.\.`))
   132  			Expect(testUI.Out).To(Say(`TIP: An app restart is required for the change to take effect\.`))
   133  
   134  			Expect(fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceCallCount()).To(Equal(1))
   135  			appName, spaceGUID, healthCheckType, httpEndpoint, processType, invocationTimeout := fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceArgsForCall(0)
   136  			Expect(appName).To(Equal("some-app"))
   137  			Expect(spaceGUID).To(Equal("some-space-guid"))
   138  			Expect(healthCheckType).To(Equal(constant.HealthCheckType("some-health-check-type")))
   139  			Expect(httpEndpoint).To(Equal("some-http-endpoint"))
   140  			Expect(processType).To(Equal("some-process-type"))
   141  			Expect(invocationTimeout).To(BeEquivalentTo(42))
   142  
   143  			Expect(testUI.Err).To(Say("warning-1"))
   144  			Expect(testUI.Err).To(Say("warning-2"))
   145  		})
   146  	})
   147  
   148  	When("app is not started", func() {
   149  		BeforeEach(func() {
   150  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(
   151  				v7action.Application{
   152  					State: constant.ApplicationStopped,
   153  				},
   154  				v7action.Warnings{"warning-1", "warning-2"},
   155  				nil)
   156  		})
   157  
   158  		It("does not display a message to restart application", func() {
   159  			Expect(executeErr).ToNot(HaveOccurred())
   160  			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\.\.\.`))
   161  			Expect(testUI.Out).NotTo(Say(`TIP: An app restart is required for the change to take effect\.`))
   162  
   163  			Expect(testUI.Err).To(Say("warning-1"))
   164  			Expect(testUI.Err).To(Say("warning-2"))
   165  		})
   166  	})
   167  })