core: add function roundupSizeMiB

This change  refactors inline roundup code to a function
roundup_size_MiB that takes a size(in bytes) as argument and returns
the smallest number of Mibibytes larger than or equal to the given size.

Signed-off-by: Michael Adam <obnox@samba.org>
This commit is contained in:
Michael Adam committed 2024-12-16 15:30:47 +01:00
1 parent df511fb58f
commit 26b4fb06d5
2 files changed
+19 -4

No files matched your search

+11 -3
View File
@@ -31,7 +31,8 @@ import (
)
const (
ImageMinSize = uint64(1048576) // 1 MB
MiB = uint64(1048576) // 1 MiB
ImageMinSize = MiB
)
type CephBlockImage struct {
@@ -141,6 +142,13 @@ func DeleteImageFromTrashInRadosNamespace(context *clusterd.Context, clusterInfo
return nil
}
// for a given size in bytes, round up to the nearest number of Mibibytes,
// i. e. return the smallest number of Mibibytes larger than or equal to size.
func roundupSizeMiB(size uint64) uint64 {
sizeMiB := (size + MiB - 1) / MiB
return sizeMiB
}
// CreateImage creates a block storage image.
// If dataPoolName is not empty, the image will use poolName as the metadata pool and the dataPoolname for data.
// If size is zero an empty image will be created. Otherwise, an image will be
@@ -156,11 +164,11 @@ func CreateImage(context *clusterd.Context, clusterInfo *ClusterInfo, name, pool
// Roundup the size of the volume image since we only create images on 1MB boundaries and we should never create an image
// size that's smaller than the requested one, e.g, requested 1048698 bytes should be 2MB while not be truncated to 1MB
sizeMB := (size + ImageMinSize - 1) / ImageMinSize
sizeMB := roundupSizeMiB(size)
imageSpec := getImageSpec(name, poolName)
args := []string{"create", imageSpec, "--size", fmt.Sprintf("%d", size)}
args := []string{"create", imageSpec, "--size", fmt.Sprintf("%d", sizeMB)}
if dataPoolName != "" {
args = append(args, fmt.Sprintf("--data-pool=%s", dataPoolName))
+8 -1
View File
@@ -29,9 +29,16 @@ import (
)
const (
sizeMB = 1048576 // 1 MB
sizeMB = MiB // 1 MB
)
func TestRoundup_size_MiB(t *testing.T) {
assert.Equal(t, uint64(1), roundupSizeMiB(MiB))
assert.Equal(t, uint64(2), roundupSizeMiB(2*MiB))
assert.Equal(t, uint64(2), roundupSizeMiB(MiB+1))
assert.Equal(t, uint64(1), roundupSizeMiB(MiB-1))
}
func TestCreateImage(t *testing.T) {
executor := &exectest.MockExecutor{}
context := &clusterd.Context{Executor: executor}