github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/command/v6/v3_set_health_check_command_test.go (about)

     1  package v6_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/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    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("v3-set-health-check Command", func() {
    21  	var (
    22  		cmd             V3SetHealthCheckCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeV3SetHealthCheckActor
    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(v6fakes.FakeV3SetHealthCheckActor)
    38  
    39  		binaryName = "faceman"
    40  		fakeConfig.BinaryNameReturns(binaryName)
    41  		app = "some-app"
    42  		healthCheckType = "some-health-check-type"
    43  
    44  		cmd = V3SetHealthCheckCommand{
    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  	It("displays the experimental warning", func() {
    73  		Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    74  	})
    75  
    76  	When("checking target fails", func() {
    77  		BeforeEach(func() {
    78  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    79  		})
    80  
    81  		It("returns an error", func() {
    82  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    83  
    84  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    85  
    86  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    87  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    88  			Expect(checkTargetedOrg).To(BeTrue())
    89  			Expect(checkTargetedSpace).To(BeTrue())
    90  		})
    91  	})
    92  
    93  	When("the user is not logged in", func() {
    94  		var expectedErr error
    95  
    96  		BeforeEach(func() {
    97  			expectedErr = errors.New("some current user error")
    98  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    99  		})
   100  
   101  		It("return an error", func() {
   102  			Expect(executeErr).To(Equal(expectedErr))
   103  		})
   104  	})
   105  
   106  	When("updating the application process health check returns an error", func() {
   107  		var expectedErr error
   108  
   109  		BeforeEach(func() {
   110  			expectedErr = actionerror.ApplicationNotFoundError{Name: app}
   111  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(v3action.Application{}, v3action.Warnings{"warning-1", "warning-2"}, expectedErr)
   112  		})
   113  
   114  		It("returns the error and prints warnings", func() {
   115  			Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   116  
   117  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   118  			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\.\.\.`))
   119  
   120  			Expect(testUI.Err).To(Say("warning-1"))
   121  			Expect(testUI.Err).To(Say("warning-2"))
   122  		})
   123  	})
   124  
   125  	When("application is started", func() {
   126  		BeforeEach(func() {
   127  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(
   128  				v3action.Application{
   129  					State: constant.ApplicationStarted,
   130  				},
   131  				v3action.Warnings{"warning-1", "warning-2"},
   132  				nil)
   133  		})
   134  
   135  		It("displays a message to restart application", func() {
   136  			Expect(executeErr).ToNot(HaveOccurred())
   137  
   138  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   139  			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\.\.\.`))
   140  			Expect(testUI.Out).To(Say(`TIP: An app restart is required for the change to take effect\.`))
   141  
   142  			Expect(fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceCallCount()).To(Equal(1))
   143  			appName, spaceGUID, healthCheckType, httpEndpoint, processType, invocationTimeout := fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceArgsForCall(0)
   144  			Expect(appName).To(Equal("some-app"))
   145  			Expect(spaceGUID).To(Equal("some-space-guid"))
   146  			Expect(healthCheckType).To(Equal(constant.HealthCheckType("some-health-check-type")))
   147  			Expect(httpEndpoint).To(Equal("some-http-endpoint"))
   148  			Expect(processType).To(Equal("some-process-type"))
   149  			Expect(invocationTimeout).To(BeEquivalentTo(42))
   150  
   151  			Expect(testUI.Err).To(Say("warning-1"))
   152  			Expect(testUI.Err).To(Say("warning-2"))
   153  		})
   154  	})
   155  
   156  	When("app is not started", func() {
   157  		BeforeEach(func() {
   158  			fakeActor.SetApplicationProcessHealthCheckTypeByNameAndSpaceReturns(
   159  				v3action.Application{
   160  					State: constant.ApplicationStopped,
   161  				},
   162  				v3action.Warnings{"warning-1", "warning-2"},
   163  				nil)
   164  		})
   165  
   166  		It("does not display a message to restart application", func() {
   167  			Expect(executeErr).ToNot(HaveOccurred())
   168  
   169  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   170  			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\.\.\.`))
   171  			Expect(testUI.Out).NotTo(Say(`TIP: An app restart is required for the change to take effect\.`))
   172  
   173  			Expect(testUI.Err).To(Say("warning-1"))
   174  			Expect(testUI.Err).To(Say("warning-2"))
   175  		})
   176  	})
   177  })