2020-08-04 21:00:16 +00:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2020-08-05 17:41:03 +00:00
|
|
|
"os"
|
2020-08-04 21:00:16 +00:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-03-03 19:45:21 +00:00
|
|
|
|
2022-01-17 21:27:04 +00:00
|
|
|
"github.com/ceph/go-ceph/internal/admintest"
|
2020-08-04 21:00:16 +00:00
|
|
|
)
|
|
|
|
|
2020-08-05 17:41:03 +00:00
|
|
|
var (
|
2022-01-17 21:27:04 +00:00
|
|
|
radosConnector = admintest.NewConnector()
|
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"
|
2022-03-18 20:17:41 +00:00
|
|
|
cephQuincy = "quincy"
|
2020-08-05 17:41:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2021-03-17 18:42:49 +00:00
|
|
|
switch vname := os.Getenv("CEPH_VERSION"); vname {
|
2022-03-18 20:17:41 +00:00
|
|
|
case cephNautilus, cephOctopus, cephPacfic, cephQuincy:
|
2021-03-17 18:42:49 +00:00
|
|
|
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 == "" {
|
2022-03-18 20:17:41 +00:00
|
|
|
t.Fatalf("server must be nautilus, octopus, pacific, or quincy (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 {
|
2022-01-17 21:27:04 +00:00
|
|
|
return NewFromConn(radosConnector.Get(t))
|
2021-06-21 18:30:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newFSAdmin(t *testing.T, configFile string) *FSAdmin {
|
2022-01-17 21:27:04 +00:00
|
|
|
return NewFromConn(
|
|
|
|
admintest.WrapConn(admintest.NewConnFromConfig(t, configFile)))
|
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)
|
|
|
|
})
|
|
|
|
}
|