diff --git a/adapters/couchdb_adapter.go b/adapters/couchdb_adapter.go index 3bc585a..5b23d9d 100644 --- a/adapters/couchdb_adapter.go +++ b/adapters/couchdb_adapter.go @@ -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 diff --git a/adapters/couchdb_adapter_test.go b/adapters/couchdb_adapter_test.go index e5d1c24..b2a7eba 100644 --- a/adapters/couchdb_adapter_test.go +++ b/adapters/couchdb_adapter_test.go @@ -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" @@ -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) +} diff --git a/adapters/mongo_adapter_test.go b/adapters/mongo_adapter_test.go index 83356dd..ad02561 100644 --- a/adapters/mongo_adapter_test.go +++ b/adapters/mongo_adapter_test.go @@ -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" @@ -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) +} diff --git a/adapters/mssql_adapter.go b/adapters/mssql_adapter.go index 8260b8a..de96c25 100644 --- a/adapters/mssql_adapter.go +++ b/adapters/mssql_adapter.go @@ -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) diff --git a/adapters/mssql_adapter_test.go b/adapters/mssql_adapter_test.go index 270fc07..5027770 100644 --- a/adapters/mssql_adapter_test.go +++ b/adapters/mssql_adapter_test.go @@ -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" @@ -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) +} diff --git a/adapters/mysql_adapter_test.go b/adapters/mysql_adapter_test.go index e3b8c42..acb4ece 100644 --- a/adapters/mysql_adapter_test.go +++ b/adapters/mysql_adapter_test.go @@ -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" @@ -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) +} diff --git a/adapters/postgres_adapter_test.go b/adapters/postgres_adapter_test.go index 6022cab..85c83ce 100644 --- a/adapters/postgres_adapter_test.go +++ b/adapters/postgres_adapter_test.go @@ -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" @@ -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) +} diff --git a/adapters/utils_test.go b/adapters/utils_test.go index b068df2..3c9d923 100644 --- a/adapters/utils_test.go +++ b/adapters/utils_test.go @@ -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 +}