go-ceph/rgw/admin/user_test.go
Sébastien Han 6be8d370cb rgwadmin: add support for RadosGW Admin Ops API
Following this discussion #492

This commit introduces a new package "rgw/admin" which helps you interact
with the [RadosGW Admin Ops API](https://docs.ceph.com/en/latest/radosgw/adminops).
Not all the API capabilities are covered by this commit, but this is a
solid foundation for adding code on top. I'm expecting a few more
iterations to make 100% complete. Also, the RadosGW Admin API is going
to implement new functions soon (like bucket creation). So this library
will live on and keep catching up.

As many unit tests as possible have been added. A new integration test
suite also runs. The "micro-osd.sh" now deploys a RGW and the
integration suite tests on it. Thus the CI should cover it.

Shout out to @QuentinPerez and @IrekFasikhov for their existing
libraries. They were a very good inspiration to get started.

Co-authored-by: Irek Fasikhov <malmyzh@gmail.com>
Co-authored-by: Quentin Perez <qperez42@gmail.com>
Signed-off-by: Sébastien Han <seb@redhat.com>
2021-05-31 17:45:31 +02:00

117 lines
3.2 KiB
Go

package admin
import (
"context"
"encoding/json"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var (
fakeUserResponse = []byte(`
{
"tenant": "",
"user_id": "dashboard-admin",
"display_name": "dashboard-admin",
"email": "",
"suspended": 0,
"max_buckets": 1000,
"subusers": [],
"keys": [
{
"user": "dashboard-admin",
"access_key": "4WD1FGM5PXKLC97YC0SZ",
"secret_key": "YSaT5bEcJTjBJCDG5yvr2NhGQ9xzoTIg8B1gQHa3"
}
],
"swift_keys": [],
"caps": [],
"op_mask": "read, write, delete",
"system": "true",
"admin": "false",
"default_placement": "",
"default_storage_class": "",
"placement_tags": [],
"bucket_quota": {
"enabled": false,
"check_on_raw": false,
"max_size": -1,
"max_size_kb": 0,
"max_objects": -1
},
"user_quota": {
"enabled": false,
"check_on_raw": false,
"max_size": -1,
"max_size_kb": 0,
"max_objects": -1
},
"temp_url_keys": [],
"type": "rgw",
"mfa_ids": []
}`)
)
func TestUnmarshal(t *testing.T) {
u := &User{}
err := json.Unmarshal(fakeUserResponse, &u)
assert.NoError(t, err)
}
func (suite *RadosGWTestSuite) TestUser() {
suite.SetupConnection()
co, err := New(suite.endpoint, suite.accessKey, suite.secretKey, nil)
co.Debug = true
assert.NoError(suite.T(), err)
suite.T().Run("fail to create user since no UID provided", func(t *testing.T) {
_, err = co.CreateUser(context.Background(), User{Email: "leseb@example.com"})
assert.Error(suite.T(), err)
assert.EqualError(suite.T(), err, errMissingUserID.Error())
})
suite.T().Run("fail to create user since no no display name provided", func(t *testing.T) {
_, err = co.CreateUser(context.Background(), User{ID: "leseb", Email: "leseb@example.com"})
assert.Error(suite.T(), err)
assert.EqualError(suite.T(), err, errMissingUserDisplayName.Error())
})
suite.T().Run("user creation success", func(t *testing.T) {
user, err := co.CreateUser(context.Background(), User{ID: "leseb", DisplayName: "This is leseb", Email: "leseb@example.com"})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "leseb@example.com", user.Email)
})
suite.T().Run("get user leseb", func(t *testing.T) {
user, err := co.GetUser(context.Background(), User{ID: "leseb"})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "leseb@example.com", user.Email)
})
suite.T().Run("modify user email", func(t *testing.T) {
user, err := co.ModifyUser(context.Background(), User{ID: "leseb", Email: "leseb@leseb.com"})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "leseb@leseb.com", user.Email)
})
suite.T().Run("user already exists", func(t *testing.T) {
_, err := co.CreateUser(context.Background(), User{ID: "admin", DisplayName: "Admin user"})
assert.Error(suite.T(), err)
assert.True(suite.T(), errors.Is(err, ErrUserExists), fmt.Sprintf("%+v", err))
})
suite.T().Run("get users", func(t *testing.T) {
users, err := co.GetUsers(context.Background())
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), 2, len(*users))
})
suite.T().Run("remove user", func(t *testing.T) {
err = co.RemoveUser(context.Background(), User{ID: "leseb"})
assert.NoError(suite.T(), err)
})
}