github.com/nimakaviani/cli@v6.37.1-0.20180619223813-e734901a73fa+incompatible/command/v3/v3_get_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-get-health-check Command", func() {
    23  	var (
    24  		cmd             v3.V3GetHealthCheckCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v3fakes.FakeV3GetHealthCheckActor
    29  		binaryName      string
    30  		executeErr      error
    31  		app             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.FakeV3GetHealthCheckActor)
    39  
    40  		binaryName = "faceman"
    41  		fakeConfig.BinaryNameReturns(binaryName)
    42  		app = "some-app"
    43  
    44  		cmd = v3.V3GetHealthCheckCommand{
    45  			RequiredArgs: flag.AppName{AppName: app},
    46  
    47  			UI:          testUI,
    48  			Config:      fakeConfig,
    49  			SharedActor: fakeSharedActor,
    50  			Actor:       fakeActor,
    51  		}
    52  
    53  		fakeConfig.TargetedOrganizationReturns(configv3.Organization{
    54  			Name: "some-org",
    55  			GUID: "some-org-guid",
    56  		})
    57  		fakeConfig.TargetedSpaceReturns(configv3.Space{
    58  			Name: "some-space",
    59  			GUID: "some-space-guid",
    60  		})
    61  
    62  		fakeConfig.CurrentUserReturns(configv3.User{Name: "steve"}, nil)
    63  		fakeActor.CloudControllerAPIVersionReturns(ccversion.MinVersionV3)
    64  	})
    65  
    66  	JustBeforeEach(func() {
    67  		executeErr = cmd.Execute(nil)
    68  	})
    69  
    70  	Context("when the API version is below the minimum", func() {
    71  		BeforeEach(func() {
    72  			fakeActor.CloudControllerAPIVersionReturns("0.0.0")
    73  		})
    74  
    75  		It("returns a MinimumAPIVersionNotMetError", func() {
    76  			Expect(executeErr).To(MatchError(translatableerror.MinimumAPIVersionNotMetError{
    77  				CurrentVersion: "0.0.0",
    78  				MinimumVersion: ccversion.MinVersionV3,
    79  			}))
    80  		})
    81  
    82  		It("displays the experimental warning", func() {
    83  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    84  		})
    85  	})
    86  
    87  	Context("when checking target fails", func() {
    88  		BeforeEach(func() {
    89  			fakeSharedActor.CheckTargetReturns(actionerror.NoOrganizationTargetedError{BinaryName: binaryName})
    90  		})
    91  
    92  		It("returns an error", func() {
    93  			Expect(executeErr).To(MatchError(actionerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    94  
    95  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    96  
    97  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    98  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    99  			Expect(checkTargetedOrg).To(BeTrue())
   100  			Expect(checkTargetedSpace).To(BeTrue())
   101  		})
   102  	})
   103  
   104  	Context("when the user is not logged in", func() {
   105  		var expectedErr error
   106  
   107  		BeforeEach(func() {
   108  			expectedErr = errors.New("some current user error")
   109  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   110  		})
   111  
   112  		It("return an error", func() {
   113  			Expect(executeErr).To(Equal(expectedErr))
   114  		})
   115  	})
   116  
   117  	Context("when getting the application process health checks returns an error", func() {
   118  		var expectedErr error
   119  
   120  		BeforeEach(func() {
   121  			expectedErr = actionerror.ApplicationNotFoundError{Name: app}
   122  			fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(nil, v3action.Warnings{"warning-1", "warning-2"}, expectedErr)
   123  		})
   124  
   125  		It("returns the error and prints warnings", func() {
   126  			Expect(executeErr).To(Equal(actionerror.ApplicationNotFoundError{Name: app}))
   127  
   128  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   129  			Expect(testUI.Out).To(Say("Getting process health check types for app some-app in org some-org / space some-space as steve\\.\\.\\."))
   130  
   131  			Expect(testUI.Err).To(Say("warning-1"))
   132  			Expect(testUI.Err).To(Say("warning-2"))
   133  		})
   134  	})
   135  
   136  	Context("when app has no processes", func() {
   137  		BeforeEach(func() {
   138  			fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(
   139  				[]v3action.ProcessHealthCheck{},
   140  				v3action.Warnings{"warning-1", "warning-2"},
   141  				nil)
   142  		})
   143  
   144  		It("displays a message that there are no processes", func() {
   145  			Expect(executeErr).ToNot(HaveOccurred())
   146  
   147  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   148  			Expect(testUI.Out).To(Say("Getting process health check types for app some-app in org some-org / space some-space as steve\\.\\.\\."))
   149  			Expect(testUI.Out).To(Say("App has no processes"))
   150  
   151  			Expect(fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceCallCount()).To(Equal(1))
   152  			appName, spaceGUID := fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceArgsForCall(0)
   153  			Expect(appName).To(Equal("some-app"))
   154  			Expect(spaceGUID).To(Equal("some-space-guid"))
   155  		})
   156  	})
   157  
   158  	Context("when app has processes", func() {
   159  		BeforeEach(func() {
   160  			appProcessHealthChecks := []v3action.ProcessHealthCheck{
   161  				{ProcessType: constant.ProcessTypeWeb, HealthCheckType: "http", Endpoint: "/foo", InvocationTimeout: 10},
   162  				{ProcessType: "queue", HealthCheckType: "port", Endpoint: "", InvocationTimeout: 0},
   163  				{ProcessType: "timer", HealthCheckType: "process", Endpoint: "", InvocationTimeout: 5},
   164  			}
   165  			fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(appProcessHealthChecks, v3action.Warnings{"warning-1", "warning-2"}, nil)
   166  		})
   167  
   168  		It("prints the health check type of each process and warnings", func() {
   169  			Expect(executeErr).ToNot(HaveOccurred())
   170  
   171  			Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   172  			Expect(testUI.Out).To(Say("Getting process health check types for app some-app in org some-org / space some-space as steve\\.\\.\\."))
   173  			Expect(testUI.Out).To(Say(`process\s+health check\s+endpoint\s+\(for http\)\s+invocation timeout\n`))
   174  			Expect(testUI.Out).To(Say(`web\s+http\s+/foo\s+10\n`))
   175  			Expect(testUI.Out).To(Say(`queue\s+port\s+1\n`))
   176  			Expect(testUI.Out).To(Say(`timer\s+process\s+5\n`))
   177  
   178  			Expect(fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceCallCount()).To(Equal(1))
   179  			appName, spaceGUID := fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceArgsForCall(0)
   180  			Expect(appName).To(Equal("some-app"))
   181  			Expect(spaceGUID).To(Equal("some-space-guid"))
   182  		})
   183  	})
   184  })