github.com/sleungcy-sap/cli@v7.1.0+incompatible/actor/v7action/process_instance_test.go (about)

     1  package v7action_test
     2  
     3  import (
     4  	"errors"
     5  	"time"
     6  
     7  	"code.cloudfoundry.org/cli/actor/actionerror"
     8  	. "code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/actor/v7action/v7actionfakes"
    10  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
    11  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3"
    12  	"code.cloudfoundry.org/cli/resources"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  )
    16  
    17  var _ = Describe("instance actions", func() {
    18  	var (
    19  		actor                     *Actor
    20  		fakeCloudControllerClient *v7actionfakes.FakeCloudControllerClient
    21  	)
    22  
    23  	BeforeEach(func() {
    24  		fakeCloudControllerClient = new(v7actionfakes.FakeCloudControllerClient)
    25  		actor = NewActor(fakeCloudControllerClient, nil, nil, nil, nil, nil)
    26  	})
    27  
    28  	Describe("Instance", func() {
    29  		Describe("StartTime", func() {
    30  			It("returns the time that the instance started", func() {
    31  				uptime, err := time.ParseDuration("86400s")
    32  				Expect(err).ToNot(HaveOccurred())
    33  				instance := ProcessInstance{Uptime: uptime}
    34  
    35  				Expect(instance.StartTime()).To(BeTemporally("~", time.Now().Add(-24*time.Hour), 10*time.Second))
    36  			})
    37  		})
    38  	})
    39  
    40  	Describe("DeleteInstanceByApplicationNameSpaceProcessTypeAndIndex", func() {
    41  		var (
    42  			executeErr error
    43  			warnings   Warnings
    44  		)
    45  
    46  		JustBeforeEach(func() {
    47  			warnings, executeErr = actor.DeleteInstanceByApplicationNameSpaceProcessTypeAndIndex("some-app-name", "some-space-guid", "some-process-type", 666)
    48  		})
    49  
    50  		When("getting the application returns an error", func() {
    51  			BeforeEach(func() {
    52  				fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{}, ccv3.Warnings{"some-get-app-warning"}, errors.New("some-get-app-error"))
    53  			})
    54  
    55  			It("returns all warnings and the error", func() {
    56  				Expect(executeErr).To(MatchError("some-get-app-error"))
    57  				Expect(warnings).To(ConsistOf("some-get-app-warning"))
    58  			})
    59  		})
    60  
    61  		When("getting the application succeeds", func() {
    62  			BeforeEach(func() {
    63  				fakeCloudControllerClient.GetApplicationsReturns([]resources.Application{{GUID: "some-app-guid"}}, ccv3.Warnings{"some-get-app-warning"}, nil)
    64  			})
    65  
    66  			When("deleting the instance returns ProcessNotFoundError", func() {
    67  				BeforeEach(func() {
    68  					fakeCloudControllerClient.DeleteApplicationProcessInstanceReturns(ccv3.Warnings{"some-delete-warning"}, ccerror.ProcessNotFoundError{})
    69  				})
    70  
    71  				It("returns all warnings and the ProcessNotFoundError error", func() {
    72  					Expect(executeErr).To(Equal(actionerror.ProcessNotFoundError{ProcessType: "some-process-type"}))
    73  					Expect(warnings).To(ConsistOf("some-get-app-warning", "some-delete-warning"))
    74  				})
    75  			})
    76  
    77  			When("deleting the instance returns InstanceNotFoundError", func() {
    78  				BeforeEach(func() {
    79  					fakeCloudControllerClient.DeleteApplicationProcessInstanceReturns(ccv3.Warnings{"some-delete-warning"}, ccerror.InstanceNotFoundError{})
    80  				})
    81  
    82  				It("returns all warnings and the ProcessInstanceNotFoundError error", func() {
    83  					Expect(executeErr).To(Equal(actionerror.ProcessInstanceNotFoundError{ProcessType: "some-process-type", InstanceIndex: 666}))
    84  					Expect(warnings).To(ConsistOf("some-get-app-warning", "some-delete-warning"))
    85  				})
    86  			})
    87  
    88  			When("deleting the instance returns other error", func() {
    89  				BeforeEach(func() {
    90  					fakeCloudControllerClient.DeleteApplicationProcessInstanceReturns(ccv3.Warnings{"some-delete-warning"}, errors.New("some-delete-error"))
    91  				})
    92  
    93  				It("returns all warnings and the error", func() {
    94  					Expect(executeErr).To(MatchError("some-delete-error"))
    95  					Expect(warnings).To(ConsistOf("some-get-app-warning", "some-delete-warning"))
    96  				})
    97  			})
    98  
    99  			When("deleting the instance succeeds", func() {
   100  				BeforeEach(func() {
   101  					fakeCloudControllerClient.DeleteApplicationProcessInstanceReturns(ccv3.Warnings{"some-delete-warning"}, nil)
   102  				})
   103  
   104  				It("returns all warnings and no error", func() {
   105  					Expect(executeErr).ToNot(HaveOccurred())
   106  					Expect(warnings).To(ConsistOf("some-get-app-warning", "some-delete-warning"))
   107  
   108  					Expect(fakeCloudControllerClient.GetApplicationsCallCount()).To(Equal(1))
   109  					Expect(fakeCloudControllerClient.GetApplicationsArgsForCall(0)).To(ConsistOf(
   110  						ccv3.Query{Key: ccv3.NameFilter, Values: []string{"some-app-name"}},
   111  						ccv3.Query{Key: ccv3.SpaceGUIDFilter, Values: []string{"some-space-guid"}},
   112  					))
   113  
   114  					Expect(fakeCloudControllerClient.DeleteApplicationProcessInstanceCallCount()).To(Equal(1))
   115  					appGUID, processType, instanceIndex := fakeCloudControllerClient.DeleteApplicationProcessInstanceArgsForCall(0)
   116  					Expect(appGUID).To(Equal("some-app-guid"))
   117  					Expect(processType).To(Equal("some-process-type"))
   118  					Expect(instanceIndex).To(Equal(666))
   119  				})
   120  			})
   121  		})
   122  	})
   123  })