github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/v3_create_app_command_test.go (about)

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