github.com/loggregator/cli@v6.33.1-0.20180224010324-82334f081791+incompatible/cf/commands/buildpack/update_buildpack_test.go (about) 1 package buildpack_test 2 3 import ( 4 "errors" 5 "fmt" 6 7 "code.cloudfoundry.org/cli/cf/api/apifakes" 8 "code.cloudfoundry.org/cli/cf/models" 9 "code.cloudfoundry.org/cli/cf/requirements" 10 "code.cloudfoundry.org/cli/cf/requirements/requirementsfakes" 11 testcmd "code.cloudfoundry.org/cli/util/testhelpers/commands" 12 testterm "code.cloudfoundry.org/cli/util/testhelpers/terminal" 13 14 "code.cloudfoundry.org/cli/cf/commandregistry" 15 . "code.cloudfoundry.org/cli/util/testhelpers/matchers" 16 . "github.com/onsi/ginkgo" 17 . "github.com/onsi/gomega" 18 ) 19 20 func successfulUpdate(ui *testterm.FakeUI, buildpackName string) { 21 Expect(ui.Outputs()).To(ContainSubstrings( 22 []string{"Updating buildpack", buildpackName}, 23 []string{"OK"}, 24 )) 25 } 26 27 func failedUpdate(ui *testterm.FakeUI, buildpackName string) { 28 Expect(ui.Outputs()).To(ContainSubstrings( 29 []string{"Updating buildpack", buildpackName}, 30 []string{"FAILED"}, 31 )) 32 } 33 34 var _ = Describe("Updating buildpack command", func() { 35 var ( 36 requirementsFactory *requirementsfakes.FakeFactory 37 ui *testterm.FakeUI 38 repo *apifakes.OldFakeBuildpackRepository 39 bitsRepo *apifakes.FakeBuildpackBitsRepository 40 deps commandregistry.Dependency 41 42 buildpackName string 43 ) 44 45 updateCommandDependency := func(pluginCall bool) { 46 deps.UI = ui 47 deps.RepoLocator = deps.RepoLocator.SetBuildpackRepository(repo) 48 deps.RepoLocator = deps.RepoLocator.SetBuildpackBitsRepository(bitsRepo) 49 commandregistry.Commands.SetCommand(commandregistry.Commands.FindCommand("update-buildpack").SetDependency(deps, pluginCall)) 50 } 51 52 BeforeEach(func() { 53 buildpackName = "my-buildpack" 54 55 requirementsFactory = new(requirementsfakes.FakeFactory) 56 requirementsFactory.NewLoginRequirementReturns(requirements.Passing{}) 57 buildpackReq := new(requirementsfakes.FakeBuildpackRequirement) 58 buildpackReq.GetBuildpackReturns(models.Buildpack{Name: buildpackName}) 59 requirementsFactory.NewBuildpackRequirementReturns(buildpackReq) 60 ui = new(testterm.FakeUI) 61 repo = new(apifakes.OldFakeBuildpackRepository) 62 bitsRepo = new(apifakes.FakeBuildpackBitsRepository) 63 }) 64 65 runCommand := func(args ...string) bool { 66 return testcmd.RunCLICommand("update-buildpack", args, requirementsFactory, updateCommandDependency, false, ui) 67 } 68 69 Context("is only successful on login and buildpack success", func() { 70 It("returns success when both are true", func() { 71 Expect(runCommand(buildpackName)).To(BeTrue()) 72 }) 73 74 It("returns failure when at requirements error", func() { 75 buildpackReq := new(requirementsfakes.FakeBuildpackRequirement) 76 buildpackReq.ExecuteReturns(errors.New("no build pack")) 77 requirementsFactory.NewBuildpackRequirementReturns(buildpackReq) 78 79 Expect(runCommand(buildpackName, "-p", "buildpack.zip", "extraArg")).To(BeFalse()) 80 }) 81 }) 82 83 Context("when a file is provided", func() { 84 It("prints error and do not call create buildpack", func() { 85 bitsRepo.CreateBuildpackZipFileReturns(nil, "", fmt.Errorf("create buildpack error")) 86 87 Expect(runCommand(buildpackName, "-p", "file")).To(BeFalse()) 88 89 Expect(ui.Outputs()).To(ContainSubstrings([]string{"Failed to create a local temporary zip file for the buildpack"})) 90 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 91 Expect(bitsRepo.UploadBuildpackCallCount()).To(Equal(0)) 92 93 }) 94 }) 95 96 Context("when a path is provided", func() { 97 It("updates buildpack", func() { 98 runCommand(buildpackName) 99 100 successfulUpdate(ui, buildpackName) 101 }) 102 }) 103 104 Context("when a URL is provided", func() { 105 It("updates buildpack", func() { 106 testcmd.RunCLICommand("update-buildpack", []string{"my-buildpack", "-p", "https://some-url.com"}, requirementsFactory, updateCommandDependency, false, ui) 107 108 Expect(bitsRepo.CreateBuildpackZipFileCallCount()).To(Equal(1)) 109 buildpackPath := bitsRepo.CreateBuildpackZipFileArgsForCall(0) 110 Expect(buildpackPath).To(Equal("https://some-url.com")) 111 successfulUpdate(ui, buildpackName) 112 }) 113 }) 114 115 Context("updates buildpack when passed the proper flags", func() { 116 Context("position flag", func() { 117 It("sets the position when passed a value", func() { 118 runCommand("-i", "999", buildpackName) 119 120 Expect(*repo.UpdateBuildpackArgs.Buildpack.Position).To(Equal(999)) 121 successfulUpdate(ui, buildpackName) 122 }) 123 124 It("defaults to nil when not passed", func() { 125 runCommand(buildpackName) 126 127 Expect(repo.UpdateBuildpackArgs.Buildpack.Position).To(BeNil()) 128 }) 129 }) 130 131 Context("enabling/disabling buildpacks", func() { 132 It("can enable buildpack", func() { 133 runCommand("--enable", buildpackName) 134 135 Expect(repo.UpdateBuildpackArgs.Buildpack.Enabled).NotTo(BeNil()) 136 Expect(*repo.UpdateBuildpackArgs.Buildpack.Enabled).To(Equal(true)) 137 138 successfulUpdate(ui, buildpackName) 139 }) 140 141 It("can disable buildpack", func() { 142 runCommand("--disable", buildpackName) 143 144 Expect(repo.UpdateBuildpackArgs.Buildpack.Enabled).NotTo(BeNil()) 145 Expect(*repo.UpdateBuildpackArgs.Buildpack.Enabled).To(Equal(false)) 146 }) 147 148 It("defaults to nil when not passed", func() { 149 runCommand(buildpackName) 150 151 Expect(repo.UpdateBuildpackArgs.Buildpack.Enabled).To(BeNil()) 152 }) 153 }) 154 155 Context("buildpack path", func() { 156 It("uploads buildpack when passed", func() { 157 runCommand("-p", "buildpack.zip", buildpackName) 158 Expect(bitsRepo.CreateBuildpackZipFileCallCount()).To(Equal(1)) 159 buildpackPath := bitsRepo.CreateBuildpackZipFileArgsForCall(0) 160 Expect(buildpackPath).To(Equal("buildpack.zip")) 161 162 successfulUpdate(ui, buildpackName) 163 }) 164 165 It("errors when passed invalid path", func() { 166 bitsRepo.UploadBuildpackReturns(fmt.Errorf("upload error")) 167 168 runCommand("-p", "bogus/path", buildpackName) 169 170 failedUpdate(ui, buildpackName) 171 }) 172 }) 173 174 Context("locking buildpack", func() { 175 It("can lock a buildpack", func() { 176 runCommand("--lock", buildpackName) 177 178 Expect(repo.UpdateBuildpackArgs.Buildpack.Locked).NotTo(BeNil()) 179 Expect(*repo.UpdateBuildpackArgs.Buildpack.Locked).To(Equal(true)) 180 181 successfulUpdate(ui, buildpackName) 182 }) 183 184 It("can unlock a buildpack", func() { 185 runCommand("--unlock", buildpackName) 186 187 successfulUpdate(ui, buildpackName) 188 }) 189 190 Context("Unsuccessful locking", func() { 191 It("lock fails when passed invalid path", func() { 192 runCommand("--lock", "-p", "buildpack.zip", buildpackName) 193 194 failedUpdate(ui, buildpackName) 195 }) 196 197 It("unlock fails when passed invalid path", func() { 198 runCommand("--unlock", "-p", "buildpack.zip", buildpackName) 199 200 failedUpdate(ui, buildpackName) 201 }) 202 }) 203 }) 204 }) 205 })