Skip to content

Commit

Permalink
Adjust upload_blob action message structure
Browse files Browse the repository at this point in the history
- Change the upload_blob action message structure to be a json object.
- Easier to work with for later.

[#129398071](https://www.pivotaltracker.com/story/show/129398071)

Signed-off-by: Jamil Shamy <[email protected]>
  • Loading branch information
andrew-su authored and pivotal-jamil-shamy committed Nov 16, 2016
1 parent e82bdd1 commit 15a9163
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
21 changes: 14 additions & 7 deletions agent/action/upload_blob.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ import (
"encoding/hex"
"errors"
"fmt"

"github.com/cloudfoundry/bosh-utils/blobstore"
)

type UploadBlobSpec struct {
BlobID string `json:"blob_id"`
Sha1 string `json:"sha1"`
Payload string `json:"payload"`
}

type UploadBlobAction struct {
blobManager blobstore.BlobManagerInterface
}
Expand All @@ -30,22 +37,22 @@ func (a UploadBlobAction) IsLoggable() bool {
return false
}

func (a UploadBlobAction) Run(base64Payload, payloadSha1, blobID string) (string, error) {
func (a UploadBlobAction) Run(content UploadBlobSpec) (string, error) {

decodedPayload, err := base64.StdEncoding.DecodeString(base64Payload)
decodedPayload, err := base64.StdEncoding.DecodeString(content.Payload)
if err != nil {
return blobID, err
return content.BlobID, err
}

if err = a.validatePayload(decodedPayload, payloadSha1); err != nil {
return blobID, err
if err = a.validatePayload(decodedPayload, content.Sha1); err != nil {
return content.BlobID, err
}

reader := bytes.NewReader(decodedPayload)

err = a.blobManager.Write(blobID, reader)
err = a.blobManager.Write(content.BlobID, reader)

return blobID, err
return content.BlobID, err
}

func (a UploadBlobAction) validatePayload(payload []byte, payloadSha1 string) error {
Expand Down
25 changes: 21 additions & 4 deletions agent/action/upload_blob_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package action_test

import (
"errors"

. "github.com/cloudfoundry/bosh-agent/agent/action"
. "github.com/cloudfoundry/bosh-utils/blobstore/fakes"
. "github.com/onsi/ginkgo"
Expand Down Expand Up @@ -31,26 +32,42 @@ func init() {
Describe("Run", func() {
Context("Payload Validation", func() {
It("validates the payload using provided SHA1", func() {
_, err := action.Run("Y2xvdWRmb3VuZHJ5", "e578935e2f0613d68ba6a4fcc0d32754b52d334d", "id")
_, err := action.Run(UploadBlobSpec{
Payload: "Y2xvdWRmb3VuZHJ5",
Sha1: "e578935e2f0613d68ba6a4fcc0d32754b52d334d",
BlobID: "id",
})
Expect(err).ToNot(HaveOccurred())
})

It("does not validate the payload when the SHA1 is incorrect", func() {
_, err := action.Run("Y2xvdWRmb3VuZHJ5", "badsha1", "id")
_, err := action.Run(UploadBlobSpec{
Payload: "Y2xvdWRmb3VuZHJ5",
Sha1: "badsha1",
BlobID: "id",
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("Payload corrupted. SHA1 mismatch. Expected badsha1 but received e578935e2f0613d68ba6a4fcc0d32754b52d334d"))
})
})

It("should call the blob manager", func() {
_, err := action.Run("Y2xvdWRmb3VuZHJ5", "e578935e2f0613d68ba6a4fcc0d32754b52d334d", "id")
_, err := action.Run(UploadBlobSpec{
Payload: "Y2xvdWRmb3VuZHJ5",
Sha1: "e578935e2f0613d68ba6a4fcc0d32754b52d334d",
BlobID: "id",
})
Expect(err).ToNot(HaveOccurred())
Expect(fakeBlobManager.WriteCallCount()).To(Equal(1))
})

It("should return an error if the blob manager fails", func() {
fakeBlobManager.WriteReturns(errors.New("blob write error"))
_, err := action.Run("Y2xvdWRmb3VuZHJ5", "e578935e2f0613d68ba6a4fcc0d32754b52d334d", "id")
_, err := action.Run(UploadBlobSpec{
Payload: "Y2xvdWRmb3VuZHJ5",
Sha1: "e578935e2f0613d68ba6a4fcc0d32754b52d334d",
BlobID: "id",
})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(Equal("blob write error"))
})
Expand Down

0 comments on commit 15a9163

Please sign in to comment.