github.com/sleungcy/cli@v7.1.0+incompatible/command/v7/stack_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/v7action"
     7  
     8  	"code.cloudfoundry.org/cli/actor/actionerror"
     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/configv3"
    13  	"code.cloudfoundry.org/cli/util/ui"
    14  	. "github.com/onsi/ginkgo"
    15  	. "github.com/onsi/gomega"
    16  	. "github.com/onsi/gomega/gbytes"
    17  )
    18  
    19  var _ = Describe("Stack Command", func() {
    20  	var (
    21  		cmd             StackCommand
    22  		testUI          *ui.UI
    23  		fakeConfig      *commandfakes.FakeConfig
    24  		fakeSharedActor *commandfakes.FakeSharedActor
    25  		fakeActor       *v7fakes.FakeActor
    26  		binaryName      string
    27  		executeErr      error
    28  		stackName       string
    29  	)
    30  
    31  	BeforeEach(func() {
    32  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    33  		fakeConfig = new(commandfakes.FakeConfig)
    34  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    35  		fakeActor = new(v7fakes.FakeActor)
    36  
    37  		cmd = StackCommand{
    38  			BaseCommand: BaseCommand{
    39  				UI:          testUI,
    40  				Config:      fakeConfig,
    41  				SharedActor: fakeSharedActor,
    42  				Actor:       fakeActor,
    43  			},
    44  		}
    45  
    46  		binaryName = "faceman"
    47  		fakeConfig.BinaryNameReturns(binaryName)
    48  		stackName = "some-stack-name"
    49  
    50  		cmd.RequiredArgs.StackName = stackName
    51  	})
    52  
    53  	JustBeforeEach(func() {
    54  		executeErr = cmd.Execute(nil)
    55  	})
    56  
    57  	Context("When the environment is not setup correctly", func() {
    58  		When("checking target fails", func() {
    59  			BeforeEach(func() {
    60  				fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    61  			})
    62  
    63  			It("returns an error", func() {
    64  				Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    65  
    66  				Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    67  				checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    68  				Expect(fakeActor.GetStackByNameCallCount()).To(Equal(0))
    69  				Expect(checkTargetedOrg).To(BeFalse())
    70  				Expect(checkTargetedSpace).To(BeFalse())
    71  			})
    72  		})
    73  
    74  		When("retrieving user information errors", func() {
    75  			var expectedErr error
    76  
    77  			BeforeEach(func() {
    78  				expectedErr = errors.New("some current user error")
    79  				fakeConfig.CurrentUserReturns(configv3.User{}, expectedErr)
    80  			})
    81  
    82  			It("return an error", func() {
    83  				Expect(executeErr).To(Equal(expectedErr))
    84  			})
    85  		})
    86  	})
    87  
    88  	Context("When the environment is setup correctly", func() {
    89  		BeforeEach(func() {
    90  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    91  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    92  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    93  		})
    94  
    95  		Context("When the stack exists", func() {
    96  			BeforeEach(func() {
    97  				stack := v7action.Stack{
    98  					Name:        "some-stack-name",
    99  					GUID:        "some-stack-guid",
   100  					Description: "some-stack-desc",
   101  				}
   102  				fakeActor.GetStackByNameReturns(stack, v7action.Warnings{"some-warning-1"}, nil)
   103  			})
   104  
   105  			When("The --guid flag is not provided", func() {
   106  				It("Displays the stack information", func() {
   107  					Expect(executeErr).ToNot(HaveOccurred())
   108  					Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
   109  					Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
   110  					// NOTE: DISPLAY EXPECTS
   111  					Expect(testUI.Err).To(Say("some-warning-1"))
   112  				})
   113  			})
   114  
   115  			When("The --guid flag is provided", func() {
   116  				BeforeEach(func() {
   117  					cmd.GUID = true
   118  				})
   119  
   120  				It("displays just the guid", func() {
   121  					Expect(executeErr).ToNot(HaveOccurred())
   122  					Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
   123  					Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
   124  					Expect(testUI.Err).To(Say("some-warning-1"))
   125  				})
   126  			})
   127  		})
   128  
   129  		When("The Stack does not Exist", func() {
   130  			expectedError := actionerror.StackNotFoundError{Name: "some-stack-name"}
   131  			BeforeEach(func() {
   132  				fakeActor.GetStackByNameReturns(
   133  					v7action.Stack{},
   134  					v7action.Warnings{"some-warning-1"},
   135  					expectedError,
   136  				)
   137  			})
   138  
   139  			It("Fails and returns a StackNotFoundError", func() {
   140  				Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
   141  				Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
   142  				Expect(executeErr).To(Equal(expectedError))
   143  				Expect(testUI.Err).To(Say("some-warning-1"))
   144  			})
   145  		})
   146  
   147  		When("There was an error in the actor", func() {
   148  			BeforeEach(func() {
   149  				fakeActor.GetStackByNameReturns(
   150  					v7action.Stack{},
   151  					v7action.Warnings{"some-warning-1"},
   152  					errors.New("some-random-error"),
   153  				)
   154  			})
   155  
   156  			It("Fails and returns a StackNotFoundError", func() {
   157  				Expect(fakeActor.GetStackByNameArgsForCall(0)).To(Equal("some-stack-name"))
   158  				Expect(fakeActor.GetStackByNameCallCount()).To(Equal(1))
   159  				Expect(executeErr).To(MatchError(errors.New("some-random-error")))
   160  				Expect(testUI.Err).To(Say("some-warning-1"))
   161  			})
   162  		})
   163  
   164  	})
   165  
   166  })