Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tenanting to the golang bindings #11300

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions bindings/go/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ set(SRCS
src/fdb/tuple/tuple_test.go
src/fdb/database.go
src/fdb/directory/directorySubspace.go
src/fdb/tenant.go
src/fdb/fdb_test.go
src/fdb/snapshot.go)

Expand Down
4 changes: 4 additions & 0 deletions bindings/go/src/fdb/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,8 @@ var (
errAPIVersionUnset = Error{2200}
errAPIVersionAlreadySet = Error{2201}
errAPIVersionNotSupported = Error{2203}

errTenantNotFound = Error{2131}
errTenantExists = Error{2132}
errTenantNameInvalid = Error{2134}
)
129 changes: 129 additions & 0 deletions bindings/go/src/fdb/fdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package fdb_test

import (
"bytes"
"fmt"
"os"
"testing"
Expand Down Expand Up @@ -368,3 +369,131 @@ func ExampleOpenWithConnectionString() {

// Output:
}

// Copied from errors.go so that these types aren't public
var (
errTenantNotFound = fdb.Error{2131}
errTenantExists = fdb.Error{2132}
errTenantNameInvalid = fdb.Error{2134}
)

func TestCreateTenant(t *testing.T) {
fdb.MustAPIVersion(API_VERSION)
db := fdb.MustOpenDefault()

testTenantName := fdb.Key("test-create-tenant")

err := db.CreateTenant(testTenantName)
if err != nil {
t.Fatalf("Unable to create tenant: %v\n", err)
}

_, err = db.OpenTenant(testTenantName)
if err != nil {
t.Fatalf("Unable to open tenant: %v\n", err)
}
}

func TestCreateExistTenant(t *testing.T) {
fdb.MustAPIVersion(API_VERSION)
db := fdb.MustOpenDefault()

testTenantName := fdb.Key("test-exist-tenant")

err := db.CreateTenant(testTenantName)
if err != nil {
t.Fatalf("Unable to create tenant: %v\n", err)
}

// This should fail
err = db.CreateTenant(testTenantName)
assertErrorCodeEqual(t, err, errTenantExists)
}

func TestOpenNotExistTenant(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is probably causing ctest failure and needs to be commented out. See #11380

fdb.MustAPIVersion(API_VERSION)
db := fdb.MustOpenDefault()

testTenantName := fdb.Key("test-not-exist-tenant")

// this should fail
_, err := db.OpenTenant(testTenantName)
assertErrorCodeEqual(t, err, errTenantNotFound)
}

func TestDeleteNotExistTenant(t *testing.T) {
fdb.MustAPIVersion(API_VERSION)
db := fdb.MustOpenDefault()

testTenantName := fdb.Key("test-not-exist-tenant")

// this should fail
err := db.DeleteTenant(testTenantName)
assertErrorCodeEqual(t, err, errTenantNotFound)
}

func inSlice(sl []fdb.Key, t fdb.Key) bool {
for _, s := range sl {
if bytes.Equal(s, t) {
return true
}
}
return false
}

func assertErrorCodeEqual(t *testing.T, actual error, expected fdb.Error) {
if actual == nil {
t.Fatalf("Error is nil when it should be: %v\n", expected.Code)
}

castErr, ok := actual.(fdb.Error)
if !ok {
t.Fatalf("Error is wrong type %v, expected %v\n", actual, expected)
}

if castErr.Code != expected.Code {
t.Fatalf("Error is wrong code, expected %v, actual %v\n", expected.Code, castErr.Code)
}
}

func TestListTenant(t *testing.T) {
fdb.MustAPIVersion(API_VERSION)
db := fdb.MustOpenDefault()

testTenantName1 := fdb.Key("1-test-list-1-tenant-1")
testTenantName2 := fdb.Key("2-test-list-2-tenant-2")

err := db.CreateTenant(testTenantName1)
if err != nil {
t.Fatalf("Unable to create tenant 1: %v\n", err)
}

err = db.CreateTenant(testTenantName2)
if err != nil {
t.Fatalf("Unable to create tenant 2: %v\n", err)
}

ls, err := db.ListTenants()
if err != nil {
t.Fatalf("Unable to list tenants: %v\n", err)
}

if !inSlice(ls, testTenantName1) {
t.Fatalf("tenant 1 not in slice %#v", ls)
}

if !inSlice(ls, testTenantName2) {
t.Fatalf("tenant 2 not in slice, %#v", ls)
}
}

func TestInvalidPrefixTenant(t *testing.T) {
fdb.MustAPIVersion(API_VERSION)
db := fdb.MustOpenDefault()

testTenantName := fdb.Key("\xFFtest-invalid-prefix-tenant")

// this should fail
err := db.CreateTenant(testTenantName)
assertErrorCodeEqual(t, err, errTenantNameInvalid)
}