github.com/LukasHeimann/cloudfoundrycli/v8@v8.4.4/command/v7/download_droplet_command_test.go (about) 1 package v7_test 2 3 import ( 4 "errors" 5 "fmt" 6 "io/ioutil" 7 "os" 8 "path/filepath" 9 "regexp" 10 11 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/actionerror" 12 "github.com/LukasHeimann/cloudfoundrycli/v8/actor/v7action" 13 "github.com/LukasHeimann/cloudfoundrycli/v8/command/commandfakes" 14 "github.com/LukasHeimann/cloudfoundrycli/v8/command/translatableerror" 15 . "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7" 16 "github.com/LukasHeimann/cloudfoundrycli/v8/command/v7/v7fakes" 17 "github.com/LukasHeimann/cloudfoundrycli/v8/util/configv3" 18 "github.com/LukasHeimann/cloudfoundrycli/v8/util/ui" 19 . "github.com/onsi/ginkgo" 20 . "github.com/onsi/gomega" 21 . "github.com/onsi/gomega/gbytes" 22 ) 23 24 var _ = Describe("download-droplet Command", func() { 25 26 var ( 27 cmd DownloadDropletCommand 28 testUI *ui.UI 29 fakeConfig *commandfakes.FakeConfig 30 fakeSharedActor *commandfakes.FakeSharedActor 31 fakeActor *v7fakes.FakeActor 32 binaryName string 33 executeErr error 34 ) 35 36 BeforeEach(func() { 37 testUI = ui.NewTestUI(nil, NewBuffer(), NewBuffer()) 38 fakeConfig = new(commandfakes.FakeConfig) 39 fakeSharedActor = new(commandfakes.FakeSharedActor) 40 fakeActor = new(v7fakes.FakeActor) 41 42 cmd = DownloadDropletCommand{ 43 BaseCommand: BaseCommand{ 44 UI: testUI, 45 Config: fakeConfig, 46 SharedActor: fakeSharedActor, 47 Actor: fakeActor, 48 }, 49 } 50 51 cmd.RequiredArgs.AppName = "some-app" 52 53 binaryName = "faceman" 54 fakeConfig.BinaryNameReturns(binaryName) 55 fakeConfig.HasTargetedOrganizationReturns(true) 56 fakeConfig.TargetedOrganizationReturns(configv3.Organization{Name: "some-org"}) 57 fakeConfig.HasTargetedSpaceReturns(true) 58 fakeConfig.TargetedSpaceReturns(configv3.Space{ 59 GUID: "some-space-guid", 60 Name: "some-space"}) 61 fakeActor.GetCurrentUserReturns( 62 configv3.User{Name: "some-user"}, 63 nil) 64 }) 65 66 JustBeforeEach(func() { 67 executeErr = cmd.Execute(nil) 68 }) 69 70 When("checking target fails", func() { 71 BeforeEach(func() { 72 fakeSharedActor.CheckTargetReturns(actionerror.NotLoggedInError{BinaryName: binaryName}) 73 }) 74 75 It("returns an error if the check fails", func() { 76 Expect(executeErr).To(MatchError(actionerror.NotLoggedInError{BinaryName: "faceman"})) 77 78 Expect(fakeSharedActor.CheckTargetCallCount()).To(Equal(1)) 79 checkTargetedOrg, checkTargetedSpace := fakeSharedActor.CheckTargetArgsForCall(0) 80 Expect(checkTargetedOrg).To(BeTrue()) 81 Expect(checkTargetedSpace).To(BeTrue()) 82 }) 83 }) 84 85 When("downloading the droplet succeeds", func() { 86 var ( 87 pathToDropletFile string 88 dropletGUID string 89 ) 90 91 BeforeEach(func() { 92 dropletGUID = RandomString("fake-droplet-guid") 93 fakeActor.DownloadCurrentDropletByAppNameReturns([]byte("some-droplet-bytes"), dropletGUID, v7action.Warnings{"some-warning"}, nil) 94 95 currentDir, _ := os.Getwd() 96 pathToDropletFile = filepath.Join(currentDir, fmt.Sprintf("droplet_%s.tgz", dropletGUID)) 97 }) 98 99 AfterEach(func() { 100 Expect(os.Remove(pathToDropletFile)).ToNot(HaveOccurred()) 101 }) 102 103 It("creates a droplet tarball in the current directory", func() { 104 Expect(executeErr).ToNot(HaveOccurred()) 105 106 Expect(fakeActor.DownloadCurrentDropletByAppNameCallCount()).To(Equal(1)) 107 appArg, spaceGUIDArg := fakeActor.DownloadCurrentDropletByAppNameArgsForCall(0) 108 Expect(appArg).To(Equal("some-app")) 109 Expect(spaceGUIDArg).To(Equal("some-space-guid")) 110 111 fileContents, err := ioutil.ReadFile(pathToDropletFile) 112 Expect(err).ToNot(HaveOccurred()) 113 Expect(string(fileContents)).To(Equal("some-droplet-bytes")) 114 }) 115 116 It("displays the file it created and returns no errors", func() { 117 Expect(testUI.Out).To(Say("Downloading current droplet for app some-app in org some-org / space some-space as some-user...")) 118 Expect(testUI.Err).To(Say("some-warning")) 119 Expect(testUI.Out).To(Say(`Droplet downloaded successfully at .*droplet_%s.tgz`, dropletGUID)) 120 Expect(testUI.Out).To(Say("OK")) 121 Expect(executeErr).ToNot(HaveOccurred()) 122 }) 123 }) 124 125 When("the droplet guid is passed in", func() { 126 var ( 127 dropletGUID string 128 pathToDropletFile string 129 ) 130 131 BeforeEach(func() { 132 dropletGUID = RandomString("fake-droplet-guid") 133 pathToDropletFile = fmt.Sprintf("droplet_%s.tgz", dropletGUID) 134 135 setFlag(&cmd, "--droplet", dropletGUID) 136 137 fakeActor.DownloadDropletByGUIDAndAppNameReturns([]byte("some-droplet-bytes"), v7action.Warnings{"some-warning"}, nil) 138 }) 139 140 AfterEach(func() { 141 Expect(os.Remove(pathToDropletFile)).ToNot(HaveOccurred()) 142 }) 143 144 It("creates a droplet tarball in the current directory", func() { 145 Expect(executeErr).ToNot(HaveOccurred()) 146 147 Expect(fakeActor.DownloadDropletByGUIDAndAppNameCallCount()).To(Equal(1)) 148 dropletGUIDArg, appArg, spaceGUIDArg := fakeActor.DownloadDropletByGUIDAndAppNameArgsForCall(0) 149 Expect(dropletGUIDArg).To(Equal(dropletGUID)) 150 Expect(appArg).To(Equal("some-app")) 151 Expect(spaceGUIDArg).To(Equal("some-space-guid")) 152 153 fileContents, err := ioutil.ReadFile(pathToDropletFile) 154 Expect(err).ToNot(HaveOccurred()) 155 Expect(string(fileContents)).To(Equal("some-droplet-bytes")) 156 }) 157 158 It("displays the file it created and returns no errors", func() { 159 Expect(testUI.Out).To(Say("Downloading droplet %s for app some-app in org some-org / space some-space as some-user...", dropletGUID)) 160 Expect(testUI.Err).To(Say("some-warning")) 161 Expect(testUI.Out).To(Say(`Droplet downloaded successfully at .*droplet_%s.tgz`, dropletGUID)) 162 Expect(testUI.Out).To(Say("OK")) 163 Expect(executeErr).ToNot(HaveOccurred()) 164 }) 165 }) 166 167 When("a path to a file is passed in", func() { 168 var filePath string 169 BeforeEach(func() { 170 filePath = RandomString("fake-file") 171 172 setFlag(&cmd, "--path", filePath) 173 fakeActor.DownloadCurrentDropletByAppNameReturns([]byte("some-droplet"), "some-droplet-guid", v7action.Warnings{"some-warning"}, nil) 174 }) 175 176 AfterEach(func() { 177 Expect(os.Remove(filePath)).ToNot(HaveOccurred()) 178 }) 179 180 It("creates a droplet tarball at the specified path", func() { 181 Expect(executeErr).ToNot(HaveOccurred()) 182 183 fileContents, err := ioutil.ReadFile(filePath) 184 Expect(err).ToNot(HaveOccurred()) 185 Expect(string(fileContents)).To(Equal("some-droplet")) 186 }) 187 188 It("displays the file it created and returns no errors", func() { 189 Expect(testUI.Out).To(Say("Downloading current droplet for app some-app in org some-org / space some-space as some-user...")) 190 Expect(testUI.Err).To(Say("some-warning")) 191 Expect(testUI.Out).To(Say(`Droplet downloaded successfully at %s`, filePath)) 192 Expect(testUI.Out).To(Say("OK")) 193 Expect(executeErr).ToNot(HaveOccurred()) 194 }) 195 }) 196 197 When("a path to an existing directory is passed in", func() { 198 var tmpDir string 199 200 BeforeEach(func() { 201 var err error 202 tmpDir, err = ioutil.TempDir("", "droplets") 203 Expect(err).NotTo(HaveOccurred()) 204 205 setFlag(&cmd, "--path", tmpDir) 206 207 fakeActor.DownloadCurrentDropletByAppNameReturns([]byte("some-droplet"), "some-droplet-guid", v7action.Warnings{"some-warning"}, nil) 208 }) 209 210 AfterEach(func() { 211 Expect(os.RemoveAll(tmpDir)).ToNot(HaveOccurred()) 212 }) 213 214 It("creates a droplet tarball at the specified path", func() { 215 Expect(executeErr).ToNot(HaveOccurred()) 216 217 fileContents, err := ioutil.ReadFile(filepath.Join(tmpDir, "droplet_some-droplet-guid.tgz")) 218 Expect(err).ToNot(HaveOccurred()) 219 Expect(string(fileContents)).To(Equal("some-droplet")) 220 }) 221 222 It("displays the file it created and returns no errors", func() { 223 Expect(executeErr).ToNot(HaveOccurred()) 224 Expect(testUI.Out).To(Say("Downloading current droplet for app some-app in org some-org / space some-space as some-user...")) 225 Expect(testUI.Err).To(Say("some-warning")) 226 pathRegExp := regexp.QuoteMeta(filepath.Join(tmpDir, "droplet_some-droplet-guid.tgz")) 227 Expect(testUI.Out).To(Say(`Droplet downloaded successfully at %s`, pathRegExp)) 228 Expect(testUI.Out).To(Say("OK")) 229 }) 230 }) 231 232 When("a path to a file in an invalid directory is passed in", func() { 233 BeforeEach(func() { 234 cmd.Path = "not/exist/some-file.tgz" 235 fakeActor.DownloadCurrentDropletByAppNameReturns([]byte("some-droplet"), "some-droplet-guid", v7action.Warnings{"some-warning"}, nil) 236 }) 237 238 It("returns an appropriate error", func() { 239 _, ok := executeErr.(translatableerror.DropletFileError) 240 Expect(ok).To(BeTrue()) 241 }) 242 }) 243 244 When("there is an error downloading the droplet", func() { 245 BeforeEach(func() { 246 fakeActor.DownloadCurrentDropletByAppNameReturns([]byte{}, "", v7action.Warnings{"some-warning"}, errors.New("something went wrong")) 247 }) 248 249 It("displays warnings and returns an error", func() { 250 Expect(testUI.Err).To(Say("some-warning")) 251 Expect(executeErr).To(MatchError("something went wrong")) 252 }) 253 }) 254 255 When("the app does not have a current droplet", func() { 256 BeforeEach(func() { 257 fakeActor.DownloadCurrentDropletByAppNameReturns([]byte{}, "", v7action.Warnings{"some-warning"}, actionerror.DropletNotFoundError{}) 258 }) 259 260 It("displays warnings and returns an error", func() { 261 Expect(testUI.Err).To(Say("some-warning")) 262 Expect(executeErr).To(MatchError(translatableerror.NoDropletForAppError{AppName: "some-app"})) 263 }) 264 }) 265 })