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