~abp998/gwacl/subscription

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
// Copyright 2013 Canonical Ltd.  This software is licensed under the
// GNU Lesser General Public License version 3 (see the file COPYING).

package gwacl

import (
    "bytes"
    "encoding/base64"
    "fmt"
    "io/ioutil"
    . "launchpad.net/gocheck"
    "net/http"
    "net/url"
    "strings"
)

type testUploadBlockBlob struct{}

var _ = Suite(&testUploadBlockBlob{})

func (suite *testUploadBlockBlob) TestSmallFile(c *C) {
    transport := &MockingTransport{}
    context := makeStorageContext(transport)
    // UploadBlockBlob uses PutBlock to upload the data.
    transport.AddExchange(makeFakeCreatedResponse(), nil)
    // UploadBlockBlob then sends the list of blocks with PutBlockList.
    transport.AddExchange(makeFakeCreatedResponse(), nil)
    // Upload a random blob of data.
    data := uploadRandomBlob(c, context, 10, "MyContainer", "MyFile")
    // There were two exchanges.
    c.Assert(transport.ExchangeCount, Equals, 2)
    // The first request is a Put Block with the block data.
    fileURL := context.GetFileURL("MyContainer", "MyFile")
    assertBlockSent(c, context, data, b64("000000000000000000000000000000"), transport.Exchanges[0], fileURL)
    // The second request is Put Block List to commit the block above.
    assertBlockListSent(c, context, []string{b64("000000000000000000000000000000")}, transport.Exchanges[1], fileURL)
}

func (suite *testUploadBlockBlob) TestLargeFile(c *C) {
    transport := &MockingTransport{}
    context := makeStorageContext(transport)
    // UploadBlockBlob uses PutBlock twice to upload the data.
    transport.AddExchange(makeFakeCreatedResponse(), nil)
    transport.AddExchange(makeFakeCreatedResponse(), nil)
    // UploadBlockBlob then sends the list of blocks with PutBlockList.
    transport.AddExchange(makeFakeCreatedResponse(), nil)
    // Upload a large random blob of data.
    data := uploadRandomBlob(c, context, 1348*1024, "MyContainer", "MyFile")
    // There were three exchanges.
    c.Assert(transport.ExchangeCount, Equals, 3)
    // The first two requests are Put Block with chunks of the block data. The
    // weird looking block IDs are base64 encodings of the strings "0" and "1".
    fileURL := context.GetFileURL("MyContainer", "MyFile")
    assertBlockSent(c, context, data[:1024*1024], b64("000000000000000000000000000000"), transport.Exchanges[0], fileURL)
    assertBlockSent(c, context, data[1024*1024:], b64("000000000000000000000000000001"), transport.Exchanges[1], fileURL)
    // The second request is Put Block List to commit the block above.
    assertBlockListSent(c, context, []string{b64("000000000000000000000000000000"), b64("000000000000000000000000000001")}, transport.Exchanges[2], fileURL)
}

func uploadRandomBlob(c *C, context *StorageContext, size int, container, filename string) []byte {
    data := MakeRandomByteSlice(size)
    err := context.UploadBlockBlob(
        container, filename, bytes.NewReader(data))
    c.Assert(err, IsNil)
    return data
}

func assertBlockSent(
    c *C, context *StorageContext, data []byte, blockID string, exchange *MockingTransportExchange, fileURL string) {
    c.Check(exchange.Request.URL.String(), Matches, fileURL+"?.*")
    c.Check(exchange.Request.URL.Query(), DeepEquals, url.Values{
        "comp":    {"block"},
        "blockid": {blockID},
    })
    body, err := ioutil.ReadAll(exchange.Request.Body)
    c.Assert(err, IsNil)
    // DeepEquals is painfully slow when comparing larger structures, so we
    // compare the expected (data) and observed (body) slices directly.
    c.Assert(len(body), Equals, len(data))
    for i := range body {
        // c.Assert also noticably slows things down; this condition is an
        // optimisation of the c.Assert call contained within.
        if body[i] != data[i] {
            c.Assert(body[i], Equals, data[i])
        }
    }
}

