github.com/franc20/ayesa_sap@v7.0.0-beta.28.0.20200124003224-302d4d52fa6c+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: constant.Process,
    35  				},
    36  				{
    37  					ProcessType:     "console",
    38  					HealthCheckType: constant.Process,
    39  				},
    40  				{
    41  					ProcessType:     constant.ProcessTypeWeb,
    42  					HealthCheckType: constant.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  		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(ConsistOf("some-warning"))
    81  			})
    82  		})
    83  
    84  		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(ConsistOf("some-warning"))
    99  			})
   100  		})
   101  
   102  		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  			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(ConsistOf("some-warning", "some-process-warning"))
   130  				})
   131  			})
   132  
   133  			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(ConsistOf("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     constant.HealthCheckType
   180  			healthCheckEndpoint string
   181  
   182  			warnings Warnings
   183  			err      error
   184  			app      Application
   185  		)
   186  
   187  		BeforeEach(func() {
   188  			healthCheckType = constant.Port
   189  			healthCheckEndpoint = "/"
   190  		})
   191  
   192  		JustBeforeEach(func() {
   193  			app, warnings, err = actor.SetApplicationProcessHealthCheckTypeByNameAndSpace(
   194  				"some-app-name",
   195  				"some-space-guid",
   196  				healthCheckType,
   197  				healthCheckEndpoint,
   198  				"some-process-type",
   199  				42,
   200  			)
   201  		})
   202  
   203  		When("the user specifies an endpoint for a non-http health check", func() {
   204  			BeforeEach(func() {
   205  				healthCheckType = constant.Port
   206  				healthCheckEndpoint = "some-http-endpoint"
   207  			})
   208  
   209  			It("returns an HTTPHealthCheckInvalidError", func() {
   210  				Expect(err).To(MatchError(actionerror.HTTPHealthCheckInvalidError{}))
   211  				Expect(warnings).To(BeNil())
   212  			})
   213  		})
   214  
   215  		When("application does not exist", func() {
   216  			BeforeEach(func() {
   217  				fakeCloudControllerClient.GetApplicationsReturns(
   218  					[]ccv3.Application{},
   219  					ccv3.Warnings{"some-warning"},
   220  					nil,
   221  				)
   222  
   223  				healthCheckType = constant.HTTP
   224  				healthCheckEndpoint = "some-http-endpoint"
   225  			})
   226  
   227  			It("returns the error and warnings", func() {
   228  				Expect(err).To(Equal(actionerror.ApplicationNotFoundError{Name: "some-app-name"}))
   229  				Expect(warnings).To(ConsistOf("some-warning"))
   230  			})
   231  		})
   232  
   233  		When("getting application returns an error", func() {
   234  			var expectedErr error
   235  
   236  			BeforeEach(func() {
   237  				expectedErr = errors.New("some-error")
   238  				fakeCloudControllerClient.GetApplicationsReturns(
   239  					[]ccv3.Application{},
   240  					ccv3.Warnings{"some-warning"},
   241  					expectedErr,
   242  				)
   243  			})
   244  
   245  			It("returns the error and warnings", func() {
   246  				Expect(err).To(Equal(expectedErr))
   247  				Expect(warnings).To(ConsistOf("some-warning"))
   248  			})
   249  		})
   250  
   251  		When("application exists", func() {
   252  			var ccv3App ccv3.Application
   253  
   254  			BeforeEach(func() {
   255  				ccv3App = ccv3.Application{
   256  					GUID: "some-app-guid",
   257  				}
   258  
   259  				fakeCloudControllerClient.GetApplicationsReturns(
   260  					[]ccv3.Application{ccv3App},
   261  					ccv3.Warnings{"some-warning"},
   262  					nil,
   263  				)
   264  			})
   265  
   266  			When("getting application process by type returns an error", func() {
   267  				var expectedErr error
   268  
   269  				When("the api returns a ProcessNotFoundError", func() {
   270  					BeforeEach(func() {
   271  						fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   272  							ccv3.Process{},
   273  							ccv3.Warnings{"some-process-warning"},
   274  							ccerror.ProcessNotFoundError{},
   275  						)
   276  					})
   277  
   278  					It("returns a ProcessNotFoundError and all warnings", func() {
   279  						Expect(err).To(Equal(actionerror.ProcessNotFoundError{ProcessType: "some-process-type"}))
   280  						Expect(warnings).To(ConsistOf("some-warning", "some-process-warning"))
   281  					})
   282  				})
   283  
   284  				Context("generic error", func() {
   285  					BeforeEach(func() {
   286  						expectedErr = errors.New("some-error")
   287  						fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   288  							ccv3.Process{},
   289  							ccv3.Warnings{"some-process-warning"},
   290  							expectedErr,
   291  						)
   292  					})
   293  
   294  					It("returns the error and warnings", func() {
   295  						Expect(err).To(Equal(expectedErr))
   296  						Expect(warnings).To(ConsistOf("some-warning", "some-process-warning"))
   297  					})
   298  				})
   299  			})
   300  
   301  			When("application process exists", func() {
   302  				BeforeEach(func() {
   303  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   304  						ccv3.Process{
   305  							GUID: "some-process-guid",
   306  						},
   307  						ccv3.Warnings{"some-process-warning"},
   308  						nil,
   309  					)
   310  				})
   311  
   312  				When("setting process health check type returns an error", func() {
   313  					var expectedErr error
   314  
   315  					BeforeEach(func() {
   316  						expectedErr = errors.New("some-error")
   317  						fakeCloudControllerClient.UpdateProcessReturns(
   318  							ccv3.Process{},
   319  							ccv3.Warnings{"some-health-check-warning"},
   320  							expectedErr,
   321  						)
   322  					})
   323  
   324  					It("returns the error and warnings", func() {
   325  						Expect(err).To(Equal(expectedErr))
   326  						Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "some-health-check-warning"))
   327  					})
   328  				})
   329  
   330  				When("setting process health check type succeeds", func() {
   331  					BeforeEach(func() {
   332  						fakeCloudControllerClient.UpdateProcessReturns(
   333  							ccv3.Process{GUID: "some-process-guid"},
   334  							ccv3.Warnings{"some-health-check-warning"},
   335  							nil,
   336  						)
   337  					})
   338  
   339  					When("the health check type is http", func() {
   340  						BeforeEach(func() {
   341  							healthCheckType = constant.HTTP
   342  							healthCheckEndpoint = "some-http-endpoint"
   343  						})
   344  
   345  						It("returns the application", func() {
   346  							Expect(err).NotTo(HaveOccurred())
   347  							Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "some-health-check-warning"))
   348  
   349  							Expect(app).To(Equal(Application{
   350  								GUID: ccv3App.GUID,
   351  							}))
   352  
   353  							Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   354  							appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   355  							Expect(appGUID).To(Equal("some-app-guid"))
   356  							Expect(processType).To(Equal("some-process-type"))
   357  
   358  							Expect(fakeCloudControllerClient.UpdateProcessCallCount()).To(Equal(1))
   359  							process := fakeCloudControllerClient.UpdateProcessArgsForCall(0)
   360  							Expect(process.GUID).To(Equal("some-process-guid"))
   361  							Expect(process.HealthCheckType).To(Equal(constant.HTTP))
   362  							Expect(process.HealthCheckEndpoint).To(BeEquivalentTo("some-http-endpoint"))
   363  							Expect(process.HealthCheckInvocationTimeout).To(BeEquivalentTo(42))
   364  						})
   365  					})
   366  
   367  					When("the health check type is not http", func() {
   368  						It("does not send the / endpoint and returns the application", func() {
   369  							Expect(err).NotTo(HaveOccurred())
   370  							Expect(warnings).To(ConsistOf("some-warning", "some-process-warning", "some-health-check-warning"))
   371  
   372  							Expect(app).To(Equal(Application{
   373  								GUID: ccv3App.GUID,
   374  							}))
   375  
   376  							Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   377  							appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   378  							Expect(appGUID).To(Equal("some-app-guid"))
   379  							Expect(processType).To(Equal("some-process-type"))
   380  
   381  							Expect(fakeCloudControllerClient.UpdateProcessCallCount()).To(Equal(1))
   382  							process := fakeCloudControllerClient.UpdateProcessArgsForCall(0)
   383  							Expect(process.GUID).To(Equal("some-process-guid"))
   384  							Expect(process.HealthCheckType).To(Equal(constant.Port))
   385  							Expect(process.HealthCheckEndpoint).To(BeEmpty())
   386  							Expect(process.HealthCheckInvocationTimeout).To(BeEquivalentTo(42))
   387  						})
   388  					})
   389  				})
   390  			})
   391  		})
   392  	})
   393  })