2023-08-14 13:28:29 +00:00
|
|
|
//go:build !nautilus
|
|
|
|
// +build !nautilus
|
2023-03-27 11:42:40 +00:00
|
|
|
|
|
|
|
package rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSparsifyWithProgress(t *testing.T) {
|
|
|
|
conn := radosConnect(t)
|
|
|
|
defer conn.Shutdown()
|
|
|
|
|
|
|
|
poolname := GetUUID()
|
|
|
|
err := conn.MakePool(poolname)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer conn.DeletePool(poolname)
|
|
|
|
|
|
|
|
ioctx, err := conn.OpenIOContext(poolname)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer ioctx.Destroy()
|
|
|
|
|
|
|
|
name := GetUUID()
|
|
|
|
err = quickCreate(ioctx, name, testImageSize, testImageOrder)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, RemoveImage(ioctx, name)) }()
|
|
|
|
|
|
|
|
t.Run("valid", func(t *testing.T) {
|
|
|
|
img, err := OpenImage(ioctx, name, NoSnapshot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, img.Close()) }()
|
|
|
|
|
|
|
|
cc := 0
|
2024-02-13 06:19:38 +00:00
|
|
|
cb := func(_, total uint64, v interface{}) int {
|
2023-03-27 11:42:40 +00:00
|
|
|
cc++
|
|
|
|
val := v.(int)
|
|
|
|
assert.Equal(t, 0, val)
|
|
|
|
assert.Equal(t, uint64(1), total)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
err = img.SparsifyWithProgress(4096, cb, 0)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.GreaterOrEqual(t, cc, 1)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("negativeReturnValue", func(t *testing.T) {
|
|
|
|
img, err := OpenImage(ioctx, name, NoSnapshot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, img.Close()) }()
|
|
|
|
|
|
|
|
cc := 0
|
2024-02-13 06:19:38 +00:00
|
|
|
cb := func(_, total uint64, v interface{}) int {
|
2023-03-27 11:42:40 +00:00
|
|
|
cc++
|
|
|
|
val := v.(int)
|
|
|
|
assert.Equal(t, 0, val)
|
|
|
|
assert.Equal(t, uint64(1), total)
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
|
|
|
|
err = img.SparsifyWithProgress(4096, cb, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("closedImage", func(t *testing.T) {
|
|
|
|
img, err := OpenImage(ioctx, name, NoSnapshot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, img.Close())
|
|
|
|
|
|
|
|
cc := 0
|
2024-02-13 06:19:38 +00:00
|
|
|
cb := func(_, total uint64, v interface{}) int {
|
2023-03-27 11:42:40 +00:00
|
|
|
cc++
|
|
|
|
val := v.(int)
|
|
|
|
assert.Equal(t, 0, val)
|
|
|
|
assert.Equal(t, uint64(1), total)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
err = img.SparsifyWithProgress(4096, cb, 0)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("invalidValue", func(t *testing.T) {
|
|
|
|
img, err := OpenImage(ioctx, name, NoSnapshot)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() { assert.NoError(t, img.Close()) }()
|
|
|
|
|
|
|
|
err = img.SparsifyWithProgress(4096, nil, nil)
|
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
}
|