github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+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  		Context("when application does not exist", func() {
    60  			BeforeEach(func() {
    61  				fakeCloudControllerClient.GetApplicationsReturns(
    62  					[]ccv3.Application{},
    63  					ccv3.Warnings{"some-warning"},
    64  					nil,
    65  				)
    66  			})
    67  
    68  			It("returns the error and warnings", func() {
    69  				_, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
    70  				Expect(err).To(Equal(actionerror.ApplicationNotFoundError{Name: "some-app-name"}))
    71  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
    72  			})
    73  		})
    74  
    75  		Context("when getting application returns an error", func() {
    76  			var expectedErr error
    77  
    78  			BeforeEach(func() {
    79  				expectedErr = errors.New("some-error")
    80  				fakeCloudControllerClient.GetApplicationsReturns(
    81  					[]ccv3.Application{},
    82  					ccv3.Warnings{"some-warning"},
    83  					expectedErr,
    84  				)
    85  			})
    86  
    87  			It("returns the error and warnings", func() {
    88  				_, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
    89  				Expect(err).To(Equal(expectedErr))
    90  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
    91  			})
    92  		})
    93  
    94  		Context("when application exists", func() {
    95  			BeforeEach(func() {
    96  				fakeCloudControllerClient.GetApplicationsReturns(
    97  					[]ccv3.Application{
    98  						{
    99  							GUID: "some-app-guid",
   100  						},
   101  					},
   102  					ccv3.Warnings{"some-warning"},
   103  					nil,
   104  				)
   105  			})
   106  
   107  			Context("when getting application processes returns an error", func() {
   108  				var expectedErr error
   109  
   110  				BeforeEach(func() {
   111  					expectedErr = errors.New("some-error")
   112  					fakeCloudControllerClient.GetApplicationProcessesReturns(
   113  						[]ccv3.Process{},
   114  						ccv3.Warnings{"some-process-warning"},
   115  						expectedErr,
   116  					)
   117  				})
   118  
   119  				It("returns the error and warnings", func() {
   120  					_, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
   121  					Expect(err).To(Equal(expectedErr))
   122  					Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   123  				})
   124  			})
   125  
   126  			Context("when application has processes", func() {
   127  				BeforeEach(func() {
   128  					fakeCloudControllerClient.GetApplicationProcessesReturns(
   129  						[]ccv3.Process{
   130  							{
   131  								GUID:                "process-guid-1",
   132  								Type:                "process-type-1",
   133  								HealthCheckType:     "health-check-type-1",
   134  								HealthCheckEndpoint: "health-check-endpoint-1",
   135  							},
   136  							{
   137  								GUID:            "process-guid-2",
   138  								Type:            "process-type-2",
   139  								HealthCheckType: "health-check-type-2",
   140  							},
   141  						},
   142  						ccv3.Warnings{"some-process-warning"},
   143  						nil,
   144  					)
   145  				})
   146  
   147  				It("returns health checks", func() {
   148  					processHealthChecks, warnings, err := actor.GetApplicationProcessHealthChecksByNameAndSpace("some-app-name", "some-space-guid")
   149  					Expect(err).NotTo(HaveOccurred())
   150  					Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   151  					Expect(processHealthChecks).To(Equal([]ProcessHealthCheck{
   152  						{
   153  							ProcessType:     "process-type-1",
   154  							HealthCheckType: "health-check-type-1",
   155  							Endpoint:        "health-check-endpoint-1",
   156  						},
   157  						{
   158  							ProcessType:     "process-type-2",
   159  							HealthCheckType: "health-check-type-2",
   160  						},
   161  					}))
   162  				})
   163  			})
   164  		})
   165  	})
   166  
   167  	Describe("SetApplicationProcessHealthCheckTypeByNameAndSpace", func() {
   168  		Context("when the user specifies an endpoint for a non-http health check", func() {
   169  			It("returns an HTTPHealthCheckInvalidError", func() {
   170  				_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "port", "some-http-endpoint", "some-process-type")
   171  				Expect(err).To(MatchError(actionerror.HTTPHealthCheckInvalidError{}))
   172  				Expect(warnings).To(BeNil())
   173  			})
   174  		})
   175  
   176  		Context("when application does not exist", func() {
   177  			BeforeEach(func() {
   178  				fakeCloudControllerClient.GetApplicationsReturns(
   179  					[]ccv3.Application{},
   180  					ccv3.Warnings{"some-warning"},
   181  					nil,
   182  				)
   183  			})
   184  
   185  			It("returns the error and warnings", func() {
   186  				_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   187  				Expect(err).To(Equal(actionerror.ApplicationNotFoundError{Name: "some-app-name"}))
   188  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
   189  			})
   190  		})
   191  
   192  		Context("when getting application returns an error", func() {
   193  			var expectedErr error
   194  
   195  			BeforeEach(func() {
   196  				expectedErr = errors.New("some-error")
   197  				fakeCloudControllerClient.GetApplicationsReturns(
   198  					[]ccv3.Application{},
   199  					ccv3.Warnings{"some-warning"},
   200  					expectedErr,
   201  				)
   202  			})
   203  
   204  			It("returns the error and warnings", func() {
   205  				_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   206  				Expect(err).To(Equal(expectedErr))
   207  				Expect(warnings).To(Equal(Warnings{"some-warning"}))
   208  			})
   209  		})
   210  
   211  		Context("when application exists", func() {
   212  			var ccv3App ccv3.Application
   213  
   214  			BeforeEach(func() {
   215  				ccv3App = ccv3.Application{
   216  					GUID: "some-app-guid",
   217  				}
   218  
   219  				fakeCloudControllerClient.GetApplicationsReturns(
   220  					[]ccv3.Application{ccv3App},
   221  					ccv3.Warnings{"some-warning"},
   222  					nil,
   223  				)
   224  			})
   225  
   226  			Context("when getting application process by type returns an error", func() {
   227  				var expectedErr error
   228  
   229  				Context("when the api returns a ProcessNotFoundError", func() {
   230  					BeforeEach(func() {
   231  						fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   232  							ccv3.Process{},
   233  							ccv3.Warnings{"some-process-warning"},
   234  							ccerror.ProcessNotFoundError{},
   235  						)
   236  					})
   237  
   238  					It("returns a ProcessNotFoundError and all warnings", func() {
   239  						_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   240  						Expect(err).To(Equal(actionerror.ProcessNotFoundError{ProcessType: "some-process-type"}))
   241  						Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   242  					})
   243  				})
   244  
   245  				Context("generic error", func() {
   246  					BeforeEach(func() {
   247  						expectedErr = errors.New("some-error")
   248  						fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   249  							ccv3.Process{},
   250  							ccv3.Warnings{"some-process-warning"},
   251  							expectedErr,
   252  						)
   253  					})
   254  
   255  					It("returns the error and warnings", func() {
   256  						_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   257  						Expect(err).To(Equal(expectedErr))
   258  						Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning"}))
   259  					})
   260  				})
   261  			})
   262  
   263  			Context("when application process exists", func() {
   264  				BeforeEach(func() {
   265  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   266  						ccv3.Process{
   267  							GUID: "some-process-guid",
   268  						},
   269  						ccv3.Warnings{"some-process-warning"},
   270  						nil,
   271  					)
   272  				})
   273  
   274  				Context("when setting process health check type returns an error", func() {
   275  					var expectedErr error
   276  
   277  					BeforeEach(func() {
   278  						expectedErr = errors.New("some-error")
   279  						fakeCloudControllerClient.PatchApplicationProcessHealthCheckReturns(
   280  							ccv3.Process{},
   281  							ccv3.Warnings{"some-health-check-warning"},
   282  							expectedErr,
   283  						)
   284  					})
   285  
   286  					It("returns the error and warnings", func() {
   287  						_, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   288  						Expect(err).To(Equal(expectedErr))
   289  						Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-health-check-warning"}))
   290  					})
   291  				})
   292  
   293  				Context("when setting process health check type succeeds", func() {
   294  					BeforeEach(func() {
   295  						fakeCloudControllerClient.PatchApplicationProcessHealthCheckReturns(
   296  							ccv3.Process{GUID: "some-process-guid"},
   297  							ccv3.Warnings{"some-health-check-warning"},
   298  							nil,
   299  						)
   300  					})
   301  					Context("when the health check type is http", func() {
   302  						It("returns the application", func() {
   303  							app, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "http", "some-http-endpoint", "some-process-type")
   304  							Expect(err).NotTo(HaveOccurred())
   305  							Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-health-check-warning"}))
   306  
   307  							Expect(app).To(Equal(Application{
   308  								GUID: ccv3App.GUID,
   309  							}))
   310  
   311  							Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   312  							appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   313  							Expect(appGUID).To(Equal("some-app-guid"))
   314  							Expect(processType).To(Equal("some-process-type"))
   315  
   316  							Expect(fakeCloudControllerClient.PatchApplicationProcessHealthCheckCallCount()).To(Equal(1))
   317  							processGUID, processHealthCheckType, processHealthCheckEndpoint := fakeCloudControllerClient.PatchApplicationProcessHealthCheckArgsForCall(0)
   318  							Expect(processGUID).To(Equal("some-process-guid"))
   319  							Expect(processHealthCheckType).To(Equal("http"))
   320  							Expect(processHealthCheckEndpoint).To(Equal("some-http-endpoint"))
   321  						})
   322  					})
   323  					Context("when the health check type is not http", func() {
   324  						It("does not send the / endpoint and returns the application", func() {
   325  							app, warnings, err := actor.SetApplicationProcessHealthCheckTypeByNameAndSpace("some-app-name", "some-space-guid", "port", "/", "some-process-type")
   326  							Expect(err).NotTo(HaveOccurred())
   327  							Expect(warnings).To(Equal(Warnings{"some-warning", "some-process-warning", "some-health-check-warning"}))
   328  
   329  							Expect(app).To(Equal(Application{
   330  								GUID: ccv3App.GUID,
   331  							}))
   332  
   333  							Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   334  							appGUID, processType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   335  							Expect(appGUID).To(Equal("some-app-guid"))
   336  							Expect(processType).To(Equal("some-process-type"))
   337  
   338  							Expect(fakeCloudControllerClient.PatchApplicationProcessHealthCheckCallCount()).To(Equal(1))
   339  							processGUID, processHealthCheckType, processHealthCheckEndpoint := fakeCloudControllerClient.PatchApplicationProcessHealthCheckArgsForCall(0)
   340  							Expect(processGUID).To(Equal("some-process-guid"))
   341  							Expect(processHealthCheckType).To(Equal("port"))
   342  							Expect(processHealthCheckEndpoint).To(BeEmpty())
   343  						})
   344  					})
   345  				})
   346  			})
   347  		})
   348  	})
   349  })