github.com/wanddynosios/cli/v8@v8.7.9-0.20240221182337-1a92e3a7017f/actor/v7action/process_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	. "code.cloudfoundry.org/cli/actor/v7action"
     8  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
     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  	"code.cloudfoundry.org/cli/types"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gstruct"
    17  )
    18  
    19  var _ = Describe("Process Actions", func() {
    20  	var (
    21  		actor                     *Actor
    22  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    23  	)
    24  
    25  	BeforeEach(func() {
    26  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    27  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    28  	})
    29  
    30  	Describe("GetProcess", func() {
    31  		var (
    32  			processGUID string
    33  
    34  			process  resources.Process
    35  			warnings Warnings
    36  			err      error
    37  		)
    38  
    39  		BeforeEach(func() {
    40  			processGUID = "some-process-guid"
    41  		})
    42  
    43  		JustBeforeEach(func() {
    44  			process, warnings, err = actor.GetProcess(processGUID)
    45  		})
    46  
    47  		When("getting the process is successful", func() {
    48  			BeforeEach(func() {
    49  				fakeCloudControllerClient.GetProcessReturns(
    50  					resources.Process{
    51  						GUID: "some-process-guid",
    52  					},
    53  					ccv3.Warnings{"some-process-warning"},
    54  					nil,
    55  				)
    56  			})
    57  
    58  			It("returns the process and warnings", func() {
    59  				Expect(err).NotTo(HaveOccurred())
    60  				Expect(warnings).To(ConsistOf("some-process-warning"))
    61  				Expect(process).To(Equal(resources.Process{
    62  					GUID: "some-process-guid",
    63  				}))
    64  
    65  				Expect(fakeCloudControllerClient.GetProcessCallCount()).To(Equal(1))
    66  				passedProcessGUID := fakeCloudControllerClient.GetProcessArgsForCall(0)
    67  				Expect(passedProcessGUID).To(Equal("some-process-guid"))
    68  			})
    69  		})
    70  
    71  		When("getting application process by type returns an error", func() {
    72  			var expectedErr error
    73  
    74  			BeforeEach(func() {
    75  				expectedErr = errors.New("some-error")
    76  				fakeCloudControllerClient.GetProcessReturns(
    77  					resources.Process{},
    78  					ccv3.Warnings{"some-process-warning"},
    79  					expectedErr,
    80  				)
    81  			})
    82  
    83  			It("returns the error and warnings", func() {
    84  				Expect(err).To(Equal(expectedErr))
    85  				Expect(warnings).To(ConsistOf("some-process-warning"))
    86  			})
    87  		})
    88  	})
    89  
    90  	Describe("GetProcessByTypeAndApplication", func() {
    91  		var (
    92  			processType string
    93  			appGUID     string
    94  
    95  			process  resources.Process
    96  			warnings Warnings
    97  			err      error
    98  		)
    99  
   100  		BeforeEach(func() {
   101  			processType = "web"
   102  			appGUID = "some-app-guid"
   103  		})
   104  
   105  		JustBeforeEach(func() {
   106  			process, warnings, err = actor.GetProcessByTypeAndApplication(processType, appGUID)
   107  		})
   108  
   109  		When("getting the application process is succesful", func() {
   110  			BeforeEach(func() {
   111  				fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   112  					resources.Process{
   113  						GUID: "some-process-guid",
   114  					},
   115  					ccv3.Warnings{"some-process-warning"},
   116  					nil,
   117  				)
   118  			})
   119  
   120  			It("returns the process and warnings", func() {
   121  				Expect(err).NotTo(HaveOccurred())
   122  				Expect(warnings).To(ConsistOf("some-process-warning"))
   123  				Expect(process).To(Equal(resources.Process{
   124  					GUID: "some-process-guid",
   125  				}))
   126  
   127  				Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   128  				passedAppGUID, passedProcessType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   129  				Expect(passedAppGUID).To(Equal(appGUID))
   130  				Expect(passedProcessType).To(Equal(processType))
   131  			})
   132  		})
   133  
   134  		When("getting application process by type returns an error", func() {
   135  			var expectedErr error
   136  
   137  			When("the api returns a ProcessNotFoundError", func() {
   138  				BeforeEach(func() {
   139  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   140  						resources.Process{},
   141  						ccv3.Warnings{"some-process-warning"},
   142  						ccerror.ProcessNotFoundError{},
   143  					)
   144  				})
   145  
   146  				It("returns a ProcessNotFoundError and all warnings", func() {
   147  					Expect(err).To(Equal(actionerror.ProcessNotFoundError{ProcessType: "web"}))
   148  					Expect(warnings).To(ConsistOf("some-process-warning"))
   149  				})
   150  			})
   151  
   152  			Context("generic error", func() {
   153  				BeforeEach(func() {
   154  					expectedErr = errors.New("some-error")
   155  					fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   156  						resources.Process{},
   157  						ccv3.Warnings{"some-process-warning"},
   158  						expectedErr,
   159  					)
   160  				})
   161  
   162  				It("returns the error and warnings", func() {
   163  					Expect(err).To(Equal(expectedErr))
   164  					Expect(warnings).To(ConsistOf("some-process-warning"))
   165  				})
   166  			})
   167  		})
   168  	})
   169  
   170  	Describe("ScaleProcessByApplication", func() {
   171  		var (
   172  			passedProcess resources.Process
   173  			warnings      Warnings
   174  			executeErr    error
   175  		)
   176  
   177  		BeforeEach(func() {
   178  			passedProcess = resources.Process{
   179  				Type:       constant.ProcessTypeWeb,
   180  				Instances:  types.NullInt{Value: 2, IsSet: true},
   181  				MemoryInMB: types.NullUint64{Value: 100, IsSet: true},
   182  				DiskInMB:   types.NullUint64{Value: 200, IsSet: true},
   183  			}
   184  		})
   185  
   186  		JustBeforeEach(func() {
   187  			warnings, executeErr = actor.ScaleProcessByApplication("some-app-guid", passedProcess)
   188  		})
   189  
   190  		When("no errors are encountered scaling the application process", func() {
   191  			BeforeEach(func() {
   192  				fakeCloudControllerClient.CreateApplicationProcessScaleReturns(
   193  					resources.Process{GUID: "some-process-guid"},
   194  					ccv3.Warnings{"scale-process-warning"},
   195  					nil)
   196  			})
   197  
   198  			It("scales correct process", func() {
   199  				Expect(executeErr).ToNot(HaveOccurred())
   200  				Expect(warnings).To(ConsistOf("scale-process-warning"))
   201  
   202  				Expect(fakeCloudControllerClient.CreateApplicationProcessScaleCallCount()).To(Equal(1))
   203  				appGUIDArg, processArg := fakeCloudControllerClient.CreateApplicationProcessScaleArgsForCall(0)
   204  				Expect(appGUIDArg).To(Equal("some-app-guid"))
   205  				Expect(processArg).To(Equal(resources.Process{
   206  					Type:       constant.ProcessTypeWeb,
   207  					Instances:  passedProcess.Instances,
   208  					MemoryInMB: passedProcess.MemoryInMB,
   209  					DiskInMB:   passedProcess.DiskInMB,
   210  				}))
   211  			})
   212  		})
   213  
   214  		When("an error is encountered scaling the application process", func() {
   215  			var expectedErr error
   216  
   217  			BeforeEach(func() {
   218  				expectedErr = errors.New("scale process error")
   219  				fakeCloudControllerClient.CreateApplicationProcessScaleReturns(
   220  					resources.Process{GUID: "some-process-guid"},
   221  					ccv3.Warnings{"scale-process-warning"},
   222  					expectedErr)
   223  			})
   224  
   225  			It("returns the error and all warnings", func() {
   226  				Expect(executeErr).To(MatchError(expectedErr))
   227  				Expect(warnings).To(ConsistOf("scale-process-warning"))
   228  			})
   229  		})
   230  
   231  		When("a ProcessNotFoundError error is encountered scaling the application process", func() {
   232  			BeforeEach(func() {
   233  				fakeCloudControllerClient.CreateApplicationProcessScaleReturns(
   234  					resources.Process{GUID: "some-process-guid"},
   235  					ccv3.Warnings{"scale-process-warning"},
   236  					ccerror.ProcessNotFoundError{},
   237  				)
   238  			})
   239  
   240  			It("returns the error and all warnings", func() {
   241  				Expect(executeErr).To(Equal(actionerror.ProcessNotFoundError{ProcessType: constant.ProcessTypeWeb}))
   242  				Expect(warnings).To(ConsistOf("scale-process-warning"))
   243  			})
   244  		})
   245  	})
   246  
   247  	Describe("UpdateProcessByTypeAndApplication", func() {
   248  		var (
   249  			processType  string
   250  			appGUID      string
   251  			inputProcess resources.Process
   252  
   253  			warnings Warnings
   254  			err      error
   255  		)
   256  
   257  		BeforeEach(func() {
   258  			processType = "web"
   259  			appGUID = "some-app-guid"
   260  			inputProcess = resources.Process{}
   261  		})
   262  
   263  		JustBeforeEach(func() {
   264  			warnings, err = actor.UpdateProcessByTypeAndApplication(processType, appGUID, inputProcess)
   265  		})
   266  
   267  		When("the user specifies an endpoint for a non-http health check", func() {
   268  			BeforeEach(func() {
   269  				inputProcess.HealthCheckType = constant.Port
   270  				inputProcess.HealthCheckEndpoint = "some-http-endpoint"
   271  			})
   272  
   273  			It("returns an HTTPHealthCheckInvalidError", func() {
   274  				Expect(err).To(MatchError(actionerror.HTTPHealthCheckInvalidError{}))
   275  				Expect(warnings).To(BeNil())
   276  			})
   277  		})
   278  
   279  		When("getting application process by type returns an error", func() {
   280  			var expectedErr error
   281  
   282  			BeforeEach(func() {
   283  				expectedErr = errors.New("some-error")
   284  				fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   285  					resources.Process{},
   286  					ccv3.Warnings{"some-process-warning"},
   287  					expectedErr,
   288  				)
   289  			})
   290  
   291  			It("returns the error and warnings", func() {
   292  				Expect(err).To(Equal(expectedErr))
   293  				Expect(warnings).To(ConsistOf("some-process-warning"))
   294  			})
   295  		})
   296  
   297  		When("application process exists", func() {
   298  			BeforeEach(func() {
   299  				fakeCloudControllerClient.GetApplicationProcessByTypeReturns(
   300  					resources.Process{
   301  						GUID: "some-process-guid",
   302  					},
   303  					ccv3.Warnings{"some-process-warning"},
   304  					nil,
   305  				)
   306  			})
   307  
   308  			When("updating the process errors", func() {
   309  				var expectedErr error
   310  
   311  				BeforeEach(func() {
   312  					inputProcess.HealthCheckType = constant.Port
   313  					inputProcess.HealthCheckEndpoint = constant.ProcessHealthCheckEndpointDefault
   314  
   315  					expectedErr = errors.New("some-error")
   316  					fakeCloudControllerClient.UpdateProcessReturns(
   317  						resources.Process{},
   318  						ccv3.Warnings{"some-health-check-warning"},
   319  						expectedErr,
   320  					)
   321  				})
   322  
   323  				It("returns the error and warnings", func() {
   324  					Expect(err).To(Equal(expectedErr))
   325  					Expect(warnings).To(ConsistOf("some-process-warning", "some-health-check-warning"))
   326  				})
   327  			})
   328  
   329  			When("update the process is successful", func() {
   330  				BeforeEach(func() {
   331  					fakeCloudControllerClient.UpdateProcessReturns(
   332  						resources.Process{GUID: "some-process-guid"},
   333  						ccv3.Warnings{"some-health-check-warning"},
   334  						nil,
   335  					)
   336  				})
   337  
   338  				It("gets the correct application process", func() {
   339  					Expect(err).NotTo(HaveOccurred())
   340  
   341  					Expect(fakeCloudControllerClient.GetApplicationProcessByTypeCallCount()).To(Equal(1))
   342  					passedAppGUID, passedProcessType := fakeCloudControllerClient.GetApplicationProcessByTypeArgsForCall(0)
   343  					Expect(passedAppGUID).To(Equal(appGUID))
   344  					Expect(passedProcessType).To(Equal(processType))
   345  				})
   346  
   347  				When("updating the command is successful", func() {
   348  					var command types.FilteredString
   349  					BeforeEach(func() {
   350  						command = *types.NewFilteredString("some-command")
   351  						inputProcess.Command = command
   352  					})
   353  
   354  					It("returns the application", func() {
   355  						Expect(err).NotTo(HaveOccurred())
   356  						Expect(warnings).To(ConsistOf("some-process-warning", "some-health-check-warning"))
   357  
   358  						Expect(fakeCloudControllerClient.UpdateProcessCallCount()).To(Equal(1))
   359  						process := fakeCloudControllerClient.UpdateProcessArgsForCall(0)
   360  						Expect(process).To(MatchFields(IgnoreExtras,
   361  							Fields{
   362  								"GUID":    Equal("some-process-guid"),
   363  								"Command": Equal(command),
   364  							}))
   365  					})
   366  				})
   367  
   368  				When("the health check type is http", func() {
   369  					BeforeEach(func() {
   370  						inputProcess.HealthCheckType = constant.HTTP
   371  						inputProcess.HealthCheckEndpoint = "some-http-endpoint"
   372  						inputProcess.HealthCheckInvocationTimeout = 42
   373  					})
   374  
   375  					It("returns the application", func() {
   376  						Expect(err).NotTo(HaveOccurred())
   377  						Expect(warnings).To(ConsistOf("some-process-warning", "some-health-check-warning"))
   378  
   379  						Expect(fakeCloudControllerClient.UpdateProcessCallCount()).To(Equal(1))
   380  						process := fakeCloudControllerClient.UpdateProcessArgsForCall(0)
   381  						Expect(process).To(MatchFields(IgnoreExtras,
   382  							Fields{
   383  								"GUID":                         Equal("some-process-guid"),
   384  								"HealthCheckType":              Equal(constant.HTTP),
   385  								"HealthCheckEndpoint":          Equal("some-http-endpoint"),
   386  								"HealthCheckInvocationTimeout": BeEquivalentTo(42),
   387  							}))
   388  					})
   389  				})
   390  
   391  				When("the health check type is not http", func() {
   392  					BeforeEach(func() {
   393  						inputProcess.HealthCheckType = constant.Port
   394  						inputProcess.HealthCheckInvocationTimeout = 42
   395  					})
   396  
   397  					When("the endpoint is set to '/'", func() {
   398  						BeforeEach(func() {
   399  							inputProcess.HealthCheckEndpoint = constant.ProcessHealthCheckEndpointDefault
   400  						})
   401  
   402  						It("does not send the / endpoint and returns the application", func() {
   403  							Expect(err).NotTo(HaveOccurred())
   404  							Expect(warnings).To(ConsistOf("some-process-warning", "some-health-check-warning"))
   405  
   406  							Expect(fakeCloudControllerClient.UpdateProcessCallCount()).To(Equal(1))
   407  							process := fakeCloudControllerClient.UpdateProcessArgsForCall(0)
   408  							Expect(process).To(MatchFields(IgnoreExtras,
   409  								Fields{
   410  									"GUID":                         Equal("some-process-guid"),
   411  									"HealthCheckType":              Equal(constant.Port),
   412  									"HealthCheckEndpoint":          BeEmpty(),
   413  									"HealthCheckInvocationTimeout": BeEquivalentTo(42),
   414  								}))
   415  						})
   416  					})
   417  
   418  					When("the endpoint is empty", func() {
   419  						It("does not send the / endpoint and returns the application", func() {
   420  							Expect(err).NotTo(HaveOccurred())
   421  							Expect(warnings).To(ConsistOf("some-process-warning", "some-health-check-warning"))
   422  
   423  							Expect(fakeCloudControllerClient.UpdateProcessCallCount()).To(Equal(1))
   424  							process := fakeCloudControllerClient.UpdateProcessArgsForCall(0)
   425  							Expect(process).To(MatchFields(IgnoreExtras,
   426  								Fields{
   427  									"GUID":                         Equal("some-process-guid"),
   428  									"HealthCheckType":              Equal(constant.Port),
   429  									"HealthCheckEndpoint":          BeEmpty(),
   430  									"HealthCheckInvocationTimeout": BeEquivalentTo(42),
   431  								}))
   432  						})
   433  					})
   434  				})
   435  			})
   436  		})
   437  	})
   438  })