...

Source file src/github.com/Azure/azure-sdk-for-go/storage/pageblob_test.go

Documentation: github.com/Azure/azure-sdk-for-go/storage

     1  package storage
     2  
     3  // Copyright (c) Microsoft Corporation. All rights reserved.
     4  // Licensed under the MIT License. See License.txt in the project root for license information.
     5  
     6  import (
     7  	"bytes"
     8  	"io/ioutil"
     9  
    10  	chk "gopkg.in/check.v1"
    11  )
    12  
    13  type PageBlobSuite struct{}
    14  
    15  var _ = chk.Suite(&PageBlobSuite{})
    16  
    17  func (s *PageBlobSuite) TestPutPageBlob(c *chk.C) {
    18  	cli := getBlobClient(c)
    19  	rec := cli.client.appendRecorder(c)
    20  	defer rec.Stop()
    21  
    22  	cnt := cli.GetContainerReference(containerName(c))
    23  	b := cnt.GetBlobReference(blobName(c))
    24  	c.Assert(cnt.Create(nil), chk.IsNil)
    25  	defer cnt.Delete(nil)
    26  
    27  	size := int64(10 * 1024 * 1024)
    28  	b.Properties.ContentLength = size
    29  	c.Assert(b.PutPageBlob(nil), chk.IsNil)
    30  
    31  	// Verify
    32  	err := b.GetProperties(nil)
    33  	c.Assert(err, chk.IsNil)
    34  	c.Assert(b.Properties.ContentLength, chk.Equals, size)
    35  	c.Assert(b.Properties.BlobType, chk.Equals, BlobTypePage)
    36  }
    37  
    38  func (s *PageBlobSuite) TestPutPagesUpdate(c *chk.C) {
    39  	cli := getBlobClient(c)
    40  	rec := cli.client.appendRecorder(c)
    41  	defer rec.Stop()
    42  
    43  	cnt := cli.GetContainerReference(containerName(c))
    44  	b := cnt.GetBlobReference(blobName(c))
    45  	c.Assert(cnt.Create(nil), chk.IsNil)
    46  	defer cnt.Delete(nil)
    47  
    48  	size := int64(10 * 1024 * 1024) // larger than we'll use
    49  	b.Properties.ContentLength = size
    50  	c.Assert(b.PutPageBlob(nil), chk.IsNil)
    51  
    52  	chunk1 := content(1024)
    53  	chunk2 := content(512)
    54  
    55  	// Append chunks
    56  	blobRange := BlobRange{
    57  		End: uint64(len(chunk1) - 1),
    58  	}
    59  	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk1), nil), chk.IsNil)
    60  	blobRange.Start = uint64(len(chunk1))
    61  	blobRange.End = uint64(len(chunk1) + len(chunk2) - 1)
    62  	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk2), nil), chk.IsNil)
    63  
    64  	// Verify contents
    65  	options := GetBlobRangeOptions{
    66  		Range: &BlobRange{
    67  			End: uint64(len(chunk1) + len(chunk2) - 1),
    68  		},
    69  	}
    70  	out, err := b.GetRange(&options)
    71  	c.Assert(err, chk.IsNil)
    72  	defer out.Close()
    73  	blobContents, err := ioutil.ReadAll(out)
    74  	c.Assert(err, chk.IsNil)
    75  	c.Assert(blobContents, chk.DeepEquals, append(chunk1, chunk2...))
    76  
    77  	// Overwrite first half of chunk1
    78  	chunk0 := content(512)
    79  	blobRange.Start = 0
    80  	blobRange.End = uint64(len(chunk0) - 1)
    81  	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk0), nil), chk.IsNil)
    82  
    83  	// Verify contents
    84  	out, err = b.GetRange(&options)
    85  	c.Assert(err, chk.IsNil)
    86  	defer out.Close()
    87  	blobContents, err = ioutil.ReadAll(out)
    88  	c.Assert(err, chk.IsNil)
    89  	c.Assert(blobContents, chk.DeepEquals, append(append(chunk0, chunk1[512:]...), chunk2...))
    90  }
    91  
    92  func (s *PageBlobSuite) TestPutPagesClear(c *chk.C) {
    93  	cli := getBlobClient(c)
    94  	rec := cli.client.appendRecorder(c)
    95  	defer rec.Stop()
    96  
    97  	cnt := cli.GetContainerReference(containerName(c))
    98  	b := cnt.GetBlobReference(blobName(c))
    99  	c.Assert(cnt.Create(nil), chk.IsNil)
   100  	defer cnt.Delete(nil)
   101  
   102  	size := int64(10 * 1024 * 1024) // larger than we'll use
   103  	b.Properties.ContentLength = size
   104  	c.Assert(b.PutPageBlob(nil), chk.IsNil)
   105  
   106  	// Put 0-2047
   107  	chunk := content(2048)
   108  	blobRange := BlobRange{
   109  		End: 2047,
   110  	}
   111  	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk), nil), chk.IsNil)
   112  
   113  	// Clear 512-1023
   114  	blobRange.Start = 512
   115  	blobRange.End = 1023
   116  	c.Assert(b.ClearRange(blobRange, nil), chk.IsNil)
   117  
   118  	// Verify contents
   119  	options := GetBlobRangeOptions{
   120  		Range: &BlobRange{
   121  			Start: 0,
   122  			End:   2047,
   123  		},
   124  	}
   125  	out, err := b.GetRange(&options)
   126  	c.Assert(err, chk.IsNil)
   127  	contents, err := ioutil.ReadAll(out)
   128  	c.Assert(err, chk.IsNil)
   129  	defer out.Close()
   130  	c.Assert(contents, chk.DeepEquals, append(append(chunk[:512], make([]byte, 512)...), chunk[1024:]...))
   131  }
   132  
   133  func (s *PageBlobSuite) TestGetPageRanges(c *chk.C) {
   134  	cli := getBlobClient(c)
   135  	rec := cli.client.appendRecorder(c)
   136  	defer rec.Stop()
   137  
   138  	cnt := cli.GetContainerReference(containerName(c))
   139  	c.Assert(cnt.Create(nil), chk.IsNil)
   140  	defer cnt.Delete(nil)
   141  
   142  	size := int64(10 * 1024) // larger than we'll use
   143  
   144  	// Get page ranges on empty blob
   145  	blob1 := cnt.GetBlobReference(blobName(c, "1"))
   146  	blob1.Properties.ContentLength = size
   147  	c.Assert(blob1.PutPageBlob(nil), chk.IsNil)
   148  	out, err := blob1.GetPageRanges(nil)
   149  	c.Assert(err, chk.IsNil)
   150  	c.Assert(len(out.PageList), chk.Equals, 0)
   151  
   152  	// Get page ranges with just one range
   153  	blob2 := cnt.GetBlobReference(blobName(c, "2"))
   154  	blob2.Properties.ContentLength = size
   155  	c.Assert(blob2.PutPageBlob(nil), chk.IsNil)
   156  	blobRange := []BlobRange{
   157  		{End: 511},
   158  		{Start: 1024, End: 2047},
   159  	}
   160  	c.Assert(blob2.WriteRange(blobRange[0], bytes.NewReader(content(512)), nil), chk.IsNil)
   161  
   162  	out, err = blob2.GetPageRanges(nil)
   163  	c.Assert(err, chk.IsNil)
   164  	c.Assert(len(out.PageList), chk.Equals, 1)
   165  	expected := []PageRange{
   166  		{End: 511},
   167  		{Start: 1024, End: 2047},
   168  	}
   169  	c.Assert(out.PageList[0], chk.Equals, expected[0])
   170  
   171  	// Get page ranges with just two range
   172  	blob3 := cnt.GetBlobReference(blobName(c, "3"))
   173  	blob3.Properties.ContentLength = size
   174  	c.Assert(blob3.PutPageBlob(nil), chk.IsNil)
   175  	for _, br := range blobRange {
   176  		c.Assert(blob3.WriteRange(br, bytes.NewReader(content(int(br.End-br.Start+1))), nil), chk.IsNil)
   177  	}
   178  	out, err = blob3.GetPageRanges(nil)
   179  	c.Assert(err, chk.IsNil)
   180  	c.Assert(len(out.PageList), chk.Equals, 2)
   181  	c.Assert(out.PageList, chk.DeepEquals, expected)
   182  }
   183  

View as plain text