github.com/loafoe/cli@v7.1.0+incompatible/command/v7/purge_service_offering_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     7  
     8  	"code.cloudfoundry.org/cli/actor/v7action"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	. "code.cloudfoundry.org/cli/command/v7"
    11  	"code.cloudfoundry.org/cli/command/v7/v7fakes"
    12  	"code.cloudfoundry.org/cli/util/ui"
    13  	. "github.com/onsi/ginkgo"
    14  	. "github.com/onsi/gomega"
    15  	. "github.com/onsi/gomega/gbytes"
    16  )
    17  
    18  var _ = Describe("purge-service-offering command", func() {
    19  	var (
    20  		cmd             PurgeServiceOfferingCommand
    21  		testUI          *ui.UI
    22  		fakeConfig      *commandfakes.FakeConfig
    23  		fakeSharedActor *commandfakes.FakeSharedActor
    24  		fakeActor       *v7fakes.FakeActor
    25  
    26  		input      *Buffer
    27  		executeErr error
    28  	)
    29  
    30  	BeforeEach(func() {
    31  		input = NewBuffer()
    32  		testUI = ui.NewTestUI(input, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = PurgeServiceOfferingCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				Actor:       fakeActor,
    42  				SharedActor: fakeSharedActor,
    43  			},
    44  		}
    45  
    46  		setPositionalFlags(&cmd, "fake-service-offering")
    47  		setFlag(&cmd, "-b", "fake-service-broker")
    48  		setFlag(&cmd, "-f")
    49  
    50  		fakeActor.PurgeServiceOfferingByNameAndBrokerReturns(v7action.Warnings{"a warning"}, nil)
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	It("checks the target", func() {
    58  		Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    59  		actualOrgRequired, actualSpaceRequired := fakeSharedActor.CheckTargetArgsForCall(0)
    60  		Expect(actualOrgRequired).To(BeFalse())
    61  		Expect(actualSpaceRequired).To(BeFalse())
    62  	})
    63  
    64  	It("calls the actor with the right arguments", func() {
    65  		Expect(fakeActor.PurgeServiceOfferingByNameAndBrokerCallCount()).To(Equal(1))
    66  		actualOffering, actualBroker := fakeActor.PurgeServiceOfferingByNameAndBrokerArgsForCall(0)
    67  		Expect(actualOffering).To(Equal("fake-service-offering"))
    68  		Expect(actualBroker).To(Equal("fake-service-broker"))
    69  	})
    70  
    71  	It("prints messages and warnings", func() {
    72  		Expect(executeErr).NotTo(HaveOccurred())
    73  
    74  		Expect(testUI.Out).To(Say(`Purging service offering fake-service-offering\.\.\.`))
    75  		Expect(testUI.Out).To(Say("OK"))
    76  
    77  		Expect(testUI.Err).To(Say("a warning"))
    78  	})
    79  
    80  	When("the -f (force) flag is not specified", func() {
    81  		BeforeEach(func() {
    82  			setFlag(&cmd, "-f", false)
    83  		})
    84  
    85  		It("prints a warning", func() {
    86  			Expect(testUI.Out).To(Say(`WARNING: This operation assumes that the service broker responsible for this service offering is no longer available, and all service instances have been deleted, leaving orphan records in Cloud Foundry's database\. All knowledge of the service offering will be removed from Cloud Foundry, including service instances and service bindings\. No attempt will be made to contact the service broker; running this command without destroying the service broker will cause orphan service instances\. After running this command you may want to run either delete-service-auth-token or delete-service-broker to complete the cleanup\.`))
    87  			Expect(testUI.Out).To(Say("Really purge service offering fake-service-offering from broker fake-service-broker from Cloud Foundry?"))
    88  		})
    89  
    90  		When("the service broker name is not specified", func() {
    91  			BeforeEach(func() {
    92  				setFlag(&cmd, "-b", "")
    93  			})
    94  
    95  			It("prints a message that does not include the service broker name", func() {
    96  				Expect(testUI.Out).To(Say("Really purge service offering fake-service-offering from Cloud Foundry?"))
    97  			})
    98  		})
    99  
   100  		When("the user chooses the default", func() {
   101  			BeforeEach(func() {
   102  				_, err := input.Write([]byte("\n"))
   103  				Expect(err).NotTo(HaveOccurred())
   104  			})
   105  
   106  			It("does not purge the service offering", func() {
   107  				Expect(executeErr).ToNot(HaveOccurred())
   108  				Expect(testUI.Out).To(Say(`Purge service offering cancelled\.`))
   109  				Expect(fakeActor.PurgeServiceOfferingByNameAndBrokerCallCount()).To(Equal(0))
   110  			})
   111  		})
   112  
   113  		When("the user chooses `no`", func() {
   114  			BeforeEach(func() {
   115  				_, err := input.Write([]byte("n\n"))
   116  				Expect(err).NotTo(HaveOccurred())
   117  			})
   118  
   119  			It("does not purge the service offering", func() {
   120  				Expect(executeErr).ToNot(HaveOccurred())
   121  				Expect(testUI.Out).To(Say(`Purge service offering cancelled\.`))
   122  				Expect(fakeActor.PurgeServiceOfferingByNameAndBrokerCallCount()).To(Equal(0))
   123  			})
   124  		})
   125  
   126  		When("the user chooses `yes`", func() {
   127  			BeforeEach(func() {
   128  				_, err := input.Write([]byte("y\n"))
   129  				Expect(err).NotTo(HaveOccurred())
   130  			})
   131  
   132  			It("purges the service offering", func() {
   133  				Expect(executeErr).ToNot(HaveOccurred())
   134  				Expect(testUI.Out).To(Say(`Purging service offering fake-service-offering\.\.\.`))
   135  				Expect(fakeActor.PurgeServiceOfferingByNameAndBrokerCallCount()).To(Equal(1))
   136  			})
   137  		})
   138  	})
   139  
   140  	When("the service offering is not found", func() {
   141  		BeforeEach(func() {
   142  			fakeActor.PurgeServiceOfferingByNameAndBrokerReturns(v7action.Warnings{"actor warning"}, ccerror.ServiceOfferingNotFoundError{})
   143  		})
   144  
   145  		It("succeeds with a message", func() {
   146  			Expect(testUI.Err).To(Say("actor warning"))
   147  			Expect(testUI.Out).To(Say(`Purging service offering fake-service-offering\.\.\.`))
   148  			Expect(testUI.Out).To(Say(`Service offering 'fake-service-offering' not found\.`))
   149  			Expect(testUI.Out).To(Say("OK"))
   150  			Expect(executeErr).NotTo(HaveOccurred())
   151  		})
   152  	})
   153  
   154  	When("the actor returns another error", func() {
   155  		BeforeEach(func() {
   156  			fakeActor.PurgeServiceOfferingByNameAndBrokerReturns(v7action.Warnings{"actor warning"}, errors.New("fake actor error"))
   157  		})
   158  
   159  		It("returns an error", func() {
   160  			Expect(testUI.Err).To(Say("actor warning"))
   161  			Expect(executeErr).To(MatchError("fake actor error"))
   162  		})
   163  	})
   164  
   165  	When("checking the target fails", func() {
   166  		BeforeEach(func() {
   167  			fakeSharedActor.CheckTargetReturns(errors.New("fake target error"))
   168  		})
   169  
   170  		It("returns an error", func() {
   171  			Expect(executeErr).To(MatchError("fake target error"))
   172  		})
   173  	})
   174  })