github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/actor/v3action/process_health_check_test.go (about)

     1  package v3action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	. "code.cloudfoundry.org/cli/actor/v3action"
     7  	"code.cloudfoundry.org/cli/actor/v3action/v3actionfakes"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     9  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
    11  	. "github.com/onsi/ginkgo"
    12  	. "github.com/onsi/gomega"
    13  )
    14  
    15  var _ = Describe("Process Health Check Actions", func() {
    16  	var (
    17  		actor                     *Actor
    18  		fakeCloudControllerClient *v3actionfakes.FakeCloudControllerClient
    19  	)
    20  
    21  	BeforeEach(func() {
    22  		fakeCloudControllerClient = new(v3actionfakes.FakeCloudControllerClient)
    23  		actor = NewActor(nil, fakeCloudControllerClient, nil)
    24  	})
    25  
    26  	Describe("ProcessHealthChecks", func() {
    27  		var healthchecks ProcessHealthChecks
    28  
    29  		BeforeEach(func() {
    30  			healthchecks = ProcessHealthChecks{
    31  				{
    32  					ProcessType:     "worker",
    33  					HealthCheckType: "process",
    34  				},
    35  				{
    36  					ProcessType:     "console",
    37  					HealthCheckType: "process",
    38  				},
    39  				{
    40  					ProcessType:     constant.ProcessTypeWeb,
    41  					HealthCheckType: "http",
    42  					Endpoint:        "/",
    43  				},
    44  			}
    45  		})
    46  
    47  		Describe("Sort", func() {
    48  			It("sorts healthchecks with web first and then alphabetically sorted", func() {
    49  				healthchecks.Sort()
    50  				Expect(healthchecks[0].ProcessType).To(Equal(constant.ProcessTypeWeb))
    51  				Expect(healthchecks[1].ProcessType).To(Equal("console"))
    52  				Expect(healthchecks[2].ProcessType).To(Equal("worker"))
    53  			})
    54  		})
    55  	})
    56  
    57  	Describe("GetApplicationProcessHealthChecksByNameAndSpace", func() {
    58  		Context("when application does not exist", func() {
    59  			BeforeEach(func() {
    60  				fakeCloudControllerClient.GetApplicationsReturns(
    61  					[]ccv3.Application{},
    62  					ccv3.Warnings{"some-warning"},
    63  					nil,
    64  				)
    65  			})
    66  
    67  			It("returns the error and warnings", func() {
    68  				_, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
    69  				Expect(err).To(Equal(ApplicationNotFoundError{Name: "some-app-name"}))
    70  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
    71  			})
    72  		})
    73  
    74  		Context("when getting application returns an error", func() {
    75  			var expectedErr error
    76  
    77  			BeforeEach(func() {
    78  				expectedErr = errors.New("some-error")
    79  				fakeCloudControllerClient.GetApplicationsReturns(
    80  					[]ccv3.Application{},
    81  					ccv3.Warnings{"some-warning"},
    82  					expectedErr,
    83  				)
    84  			})
    85  
    86  			It("returns the error and warnings", func() {
    87  				_, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
    88  				Expect(err).To(Equal(expectedErr))
    89  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
    90  			})
    91  		})
    92  
    93  		Context("when application exists", func() {
    94  			BeforeEach(func() {
    95  				fakeCloudControllerClient.GetApplicationsReturns(
    96  					[]ccv3.Application{
    97  						{
    98  							GUID: "some-app-guid",
    99  						},
   100  					},
   101  					ccv3.Warnings{"some-warning"},
   102  					nil,
   103  				)
   104  			})
   105  
   106  			Context("when getting application processes returns an error", func() {
   107  				var expectedErr error
   108  
   109  				BeforeEach(func() {
   110  					expectedErr = errors.New("some-error")
   111  					fakeCloudControllerClient.GetApplicationProcessesReturns(
   112  						[]ccv3.Process{},
   113  						ccv3.Warnings{"some-process-warning"},
   114  						expectedErr,
   115  					)
   116  				})
   117  
   118  				It("returns the error and warnings", func() {
   119  					_, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
   120  					Expect(err).To(Equal(expectedErr))
   121  					Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   122  				})
   123  			})
   124  
   125  			Context("when application has processes", func() {
   126  				BeforeEach(func() {
   127  					fakeCloudControllerClient.GetApplicationProcessesReturns(
   128  						[]ccv3.Process{
   129  							{
   130  								GUID: "process-guid-1",
   131  								Type: "process-type-1",
   132  								HealthCheck: ccv3.ProcessHealthCheck{
   133  									Type: "health-check-type-1",
   134  									Data: ccv3.ProcessHealthCheckData{
   135  										Endpoint: "health-check-endpoint-1",
   136  									},
   137  								},
   138  							},
   139  							{
   140  								GUID: "process-guid-2",
   141  								Type: "process-type-2",
   142  								HealthCheck: ccv3.ProcessHealthCheck{
   143  									Type: "health-check-type-2",
   144  									Data: ccv3.ProcessHealthCheckData{},
   145  								},
   146  							},
   147  						},
   148  						ccv3.Warnings{"some-process-warning"},
   149  						nil,
   150  					)
   151  				})
   152  
   153  				It("returns health checks", func() {
   154  					processHealthChecks, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
   155  					Expect(err).NotTo(HaveOccurred())
   156  					Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   157  					Expect(processHealthChecks).To(Equal([]ProcessHealthCheck{
   158  						{
   159  							ProcessType:     "process-type-1",
   160  							HealthCheckType: "health-check-type-1",
   161  							Endpoint:        "health-check-endpoint-1",
   162  						},
   163  						{
   164  							ProcessType:     "process-type-2",
   165  							HealthCheckType: "health-check-type-2",
   166  						},
   167  					}))
   168  				})
   169  			})
   170  		})
   171  	})
   172  
   173  	Describe("SetApplicationProcessHealthCheckTypeByNameAndSpace", func() {
   174  		Context("when the user specifies an endpoint for a non-http health check", func() {
   175  			It("returns an HTTPHealthCheckInvalidError", func() {
   176  				_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "port", "some-http-endpoint", "some-process-type")
   177  				Expect(err).To(MatchError(HTTPHealthCheckInvalidError{}))
   178  				Expect(warnings).To(BeNil())
   179  			})
   180  		})
   181  
   182  		Context("when application does not exist", func() {
   183  			BeforeEach(func() {
   184  				fakeCloudControllerClient.GetApplicationsReturns(
   185  					[]ccv3.Application{},
   186  					ccv3.Warnings{"some-warning"},
   187  					nil,
   188  				)
   189  			})
   190  
   191  			It("returns the error and warnings", func() {
   192  				_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   193  				Expect(err).To(Equal(ApplicationNotFoundError{Name: "some-app-name"}))
   194  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
   195  			})
   196  		})
   197  
   198  		Context("when getting application returns an error", func() {
   199  			var expectedErr error
   200  
   201  			BeforeEach(func() {
   202  				expectedErr = errors.New("some-error")
   203  				fakeCloudControllerClient.GetApplicationsReturns(
   204  					[]ccv3.Application{},
   205  					ccv3.Warnings{"some-warning"},
   206  					expectedErr,
   207  				)
   208  			})
   209  
   210  			It("returns the error and warnings", func() {
   211  				_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   212  				Expect(err).To(Equal(expectedErr))
   213  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
   214  			})
   215  		})
   216  
   217  		Context("when application exists", func() {
   218  			var ccv3App ccv3.Application
   219  
   220  			BeforeEach(func() {
   221  				ccv3App = ccv3.Application{
   222  					GUID: "some-app-guid",
   223  				}
   224  
   225  				fakeCloudControllerClient.GetApplicationsReturns(
   226  					[]ccv3.Application{ccv3App},
   227  					ccv3.Warnings{"some-warning"},
   228  					nil,
   229  				)
   230  			})
   231  
   232  			Context("when getting application process by type returns an error", func() {
   233  				var expectedErr error
   234  
   235  				Context("when the api returns a ProcessNotFoundError", func() {
   236  					BeforeEach(func() {
   237  						fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   238  							ccv3.Process{},
   239  							ccv3.Warnings{"some-process-warning"},
   240  							ccerror.ProcessNotFoundError{},
   241  						)
   242  					})
   243  
   244  					It("returns a ProcessNotFoundError and all warnings", func() {
   245  						_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   246  						Expect(err).To(Equal(ProcessNotFoundError{ProcessType: "some-process-type"}))
   247  						Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   248  					})
   249  				})
   250  
   251  				Context("generic error", func() {
   252  					BeforeEach(func() {
   253  						expectedErr = errors.New("some-error")
   254  						fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   255  							ccv3.Process{},
   256  							ccv3.Warnings{"some-process-warning"},
   257  							expectedErr,
   258  						)
   259  					})
   260  
   261  					It("returns the error and warnings", func() {
   262  						_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   263  						Expect(err).To(Equal(expectedErr))
   264  						Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   265  					})
   266  				})
   267  			})
   268  
   269  			Context("when application process exists", func() {
   270  				BeforeEach(func() {
   271  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   272  						ccv3.Process{
   273  							GUID: "some-process-guid",
   274  						},
   275  						ccv3.Warnings{"some-process-warning"},
   276  						nil,
   277  					)
   278  				})
   279  
   280  				Context("when setting process health check type returns an error", func() {
   281  					var expectedErr error
   282  
   283  					BeforeEach(func() {
   284  						expectedErr = errors.New("some-error")
   285  						fakeCloudControllerClient.PatchApplicationProcessHealthCheckReturns(
   286  							ccv3.Warnings{"some-health-check-warning"},
   287  							expectedErr,
   288  						)
   289  					})
   290  
   291  					It("returns the error and warnings", func() {
   292  						_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   293  						Expect(err).To(Equal(expectedErr))
   294  						Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-health-check-warning"}))
   295  					})
   296  				})
   297  
   298  				Context("when setting process health check type succeeds", func() {
   299  					BeforeEach(func() {
   300  						fakeCloudControllerClient.PatchApplicationProcessHealthCheckReturns(
   301  							ccv3.Warnings{"some-health-check-warning"},
   302  							nil,
   303  						)
   304  					})
   305  					Context("when the health check type is http", func() {
   306  						It("returns the application", func() {
   307  							app, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   308  							Expect(err).NotTo(HaveOccurred())
   309  							Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-health-check-warning"}))
   310  
   311  							Expect(app).To(Equal(Application{
   312  								GUID: ccv3App.GUID,
   313  							}))
   314  
   315  							Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   316  							appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   317  							Expect(appGUID).To(Equal("some-app-guid"))
   318  							Expect(processType).To(Equal("some-process-type"))
   319  
   320  							Expect(fakeCloudControllerClient.PatchApplicationProcessHealthCheckCallCount()).To(Equal(1))
   321  							processGUID, processHealthCheckType, processHealthCheckEndpoint := fakeCloudControllerClient.PatchApplicationProcessHealthCheckArgsForCall(0)
   322  							Expect(processGUID).To(Equal("some-process-guid"))
   323  							Expect(processHealthCheckType).To(Equal("http"))
   324  							Expect(processHealthCheckEndpoint).To(Equal("some-http-endpoint"))
   325  						})
   326  					})
   327  					Context("when the health check type is not http", func() {
   328  						It("does not send the / endpoint and returns the application", func() {
   329  							app, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "port", "/", "some-process-type")
   330  							Expect(err).NotTo(HaveOccurred())
   331  							Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-health-check-warning"}))
   332  
   333  							Expect(app).To(Equal(Application{
   334  								GUID: ccv3App.GUID,
   335  							}))
   336  
   337  							Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   338  							appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   339  							Expect(appGUID).To(Equal("some-app-guid"))
   340  							Expect(processType).To(Equal("some-process-type"))
   341  
   342  							Expect(fakeCloudControllerClient.PatchApplicationProcessHealthCheckCallCount()).To(Equal(1))
   343  							processGUID, processHealthCheckType, processHealthCheckEndpoint := fakeCloudControllerClient.PatchApplicationProcessHealthCheckArgsForCall(0)
   344  							Expect(processGUID).To(Equal("some-process-guid"))
   345  							Expect(processHealthCheckType).To(Equal("port"))
   346  							Expect(processHealthCheckEndpoint).To(BeEmpty())
   347  						})
   348  					})
   349  				})
   350  			})
   351  		})
   352  	})
   353  })