Skip to content

Commit

Permalink
Fix delete user call when user was not created before (#75)
Browse files Browse the repository at this point in the history
* Fix delete user call when user was not created before

* Add integration test for the fix
  • Loading branch information
marceloneppel authored Jan 12, 2023
1 parent 07857b6 commit 0b43fea
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/charms/postgresql_k8s/v0/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

# Increment this PATCH version before using `charmcraft publish-lib` or reset
# to 0 if you are raising the major API version
LIBPATCH = 6
LIBPATCH = 7


logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -184,6 +184,11 @@ def delete_user(self, user: str) -> None:
Args:
user: user to be deleted.
"""
# First of all, check whether the user exists. Otherwise, do nothing.
users = self.list_users()
if user not in users:
return

# List all databases.
try:
with self._connect_to_database() as connection, connection.cursor() as cursor:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ requires:
aliased-multiple-database-clusters:
interface: postgresql_client
limit: 2
no-database:
interface: postgresql_client
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ def __init__(self, *args):
self._on_cluster2_endpoints_changed,
)

# Relation used to test the situation where no database name is provided.
self.no_database = DatabaseRequires(self, "no-database", database_name="")

def _on_start(self, _) -> None:
"""Only sets an Active status."""
self.unit.status = ActiveStatus()
Expand Down
18 changes: 18 additions & 0 deletions tests/integration/new_relations/test_new_relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
SECOND_DATABASE_RELATION_NAME = "second-database"
MULTIPLE_DATABASE_CLUSTERS_RELATION_NAME = "multiple-database-clusters"
ALIASED_MULTIPLE_DATABASE_CLUSTERS_RELATION_NAME = "aliased-multiple-database-clusters"
NO_DATABASE_RELATION_NAME = "no-database"


@pytest.mark.abort_on_fail
Expand Down Expand Up @@ -358,3 +359,20 @@ async def test_restablish_relation(ops_test: OpsTest):
cursor.execute("SELECT data FROM test;")
data = cursor.fetchone()
assert data[0] == "other data"


@pytest.mark.database_relation_tests
async def test_relation_with_no_database_name(ops_test: OpsTest):
"""Test that a relation with no database name doesn't block the charm."""
async with ops_test.fast_forward():
# Relate the charms using a relation that doesn't provide a database name.
await ops_test.model.add_relation(
f"{APPLICATION_APP_NAME}:{NO_DATABASE_RELATION_NAME}", DATABASE_APP_NAME
)
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active", raise_on_blocked=True)

# Break the relation.
await ops_test.model.applications[DATABASE_APP_NAME].remove_relation(
f"{DATABASE_APP_NAME}", f"{APPLICATION_APP_NAME}:{NO_DATABASE_RELATION_NAME}"
)
await ops_test.model.wait_for_idle(apps=APP_NAMES, status="active", raise_on_blocked=True)

0 comments on commit 0b43fea

Please sign in to comment.