github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/create_package_command_test.go (about)

     1  package v7_test
     2  
     3  import (
     4  	"errors"
     5  
     6  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror"
     7  	"github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action"
     8  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes"
     9  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/flag"
    10  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror"
    11  	v7 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7"
    12  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/shared"
    13  	"github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes"
    14  	"github.com/LukasHeimann/cloudfoundrycli/v8/resources"
    15  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3"
    16  	"github.com/LukasHeimann/cloudfoundrycli/v8/util/ui"
    17  	. "github.com/onsi/ginkgo"
    18  	. "github.com/onsi/gomega"
    19  	. "github.com/onsi/gomega/gbytes"
    20  )
    21  
    22  var _ = Describe("create-package Command", func() {
    23  	var (
    24  		cmd             v7.CreatePackageCommand
    25  		testUI          *ui.UI
    26  		fakeConfig      *commandfakes.FakeConfig
    27  		fakeSharedActor *commandfakes.FakeSharedActor
    28  		fakeActor       *v7fakes.FakeActor
    29  		binaryName      string
    30  		executeErr      error
    31  		app             string
    32  	)
    33  
    34  	BeforeEach(func() {
    35  		testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer())
    36  		fakeConfig = new(commandfakes.FakeConfig)
    37  		fakeSharedActor = new(commandfakes.FakeSharedActor)
    38  		fakeActor = new(v7fakes.FakeActor)
    39  
    40  		binaryName = "faceman"
    41  		fakeConfig.BinaryNameReturns(binaryName)
    42  		app = "some-app"
    43  
    44  		packageDisplayer := shared.NewPackageDisplayer(
    45  			testUI,
    46  			fakeConfig,
    47  		)
    48  
    49  		cmd = v7.CreatePackageCommand{
    50  			BaseCommand: v7.BaseCommand{
    51  				UI:          testUI,
    52  				Config:      fakeConfig,
    53  				SharedActor: fakeSharedActor,
    54  				Actor:       fakeActor,
    55  			},
    56  			RequiredArgs:     flag.AppName{AppName: app},
    57  			PackageDisplayer: packageDisplayer,
    58  		}
    59  	})
    60  
    61  	JustBeforeEach(func() {
    62  		executeErr = cmd.Execute(nil)
    63  	})
    64  
    65  	It("displays the experimental warning", func() {
    66  		Expect(testUI.Err).NotTo(Say("This command is in EXPERIMENTAL stage and may change without notice"))
    67  	})
    68  
    69  	When("checking target fails", func() {
    70  		BeforeEach(func() {
    71  			fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName})
    72  		})
    73  
    74  		It("returns an error", func() {
    75  			Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: binaryName}))
    76  
    77  			Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1))
    78  			checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0)
    79  			Expect(checkTargetedOrg).To(BeTrue())
    80  			Expect(checkTargetedSpace).To(BeTrue())
    81  		})
    82  	})
    83  
    84  	When("the user is logged in", func() {
    85  		BeforeEach(func() {
    86  			fakeActor.GetCurrentUserReturns(configv3.User{Name: "banana"}, nil)
    87  			fakeConfig.TargetedSpaceReturns(configv3.Space{Name: "some-space", GUID: "some-space-guid"})
    88  			fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"})
    89  		})
    90  
    91  		When("no flags are set", func() {
    92  			When("the create is successful", func() {
    93  				BeforeEach(func() {
    94  					myPackage := resources.Package{GUID: "1234"}
    95  					fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceReturns(myPackage, v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
    96  				})
    97  
    98  				It("displays the header and ok", func() {
    99  					Expect(executeErr).ToNot(HaveOccurred())
   100  
   101  					Expect(testUI.Out).To(Say("Creating and uploading bits package for app some-app in org some-org / space some-space as banana..."))
   102  					Expect(testUI.Out).To(Say(`Package with guid '1234' has been created\.`))
   103  					Expect(testUI.Out).To(Say("OK"))
   104  
   105  					Expect(testUI.Err).To(Say("I am a warning"))
   106  					Expect(testUI.Err).To(Say("I am also a warning"))
   107  
   108  					Expect(fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceCallCount()).To(Equal(1))
   109  
   110  					appName, spaceGUID, bitsPath := fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceArgsForCall(0)
   111  					Expect(appName).To(Equal(app))
   112  					Expect(spaceGUID).To(Equal("some-space-guid"))
   113  					Expect(bitsPath).To(BeEmpty())
   114  				})
   115  			})
   116  
   117  			When("the create is unsuccessful", func() {
   118  				var expectedErr error
   119  
   120  				BeforeEach(func() {
   121  					expectedErr = errors.New("I am an error")
   122  					fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceReturns(resources.Package{}, v7action.Warnings{"I am a warning", "I am also a warning"}, expectedErr)
   123  				})
   124  
   125  				It("displays the header and error", func() {
   126  					Expect(executeErr).To(MatchError(expectedErr))
   127  
   128  					Expect(testUI.Out).To(Say("Creating and uploading bits package for app some-app in org some-org / space some-space as banana..."))
   129  
   130  					Expect(testUI.Err).To(Say("I am a warning"))
   131  					Expect(testUI.Err).To(Say("I am also a warning"))
   132  				})
   133  			})
   134  		})
   135  
   136  		When("the docker image is provided", func() {
   137  			BeforeEach(func() {
   138  				cmd.DockerImage.Path = "some-docker-image"
   139  				fakeActor.CreateDockerPackageByApplicationNameAndSpaceReturns(resources.Package{GUID: "1234"}, v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
   140  			})
   141  
   142  			It("creates the docker package", func() {
   143  				Expect(executeErr).ToNot(HaveOccurred())
   144  
   145  				Expect(testUI.Out).To(Say("Creating docker package for app some-app in org some-org / space some-space as banana..."))
   146  				Expect(testUI.Out).To(Say(`Package with guid '1234' has been created\.`))
   147  				Expect(testUI.Out).To(Say("OK"))
   148  
   149  				Expect(testUI.Err).To(Say("I am a warning"))
   150  				Expect(testUI.Err).To(Say("I am also a warning"))
   151  
   152  				Expect(fakeActor.CreateDockerPackageByApplicationNameAndSpaceCallCount()).To(Equal(1))
   153  
   154  				appName, spaceGUID, dockerImageCredentials := fakeActor.CreateDockerPackageByApplicationNameAndSpaceArgsForCall(0)
   155  				Expect(appName).To(Equal(app))
   156  				Expect(spaceGUID).To(Equal("some-space-guid"))
   157  				Expect(dockerImageCredentials.Path).To(Equal("some-docker-image"))
   158  			})
   159  		})
   160  
   161  		When("the path is provided", func() {
   162  			BeforeEach(func() {
   163  				cmd.AppPath = "some-app-path"
   164  				fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceReturns(resources.Package{GUID: "1234"}, v7action.Warnings{"I am a warning", "I am also a warning"}, nil)
   165  			})
   166  
   167  			It("creates the package using the specified directory", func() {
   168  				Expect(executeErr).ToNot(HaveOccurred())
   169  
   170  				Expect(testUI.Out).To(Say("Creating and uploading bits package for app some-app in org some-org / space some-space as banana..."))
   171  
   172  				Expect(testUI.Err).To(Say("I am a warning"))
   173  				Expect(testUI.Err).To(Say("I am also a warning"))
   174  
   175  				Expect(fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceCallCount()).To(Equal(1))
   176  
   177  				appName, spaceGUID, appPath := fakeActor.CreateAndUploadBitsPackageByApplicationNameAndSpaceArgsForCall(0)
   178  				Expect(appName).To(Equal(app))
   179  				Expect(spaceGUID).To(Equal("some-space-guid"))
   180  				Expect(appPath).To(Equal("some-app-path"))
   181  			})
   182  		})
   183  
   184  		When("the docker image and path are both provided", func() {
   185  			BeforeEach(func() {
   186  				cmd.AppPath = "some-app-path"
   187  				cmd.DockerImage.Path = "some-docker-image"
   188  			})
   189  
   190  			It("displays an argument combination error", func() {
   191  				argumentCombinationError := translatableerror.ArgumentCombinationError{
   192  					Args: []string{"--docker-image", "-o", "-p"},
   193  				}
   194  
   195  				Expect(executeErr).To(MatchError(argumentCombinationError))
   196  			})
   197  		})
   198  	})
   199  })