github.com/arunkumar7540/cli@v6.45.0+incompatible/command/v6/v3_create_app_command_test.go (about)

     1  package v6_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"code.cloudfoundry.org/cli/actor/actionerror"
     7  	"code.cloudfoundry.org/cli/actor/v3action"
     8  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     9  	"code.cloudfoundry.org/cli/command/commandfakes"
    10  	"code.cloudfoundry.org/cli/command/flag"
    11  	. "code.cloudfoundry.org/cli/command/v6"
    12  	"code.cloudfoundry.org/cli/command/v6/v6fakes"
    13  	"code.cloudfoundry.org/cli/util/configv3"
    14  	"code.cloudfoundry.org/cli/util/ui"
    15  	. "github.com/onsi/ginkgo"
    16  	. "github.com/onsi/gomega"
    17  	. "github.com/onsi/gomega/gbytes"
    18  )
    19  
    20  var _ = Describe("v3-create-app Command", func() {
    21  	var (
    22  		cmd             V3CreateAppCommand
    23  		testUI          *ui.UI
    24  		fakeConfig      *commandfakes.FakeConfig
    25  		fakeSharedActor *commandfakes.FakeSharedActor
    26  		fakeActor       *v6fakes.FakeV3CreateAppActor
    27  		binaryName      string
    28  		executeErr      error
    29  		app             string
    30  	)
    31  
    32  	BeforeEach(func() {
    33  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    34  		fakeConfig = new(commandfakes.FakeConfig)
    35  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    36  		fakeActor = new(v6fakes.FakeV3CreateAppActor)
    37  
    38  		binaryName = "faceman"
    39  		fakeConfig.BinaryNameReturns(binaryName)
    40  		app = "some-app"
    41  
    42  		cmd = V3CreateAppCommand{
    43  			UI:           testUI,
    44  			Config:       fakeConfig,
    45  			SharedActor:  fakeSharedActor,
    46  			Actor:        fakeActor,
    47  			RequiredArgs: flag.AppName{AppName: app},
    48  		}
    49  	})
    50  
    51  	JustBeforeEach(func() {
    52  		executeErr = cmd.Execute(nil)
    53  	})
    54  
    55  	It("displays the experimental warning", func() {
    56  		Expect(testUI.Err).To(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    57  	})
    58  
    59  	When("checking target fails", func() {
    60  		BeforeEach(func() {
    61  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    62  		})
    63  
    64  		It("returns an error", func() {
    65  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    66  
    67  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    68  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    69  			Expect(checkTargetedOrg).To(BeTrue())
    70  			Expect(checkTargetedSpace).To(BeTrue())
    71  		})
    72  	})
    73  
    74  	When("the user is logged in", func() {
    75  		BeforeEach(func() {
    76  			fakeConfig.CurrentUserReturns(configv3.User{Name: "banana"}, nil)
    77  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    78  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    79  		})
    80  
    81  		When("the create is successful", func() {
    82  			BeforeEach(func() {
    83  				fakeActor.CreateApplicationInSpaceReturns(v3action.Application{}, v3action.Warnings{"I am a warning", "I am also a warning"}, nil)
    84  			})
    85  
    86  			It("displays the header and ok", func() {
    87  				Expect(executeErr).ToNot(HaveOccurred())
    88  
    89  				Expect(testUI.Out).To(Say("Creating V3 app some-app in org some-org / space some-space as banana..."))
    90  				Expect(testUI.Out).To(Say("OK"))
    91  
    92  				Expect(testUI.Err).To(Say("I am a warning"))
    93  				Expect(testUI.Err).To(Say("I am also a warning"))
    94  
    95  				Expect(fakeActor.CreateApplicationInSpaceCallCount()).To(Equal(1))
    96  
    97  				createApp, createSpaceGUID := fakeActor.CreateApplicationInSpaceArgsForCall(0)
    98  				Expect(createApp).To(Equal(v3action.Application{
    99  					Name: app,
   100  				}))
   101  				Expect(createSpaceGUID).To(Equal("some-space-guid"))
   102  			})
   103  
   104  			When("app type is specified", func() {
   105  				BeforeEach(func() {
   106  					cmd.AppType = "docker"
   107  				})
   108  
   109  				It("creates an app with specified app type", func() {
   110  					Expect(executeErr).ToNot(HaveOccurred())
   111  
   112  					Expect(fakeActor.CreateApplicationInSpaceCallCount()).To(Equal(1))
   113  
   114  					createApp, createSpaceGUID := fakeActor.CreateApplicationInSpaceArgsForCall(0)
   115  					Expect(createApp).To(Equal(v3action.Application{
   116  						Name:          app,
   117  						LifecycleType: constant.AppLifecycleTypeDocker,
   118  					}))
   119  					Expect(createSpaceGUID).To(Equal("some-space-guid"))
   120  				})
   121  			})
   122  		})
   123  
   124  		When("the create is unsuccessful", func() {
   125  			Context("due to an unexpected error", func() {
   126  				var expectedErr error
   127  
   128  				BeforeEach(func() {
   129  					expectedErr = errors.New("I am an error")
   130  					fakeActor.CreateApplicationInSpaceReturns(v3action.Application{}, v3action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   131  				})
   132  
   133  				It("displays the header and error", func() {
   134  					Expect(executeErr).To(MatchError(expectedErr))
   135  
   136  					Expect(testUI.Out).To(Say("Creating V3 app some-app in org some-org / space some-space as banana..."))
   137  
   138  					Expect(testUI.Err).To(Say("I am a warning"))
   139  					Expect(testUI.Err).To(Say("I am also a warning"))
   140  				})
   141  			})
   142  
   143  			Context("due to an ApplicationAlreadyExistsError", func() {
   144  				BeforeEach(func() {
   145  					fakeActor.CreateApplicationInSpaceReturns(v3action.Application{}, v3action.Warnings{"I am a warning", "I am also a warning"}, actionerror.ApplicationAlreadyExistsError{})
   146  				})
   147  
   148  				It("displays the header and ok", func() {
   149  					Expect(executeErr).ToNot(HaveOccurred())
   150  
   151  					Expect(testUI.Out).To(Say("Creating V3 app some-app in org some-org / space some-space as banana..."))
   152  					Expect(testUI.Out).To(Say("OK"))
   153  
   154  					Expect(testUI.Err).To(Say("I am a warning"))
   155  					Expect(testUI.Err).To(Say("I am also a warning"))
   156  					Expect(testUI.Err).To(Say("App %s already exists", app))
   157  				})
   158  			})
   159  		})
   160  	})
   161  })