Merge lp:~julian-edwards/gwacl/putblocklist into lp:gwacl

Proposed by Julian Edwards
Status: Merged
Approved by: Julian Edwards
Approved revision: 47
Merged at revision: 46
Proposed branch: lp:~julian-edwards/gwacl/putblocklist
Merge into: lp:gwacl
Diff against target: 107 lines (+85/-0)
2 files modified
storage.go (+25/-0)
storage_test.go (+60/-0)
To merge this branch: bzr merge lp:~julian-edwards/gwacl/putblocklist
Reviewer Review Type Date Requested Status
Gavin Panella Approve
Review via email: mp+154618@code.launchpad.net

Commit message

Add a PutBlockList method for piecing together block uploads.

To post a comment you must log in.
Revision history for this message
Gavin Panella (allenap) wrote :

Here and elsewhere we're not checking that the HTTP method is correct in the request.

review: Approve
47. By Julian Edwards

check request method

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'storage.go'
2--- storage.go 2013-03-21 06:58:25 +0000
3+++ storage.go 2013-03-21 07:49:22 +0000
4@@ -341,3 +341,28 @@
5 }
6 return nil
7 }
8+
9+// Send a request to piece together blocks into a list that specifies a blob.
10+func (context *StorageContext) PutBlockList(container, filename string, blocklist *BlockList) error {
11+ uri := fmt.Sprintf(
12+ "http://%s.blob.core.windows.net/%s/%s?comp=blocklist",
13+ context.Account, container, filename)
14+ data, err := blocklist.Serialize()
15+ if err != nil {
16+ return err
17+ }
18+ data_reader := bytes.NewReader(data)
19+ req, err := http.NewRequest("PUT", uri, data_reader)
20+ if err != nil {
21+ return err
22+ }
23+ addStandardHeaders(req, context.Account, context.Key, "2012-02-12")
24+ resp, err := context.send(req, nil)
25+ if err != nil {
26+ return err
27+ }
28+ if resp.StatusCode != http.StatusCreated {
29+ return fmt.Errorf("failed to put blocklist: %s", resp.Status)
30+ }
31+ return nil
32+}
33
34=== modified file 'storage_test.go'
35--- storage_test.go 2013-03-21 06:58:25 +0000
36+++ storage_test.go 2013-03-21 07:49:22 +0000
37@@ -9,6 +9,7 @@
38 "fmt"
39 "io/ioutil"
40 . "launchpad.net/gocheck"
41+ "launchpad.net/gwacl/dedent"
42 "net/http"
43 "net/url"
44 "strings"
45@@ -593,3 +594,62 @@
46 err := context.PutBlock("container", "blobname", "blockid", data_reader)
47 c.Assert(err.Error(), Equals, "failed to put block: 202 Frotzed")
48 }
49+
50+type TestPutBlockList struct{}
51+
52+var _ = Suite(&TestPutBlockList{})
53+
54+func (suite *TestPutBlockList) Test(c *C) {
55+ context := makeStorageContext()
56+ response := &http.Response{
57+ Status: fmt.Sprintf("%d", http.StatusCreated),
58+ StatusCode: http.StatusCreated,
59+ }
60+ transport := &TestTransport{Response: response}
61+ context.client = &http.Client{Transport: transport}
62+ blocklist := &BlockList{}
63+ blocklist.Add(BlockListLatest, "b1")
64+ blocklist.Add(BlockListLatest, "b2")
65+ err := context.PutBlockList("container", "blobname", blocklist)
66+ c.Assert(err, IsNil)
67+
68+ c.Check(transport.Request.Method, Equals, "PUT")
69+ c.Check(transport.Request.URL.String(), Equals, fmt.Sprintf(
70+ "http://%s.blob.core.windows.net/container/blobname?comp=blocklist",
71+ context.Account))
72+ c.Check(transport.Request.Header.Get("Authorization"), Not(Equals), "")
73+
74+ data, err := ioutil.ReadAll(transport.Request.Body)
75+ c.Assert(err, IsNil)
76+ expected := dedent.Dedent(`
77+ <BlockList>
78+ <Latest>b1</Latest>
79+ <Latest>b2</Latest>
80+ </BlockList>`)
81+ c.Check(string(data), Equals, expected)
82+}
83+
84+// Client-side errors from the HTTP client are propagated back to the caller.
85+func (suite *TestPutBlockList) TestError(c *C) {
86+ error := fmt.Errorf("canned-error")
87+ transport := &TestTransport{Error: error}
88+ context := makeStorageContext()
89+ context.client = &http.Client{Transport: transport}
90+ blocklist := &BlockList{}
91+ err := context.PutBlockList("container", "blobname", blocklist)
92+ c.Assert(err, Not(IsNil))
93+}
94+
95+// Server-side errors are propagated back to the caller.
96+func (suite *TestPutBlockList) TestErrorResponse(c *C) {
97+ response := &http.Response{
98+ Status: "202 Frotzed",
99+ StatusCode: 202,
100+ }
101+ transport := &TestTransport{Response: response}
102+ context := makeStorageContext()
103+ context.client = &http.Client{Transport: transport}
104+ blocklist := &BlockList{}
105+ err := context.PutBlockList("container", "blobname", blocklist)
106+ c.Assert(err.Error(), Equals, "failed to put blocklist: 202 Frotzed")
107+}

Subscribers

People subscribed via source and target branches

to all changes: