2020-08-04 21:00:16 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-08-05 17:41:03 +00:00
|
|
|
"os"
|
2020-08-06 18:18:15 +00:00
|
|
|
"strconv"
|
2020-08-04 21:00:16 +00:00
|
|
|
"testing"
|
2020-08-06 13:27:47 +00:00
|
|
|
"time"
|
2020-08-04 21:00:16 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-03-03 19:45:21 +00:00
|
|
|
|
|
|
|
"github.com/ceph/go-ceph/internal/commands"
|
2021-06-21 18:30:47 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2020-08-04 21:00:16 +00:00
|
|
|
)
|
|
|
|
|
2020-08-05 17:41:03 +00:00
|
|
|
var (
|
|
|
|
cachedFSAdmin *FSAdmin
|
|
|
|
|
|
|
|
// set debugTrace to true to use tracing in tests
|
|
|
|
debugTrace = false
|
2020-09-30 19:54:02 +00:00
|
|
|
|
|
|
|
// some tests are sensitive to the server version
|
2021-03-17 18:42:49 +00:00
|
|
|
serverVersion string
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
cephNautilus = "nautilus"
|
|
|
|
cephOctopus = "octopus"
|
|
|
|
cephPacfic = "pacific"
|
2020-08-05 17:41:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
dt := os.Getenv("GO_CEPH_TEST_DEBUG_TRACE")
|
2020-08-06 18:18:15 +00:00
|
|
|
if ok, err := strconv.ParseBool(dt); ok && err == nil {
|
2020-08-05 17:41:03 +00:00
|
|
|
debugTrace = true
|
|
|
|
}
|
2021-03-17 18:42:49 +00:00
|
|
|
switch vname := os.Getenv("CEPH_VERSION"); vname {
|
|
|
|
case cephNautilus, cephOctopus, cephPacfic:
|
|
|
|
serverVersion = vname
|
2020-09-30 19:54:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestServerSentinel(t *testing.T) {
|
|
|
|
// there probably *is* a better way to do this but I'm doing what's easy
|
|
|
|
// and expedient at the moment. That's tying the tests to the environment
|
|
|
|
// var to tell us what version of the *server* we are testing against. The
|
|
|
|
// build tags control what version of the *client libs* we use. These
|
|
|
|
// happen to be the same for our CI tests today, but its a lousy way to
|
|
|
|
// organize things IMO.
|
|
|
|
// This check is intended to fail the test suite if you don't tell it a
|
|
|
|
// server version it expects and force us to update the tests if a new
|
|
|
|
// version of ceph is added.
|
2021-03-17 18:42:49 +00:00
|
|
|
if serverVersion == "" {
|
|
|
|
t.Fatalf("server must be nautilus, octopus, or pacific (do the tests need updating?)")
|
2020-09-30 19:54:02 +00:00
|
|
|
}
|
2020-08-05 17:41:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-04 21:00:16 +00:00
|
|
|
func getFSAdmin(t *testing.T) *FSAdmin {
|
|
|
|
if cachedFSAdmin != nil {
|
|
|
|
return cachedFSAdmin
|
|
|
|
}
|
2021-06-21 18:30:47 +00:00
|
|
|
cachedFSAdmin = newFSAdmin(t, "")
|
|
|
|
return cachedFSAdmin
|
|
|
|
}
|
|
|
|
|
|
|
|
func newFSAdmin(t *testing.T, configFile string) *FSAdmin {
|
|
|
|
conn, err := rados.NewConn()
|
|
|
|
require.NoError(t, err)
|
|
|
|
if configFile == "" {
|
|
|
|
err = conn.ReadDefaultConfigFile()
|
|
|
|
require.NoError(t, err)
|
|
|
|
} else {
|
|
|
|
err = conn.ReadConfigFile(configFile)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
err = conn.Connect()
|
2020-08-04 21:00:16 +00:00
|
|
|
require.NoError(t, err)
|
2021-06-21 18:30:47 +00:00
|
|
|
|
|
|
|
var cmdr RadosCommander = conn
|
2020-08-05 17:41:03 +00:00
|
|
|
if debugTrace {
|
2021-06-21 18:30:47 +00:00
|
|
|
// We wrap the "real" connection, which meets the RadosCommander interface,
|
|
|
|
// with a trace commander when debugTrace is set.
|
|
|
|
cmdr = commands.NewTraceCommander(conn)
|
2020-08-05 17:41:03 +00:00
|
|
|
}
|
2021-06-21 18:30:47 +00:00
|
|
|
|
2020-08-06 13:27:47 +00:00
|
|
|
// We sleep briefly before returning in order to ensure we have a mgr map
|
|
|
|
// before we start executing the tests.
|
|
|
|
time.Sleep(50 * time.Millisecond)
|
2021-06-21 18:30:47 +00:00
|
|
|
return NewFromConn(cmdr)
|
2020-08-04 21:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestInvalidFSAdmin(t *testing.T) {
|
|
|
|
fsa := &FSAdmin{}
|
2020-09-21 18:53:29 +00:00
|
|
|
res := fsa.rawMgrCommand([]byte("FOOBAR!"))
|
|
|
|
assert.Error(t, res.Unwrap())
|
2020-08-04 21:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type badMarshalType bool
|
|
|
|
|
|
|
|
func (badMarshalType) MarshalJSON() ([]byte, error) {
|
|
|
|
return nil, errors.New("Zowie! wow")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBadMarshal(t *testing.T) {
|
|
|
|
fsa := getFSAdmin(t)
|
|
|
|
|
|
|
|
var bad badMarshalType
|
2020-09-21 18:53:29 +00:00
|
|
|
res := fsa.marshalMgrCommand(bad)
|
|
|
|
assert.Error(t, res.Unwrap())
|
2020-08-04 21:00:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParseListNames(t *testing.T) {
|
2020-09-21 18:53:29 +00:00
|
|
|
R := newResponse
|
2020-08-04 21:00:16 +00:00
|
|
|
t.Run("error", func(t *testing.T) {
|
2020-09-21 18:53:29 +00:00
|
|
|
_, err := parseListNames(R(nil, "", errors.New("bonk")))
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "bonk", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("statusSet", func(t *testing.T) {
|
2020-09-21 18:53:29 +00:00
|
|
|
_, err := parseListNames(R(nil, "unexpected!", nil))
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("badJSON", func(t *testing.T) {
|
2020-09-21 18:53:29 +00:00
|
|
|
_, err := parseListNames(R([]byte("Foo[[["), "", nil))
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
2020-09-21 18:53:29 +00:00
|
|
|
l, err := parseListNames(R([]byte(`[{"name":"bob"}]`), "", nil))
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
if assert.Len(t, l, 1) {
|
|
|
|
assert.Equal(t, "bob", l[0])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCheckEmptyResponseExpected(t *testing.T) {
|
2020-09-21 18:53:29 +00:00
|
|
|
R := newResponse
|
2020-08-04 21:00:16 +00:00
|
|
|
t.Run("error", func(t *testing.T) {
|
2021-03-03 19:45:21 +00:00
|
|
|
err := R(nil, "", errors.New("bonk")).NoData().End()
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Equal(t, "bonk", err.Error())
|
|
|
|
})
|
|
|
|
t.Run("statusSet", func(t *testing.T) {
|
2021-03-03 19:45:21 +00:00
|
|
|
err := R(nil, "unexpected!", nil).NoData().End()
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("someJSON", func(t *testing.T) {
|
2021-03-03 19:45:21 +00:00
|
|
|
err := R([]byte(`{"trouble": true}`), "", nil).NoData().End()
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.Error(t, err)
|
|
|
|
})
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
2021-03-03 19:45:21 +00:00
|
|
|
err := R([]byte{}, "", nil).NoData().End()
|
2020-08-04 21:00:16 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|