Skip to content

Commit

Permalink
rbd: clean up rbd encryption load test case
Browse files Browse the repository at this point in the history
Restructure the existing rbd encryption load test function so that it
makes use of defer and uses subtests to divide things up more clearly.
I needed to make this change in preparation for adding a binding for
rbd_encryption_load2 and a similar test - but I couldn't make sense of
the existing test in it's more monolithic form.

Signed-off-by: John Mulligan <[email protected]>
  • Loading branch information
phlogistonjohn committed Jan 22, 2025
1 parent 795b40e commit f6c084c
Showing 1 changed file with 47 additions and 41 deletions.
88 changes: 47 additions & 41 deletions rbd/encryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,16 @@ func TestEncryptionFormat(t *testing.T) {

func TestEncryptionLoad(t *testing.T) {
conn := radosConnect(t)
defer conn.Shutdown()

poolname := GetUUID()
err := conn.MakePool(poolname)
assert.NoError(t, err)
defer conn.DeletePool(poolname)

ioctx, err := conn.OpenIOContext(poolname)
require.NoError(t, err)
defer ioctx.Destroy()

name := GetUUID()
testImageSize := uint64(1 << 23) // format requires more than 4194304 bytes
Expand All @@ -78,55 +81,58 @@ func TestEncryptionLoad(t *testing.T) {
// then write some encrypted data at the end of the image
err = img.Close()
assert.NoError(t, err)
img, err = OpenImage(ioctx, name, NoSnapshot)
err = img.EncryptionLoad(opts)
assert.NoError(t, err)
defer func() {
assert.NoError(t, img.Remove())
}()

outData := []byte("Hi rbd! Nice to talk through go-ceph :)")

stats, err := img.Stat()
require.NoError(t, err)
offset := int64(stats.Size) - int64(len(outData))

nOut, err := img.WriteAt(outData, offset)
assert.Equal(t, len(outData), nOut)
assert.NoError(t, err)

err = img.Close()
assert.NoError(t, err)
testData := []byte("Hi rbd! Nice to talk through go-ceph :)")
var offset int64

// Re-open the image, load the encryption format, and read the encrypted data
img, err = OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
err = img.EncryptionLoad(opts)
assert.NoError(t, err)
t.Run("prepare", func(t *testing.T) {
img, err = OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
defer img.Close()
err = img.EncryptionLoad(opts)
assert.NoError(t, err)

inData := make([]byte, len(outData))
nIn, err := img.ReadAt(inData, offset)
assert.Equal(t, nIn, len(inData))
assert.Equal(t, inData, outData)
assert.NoError(t, err)
stats, err := img.Stat()
require.NoError(t, err)
offset = int64(stats.Size) - int64(len(testData))

err = img.Close()
assert.NoError(t, err)
nOut, err := img.WriteAt(testData, offset)
assert.Equal(t, len(testData), nOut)
assert.NoError(t, err)
})

// Re-open the image and attempt to read the encrypted data without loading the encryption
img, err = OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
t.Run("readEnc", func(t *testing.T) {
require.NotEqual(t, offset, 0)
// Re-open the image, load the encryption format, and read the encrypted data
img, err = OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
defer img.Close()
err = img.EncryptionLoad(opts)
assert.NoError(t, err)

nIn, err = img.ReadAt(inData, offset)
assert.Equal(t, nIn, len(inData))
assert.NotEqual(t, inData, outData)
assert.NoError(t, err)
inData := make([]byte, len(testData))
nIn, err := img.ReadAt(inData, offset)
assert.Equal(t, nIn, len(inData))
assert.Equal(t, inData, testData)
assert.NoError(t, err)
})

err = img.Close()
assert.NoError(t, err)
err = img.Remove()
assert.NoError(t, err)
t.Run("noEnc", func(t *testing.T) {
require.NotEqual(t, offset, 0)
// Re-open the image and attempt to read the encrypted data without loading the encryption
img, err = OpenImage(ioctx, name, NoSnapshot)
assert.NoError(t, err)
defer img.Close()

ioctx.Destroy()
conn.DeletePool(poolname)
conn.Shutdown()
inData := make([]byte, len(testData))
nIn, err := img.ReadAt(inData, offset)
assert.Equal(t, nIn, len(inData))
assert.NotEqual(t, inData, testData)
assert.NoError(t, err)
})
}

func TestEncryptedResize(t *testing.T) {
Expand Down

0 comments on commit f6c084c

Please sign in to comment.