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