...

Source file src/github.com/Azure/azure-sdk-for-go/storage/blockblob_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  	"encoding/base64"
     9  	"io"
    10  	"io/ioutil"
    11  
    12  	chk "gopkg.in/check.v1"
    13  )
    14  
    15  type BlockBlobSuite struct{}
    16  
    17  var _ = chk.Suite(&BlockBlobSuite{})
    18  
    19  func (s *BlockBlobSuite) TestCreateBlockBlobFromReader(c *chk.C) {
    20  	cli := getBlobClient(c)
    21  	rec := cli.client.appendRecorder(c)
    22  	defer rec.Stop()
    23  
    24  	cnt := cli.GetContainerReference(containerName(c))
    25  	b := cnt.GetBlobReference(blobName(c))
    26  	c.Assert(cnt.Create(nil), chk.IsNil)
    27  	defer cnt.Delete(nil)
    28  
    29  	length := 8888
    30  	data := content(length)
    31  	err := b.CreateBlockBlobFromReader(bytes.NewReader(data), nil)
    32  	c.Assert(err, chk.IsNil)
    33  	c.Assert(b.Properties.ContentLength, chk.Equals, int64(length))
    34  
    35  	resp, err := b.Get(nil)
    36  	c.Assert(err, chk.IsNil)
    37  	gotData, err := ioutil.ReadAll(resp)
    38  	defer resp.Close()
    39  
    40  	c.Assert(err, chk.IsNil)
    41  	c.Assert(gotData, chk.DeepEquals, data)
    42  }
    43  
    44  func (s *BlockBlobSuite) TestPutBlock(c *chk.C) {
    45  	cli := getBlobClient(c)
    46  	rec := cli.client.appendRecorder(c)
    47  	defer rec.Stop()
    48  
    49  	cnt := cli.GetContainerReference(containerName(c))
    50  	b := cnt.GetBlobReference(blobName(c))
    51  	c.Assert(cnt.Create(nil), chk.IsNil)
    52  	defer cnt.Delete(nil)
    53  
    54  	chunk := content(1024)
    55  	blockID := base64.StdEncoding.EncodeToString([]byte("lol"))
    56  	c.Assert(b.PutBlock(blockID, chunk, nil), chk.IsNil)
    57  }
    58  
    59  func (s *BlockBlobSuite) TestGetBlockList_PutBlockList(c *chk.C) {
    60  	cli := getBlobClient(c)
    61  	rec := cli.client.appendRecorder(c)
    62  	defer rec.Stop()
    63  
    64  	cnt := cli.GetContainerReference(containerName(c))
    65  	b := cnt.GetBlobReference(blobName(c))
    66  	c.Assert(cnt.Create(nil), chk.IsNil)
    67  	defer cnt.Delete(nil)
    68  
    69  	chunk := content(1024)
    70  	blockID := base64.StdEncoding.EncodeToString([]byte("lol"))
    71  
    72  	// Put one block
    73  	c.Assert(b.PutBlock(blockID, chunk, nil), chk.IsNil)
    74  	defer b.Delete(nil)
    75  
    76  	// Get committed blocks
    77  	committed, err := b.GetBlockList(BlockListTypeCommitted, nil)
    78  	c.Assert(err, chk.IsNil)
    79  
    80  	if len(committed.CommittedBlocks) > 0 {
    81  		c.Fatal("There are committed blocks")
    82  	}
    83  
    84  	// Get uncommitted blocks
    85  	uncommitted, err := b.GetBlockList(BlockListTypeUncommitted, nil)
    86  	c.Assert(err, chk.IsNil)
    87  
    88  	c.Assert(len(uncommitted.UncommittedBlocks), chk.Equals, 1)
    89  	// Commit block list
    90  	c.Assert(b.PutBlockList([]Block{{blockID, BlockStatusUncommitted}}, nil), chk.IsNil)
    91  
    92  	// Get all blocks
    93  	all, err := b.GetBlockList(BlockListTypeAll, nil)
    94  	c.Assert(err, chk.IsNil)
    95  	c.Assert(len(all.CommittedBlocks), chk.Equals, 1)
    96  	c.Assert(len(all.UncommittedBlocks), chk.Equals, 0)
    97  
    98  	// Verify the block
    99  	thatBlock := all.CommittedBlocks[0]
   100  	c.Assert(thatBlock.Name, chk.Equals, blockID)
   101  	c.Assert(thatBlock.Size, chk.Equals, int64(len(chunk)))
   102  }
   103  
   104  func (s *BlockBlobSuite) TestCreateBlockBlob(c *chk.C) {
   105  	cli := getBlobClient(c)
   106  	rec := cli.client.appendRecorder(c)
   107  	defer rec.Stop()
   108  
   109  	cnt := cli.GetContainerReference(containerName(c))
   110  	b := cnt.GetBlobReference(blobName(c))
   111  	c.Assert(cnt.Create(nil), chk.IsNil)
   112  	defer cnt.Delete(nil)
   113  
   114  	c.Assert(b.CreateBlockBlob(nil), chk.IsNil)
   115  
   116  	// Verify
   117  	blocks, err := b.GetBlockList(BlockListTypeAll, nil)
   118  	c.Assert(err, chk.IsNil)
   119  	c.Assert(len(blocks.CommittedBlocks), chk.Equals, 0)
   120  	c.Assert(len(blocks.UncommittedBlocks), chk.Equals, 0)
   121  }
   122  
   123  func (s *BlockBlobSuite) TestPutEmptyBlockBlob(c *chk.C) {
   124  	cli := getBlobClient(c)
   125  	rec := cli.client.appendRecorder(c)
   126  	defer rec.Stop()
   127  
   128  	cnt := cli.GetContainerReference(containerName(c))
   129  	b := cnt.GetBlobReference(blobName(c))
   130  	c.Assert(cnt.Create(nil), chk.IsNil)
   131  	defer cnt.Delete(nil)
   132  
   133  	c.Assert(b.putSingleBlockBlob([]byte("Hello!")), chk.IsNil)
   134  
   135  	err := b.GetProperties(nil)
   136  	c.Assert(err, chk.IsNil)
   137  	c.Assert(b.Properties.ContentLength, chk.Not(chk.Equals), 0)
   138  }
   139  
   140  func (s *BlockBlobSuite) TestPutBlockWithLengthUsingLimitReader(c *chk.C) {
   141  	cli := getBlobClient(c)
   142  	rec := cli.client.appendRecorder(c)
   143  	defer rec.Stop()
   144  
   145  	cnt := cli.GetContainerReference(containerName(c))
   146  	b := cnt.GetBlobReference(blobName(c))
   147  	c.Assert(cnt.Create(nil), chk.IsNil)
   148  	defer cnt.Delete(nil)
   149  
   150  	length := 512
   151  	data := content(length)
   152  
   153  	lr := io.LimitReader(bytes.NewReader(data), 256)
   154  	c.Assert(b.PutBlockWithLength("0000", 256, lr, nil), chk.IsNil)
   155  }
   156  
   157  func (s *BlockBlobSuite) TestPutBlockFromURL(c *chk.C) {
   158  	cli := getBlobClient(c)
   159  	rec := cli.client.appendRecorder(c)
   160  	defer rec.Stop()
   161  
   162  	cnt := cli.GetContainerReference(containerName(c))
   163  	c.Assert(cnt.Create(&CreateContainerOptions{
   164  		Access: ContainerAccessTypeContainer,
   165  	}), chk.IsNil)
   166  	defer cnt.Delete(nil)
   167  
   168  	length := 512
   169  	data := content(length)
   170  	lr := io.LimitReader(bytes.NewReader(data), 256)
   171  	srcBlob := cnt.GetBlobReference(blobName(c, "src"))
   172  	c.Assert(srcBlob.PutBlockWithLength("0000", 256, lr, nil), chk.IsNil)
   173  	c.Assert(srcBlob.PutBlockList([]Block{
   174  		{
   175  			ID:     "0000",
   176  			Status: BlockStatusLatest,
   177  		},
   178  	}, nil), chk.IsNil)
   179  	dstBlob := cnt.GetBlobReference(blobName(c, "dst"))
   180  	c.Assert(dstBlob.PutBlockFromURL("0000", srcBlob.GetURL(), 0, 64, nil), chk.IsNil)
   181  }
   182  

View as plain text