func assertBlockListSent(
    c *C, context *StorageContext, blockIDs []string, exchange *MockingTransportExchange, fileURL string) {
    c.Check(exchange.Request.URL.String(), Matches, fileURL+"?.*")
    c.Check(exchange.Request.URL.Query(), DeepEquals, url.Values{
        "comp": {"blocklist"},
    })
    body, err := ioutil.ReadAll(exchange.Request.Body)
    c.Check(err, IsNil)
    expected := "<BlockList>\n"
    for _, blockID := range blockIDs {
        expected += "  <Latest>" + blockID + "</Latest>\n"
    }
    expected += "</BlockList>"
    c.Check(strings.TrimSpace(string(body)), Equals, strings.TrimSpace(expected))
}

type testListAllBlobs struct{}

var _ = Suite(&testListAllBlobs{})

// The ListAllBlobs Storage API call returns a BlobEnumerationResults struct
// on success.
func (suite *testListAllBlobs) Test(c *C) {
    responseBody := `
        <?xml version="1.0" encoding="utf-8"?>
        <EnumerationResults ContainerName="http://myaccount.blob.core.windows.net/mycontainer">
          <Prefix>prefix</Prefix>
          <Marker>marker</Marker>
          <MaxResults>maxresults</MaxResults>
          <Delimiter>delimiter</Delimiter>
          <Blobs>
            <Blob>
              <Name>blob-name</Name>
              <Snapshot>snapshot-date-time</Snapshot>
              <Url>blob-address</Url>
              <Properties>
                <Last-Modified>last-modified</Last-Modified>
                <Etag>etag</Etag>
                <Content-Length>size-in-bytes</Content-Length>
                <Content-Type>blob-content-type</Content-Type>
                <Content-Encoding />
                <Content-Language />
                <Content-MD5 />
                <Cache-Control />
                <x-ms-blob-sequence-number>sequence-number</x-ms-blob-sequence-number>
                <BlobType>blobtype</BlobType>
                <LeaseStatus>leasestatus</LeaseStatus>
                <LeaseState>leasestate</LeaseState>
                <LeaseDuration>leasesduration</LeaseDuration>
                <CopyId>id</CopyId>
                <CopyStatus>copystatus</CopyStatus>
                <CopySource>copysource</CopySource>
                <CopyProgress>copyprogress</CopyProgress>
                <CopyCompletionTime>copycompletiontime</CopyCompletionTime>
                <CopyStatusDescription>copydesc</CopyStatusDescription>
              </Properties>
              <Metadata>
                <MetaName1>metadataname1</MetaName1>
                <MetaName2>metadataname2</MetaName2>
              </Metadata>
            </Blob>
            <BlobPrefix>
              <Name>blob-prefix</Name>
            </BlobPrefix>
          </Blobs>
          <NextMarker />
        </EnumerationResults>`
    response := &http.Response{
        Status:     fmt.Sprintf("%d", http.StatusOK),
        StatusCode: http.StatusOK,
        Body:       makeResponseBody(responseBody),
    }
    transport := &TestTransport{Response: response}
    context := makeStorageContext(transport)
    request := &ListBlobsRequest{Container: "container"}
    results, err := context.ListAllBlobs(request)
    c.Assert(err, IsNil)
    c.Check(transport.Request.URL.String(), Matches, context.getContainerURL("container")+"?.*")
    c.Check(transport.Request.URL.Query(), DeepEquals, url.Values{
        "restype": {"container"},
        "comp":    {"list"},
    })
    c.Check(transport.Request.Header.Get("Authorization"), Not(Equals), "")
    c.Assert(results, NotNil)
}

// Client-side errors from the HTTP client are propagated back to the caller.
func (suite *testListAllBlobs) TestError(c *C) {
    error := fmt.Errorf("canned-error")
    context := makeStorageContext(&TestTransport{Error: error})
    request := &ListBlobsRequest{Container: "container"}
    _, err := context.ListAllBlobs(request)
    c.Assert(err, NotNil)
}

