github.com/swisscom/cloudfoundry-cli@v7.1.0+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/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 func successfulUpdate(ui *testterm.FakeUI, buildpackName string) { 21 Expect(ui.Outputs()).To(ContainSubstrings( 22 []string{"Updating buildpack", buildpackName, " with stack cflinuxfs99"}, 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, GUID: "buildpack-guid", Stack: "cflinuxfs99"}) 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 with valid arguments 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 invalid arguments are passed", func() { 75 buildpackReq := new(requirementsfakes.FakeBuildpackRequirement) 76 requirementsFactory.NewBuildpackRequirementReturns(buildpackReq) 77 78 Expect(runCommand(buildpackName, "-p", "buildpack.zip", "extraArg")).To(BeFalse()) 79 Expect(ui.Outputs()).To(ContainSubstrings([]string{"Incorrect Usage"})) 80 }) 81 82 It("returns error messages from requirements errors", func() { 83 buildpackReq := new(requirementsfakes.FakeBuildpackRequirement) 84 buildpackReq.ExecuteReturns(errors.New("no build pack")) 85 requirementsFactory.NewBuildpackRequirementReturns(buildpackReq) 86 87 Expect(runCommand(buildpackName, "-p", "buildpack.zip")).To(BeFalse()) 88 Expect(ui.Outputs()).To(ContainSubstrings([]string{"no build pack"})) 89 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 90 }) 91 }) 92 93 Context("when a file is provided", func() { 94 It("prints error and do not call create buildpack", func() { 95 bitsRepo.CreateBuildpackZipFileReturns(nil, "", fmt.Errorf("create buildpack error")) 96 97 Expect(runCommand(buildpackName, "-p", "file")).To(BeFalse()) 98 99 Expect(ui.Outputs()).To(ContainSubstrings([]string{"Failed to create a local temporary zip file for the buildpack"})) 100 Expect(ui.Outputs()).To(ContainSubstrings([]string{"FAILED"})) 101 Expect(bitsRepo.UploadBuildpackCallCount()).To(Equal(0)) 102 103 }) 104 }) 105 106 Context("when a path is provided", func() { 107 It("updates buildpack", func() { 108 runCommand(buildpackName) 109 110 successfulUpdate(ui, buildpackName) 111 }) 112 }) 113 114 Context("when a URL is provided", func() { 115 It("updates buildpack", func() { 116 testcmd.RunCLICommand("update-buildpack", []string{"my-buildpack", "-p", "https://some-url.com"}, requirementsFactory, updateCommandDependency, false, ui) 117 118 Expect(bitsRepo.CreateBuildpackZipFileCallCount()).To(Equal(1)) 119 buildpackPath := bitsRepo.CreateBuildpackZipFileArgsForCall(0) 120 Expect(buildpackPath).To(Equal("https://some-url.com")) 121 successfulUpdate(ui, buildpackName) 122 }) 123 }) 124 125 Describe("flags", func() { 126 Context("stack flag", func() { 127 It("updates the specific buildpack by name and stack, when stack is provided", func() { 128 runCommand("-i", "999", buildpackName, "-s", "cflinuxfs99") 129 130 Expect(requirementsFactory.NewBuildpackRequirementCallCount()).To(Equal(1)) 131 buildpack, stack := requirementsFactory.NewBuildpackRequirementArgsForCall(0) 132 Expect(buildpack).To(Equal(buildpackName)) 133 Expect(stack).To(Equal("cflinuxfs99")) 134 135 Expect(*repo.UpdateBuildpackArgs.Buildpack.Position).To(Equal(999)) 136 Expect(repo.UpdateBuildpackArgs.Buildpack.GUID).To(Equal("buildpack-guid")) 137 successfulUpdate(ui, buildpackName) 138 }) 139 }) 140 141 Context("position flag", func() { 142 It("sets the position when passed a value", func() { 143 runCommand("-i", "999", buildpackName) 144 145 Expect(*repo.UpdateBuildpackArgs.Buildpack.Position).To(Equal(999)) 146 successfulUpdate(ui, buildpackName) 147 }) 148 149 It("defaults to nil when not passed", func() { 150 runCommand(buildpackName) 151 152 Expect(repo.UpdateBuildpackArgs.Buildpack.Position).To(BeNil()) 153 }) 154 }) 155 156 Context("enabling/disabling buildpacks", func() { 157 It("can enable buildpack", func() { 158 runCommand("--enable", buildpackName) 159 160 Expect(repo.UpdateBuildpackArgs.Buildpack.Enabled).NotTo(BeNil()) 161 Expect(*repo.UpdateBuildpackArgs.Buildpack.Enabled).To(Equal(true)) 162 163 successfulUpdate(ui, buildpackName) 164 }) 165 166 It("can disable buildpack", func() { 167 runCommand("--disable", buildpackName) 168 169 Expect(repo.UpdateBuildpackArgs.Buildpack.Enabled).NotTo(BeNil()) 170 Expect(*repo.UpdateBuildpackArgs.Buildpack.Enabled).To(Equal(false)) 171 }) 172 173 It("defaults to nil when not passed", func() { 174 runCommand(buildpackName) 175 176 Expect(repo.UpdateBuildpackArgs.Buildpack.Enabled).To(BeNil()) 177 }) 178 }) 179 180 Context("buildpack path", func() { 181 It("uploads buildpack when passed", func() { 182 runCommand("-p", "buildpack.zip", buildpackName) 183 Expect(bitsRepo.CreateBuildpackZipFileCallCount()).To(Equal(1)) 184 buildpackPath := bitsRepo.CreateBuildpackZipFileArgsForCall(0) 185 Expect(buildpackPath).To(Equal("buildpack.zip")) 186 187 successfulUpdate(ui, buildpackName) 188 }) 189 190 It("errors when passed invalid path", func() { 191 bitsRepo.UploadBuildpackReturns(fmt.Errorf("upload error")) 192 193 runCommand("-p", "bogus/path", buildpackName) 194 195 failedUpdate(ui, buildpackName) 196 }) 197 }) 198 199 Context("locking buildpack", func() { 200 It("can lock a buildpack", func() { 201 runCommand("--lock", buildpackName) 202 203 Expect(repo.UpdateBuildpackArgs.Buildpack.Locked).NotTo(BeNil()) 204 Expect(*repo.UpdateBuildpackArgs.Buildpack.Locked).To(Equal(true)) 205 206 successfulUpdate(ui, buildpackName) 207 }) 208 209 It("can unlock a buildpack", func() { 210 runCommand("--unlock", buildpackName) 211 212 successfulUpdate(ui, buildpackName) 213 }) 214 215 Context("Unsuccessful locking", func() { 216 It("lock fails when passed invalid path", func() { 217 runCommand("--lock", "-p", "buildpack.zip", buildpackName) 218 219 failedUpdate(ui, buildpackName) 220 }) 221 222 It("unlock fails when passed invalid path", func() { 223 runCommand("--unlock", "-p", "buildpack.zip", buildpackName) 224 225 failedUpdate(ui, buildpackName) 226 }) 227 }) 228 }) 229 }) 230 })