github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/buildpack/create_buildpack_test.go (about)

     1  package buildpack_test
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf"
     7  	"code.cloudfoundry.org/cli/cf/api/apifakes"
     8  	"code.cloudfoundry.org/cli/cf/errors"
     9  	"code.cloudfoundry.org/cli/cf/requirements"
    10  	"code.cloudfoundry.org/cli/cf/requirements/requirementsfakes"
    11  	testcmd "code.cloudfoundry.org/cli/cf/util/testhelpers/commands"
    12  	testterm "code.cloudfoundry.org/cli/cf/util/testhelpers/terminal"
    13  
    14  	"code.cloudfoundry.org/cli/cf/commandregistry"
    15  	. "code.cloudfoundry.org/cli/cf/util/testhelpers/matchers"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("create-buildpack command", func() {
    21  	var (
    22  		requirementsFactory *requirementsfakes.FakeFactory
    23  		repo                *apifakes.OldFakeBuildpackRepository
    24  		bitsRepo            *apifakes.FakeBuildpackBitsRepository
    25  		ui                  *testterm.FakeUI
    26  		deps                commandregistry.Dependency
    27  	)
    28  
    29  	updateCommandDependency := func(pluginCall bool) {
    30  		deps.UI = ui
    31  		deps.RepoLocator = deps.RepoLocator.SetBuildpackRepository(repo)
    32  		deps.RepoLocator = deps.RepoLocator.SetBuildpackBitsRepository(bitsRepo)
    33  		commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("create-buildpack").SetDependency(deps, pluginCall))
    34  	}
    35  
    36  	BeforeEach(func() {
    37  		requirementsFactory = new(requirementsfakes.FakeFactory)
    38  		requirementsFactory.NewLoginRequirementReturns(requirements.Passing{})
    39  		repo = new(apifakes.OldFakeBuildpackRepository)
    40  		bitsRepo = new(apifakes.FakeBuildpackBitsRepository)
    41  		ui = &testterm.FakeUI{}
    42  	})
    43  
    44  	It("fails requirements when the user is not logged in", func() {
    45  		requirementsFactory.NewLoginRequirementReturns(requirements.Failing{Message: "not logged in"})
    46  		Expect(testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "my-dir", "0"}, requirementsFactory, updateCommandDependency, false, ui)).To(BeFalse())
    47  	})
    48  
    49  	It("fails with usage when given fewer than three arguments", func() {
    50  		testcmd.RunCLICommand("create-buildpack", []string{}, requirementsFactory, updateCommandDependency, false, ui)
    51  		Expect(ui.Outputs()).To(ContainSubstrings(
    52  			[]string{"Incorrect Usage", "Requires", "arguments"},
    53  		))
    54  	})
    55  
    56  	Context("when a file is provided", func() {
    57  		It("prints error and do not call create buildpack", func() {
    58  			bitsRepo.CreateBuildpackZipFileReturns(nil, "", fmt.Errorf("create buildpack error"))
    59  
    60  			testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "file", "5"}, requirementsFactory, updateCommandDependency, false, ui)
    61  
    62  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"}))
    63  			Expect(ui.Outputs()).To(ContainSubstrings([]string{"Failed to create a local temporary zip file for the buildpack"}))
    64  			Expect(ui.Outputs()).NotTo(ContainSubstrings([]string{"Creating buildpack"}))
    65  
    66  		})
    67  	})
    68  
    69  	Context("when a directory is provided", func() {
    70  		It("creates and uploads buildpacks", func() {
    71  			testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "my.war", "5"}, requirementsFactory, updateCommandDependency, false, ui)
    72  
    73  			Expect(repo.CreateBuildpack.Enabled).To(BeNil())
    74  			Expect(bitsRepo.CreateBuildpackZipFileCallCount()).To(Equal(1))
    75  			buildpackPath := bitsRepo.CreateBuildpackZipFileArgsForCall(0)
    76  			Expect(buildpackPath).To(Equal("my.war"))
    77  			Expect(ui.Outputs()).To(ContainSubstrings(
    78  				[]string{"Creating buildpack", "my-buildpack"},
    79  				[]string{"OK"},
    80  				[]string{"Uploading buildpack", "my-buildpack"},
    81  				[]string{"OK"},
    82  			))
    83  			Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"}))
    84  		})
    85  	})
    86  
    87  	Context("when a URL is provided", func() {
    88  		It("creates and uploads buildpacks", func() {
    89  			testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "https://some-url.com", "5"}, requirementsFactory, updateCommandDependency, false, ui)
    90  
    91  			Expect(repo.CreateBuildpack.Enabled).To(BeNil())
    92  			Expect(bitsRepo.CreateBuildpackZipFileCallCount()).To(Equal(1))
    93  			buildpackPath := bitsRepo.CreateBuildpackZipFileArgsForCall(0)
    94  			Expect(buildpackPath).To(Equal("https://some-url.com"))
    95  			Expect(ui.Outputs()).To(ContainSubstrings(
    96  				[]string{"Creating buildpack", "my-buildpack"},
    97  				[]string{"OK"},
    98  				[]string{"Uploading buildpack", "my-buildpack"},
    99  				[]string{"OK"},
   100  			))
   101  			Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"}))
   102  		})
   103  	})
   104  
   105  	It("warns the user when the buildpack with nil stack already exists", func() {
   106  		repo.CreateBuildpackWithNilStackExists = true
   107  		testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "my.war", "5"}, requirementsFactory, updateCommandDependency, false, ui)
   108  
   109  		Expect(ui.Outputs()).To(ContainSubstrings(
   110  			[]string{"Creating buildpack", "my-buildpack"},
   111  			[]string{"OK"},
   112  			[]string{"my-buildpack", "already exists without a stack"},
   113  			[]string{"TIP", "use", cf.Name, "delete-buildpack"},
   114  		))
   115  		Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"}))
   116  	})
   117  
   118  	It("warns the user when the buildpack with actual stack already exists", func() {
   119  		bitsRepo.UploadBuildpackReturns(errors.NewHTTPError(433, errors.BuildpackNameStackTaken, "test error"))
   120  		testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "my.war", "5"}, requirementsFactory, updateCommandDependency, false, ui)
   121  
   122  		By("printing server error in UI because that's the only way to include the stack in the error message")
   123  		Expect(ui.Outputs()).To(ContainSubstrings(
   124  			[]string{"Creating buildpack", "my-buildpack"},
   125  			[]string{"OK"},
   126  			[]string{"Uploading buildpack", "my-buildpack"},
   127  			[]string{"OK"},
   128  			[]string{"test error"},
   129  			[]string{"TIP", "use", cf.Name, "update-buildpack"},
   130  		))
   131  		Expect(ui.Outputs()).ToNot(ContainSubstrings([]string{"FAILED"}))
   132  	})
   133  
   134  	It("enables the buildpack when given the --enabled flag", func() {
   135  		testcmd.RunCLICommand("create-buildpack", []string{"--enable", "my-buildpack", "my.war", "5"}, requirementsFactory, updateCommandDependency, false, ui)
   136  
   137  		Expect(*repo.CreateBuildpack.Enabled).To(Equal(true))
   138  	})
   139  
   140  	It("disables the buildpack when given the --disable flag", func() {
   141  		testcmd.RunCLICommand("create-buildpack", []string{"--disable", "my-buildpack", "my.war", "5"}, requirementsFactory, updateCommandDependency, false, ui)
   142  
   143  		Expect(*repo.CreateBuildpack.Enabled).To(Equal(false))
   144  	})
   145  
   146  	It("alerts the user when uploading the buildpack bits fails", func() {
   147  		bitsRepo.UploadBuildpackReturns(fmt.Errorf("upload error"))
   148  
   149  		testcmd.RunCLICommand("create-buildpack", []string{"my-buildpack", "bogus/path", "5"}, requirementsFactory, updateCommandDependency, false, ui)
   150  
   151  		Expect(ui.Outputs()).To(ContainSubstrings(
   152  			[]string{"Creating buildpack", "my-buildpack"},
   153  			[]string{"OK"},
   154  			[]string{"Uploading buildpack"},
   155  			[]string{"FAILED"},
   156  		))
   157  	})
   158  })