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