// Server-side errors are propagated back to the caller.
func (suite *testListAllBlobs) TestErrorResponse(c *C) {
    response := &http.Response{
        Status:     fmt.Sprintf("%d", http.StatusNotFound),
        StatusCode: http.StatusNotFound,
    }
    context := makeStorageContext(&TestTransport{Response: response})
    request := &ListBlobsRequest{Container: "container"}
    _, err := context.ListAllBlobs(request)
    c.Assert(err, NotNil)
}

// ListAllBlobs combines multiple batches of output.
func (suite *testListAllBlobs) TestBatchedResult(c *C) {
    firstBlob := "blob1"
    lastBlob := "blob2"
    marker := "moreplease"
    firstBatch := http.Response{
        StatusCode: http.StatusOK,
        Body: makeResponseBody(fmt.Sprintf(`
            <EnumerationResults>
              <Blobs>
                <Blob>
                  <Name>%s</Name>
                </Blob>
              </Blobs>
              <NextMarker>%s</NextMarker>
            </EnumerationResults>
        `, firstBlob, marker)),
    }
    lastBatch := http.Response{
        StatusCode: http.StatusOK,
        Body: makeResponseBody(fmt.Sprintf(`
            <EnumerationResults>
              <Blobs>
                <Blob>
                  <Name>%s</Name>
                </Blob>
              </Blobs>
            </EnumerationResults>
        `, lastBlob)),
    }
    transport := &MockingTransport{}
    transport.AddExchange(&firstBatch, nil)
    transport.AddExchange(&lastBatch, nil)
    context := makeStorageContext(transport)

    request := &ListBlobsRequest{Container: "mycontainer"}
    blobs, err := context.ListAllBlobs(request)
    c.Assert(err, IsNil)

    c.Check(len(blobs.Blobs), Equals, 2)
    c.Check(blobs.Blobs[0].Name, Equals, firstBlob)
    c.Check(blobs.Blobs[1].Name, Equals, lastBlob)
}

type testListAllContainers struct{}

var _ = Suite(&testListAllContainers{})

// The ListAllContainers Storage API call returns a ContainerEnumerationResults
// struct on success.
func (suite *testListAllContainers) Test(c *C) {
    responseBody := `
        <?xml version="1.0" encoding="utf-8"?>
        <EnumerationResults AccountName="http://myaccount.blob.core.windows.net">
          <Prefix>prefix-value</Prefix>
          <Marker>marker-value</Marker>
          <MaxResults>max-results-value</MaxResults>
          <Containers>
            <Container>
              <Name>name-value</Name>
              <URL>url-value</URL>
              <Properties>
                <Last-Modified>date/time-value</Last-Modified>
                <Etag>etag-value</Etag>
                <LeaseStatus>lease-status-value</LeaseStatus>
                <LeaseState>lease-state-value</LeaseState>
                <LeaseDuration>lease-duration-value</LeaseDuration>
              </Properties>
              <Metadata>
                <metadata-name>metadata-value</metadata-name>
              </Metadata>
            </Container>
          </Containers>
          <NextMarker/>
        </EnumerationResults>`
    response := &http.Response{
        Status:     fmt.Sprintf("%d", http.StatusOK),
        StatusCode: http.StatusOK,
        Body:       makeResponseBody(responseBody),
    }
    transport := &TestTransport{Response: response}
    context := makeStorageContext(transport)
    context.AzureEndpoint = "http://example.com/"
    results, err := context.ListAllContainers()
    c.Assert(err, IsNil)
    c.Check(transport.Request.URL.String(), Equals, fmt.Sprintf(
        "http://%s.blob.example.com/?comp=list", context.Account))
    c.Check(transport.Request.Header.Get("Authorization"), Not(Equals), "")
    c.Assert(results, NotNil)
    c.Assert(results.Containers[0].Name, Equals, "name-value")
}

// Client-side errors from the HTTP client are propagated back to the caller.
func (suite *testListAllContainers) TestError(c *C) {
    error := fmt.Errorf("canned-error")
    context := makeStorageContext(&TestTransport{Error: error})
    _, err := context.ListAllContainers()
    c.Assert(err, NotNil)
}

