mirror of
https://github.com/containers/podman.git
synced 2025-06-29 15:08:09 +08:00
Expand sparseWriter tests
- Add more test cases - Test that we create the expected (large) holes; don't enforce anything for the <zerosThresholt ones. O)nly changes test code, should not change behavior. Signed-off-by: Miloslav Trmač <mitr@redhat.com>
This commit is contained in:
@ -3,22 +3,32 @@ package compression
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
type memorySparseFile struct {
|
||||
buffer bytes.Buffer
|
||||
pos int64
|
||||
sparse int64
|
||||
}
|
||||
|
||||
func (m *memorySparseFile) Seek(offset int64, whence int) (int64, error) {
|
||||
logrus.Debugf("Seek %d %d", offset, whence)
|
||||
var newPos int64
|
||||
switch whence {
|
||||
case io.SeekStart:
|
||||
newPos = offset
|
||||
panic("unexpected")
|
||||
case io.SeekCurrent:
|
||||
newPos = m.pos + offset
|
||||
if offset < -1 {
|
||||
panic("unexpected")
|
||||
}
|
||||
m.sparse += offset
|
||||
case io.SeekEnd:
|
||||
newPos = int64(m.buffer.Len()) + offset
|
||||
default:
|
||||
@ -34,6 +44,7 @@ func (m *memorySparseFile) Seek(offset int64, whence int) (int64, error) {
|
||||
}
|
||||
|
||||
func (m *memorySparseFile) Write(b []byte) (n int, err error) {
|
||||
logrus.Debugf("Write %d", len(b))
|
||||
if int64(m.buffer.Len()) < m.pos {
|
||||
padding := make([]byte, m.pos-int64(m.buffer.Len()))
|
||||
_, err := m.buffer.Write(padding)
|
||||
@ -42,8 +53,6 @@ func (m *memorySparseFile) Write(b []byte) (n int, err error) {
|
||||
}
|
||||
}
|
||||
|
||||
m.buffer.Next(int(m.pos) - m.buffer.Len())
|
||||
|
||||
n, err = m.buffer.Write(b)
|
||||
m.pos += int64(n)
|
||||
return n, err
|
||||
@ -53,7 +62,7 @@ func (m *memorySparseFile) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func testInputWithWriteLen(t *testing.T, input []byte, chunkSize int) {
|
||||
func testInputWithWriteLen(t *testing.T, input []byte, minSparse int64, chunkSize int) {
|
||||
m := &memorySparseFile{}
|
||||
sparseWriter := NewSparseWriter(m)
|
||||
|
||||
@ -71,15 +80,16 @@ func testInputWithWriteLen(t *testing.T, input []byte, chunkSize int) {
|
||||
if err != nil {
|
||||
t.Fatalf("Expected no error, got %v", err)
|
||||
}
|
||||
if !bytes.Equal(input, m.buffer.Bytes()) {
|
||||
t.Fatalf("Incorrect output")
|
||||
}
|
||||
assert.Equal(t, string(input), m.buffer.String())
|
||||
assert.GreaterOrEqual(t, m.sparse, minSparse)
|
||||
}
|
||||
|
||||
func testInput(t *testing.T, inputBytes []byte) {
|
||||
func testInput(t *testing.T, name string, inputBytes []byte, minSparse int64) {
|
||||
currentLen := 1
|
||||
for {
|
||||
testInputWithWriteLen(t, inputBytes, currentLen)
|
||||
t.Run(fmt.Sprintf("%s@%d", name, currentLen), func(t *testing.T) {
|
||||
testInputWithWriteLen(t, inputBytes, minSparse, currentLen)
|
||||
})
|
||||
currentLen <<= 1
|
||||
if currentLen > len(inputBytes) {
|
||||
break
|
||||
@ -88,22 +98,30 @@ func testInput(t *testing.T, inputBytes []byte) {
|
||||
}
|
||||
|
||||
func TestSparseWriter(t *testing.T) {
|
||||
testInput(t, []byte("hello"))
|
||||
testInput(t, append(make([]byte, 100), []byte("hello")...))
|
||||
testInput(t, []byte(""))
|
||||
testInput(t, "small contents", []byte("hello"), 0)
|
||||
testInput(t, "small zeroes", append(make([]byte, 100), []byte("hello")...), 0)
|
||||
testInput(t, "empty", []byte(""), 0)
|
||||
testInput(t, "small iterated", []byte{'a', 0, 'a', 0, 'a', 0}, 0)
|
||||
testInput(t, "small iterated2", []byte{0, 'a', 0, 'a', 0, 'a'}, 0)
|
||||
|
||||
// add "hello" at the beginning
|
||||
largeInput := make([]byte, 1024*1024)
|
||||
const largeSize = 1024 * 1024
|
||||
largeInput := make([]byte, largeSize)
|
||||
copy(largeInput, []byte("hello"))
|
||||
testInput(t, largeInput)
|
||||
testInput(t, "sparse end", largeInput, largeSize-5-1) // -1 for the final byte establishing file size
|
||||
|
||||
// add "hello" at the end
|
||||
largeInput = make([]byte, 1024*1024)
|
||||
copy(largeInput[1024*1024-5:], []byte("hello"))
|
||||
testInput(t, largeInput)
|
||||
largeInput = make([]byte, largeSize)
|
||||
copy(largeInput[largeSize-5:], []byte("hello"))
|
||||
testInput(t, "sparse beginning", largeInput, largeSize-5)
|
||||
|
||||
// add "hello" in the middle
|
||||
largeInput = make([]byte, 1024*1024)
|
||||
largeInput = make([]byte, largeSize)
|
||||
copy(largeInput[len(largeInput)/2:], []byte("hello"))
|
||||
testInput(t, largeInput)
|
||||
testInput(t, "sparse both ends", largeInput, largeSize-5-1) // -1 for the final byte establishing file size
|
||||
|
||||
largeInput = make([]byte, largeSize)
|
||||
copy(largeInput[0:5], []byte("hello"))
|
||||
copy(largeInput[largeSize-5:], []byte("HELLO"))
|
||||
testInput(t, "sparse middle", largeInput, largeSize-10)
|
||||
}
|
||||
|
Reference in New Issue
Block a user