From 05a8b4868828ec3b96018f52844473ff770da989 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 22 Jan 2021 11:28:54 +0100 Subject: [PATCH] rbd: Add TestWriteSame/zerofill test case Using WriteSame() to implement a zerofill function is less straight forward than expected. `rbd_discard_on_zeroed_write_same` is a Ceph option than affects the behaviour of WriteSame(). By default the option is enabled, causing WriteSame() to call discard instead of writing zeros. Signed-off-by: Niels de Vos --- rbd/rbd_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/rbd/rbd_test.go b/rbd/rbd_test.go index 62291d1..f095142 100644 --- a/rbd/rbd_test.go +++ b/rbd/rbd_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "io" + "os/exec" "sort" "testing" "time" @@ -539,6 +540,12 @@ func TestWriteSame(t *testing.T) { err = CreateImage(ioctx, name, testImageSize, options) require.NoError(t, err) + // WriteSame() by default calls discard when writing only zeros. This + // can be unexpected when trying to allocate a whole RBD image. To + // disable this behaviour, and actually write zeros, the option + // `rbd_discard_on_zeroed_write_same` needs to be disabled. + conn.SetConfigOption("rbd_discard_on_zeroed_write_same", "false") + img, err := OpenImage(ioctx, name, NoSnapshot) assert.NoError(t, err) @@ -578,6 +585,31 @@ func TestWriteSame(t *testing.T) { assert.Equal(t, int64(0), n_in) }) + t.Run("zerofill", func(t *testing.T) { + // implement allocating an image by writing zeros to it + sc, err := img.GetStripeCount() + assert.NoError(t, err) + + // fetch the actual size of the image as available in the pool, can be + // margially different from the requested image size + st, err := img.Stat() + assert.NoError(t, err) + size := st.Size + + // zeroBlock is the stripe-period: size of the object-size multiplied + // by the stripe-count + order := uint(st.Order) // signed shifting requires newer golang?! + zeroBlock := make([]byte, sc*(1<