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

fix: check if db still exists where needed #10

Merged
merged 4 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
10 changes: 10 additions & 0 deletions adapters/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,13 @@ 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")
}
anbraten marked this conversation as resolved.
Show resolved Hide resolved
}
Loading