More tests for errors

[finishes #104040604]

Signed-off-by: Chris Sun <lsun@pivotal.io>
This commit is contained in:
Evan Short
2015-10-21 15:04:44 -07:00
committed by Chris Sun
parent 95ab1bda22
commit e3a3a53dba
2 changed files with 149 additions and 116 deletions

View File

@@ -158,7 +158,7 @@ func (c *InCommand) downloadFile(url, destPath string) error {
defer resp.Body.Close() defer resp.Body.Close()
if resp.StatusCode != http.StatusOK { if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to download file: HTTP status %d: %s", resp.StatusCode, resp.Status) return fmt.Errorf("failed to download file `%s`: HTTP status %d", filepath.Base(destPath), resp.StatusCode)
} }
_, err = io.Copy(out, resp.Body) _, err = io.Copy(out, resp.Body)

View File

@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"io/ioutil" "io/ioutil"
"net/http"
"net/url" "net/url"
"os" "os"
"path" "path"
@@ -94,19 +95,23 @@ var _ = Describe("In Command", func() {
inRequest.Params = resource.InParams{ inRequest.Params = resource.InParams{
Globs: []string{"*.txt", "*.rtf"}, Globs: []string{"*.txt", "*.rtf"},
} }
inResponse, inErr = command.Run(destDir, inRequest)
}) })
It("succeeds", func() { It("succeeds", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Ω(inErr).ShouldNot(HaveOccurred()) Ω(inErr).ShouldNot(HaveOccurred())
}) })
It("returns the fetched version", func() { It("returns the fetched version", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Ω(inResponse.Version).Should(Equal(resource.Version{Tag: "v0.35.0"})) Ω(inResponse.Version).Should(Equal(resource.Version{Tag: "v0.35.0"}))
}) })
It("has some sweet metadata", func() { It("has some sweet metadata", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Ω(inResponse.Metadata).Should(ConsistOf( Ω(inResponse.Metadata).Should(ConsistOf(
resource.MetadataPair{Name: "url", Value: "http://google.com"}, resource.MetadataPair{Name: "url", Value: "http://google.com"},
resource.MetadataPair{Name: "name", Value: "release-name", URL: "http://google.com"}, resource.MetadataPair{Name: "name", Value: "release-name", URL: "http://google.com"},
@@ -115,12 +120,16 @@ var _ = Describe("In Command", func() {
}) })
It("downloads only the files that match the globs", func() { It("downloads only the files that match the globs", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(2)) Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(2))
Ω(githubClient.DownloadReleaseAssetArgsForCall(0)).Should(Equal(buildAsset(0, "example.txt"))) Ω(githubClient.DownloadReleaseAssetArgsForCall(0)).Should(Equal(buildAsset(0, "example.txt")))
Ω(githubClient.DownloadReleaseAssetArgsForCall(1)).Should(Equal(buildAsset(1, "example.rtf"))) Ω(githubClient.DownloadReleaseAssetArgsForCall(1)).Should(Equal(buildAsset(1, "example.rtf")))
}) })
It("does create the tag and version files", func() { It("does create the tag and version files", func() {
inResponse, inErr = command.Run(destDir, inRequest)
contents, err := ioutil.ReadFile(path.Join(destDir, "tag")) contents, err := ioutil.ReadFile(path.Join(destDir, "tag"))
Ω(err).ShouldNot(HaveOccurred()) Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("v0.35.0")) Ω(string(contents)).Should(Equal("v0.35.0"))
@@ -129,141 +138,165 @@ var _ = Describe("In Command", func() {
Ω(err).ShouldNot(HaveOccurred()) Ω(err).ShouldNot(HaveOccurred())
Ω(string(contents)).Should(Equal("0.35.0")) Ω(string(contents)).Should(Equal("0.35.0"))
}) })
})
Context("when valid asset filename globs are given and include_source_tarball is true", func() { Context("when include_source_tarball is true", func() {
BeforeEach(func() { var tarballUrl *url.URL
inRequest.Params = resource.InParams{
Globs: []string{"*.txt", "*.rtf"},
}
inRequest.Params.IncludeSourceTarball = true
tarballUrl, _ := url.Parse(githubServer.URL()) BeforeEach(func() {
tarballUrl.Path = "/gimme-a-tarball/" inRequest.Params.IncludeSourceTarball = true
githubClient.GetTarballLinkReturns(tarballUrl, nil)
githubServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", tarballUrl.Path),
ghttp.RespondWith(200, "source-tar-file-contents"),
),
)
inResponse, inErr = command.Run(destDir, inRequest) tarballUrl, _ = url.Parse(githubServer.URL())
tarballUrl.Path = "/gimme-a-tarball/"
})
Context("when getting the tarball link succeeds", func() {
BeforeEach(func() {
githubClient.GetTarballLinkReturns(tarballUrl, nil)
})
Context("when downloading the tarball succeeds", func() {
BeforeEach(func() {
githubServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", tarballUrl.Path),
ghttp.RespondWith(http.StatusOK, "source-tar-file-contents"),
),
)
})
It("succeeds", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Expect(inErr).ToNot(HaveOccurred())
})
It("downloads the source tarball", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Expect(githubServer.ReceivedRequests()).To(HaveLen(1))
})
It("saves the source tarball in the destination directory", func() {
inResponse, inErr = command.Run(destDir, inRequest)
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.tar.gz"))
fContents := string(fileContents)
Expect(err).NotTo(HaveOccurred())
Expect(fContents).To(Equal("source-tar-file-contents"))
})
})
Context("when downloading the tarball fails", func() {
BeforeEach(func() {
githubServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", tarballUrl.Path),
ghttp.RespondWith(http.StatusInternalServerError, ""),
),
)
})
It("returns an appropriate error", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Expect(inErr).To(MatchError("failed to download file `source.tar.gz`: HTTP status 500"))
})
})
})
Context("when getting the tarball link fails", func() {
disaster := errors.New("oh my")
BeforeEach(func() {
githubClient.GetTarballLinkReturns(nil, disaster)
})
It("returns the error", func() {
inResponse, inErr = command.Run(destDir, inRequest)
Expect(inErr).To(Equal(disaster))
})
})
}) })
It("succeeds", func() { Context("when include_source_zip is true", func() {
Ω(inErr).ShouldNot(HaveOccurred()) var zipUrl *url.URL
})
It("downloads only the files that match the globs", func() { BeforeEach(func() {
Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(2)) inRequest.Params.IncludeSourceZip = true
Ω(githubClient.DownloadReleaseAssetArgsForCall(0)).Should(Equal(buildAsset(0, "example.txt")))
Ω(githubClient.DownloadReleaseAssetArgsForCall(1)).Should(Equal(buildAsset(1, "example.rtf")))
})
It("downloads the source tarball", func() { zipUrl, _ = url.Parse(githubServer.URL())
Expect(githubServer.ReceivedRequests()).To(HaveLen(1)) zipUrl.Path = "/gimme-a-zip/"
}) })
It("saves the source tarball in the destination directory", func() { Context("when getting the zip link succeeds", func() {
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.tar.gz")) BeforeEach(func() {
fContents := string(fileContents) githubClient.GetZipballLinkReturns(zipUrl, nil)
Expect(err).NotTo(HaveOccurred()) })
Expect(fContents).To(Equal("source-tar-file-contents"))
})
})
Context("when include_source_tarball is true and no globs are specified", func() { Context("when downloading the zip succeeds", func() {
BeforeEach(func() { BeforeEach(func() {
inRequest.Params = resource.InParams{} githubServer.AppendHandlers(
inRequest.Params.IncludeSourceTarball = true ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", zipUrl.Path),
ghttp.RespondWith(http.StatusOK, "source-zip-file-contents"),
),
)
})
tarballUrl, _ := url.Parse(githubServer.URL()) It("succeeds", func() {
tarballUrl.Path = "/gimme-a-tarball/" inResponse, inErr = command.Run(destDir, inRequest)
githubClient.GetTarballLinkReturns(tarballUrl, nil)
githubServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", tarballUrl.Path),
ghttp.RespondWith(200, "source-tar-file-contents"),
),
)
inResponse, inErr = command.Run(destDir, inRequest) Expect(inErr).ToNot(HaveOccurred())
}) })
It("succeeds", func() { It("downloads the source zip", func() {
Ω(inErr).ShouldNot(HaveOccurred()) inResponse, inErr = command.Run(destDir, inRequest)
})
It("downloads all the assets", func() { Expect(githubServer.ReceivedRequests()).To(HaveLen(1))
Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(3)) })
Expect(githubClient.DownloadReleaseAssetArgsForCall(0)).To(Equal(buildAsset(0, "example.txt")))
Expect(githubClient.DownloadReleaseAssetArgsForCall(1)).To(Equal(buildAsset(1, "example.rtf")))
Expect(githubClient.DownloadReleaseAssetArgsForCall(2)).To(Equal(buildAsset(2, "example.wtf")))
})
It("downloads the source tarball", func() { It("saves the source zip in the destination directory", func() {
Expect(githubServer.ReceivedRequests()).To(HaveLen(1)) inResponse, inErr = command.Run(destDir, inRequest)
})
It("saves the source tarball in the destination directory", func() { fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.zip"))
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.tar.gz")) fContents := string(fileContents)
Expect(err).NotTo(HaveOccurred()) Expect(err).NotTo(HaveOccurred())
Expect(string(fileContents)).To(Equal("source-tar-file-contents")) Expect(fContents).To(Equal("source-zip-file-contents"))
}) })
}) })
Context("when include_source_zip is true and no globs are specified", func() { Context("when downloading the zip fails", func() {
BeforeEach(func() { BeforeEach(func() {
inRequest.Params = resource.InParams{} githubServer.AppendHandlers(
inRequest.Params.IncludeSourceZip = true ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", zipUrl.Path),
ghttp.RespondWith(http.StatusInternalServerError, ""),
),
)
})
tarballUrl, _ := url.Parse(githubServer.URL()) It("returns an appropriate error", func() {
tarballUrl.Path = "/gimme-a-zip/" inResponse, inErr = command.Run(destDir, inRequest)
githubClient.GetZipballLinkReturns(tarballUrl, nil)
githubServer.AppendHandlers(
ghttp.CombineHandlers(
ghttp.VerifyRequest("GET", tarballUrl.Path),
ghttp.RespondWith(200, "source-zip-file-contents"),
),
)
inResponse, inErr = command.Run(destDir, inRequest) Expect(inErr).To(MatchError("failed to download file `source.zip`: HTTP status 500"))
}) })
})
})
It("succeeds", func() { Context("when getting the zip link fails", func() {
Ω(inErr).ShouldNot(HaveOccurred()) disaster := errors.New("oh my")
})
It("downloads all the assets", func() { BeforeEach(func() {
Expect(githubClient.DownloadReleaseAssetCallCount()).To(Equal(3)) githubClient.GetZipballLinkReturns(nil, disaster)
Expect(githubClient.DownloadReleaseAssetArgsForCall(0)).To(Equal(buildAsset(0, "example.txt"))) })
Expect(githubClient.DownloadReleaseAssetArgsForCall(1)).To(Equal(buildAsset(1, "example.rtf")))
Expect(githubClient.DownloadReleaseAssetArgsForCall(2)).To(Equal(buildAsset(2, "example.wtf")))
})
It("downloads the source zip", func() { It("returns the error", func() {
Expect(githubServer.ReceivedRequests()).To(HaveLen(1)) inResponse, inErr = command.Run(destDir, inRequest)
})
It("saves the source zip in the destination directory", func() { Expect(inErr).To(Equal(disaster))
fileContents, err := ioutil.ReadFile(filepath.Join(destDir, "source.zip")) })
Expect(err).NotTo(HaveOccurred()) })
Expect(string(fileContents)).To(Equal("source-zip-file-contents"))
})
})
Context("when an invalid asset filename glob is given", func() {
BeforeEach(func() {
inRequest.Params = resource.InParams{
Globs: []string{`[`},
}
inResponse, inErr = command.Run(destDir, inRequest)
})
It("returns an error", func() {
Ω(inErr).Should(HaveOccurred())
}) })
}) })