github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+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/sharedaction"
     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  
    83  	Context("when checking target fails", func() {
    84  		BeforeEach(func() {
    85  			fakeSharedActor.CheckTargetReturns(sharedaction.NoOrganizationTargetedError{BinaryName: binaryName})
    86  		})
    87  
    88  		It("returns an error", func() {
    89  			Expect(executeErr).To(MatchError(translatableerror.NoOrganizationTargetedError{BinaryName: binaryName}))
    90  
    91  			Expect(testUI.Out).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    92  
    93  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    94  			_, checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    95  			Expect(checkTargetedOrg).To(BeTrue())
    96  			Expect(checkTargetedSpace).To(BeTrue())
    97  		})
    98  	})
    99  
   100  	Context("when the user is not logged in", func() {
   101  		var expectedErr error
   102  
   103  		BeforeEach(func() {
   104  			expectedErr = errors.New("some current user error")
   105  			fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
   106  		})
   107  
   108  		It("return an error", func() {
   109  			Expect(executeErr).To(Equal(expectedErr))
   110  		})
   111  	})
   112  
   113  	Context("when getting the application process health checks returns an error", func() {
   114  		var expectedErr error
   115  
   116  		BeforeEach(func() {
   117  			expectedErr = v3action.ApplicationNotFoundError{Name: app}
   118  			fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(nil, v3action.Warnings{"warning-1", "warning-2"}, expectedErr)
   119  		})
   120  
   121  		It("returns the error and prints warnings", func() {
   122  			Expect(executeErr).To(Equal(translatableerror.ApplicationNotFoundError{Name: app}))
   123  
   124  			Expect(testUI.Out).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   125  			Expect(testUI.Out).To(Say("Getting process health check types for app some-app in org some-org / space some-space as steve\\.\\.\\."))
   126  
   127  			Expect(testUI.Err).To(Say("warning-1"))
   128  			Expect(testUI.Err).To(Say("warning-2"))
   129  		})
   130  	})
   131  
   132  	Context("when app has no processes", func() {
   133  		BeforeEach(func() {
   134  			fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(
   135  				[]v3action.ProcessHealthCheck{},
   136  				v3action.Warnings{"warning-1", "warning-2"},
   137  				nil)
   138  		})
   139  
   140  		It("displays a message that there are no processes", func() {
   141  			Expect(executeErr).ToNot(HaveOccurred())
   142  
   143  			Expect(testUI.Out).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   144  			Expect(testUI.Out).To(Say("Getting process health check types for app some-app in org some-org / space some-space as steve\\.\\.\\."))
   145  			Expect(testUI.Out).To(Say("App has no processes"))
   146  
   147  			Expect(fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceCallCount()).To(Equal(1))
   148  			appName, spaceGUID := fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceArgsForCall(0)
   149  			Expect(appName).To(Equal("some-app"))
   150  			Expect(spaceGUID).To(Equal("some-space-guid"))
   151  		})
   152  	})
   153  
   154  	Context("when app has processes", func() {
   155  		BeforeEach(func() {
   156  			appProcessHealthChecks := []v3action.ProcessHealthCheck{
   157  				{ProcessType: constant.ProcessTypeWeb, HealthCheckType: "http", Endpoint: "/foo"},
   158  				{ProcessType: "queue", HealthCheckType: "port", Endpoint: ""},
   159  				{ProcessType: "timer", HealthCheckType: "process", Endpoint: ""},
   160  			}
   161  			fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceReturns(appProcessHealthChecks, v3action.Warnings{"warning-1", "warning-2"}, nil)
   162  		})
   163  
   164  		It("prints the health check type of each process and warnings", func() {
   165  			Expect(executeErr).ToNot(HaveOccurred())
   166  
   167  			Expect(testUI.Out).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
   168  			Expect(testUI.Out).To(Say("Getting process health check types for app some-app in org some-org / space some-space as steve\\.\\.\\."))
   169  			Expect(testUI.Out).To(Say(`process\s+health check\s+endpoint\s+\(for http\)\n`))
   170  			Expect(testUI.Out).To(Say(`web\s+http\s+/foo\n`))
   171  			Expect(testUI.Out).To(Say(`queue\s+port\s+\n`))
   172  			Expect(testUI.Out).To(Say(`timer\s+process\s+\n`))
   173  
   174  			Expect(fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceCallCount()).To(Equal(1))
   175  			appName, spaceGUID := fakeActor.GetApplicationProcessHealthChecksByNameAndSpaceArgsForCall(0)
   176  			Expect(appName).To(Equal("some-app"))
   177  			Expect(spaceGUID).To(Equal("some-space-guid"))
   178  		})
   179  	})
   180  })