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