// Server-side errors are propagated back to the caller.
func (suite *testListAllContainers) TestErrorResponse(c *C) {
    response := &http.Response{
        Status:     fmt.Sprintf("%d", http.StatusNotFound),
        StatusCode: http.StatusNotFound,
    }
    context := makeStorageContext(&TestTransport{Response: response})
    _, err := context.ListAllContainers()
    c.Assert(err, NotNil)
}

// ListAllContainers combines multiple batches of output.
func (suite *testListAllContainers) TestBatchedResult(c *C) {
    firstContainer := "container1"
    lastContainer := "container2"
    marker := "moreplease"
    firstBatch := http.Response{
        StatusCode: http.StatusOK,
        Body: makeResponseBody(fmt.Sprintf(`
            <EnumerationResults>
              <Containers>
                <Container>
                  <Name>%s</Name>
                  <URL>container-address</URL>
                </Container>
              </Containers>
              <NextMarker>%s</NextMarker>
            </EnumerationResults>
        `, firstContainer, marker)),
    }
    lastBatch := http.Response{
        StatusCode: http.StatusOK,
        Body: makeResponseBody(fmt.Sprintf(`
            <EnumerationResults>
              <Containers>
                <Container>
                  <Name>%s</Name>
                  <URL>container-address</URL>
                </Container>
              </Containers>
            </EnumerationResults>
        `, lastContainer)),
    }
    transport := &MockingTransport{}
    transport.AddExchange(&firstBatch, nil)
    transport.AddExchange(&lastBatch, nil)
    context := makeStorageContext(transport)

    containers, err := context.ListAllContainers()
    c.Assert(err, IsNil)

    c.Check(len(containers.Containers), Equals, 2)
    c.Check(containers.Containers[0].Name, Equals, firstContainer)
    c.Check(containers.Containers[1].Name, Equals, lastContainer)
}

type testDeleteAllBlobs struct{}

var _ = Suite(&testDeleteAllBlobs{})

func (s *testDeleteAllBlobs) makeListingResponse() *http.Response {
    return &http.Response{
        Status:     fmt.Sprintf("%d", http.StatusOK),
        StatusCode: http.StatusOK,
        Body: makeResponseBody(`
            <?xml version="1.0" encoding="utf-8"?>
            <EnumerationResults ContainerName="http://myaccount.blob.core.windows.net/mycontainer">
              <Prefix>prefix</Prefix>
              <Marker>marker</Marker>
              <MaxResults>maxresults</MaxResults>
              <Delimiter>delimiter</Delimiter>
              <Blobs>
                <Blob>
                  <Name>blob-name</Name>
                </Blob>
                <Blob>
                  <Name>blob-name2</Name>
                </Blob>
              </Blobs>
              <NextMarker />
            </EnumerationResults>`),
    }
}

func (s *testDeleteAllBlobs) TestHappyPath(c *C) {
    listResponse := s.makeListingResponse()
    deleteResponse := &http.Response{
        Status:     fmt.Sprintf("%d", http.StatusAccepted),
        StatusCode: http.StatusAccepted,
    }

    transport := &MockingTransport{}
    transport.AddExchange(listResponse, nil)
    transport.AddExchange(deleteResponse, nil)
    transport.AddExchange(deleteResponse, nil)
    context := makeStorageContext(transport)

    err := context.DeleteAllBlobs(&DeleteAllBlobsRequest{Container: "container"})
    c.Assert(err, IsNil)
    c.Assert(transport.ExchangeCount, Equals, 3)

    // Check the ListAllBlobs exchange.
    c.Check(
        transport.Exchanges[0].Request.URL.String(),
        Matches, context.getContainerURL("container")+"[?].*")
    c.Check(transport.Exchanges[0].Request.URL.Query(),
        DeepEquals, url.Values{
            "restype": {"container"},
            "comp":    {"list"},
        })

    // Check the DeleteBlob exchanges.
    c.Check(
        transport.Exchanges[1].Request.URL.String(),
        Equals, context.GetFileURL("container", "blob-name"))
    c.Check(transport.Exchanges[1].Request.Method, Equals, "DELETE")

    c.Check(
        transport.Exchanges[2].Request.URL.String(),
        Equals, context.GetFileURL("container", "blob-name2"))
    c.Check(transport.Exchanges[2].Request.Method, Equals, "DELETE")
}

