Skip to content

Commit

Permalink
fix: check if db still exists where needed (#10)
Browse files Browse the repository at this point in the history
Co-authored-by: Anbraten <[email protected]>
  • Loading branch information
mariusheine and anbraten authored Dec 12, 2023
1 parent fe643c8 commit 33090a2
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 5 deletions.
8 changes: 8 additions & 0 deletions adapters/couchdb_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ func (adapter couchdbAdapter) DeleteDatabase(ctx context.Context, database strin
}

func (adapter couchdbAdapter) HasDatabaseUserWithAccess(ctx context.Context, database string, username string) (bool, error) {
dbExists, dbExistsErr := adapter.HasDatabase(ctx, database)
if dbExistsErr != nil {
return false, dbExistsErr
}
if !dbExists {
return false, nil
}

sc, err := adapter.db.DB(database).Security(ctx)
if err != nil {
return false, err
Expand Down
14 changes: 13 additions & 1 deletion adapters/couchdb_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/go-kivik/kivik/v4"
)

func TestCouchDB(t *testing.T) {
func prepareCouchDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) {
databaseHost := "localhost"
databasePort := "5984"

Expand All @@ -32,5 +32,17 @@ func TestCouchDB(t *testing.T) {
return err
}

return ctx, adapter, clientConnectTest
}

func TestCouchDB(t *testing.T) {
ctx, adapter, clientConnectTest := prepareCouchDB(t)

testHelper(t, ctx, adapter, clientConnectTest)
}

func TestCouchDBCleanup(t *testing.T) {
ctx, adapter, _ := prepareCouchDB(t)

cleanupTestHelper(t, ctx, adapter)
}
14 changes: 13 additions & 1 deletion adapters/mongo_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"go.mongodb.org/mongo-driver/mongo/options"
)

func TestMongoDB(t *testing.T) {
func prepareMongoDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) {
databaseHost := "localhost"
databasePort := "27017"

Expand All @@ -34,5 +34,17 @@ func TestMongoDB(t *testing.T) {
return err
}

return ctx, adapter, clientConnectTest
}

func TestMongoDB(t *testing.T) {
ctx, adapter, clientConnectTest := prepareMongoDB(t)

testHelper(t, ctx, adapter, clientConnectTest)
}

func TestMongoDBCleanup(t *testing.T) {
ctx, adapter, _ := prepareMongoDB(t)

cleanupTestHelper(t, ctx, adapter)
}
8 changes: 8 additions & 0 deletions adapters/mssql_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ func (adapter mssqlAdapter) DeleteDatabase(ctx context.Context, database string)
}

func (adapter mssqlAdapter) HasDatabaseUserWithAccess(ctx context.Context, database string, username string) (bool, error) {
dbExists, dbExistsErr := adapter.HasDatabase(ctx, database)
if dbExistsErr != nil {
return false, dbExistsErr
}
if !dbExists {
return false, nil
}

var count int
query := fmt.Sprintf("USE [%s]; SELECT COUNT(*) FROM sys.database_principals WHERE authentication_type=2 AND name='%s';", database, username)
err := adapter.db.QueryRowContext(ctx, query).Scan(&count)
Expand Down
14 changes: 13 additions & 1 deletion adapters/mssql_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/anbraten/k8s-external-database-operator/adapters"
)

func TestMsSqlDB(t *testing.T) {
func prepareMsSqlDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) {
databaseHost := "localhost"
databasePort := "1433"

Expand All @@ -32,5 +32,17 @@ func TestMsSqlDB(t *testing.T) {
return err
}

return ctx, adapter, clientConnectTest
}

func TestMsSqlDB(t *testing.T) {
ctx, adapter, clientConnectTest := prepareMsSqlDB(t)

testHelper(t, ctx, adapter, clientConnectTest)
}

func TestMsSqlDBCleanup(t *testing.T) {
ctx, adapter, _ := prepareMsSqlDB(t)

cleanupTestHelper(t, ctx, adapter)
}
14 changes: 13 additions & 1 deletion adapters/mysql_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/anbraten/k8s-external-database-operator/adapters"
)

func TestMySqlDB(t *testing.T) {
func prepareMySqlDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) {
databaseHost := "localhost"
databasePort := "3306"

Expand All @@ -32,5 +32,17 @@ func TestMySqlDB(t *testing.T) {
return err
}

return ctx, adapter, clientConnectTest
}

func TestMySqlDB(t *testing.T) {
ctx, adapter, clientConnectTest := prepareMySqlDB(t)

testHelper(t, ctx, adapter, clientConnectTest)
}

func TestMySqlDBCleanup(t *testing.T) {
ctx, adapter, _ := prepareMySqlDB(t)

cleanupTestHelper(t, ctx, adapter)
}
14 changes: 13 additions & 1 deletion adapters/postgres_adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/jackc/pgx/v4"
)

func TestPostgresDB(t *testing.T) {
func preparePostgresDB(t *testing.T) (context.Context, adapters.DatabaseAdapter, ClientConnectTest) {
databaseHost := "localhost"
databasePort := "5432"

Expand All @@ -32,5 +32,17 @@ func TestPostgresDB(t *testing.T) {
return err
}

return ctx, adapter, clientConnectTest
}

func TestPostgresDB(t *testing.T) {
ctx, adapter, clientConnectTest := preparePostgresDB(t)

testHelper(t, ctx, adapter, clientConnectTest)
}

func TestPostgresDBCleanup(t *testing.T) {
ctx, adapter, _ := preparePostgresDB(t)

cleanupTestHelper(t, ctx, adapter)
}
13 changes: 13 additions & 0 deletions adapters/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,16 @@ func testHelper(t *testing.T, ctx context.Context, adapter adapters.DatabaseAdap
t.Fatalf("Database user does not exists")
}
}

func cleanupTestHelper(t *testing.T, ctx context.Context, adapter adapters.DatabaseAdapter) {
result, err := adapter.HasDatabaseUserWithAccess(ctx, "non-existing-db", "non-existing-user")
if err != nil {
t.Fatalf("Checking for existing database user failed: %s", err)
}
if result {
t.Fatalf("database and user existing but expecting to be non-existing")
}

// TODO: test db.DeleteDatabaseUser
// TODO: test db.DeleteDatabase
}

0 comments on commit 33090a2

Please sign in to comment.