forked from mcardosos/azure-storage-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathblockblob_test.go
152 lines (118 loc) · 4.06 KB
/
blockblob_test.go
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
package storage
import (
"bytes"
"encoding/base64"
"io/ioutil"
chk "gopkg.in/check.v1"
)
type BlockBlobSuite struct{}
var _ = chk.Suite(&BlockBlobSuite{})
func (s *BlockBlobSuite) TestCreateBlockBlobFromReader(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
b := cnt.GetBlobReference(blobName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
data := content(8888)
b.Properties.ContentLength = int64(len(data))
c.Assert(b.CreateBlockBlobFromReader(bytes.NewReader(data), nil), chk.IsNil)
resp, err := b.Get(nil)
c.Assert(err, chk.IsNil)
gotData, err := ioutil.ReadAll(resp)
defer resp.Close()
c.Assert(err, chk.IsNil)
c.Assert(gotData, chk.DeepEquals, data)
}
func (s *BlockBlobSuite) TestCreateBlockBlobFromReaderWithShortData(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
b := cnt.GetBlobReference(blobName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
data := content(8888)
b.Properties.ContentLength = 9999
err := b.CreateBlockBlobFromReader(bytes.NewReader(data), nil)
c.Assert(err, chk.NotNil)
_, err = b.Get(nil)
// Upload was incomplete: blob should not have been created.
c.Assert(err, chk.NotNil)
}
func (s *BlockBlobSuite) TestPutBlock(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
b := cnt.GetBlobReference(blobName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
chunk := content(1024)
blockID := base64.StdEncoding.EncodeToString([]byte("lol"))
c.Assert(b.PutBlock(blockID, chunk, nil), chk.IsNil)
}
func (s *BlockBlobSuite) TestGetBlockList_PutBlockList(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
b := cnt.GetBlobReference(blobName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
chunk := content(1024)
blockID := base64.StdEncoding.EncodeToString([]byte("lol"))
// Put one block
c.Assert(b.PutBlock(blockID, chunk, nil), chk.IsNil)
defer b.Delete(nil)
// Get committed blocks
committed, err := b.GetBlockList(BlockListTypeCommitted, nil)
c.Assert(err, chk.IsNil)
if len(committed.CommittedBlocks) > 0 {
c.Fatal("There are committed blocks")
}
// Get uncommitted blocks
uncommitted, err := b.GetBlockList(BlockListTypeUncommitted, nil)
c.Assert(err, chk.IsNil)
c.Assert(len(uncommitted.UncommittedBlocks), chk.Equals, 1)
// Commit block list
c.Assert(b.PutBlockList([]Block{{blockID, BlockStatusUncommitted}}, nil), chk.IsNil)
// Get all blocks
all, err := b.GetBlockList(BlockListTypeAll, nil)
c.Assert(err, chk.IsNil)
c.Assert(len(all.CommittedBlocks), chk.Equals, 1)
c.Assert(len(all.UncommittedBlocks), chk.Equals, 0)
// Verify the block
thatBlock := all.CommittedBlocks[0]
c.Assert(thatBlock.Name, chk.Equals, blockID)
c.Assert(thatBlock.Size, chk.Equals, int64(len(chunk)))
}
func (s *BlockBlobSuite) TestCreateBlockBlob(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
b := cnt.GetBlobReference(blobName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
c.Assert(b.CreateBlockBlob(nil), chk.IsNil)
// Verify
blocks, err := b.GetBlockList(BlockListTypeAll, nil)
c.Assert(err, chk.IsNil)
c.Assert(len(blocks.CommittedBlocks), chk.Equals, 0)
c.Assert(len(blocks.UncommittedBlocks), chk.Equals, 0)
}
func (s *BlockBlobSuite) TestPutEmptyBlockBlob(c *chk.C) {
cli := getBlobClient(c)
rec := cli.client.appendRecorder(c)
defer rec.Stop()
cnt := cli.GetContainerReference(containerName(c))
b := cnt.GetBlobReference(blobName(c))
c.Assert(cnt.Create(nil), chk.IsNil)
defer cnt.Delete(nil)
c.Assert(b.putSingleBlockBlob([]byte("Hello!")), chk.IsNil)
err := b.GetProperties(nil)
c.Assert(err, chk.IsNil)
c.Assert(b.Properties.ContentLength, chk.Not(chk.Equals), 0)
}