func (s *testDeleteAllBlobs) TestErrorsListing(c *C) {
    transport := &MockingTransport{}
    transport.AddExchange(&http.Response{
        Status:     fmt.Sprintf("%d", http.StatusNotFound),
        StatusCode: http.StatusNotFound}, nil)
    context := makeStorageContext(transport)
    err := context.DeleteAllBlobs(&DeleteAllBlobsRequest{Container: "c"})
    c.Assert(err, ErrorMatches, `request for blobs list failed: Azure request failed \(404: Not Found\)`)
}

func (s *testDeleteAllBlobs) TestErrorsDeleting(c *C) {
    transport := &MockingTransport{}
    listResponse := s.makeListingResponse()
    transport.AddExchange(listResponse, nil)
    transport.AddExchange(&http.Response{
        Status:     fmt.Sprintf("%d", http.StatusBadRequest),
        StatusCode: http.StatusBadRequest}, nil)
    context := makeStorageContext(transport)
    err := context.DeleteAllBlobs(&DeleteAllBlobsRequest{Container: "c"})
    c.Assert(err, ErrorMatches, `failed to delete blob blob-name: Azure request failed \(400: Bad Request\)`)
}

type testCreateInstanceDataVHD struct{}

var _ = Suite(&testCreateInstanceDataVHD{})

func (s *testCreateInstanceDataVHD) TestHappyPath(c *C) {
    response := http.Response{
        Status:     fmt.Sprintf("%d", http.StatusOK),
        StatusCode: http.StatusCreated,
    }
    transport := &MockingTransport{}
    transport.AddExchange(&response, nil) // putblob response
    transport.AddExchange(&response, nil) // first putpage
    transport.AddExchange(&response, nil) // second putpage
    context := makeStorageContext(transport)

    randomData := MakeRandomByteSlice(512)
    dataReader := bytes.NewReader(randomData)

    var err error

    err = context.CreateInstanceDataVHD(&CreateVHDRequest{
        Container: "container", Filename: "filename",
        FilesystemData: dataReader, Size: 512})
    c.Assert(err, IsNil)

    // Check the PutBlob exchange.
    c.Check(
        transport.Exchanges[0].Request.Header.Get("x-ms-blob-type"),
        Equals, "PageBlob")
    expectedSize := fmt.Sprintf("%d", VHD_SIZE)
    c.Check(
        transport.Exchanges[0].Request.Header.Get("x-ms-blob-content-length"),
        Equals, expectedSize)

    // Check the PutPage for the footer exchange.
    data, err := ioutil.ReadAll(transport.Exchanges[1].Request.Body)
    c.Assert(err, IsNil)
    expectedData, err := base64.StdEncoding.DecodeString(VHD_FOOTER)
    c.Assert(err, IsNil)
    c.Check(data, DeepEquals, expectedData)
    expectedRange := fmt.Sprintf("bytes=%d-%d", VHD_SIZE-512, VHD_SIZE-1)
    c.Check(
        transport.Exchanges[1].Request.Header.Get("x-ms-range"),
        Equals, expectedRange)

    // Check the PutPage for the data exchange.
    data, err = ioutil.ReadAll(transport.Exchanges[2].Request.Body)
    c.Assert(err, IsNil)
    c.Check(data, DeepEquals, randomData)

    c.Check(
        transport.Exchanges[2].Request.Header.Get("x-ms-range"),
        Equals, "bytes=0-511")
}

func (s *testCreateInstanceDataVHD) TestSizeConstraints(c *C) {
    var err error
    context := makeStorageContext(&TestTransport{})

    err = context.CreateInstanceDataVHD(&CreateVHDRequest{Size: 10})
    c.Check(err, ErrorMatches, "Size must be a multiple of 512")

    err = context.CreateInstanceDataVHD(&CreateVHDRequest{
        Size: VHD_SIZE})
    errString := fmt.Sprintf("Size cannot be bigger than %d", VHD_SIZE-512)
    c.Check(err, ErrorMatches, errString)
}