File: pageblob_test.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (182 lines) | stat: -rw-r--r-- 5,150 bytes parent folder | download
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
package storage

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

import (
	"bytes"
	"io/ioutil"

	chk "gopkg.in/check.v1"
)

type PageBlobSuite struct{}

var _ = chk.Suite(&PageBlobSuite{})

func (s *PageBlobSuite) TestPutPageBlob(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)

	size := int64(10 * 1024 * 1024)
	b.Properties.ContentLength = size
	c.Assert(b.PutPageBlob(nil), chk.IsNil)

	// Verify
	err := b.GetProperties(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(b.Properties.ContentLength, chk.Equals, size)
	c.Assert(b.Properties.BlobType, chk.Equals, BlobTypePage)
}

func (s *PageBlobSuite) TestPutPagesUpdate(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)

	size := int64(10 * 1024 * 1024) // larger than we'll use
	b.Properties.ContentLength = size
	c.Assert(b.PutPageBlob(nil), chk.IsNil)

	chunk1 := content(1024)
	chunk2 := content(512)

	// Append chunks
	blobRange := BlobRange{
		End: uint64(len(chunk1) - 1),
	}
	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk1), nil), chk.IsNil)
	blobRange.Start = uint64(len(chunk1))
	blobRange.End = uint64(len(chunk1) + len(chunk2) - 1)
	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk2), nil), chk.IsNil)

	// Verify contents
	options := GetBlobRangeOptions{
		Range: &BlobRange{
			End: uint64(len(chunk1) + len(chunk2) - 1),
		},
	}
	out, err := b.GetRange(&options)
	c.Assert(err, chk.IsNil)
	defer out.Close()
	blobContents, err := ioutil.ReadAll(out)
	c.Assert(err, chk.IsNil)
	c.Assert(blobContents, chk.DeepEquals, append(chunk1, chunk2...))

	// Overwrite first half of chunk1
	chunk0 := content(512)
	blobRange.Start = 0
	blobRange.End = uint64(len(chunk0) - 1)
	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk0), nil), chk.IsNil)

	// Verify contents
	out, err = b.GetRange(&options)
	c.Assert(err, chk.IsNil)
	defer out.Close()
	blobContents, err = ioutil.ReadAll(out)
	c.Assert(err, chk.IsNil)
	c.Assert(blobContents, chk.DeepEquals, append(append(chunk0, chunk1[512:]...), chunk2...))
}

func (s *PageBlobSuite) TestPutPagesClear(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)

	size := int64(10 * 1024 * 1024) // larger than we'll use
	b.Properties.ContentLength = size
	c.Assert(b.PutPageBlob(nil), chk.IsNil)

	// Put 0-2047
	chunk := content(2048)
	blobRange := BlobRange{
		End: 2047,
	}
	c.Assert(b.WriteRange(blobRange, bytes.NewReader(chunk), nil), chk.IsNil)

	// Clear 512-1023
	blobRange.Start = 512
	blobRange.End = 1023
	c.Assert(b.ClearRange(blobRange, nil), chk.IsNil)

	// Verify contents
	options := GetBlobRangeOptions{
		Range: &BlobRange{
			Start: 0,
			End:   2047,
		},
	}
	out, err := b.GetRange(&options)
	c.Assert(err, chk.IsNil)
	contents, err := ioutil.ReadAll(out)
	c.Assert(err, chk.IsNil)
	defer out.Close()
	c.Assert(contents, chk.DeepEquals, append(append(chunk[:512], make([]byte, 512)...), chunk[1024:]...))
}

func (s *PageBlobSuite) TestGetPageRanges(c *chk.C) {
	cli := getBlobClient(c)
	rec := cli.client.appendRecorder(c)
	defer rec.Stop()

	cnt := cli.GetContainerReference(containerName(c))
	c.Assert(cnt.Create(nil), chk.IsNil)
	defer cnt.Delete(nil)

	size := int64(10 * 1024) // larger than we'll use

	// Get page ranges on empty blob
	blob1 := cnt.GetBlobReference(blobName(c, "1"))
	blob1.Properties.ContentLength = size
	c.Assert(blob1.PutPageBlob(nil), chk.IsNil)
	out, err := blob1.GetPageRanges(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(len(out.PageList), chk.Equals, 0)

	// Get page ranges with just one range
	blob2 := cnt.GetBlobReference(blobName(c, "2"))
	blob2.Properties.ContentLength = size
	c.Assert(blob2.PutPageBlob(nil), chk.IsNil)
	blobRange := []BlobRange{
		{End: 511},
		{Start: 1024, End: 2047},
	}
	c.Assert(blob2.WriteRange(blobRange[0], bytes.NewReader(content(512)), nil), chk.IsNil)

	out, err = blob2.GetPageRanges(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(len(out.PageList), chk.Equals, 1)
	expected := []PageRange{
		{End: 511},
		{Start: 1024, End: 2047},
	}
	c.Assert(out.PageList[0], chk.Equals, expected[0])

	// Get page ranges with just two range
	blob3 := cnt.GetBlobReference(blobName(c, "3"))
	blob3.Properties.ContentLength = size
	c.Assert(blob3.PutPageBlob(nil), chk.IsNil)
	for _, br := range blobRange {
		c.Assert(blob3.WriteRange(br, bytes.NewReader(content(int(br.End-br.Start+1))), nil), chk.IsNil)
	}
	out, err = blob3.GetPageRanges(nil)
	c.Assert(err, chk.IsNil)
	c.Assert(len(out.PageList), chk.Equals, 2)
	c.Assert(out.PageList, chk.DeepEquals, expected)
}