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