Skip to content

Commit

Permalink
Use different DBs for example tests (#61)
Browse files Browse the repository at this point in the history
- Example tests are failing if they are using same DB sometimes, instead
using different DBs using FromEnvWithDB() method

Fixes #53
  • Loading branch information
krishnamiriyala committed Sep 19, 2023
1 parent 65e8a2f commit 34094f9
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/daily.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ on:
description: 'Log level'
required: true
default: 'trace'

jobs:
test:
name: Unit test
Expand All @@ -24,6 +23,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
check-latest: true
cache-dependency-path: go.sum
- name: Run unit tests
run: |
mkdir -p $(go env GOPATH)
Expand All @@ -36,7 +36,6 @@ jobs:
file: test-results/coverage.out
flags: unit-tests
name: codecov-unit-test

lint:
name: Check lint
runs-on: [ubuntu-latest]
Expand All @@ -50,6 +49,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
check-latest: true
cache-dependency-path: go.sum
- name: Run lint
run: |
mkdir -p $(go env GOPATH)
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ on:
description: 'Log level'
required: true
default: 'warning'

jobs:
test:
name: Unit test
Expand All @@ -36,7 +35,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
check-latest: true

cache-dependency-path: go.sum
- name: Run unit tests
run: |
mkdir -p $(go env GOPATH)
Expand All @@ -49,7 +48,6 @@ jobs:
file: test-results/coverage.out
flags: unit-tests
name: codecov-unit-test

lint:
name: Check lint
runs-on: [ubuntu-latest]
Expand All @@ -63,6 +61,7 @@ jobs:
with:
go-version: ${{ matrix.go }}
check-latest: true
cache-dependency-path: go.sum
- name: Run lint
run: |
mkdir -p $(go env GOPATH)
Expand Down
35 changes: 24 additions & 11 deletions docs/DOCUMENTATION.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ DataStore interface exposes basic methods like Find/FindAll/Upsert/Delete. For r
- [type DataStore](<#DataStore>)
- [func FromConfig\(l \*logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig\) \(d DataStore, err error\)](<#FromConfig>)
- [func FromEnv\(l \*logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer\) \(d DataStore, err error\)](<#FromEnv>)
- [func FromEnvWithDB\(l \*logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, dbName string\) \(d DataStore, err error\)](<#FromEnvWithDB>)
- [type Helper](<#Helper>)
- [type Pagination](<#Pagination>)
- [func DefaultPagination\(\) \*Pagination](<#DefaultPagination>)
Expand All @@ -319,6 +320,8 @@ DataStore interface exposes basic methods like Find/FindAll/Upsert/Delete. For r
const (
DbConfigOrgId = "multitenant.orgId" // Name of Postgres run-time config. parameter that will store current user's org. ID
DbConfigInstanceId = "multitenant.instanceId" // Name of Postgres run-time config. parameter that will store current session's instance ID

MaxIdleConns = 1
)
```

Expand All @@ -335,7 +338,8 @@ const (
DB_ADMIN_PASSWORD_ENV_VAR = "DB_ADMIN_PASSWORD"

// SQL Error Codes.
ERROR_DUPLICATE_KEY = "SQLSTATE 23505"
ERROR_DUPLICATE_KEY = "SQLSTATE 23505"
ERROR_DUPLICATE_DATABASE = "SQLSTATE 42P04"
)
```

Expand Down Expand Up @@ -390,7 +394,7 @@ var TRACE = func(format string, v ...any) {
```

<a name="DBCreate"></a>
## func [DBCreate](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L220>)
## func [DBCreate](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L231>)

```go
func DBCreate(cfg DBConfig) error
Expand All @@ -399,7 +403,7 @@ func DBCreate(cfg DBConfig) error
Create a Postgres DB using the provided config if it doesn't exist.

<a name="DBExists"></a>
## func [DBExists](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L242>)
## func [DBExists](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L253>)

```go
func DBExists(cfg DBConfig) bool
Expand Down Expand Up @@ -561,7 +565,7 @@ func TypeName(x interface{}) string
TypeName returns name of the data type of the given variable.

<a name="DBConfig"></a>
## type [DBConfig](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L50-L57>)
## type [DBConfig](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L52-L59>)



Expand All @@ -572,7 +576,7 @@ type DBConfig struct {
```

<a name="ConfigFromEnv"></a>
### func [ConfigFromEnv](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L62>)
### func [ConfigFromEnv](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L64>)

```go
func ConfigFromEnv(dbName string) DBConfig
Expand Down Expand Up @@ -663,7 +667,7 @@ func main() {
ProdInstanceCtx := instancer.WithInstanceId(ServiceAdminCtx, "Prod")

// Initializes the Datastore using the metadata authorizer and connection details obtained from the ENV variables.
ds, err := datastore.FromEnv(datastore.GetCompLogger(), mdAuthorizer, instancer)
ds, err := datastore.FromEnvWithDB(datastore.GetCompLogger(), mdAuthorizer, instancer, "ExampleDataStore_multiInstance")
if err != nil {
log.Fatalf("datastore initialization from env errored: %s", err)
}
Expand Down Expand Up @@ -782,7 +786,7 @@ func main() {
PepsiOrgCtx := mdAuthorizer.GetAuthContext("Pepsi", TENANT_ADMIN)

// Initializes the Datastore using the metadata authorizer and connection details obtained from the ENV variables.
ds, err := datastore.FromEnv(datastore.GetCompLogger(), mdAuthorizer, nil)
ds, err := datastore.FromEnvWithDB(datastore.GetCompLogger(), mdAuthorizer, nil, "ExampleDataStore_multiTenancy")
if err != nil {
log.Fatalf("datastore initialization from env errored: %s", err)
}
Expand Down Expand Up @@ -859,7 +863,7 @@ err != nil - true
</details>

<a name="FromConfig"></a>
### func [FromConfig](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L104>)
### func [FromConfig](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L115>)

```go
func FromConfig(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, cfg DBConfig) (d DataStore, err error)
Expand All @@ -868,14 +872,23 @@ func FromConfig(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.I


<a name="FromEnv"></a>
### func [FromEnv](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L100>)
### func [FromEnv](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L102>)

```go
func FromEnv(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer) (d DataStore, err error)
```



<a name="FromEnvWithDB"></a>
### func [FromEnvWithDB](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/helper.go#L106>)

```go
func FromEnvWithDB(l *logrus.Entry, a authorizer.Authorizer, instancer authorizer.Instancer, dbName string) (d DataStore, err error)
```



<a name="Helper"></a>
## type [Helper](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/datastore.go#L77-L83>)

Expand Down Expand Up @@ -951,7 +964,7 @@ func GetRecordInstanceFromSlice(x interface{}) Record


<a name="TenancyInfo"></a>
## type [TenancyInfo](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/database.go#L73-L77>)
## type [TenancyInfo](<https://github.com/vmware-labs/multi-tenant-persistence-for-saas/blob/main/pkg/datastore/database.go#L75-L79>)



Expand Down Expand Up @@ -1403,7 +1416,7 @@ func main() {
// Initialize protostore with proper logger, authorizer and datastore
myLogger := datastore.GetCompLogger()
mdAuthorizer := authorizer.MetadataBasedAuthorizer{}
myDatastore, _ := datastore.FromEnv(myLogger, mdAuthorizer, nil)
myDatastore, _ := datastore.FromEnvWithDB(myLogger, mdAuthorizer, nil, "ExampleProtoStore")
ctx := mdAuthorizer.GetAuthContext("Coke", "service_admin")
myProtostore := protostore.GetProtoStore(myLogger, myDatastore)
Expand Down
9 changes: 9 additions & 0 deletions pkg/datastore/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ import (
const (
DbConfigOrgId = "multitenant.orgId" // Name of Postgres run-time config. parameter that will store current user's org. ID
DbConfigInstanceId = "multitenant.instanceId" // Name of Postgres run-time config. parameter that will store current session's instance ID

MaxIdleConns = 1
)

/*
Expand Down Expand Up @@ -196,6 +198,13 @@ func (db *relationalDb) configureTxWithTenancyScope(tenancyInfo TenancyInfo) (*g

// Reset Resets DB connection pools.
func (db *relationalDb) Reset() {
for _, dbConn := range db.gormDBMap {
sqlDB, err := dbConn.DB()
if err == nil {
sqlDB.Close()
sqlDB.SetMaxIdleConns(MaxIdleConns)
}
}
db.gormDBMap = make(map[dbrole.DbRole]*gorm.DB)
db.authorizer = authorizer.MetadataBasedAuthorizer{}
}
Expand Down
17 changes: 17 additions & 0 deletions pkg/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func TestHasTable(t *testing.T) {
assert := assert.New(t)

ds, _ := SetupDataStore("TestTruncate")
defer ds.Reset()
_, _, _ = SetupDbTables(ds)

exists, err := ds.TestHelper().HasTable("non-existent-table")
Expand All @@ -64,6 +65,7 @@ func TestTruncate(t *testing.T) {
assert := assert.New(t)

ds, _ := SetupDataStore("TestTruncate")
defer ds.Reset()
_, _, _ = SetupDbTables(ds)

queryResults := make([]App, 0)
Expand All @@ -85,6 +87,7 @@ func TestTruncate(t *testing.T) {
func TestTruncateNonExistent(t *testing.T) {
assert := assert.New(t)
ds, _ := SetupDataStore("TestTruncateNonExistent")
defer ds.Reset()
err := ds.TestHelper().Truncate("non_existent_table")
assert.NoError(err, "Expected no error when trying to truncate a non-existent table")
}
Expand All @@ -93,6 +96,7 @@ func TestFindInEmpty(t *testing.T) {
assert := assert.New(t)

ds, _ := SetupDataStore("TestFindInEmpty")
defer ds.Reset()
RecreateAllTables(ds)

{
Expand Down Expand Up @@ -189,6 +193,7 @@ func BenchmarkCrud(b *testing.B) {

var t testing.T
ds, _ := SetupDataStore("BenchmarkCrud")
defer ds.Reset()
myCokeApp, user1, user2 := SetupDbTables(ds)
for n := 0; n < b.N; n++ {
testCrud(&t, ds, CokeAdminCtx, myCokeApp, user1, user2)
Expand All @@ -197,13 +202,15 @@ func BenchmarkCrud(b *testing.B) {

func TestCrud(t *testing.T) {
ds, _ := SetupDataStore("TestCrud")
defer ds.Reset()
myCokeApp, user1, user2 := SetupDbTables(ds)
testCrud(t, ds, CokeAdminCtx, myCokeApp, user1, user2)
}

func TestFindAll(t *testing.T) {
assert := assert.New(t)
ds, _ := SetupDataStore("TestFindAll")
defer ds.Reset()
_, user1, user2 := SetupDbTables(ds)

// FindAll should return all (two) records
Expand All @@ -227,6 +234,7 @@ func myID(i int) string {
func TestFindAllWithPagination(t *testing.T) {
assert := assert.New(t)
ds, _ := SetupDataStore("TestFindAllWithPagination")
defer ds.Reset()
RecreateAllTables(ds)

a1 := &App{}
Expand Down Expand Up @@ -269,6 +277,7 @@ func TestFindWithCriteria(t *testing.T) {
t.Helper()
assert := assert.New(t)
ds, _ := SetupDataStore("TestFindWithCriteria")
defer ds.Reset()
_, user1, user2 := SetupDbTables(ds)

expected := []*AppUser{user1, user2}
Expand All @@ -295,6 +304,7 @@ func TestCrudWithMissingOrgId(t *testing.T) {
assert := assert.New(t)

ds, _ := SetupDataStore("TestCrudWithMissingOrgId")
defer ds.Reset()
RecreateAllTables(ds)
_, apps := make([]AppUser, 0), make([]App, 0)

Expand Down Expand Up @@ -332,6 +342,7 @@ func TestCrudWithInvalidParams(t *testing.T) {
t.Helper()
assert := assert.New(t)
ds, _ := SetupDataStore("TestCrudWithInvalidParams")
defer ds.Reset()
RecreateAllTables(ds)
ctx := CokeAdminCtx

Expand Down Expand Up @@ -370,6 +381,7 @@ func TestDALRegistration(t *testing.T) {
t.Helper()
assert := assert.New(t)
ds, _ := SetupDataStore("TestDALRegistration")
defer ds.Reset()

roleMapping := map[string]dbrole.DbRole{SERVICE_AUDITOR: dbrole.READER}
// When registering a struct with DAL, you should be able to pass it either by value or by reference
Expand Down Expand Up @@ -406,6 +418,7 @@ func TestDeleteWithMultipleCSPRoles(t *testing.T) {
const APP_ADMIN = "app_admin"
assert := assert.New(t)
ds, _ := SetupDataStore("TestDeleteWithMultipleCSPRoles")
defer ds.Reset()

// Create context for custom admin who will have 2 service roles
customCtx := ds.GetAuthorizer().GetAuthContext(COKE, APP_ADMIN, SERVICE_ADMIN)
Expand Down Expand Up @@ -435,6 +448,7 @@ Tries to perform a query with a service role that has not been authorized to acc
func TestUnauthorizedAccess(t *testing.T) {
assert := assert.New(t)
ds, _ := SetupDataStore("TestUnauthorizedAccess")
defer ds.Reset()

myCokeApp, _, _ := SetupDbTables(ds)
ctx := ds.GetAuthorizer().GetAuthContext(COKE, "unauthorized service role")
Expand All @@ -450,6 +464,7 @@ func TestCrudWithMismatchingOrgId(t *testing.T) {
t.Helper()
assert := assert.New(t)
ds, _ := SetupDataStore("TestCrudWithMismatchingOrgId")
defer ds.Reset()
myCokeApp, _, _ := SetupDbTables(ds)
tenantStr := "tenant=Pepsi"
orgIdStr := "orgIdCol=Coke"
Expand Down Expand Up @@ -597,6 +612,7 @@ Tests revision blocking updates that are outdated.
func TestRevision(t *testing.T) {
assert := assert.New(t)
ds, _ := SetupDataStore("TestRevision")
defer ds.Reset()

if err := ds.TestHelper().DropTables(&Group{}); err != nil {
assert.FailNow("Failed to drop DB tables for the following reason:\n" + err.Error())
Expand Down Expand Up @@ -701,6 +717,7 @@ func TestRevision(t *testing.T) {
func TestTransactions(t *testing.T) {
assert := assert.New(t)
ds, ps := SetupDataStore("TestTransactions")
defer ds.Reset()
_, _, _ = SetupDbTables(ds)
roleMapping := map[string]dbrole.DbRole{
TENANT_AUDITOR: dbrole.TENANT_READER,
Expand Down
2 changes: 2 additions & 0 deletions pkg/datastore/datastore_with_txcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ func BenchmarkTxCrud(b *testing.B) {

var t testing.T
ds, _ := SetupDataStore("BenchmarkCrud")
defer ds.Reset()
myCokeApp, user1, user2 := SetupDbTables(ds)
for n := 0; n < b.N; n++ {
testCrud(&t, ds, CokeAdminCtx, myCokeApp, user1, user2)
Expand All @@ -135,6 +136,7 @@ func BenchmarkTxCrud(b *testing.B) {

func TestTxCrud(t *testing.T) {
ds, _ := SetupDataStore("TestTxCrud")
defer ds.Reset()
myCokeApp, user1, user2 := SetupDbTables(ds)
testTxCrud(t, ds, CokeAdminCtx, myCokeApp, user1, user2)
}
2 changes: 1 addition & 1 deletion pkg/datastore/example_multiinstance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func ExampleDataStore_multiInstance() {
ProdInstanceCtx := instancer.WithInstanceId(ServiceAdminCtx, "Prod")

// Initializes the Datastore using the metadata authorizer and connection details obtained from the ENV variables.
ds, err := datastore.FromEnv(datastore.GetCompLogger(), mdAuthorizer, instancer)
ds, err := datastore.FromEnvWithDB(datastore.GetCompLogger(), mdAuthorizer, instancer, "ExampleDataStore_multiInstance")
if err != nil {
log.Fatalf("datastore initialization from env errored: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/datastore/example_multitenancy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ExampleDataStore_multiTenancy() {
PepsiOrgCtx := mdAuthorizer.GetAuthContext("Pepsi", TENANT_ADMIN)

// Initializes the Datastore using the metadata authorizer and connection details obtained from the ENV variables.
ds, err := datastore.FromEnv(datastore.GetCompLogger(), mdAuthorizer, nil)
ds, err := datastore.FromEnvWithDB(datastore.GetCompLogger(), mdAuthorizer, nil, "ExampleDataStore_multiTenancy")
if err != nil {
log.Fatalf("datastore initialization from env errored: %s", err)
}
Expand Down
Loading

0 comments on commit 34094f9

Please sign in to comment.