go-ceph/internal/cutil/command_output_test.go
John Mulligan e2a78eec02 cutil: allow passing free functions to command output type
The *_command functions in librados and libcephfs document the use
of specific free functions for data allocated. These functions are
currently just wrappers around C's free() function. However, to be
more strictly compliant this change adds a free-function callback
to the CommandOutput type and the specific free functions are now
used outside the unit tests.

Signed-off-by: John Mulligan <jmulligan@redhat.com>
2020-05-12 17:18:08 -04:00

57 lines
1.4 KiB
Go

package cutil
import (
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
func TestCommandOutput(t *testing.T) {
t.Run("newAndFree", func(t *testing.T) {
co := NewCommandOutput()
assert.NotNil(t, co)
co.Free()
})
t.Run("setValues", func(t *testing.T) {
co := NewCommandOutput()
assert.NotNil(t, co)
defer co.Free()
testSetString(co.OutBuf(), co.OutBufLen(), "i got style")
testSetString(co.Outs(), co.OutsLen(), "i got rhythm")
b, s := co.GoValues()
assert.EqualValues(t, []byte("i got style"), b)
assert.EqualValues(t, "i got rhythm", s)
})
t.Run("setOnlyOutBuf", func(t *testing.T) {
co := NewCommandOutput()
assert.NotNil(t, co)
defer co.Free()
testSetString(co.OutBuf(), co.OutBufLen(), "i got style")
b, s := co.GoValues()
assert.EqualValues(t, []byte("i got style"), b)
assert.EqualValues(t, "", s)
})
t.Run("setOnlyOuts", func(t *testing.T) {
co := NewCommandOutput()
assert.NotNil(t, co)
defer co.Free()
testSetString(co.Outs(), co.OutsLen(), "i got rhythm")
b, s := co.GoValues()
assert.Nil(t, b)
assert.EqualValues(t, "i got rhythm", s)
})
t.Run("customFreeFunc", func(t *testing.T) {
callCount := 0
co := NewCommandOutput().SetFreeFunc(func(p unsafe.Pointer) {
callCount++
free(p)
})
assert.NotNil(t, co)
testSetString(co.OutBuf(), co.OutBufLen(), "i got style")
testSetString(co.Outs(), co.OutsLen(), "i got rhythm")
co.Free()
assert.Equal(t, 2, callCount)
})
}