Skip to content

Commit

Permalink
added correct exception handling in delete
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianneubauer committed Sep 28, 2017
1 parent 5b1f701 commit b6d9fba
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
9 changes: 7 additions & 2 deletions postgraas_server/backends/docker/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ def create(self, entity, connection_info):
raise PostgraasApiException(str(e))

def delete(self, entity):
from docker.errors import APIError
from docker.errors import APIError, NullResource, NotFound
from . import postgres_instance_driver as pg
if not entity.container_id:
raise PostgraasApiException("container ID not provided")
try:
return pg.delete_postgres_instance(entity.container_id)
except APIError as e:
except NotFound as e:
raise PostgraasApiException("Could not delete, does not exist {}".format(entity.container_id))
except (APIError, NullResource) as e:
raise PostgraasApiException(str(e))


def exists(self, entity):
from . import postgres_instance_driver as pg
if entity.postgraas_instance_name:
Expand Down
11 changes: 9 additions & 2 deletions postgraas_server/backends/postgres_cluster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from ..exceptions import PostgraasApiException

class PGClusterBackend(object):

def __init__(self, config):
self.config = config

Expand All @@ -13,8 +14,14 @@ def create(self, entity, connection_info):
return None

def delete(self, entity):
pgcd.delete_database(entity.db_name, self.config)
pgcd.delete_user(entity.username, self.config)
try:
pgcd.delete_database(entity.db_name, self.config)
pgcd.delete_user(entity.username, self.config)
except ValueError as e:
if 'does not exist' in e.message:
raise PostgraasApiException("Could not delete, does not exist {}".format(entity.db_name))
else:
raise PostgraasApiException(str(e))

def exists(self, entity):
return pgcd.check_db_or_user_exists(entity.db_name, entity.username, self.config)
Expand Down
22 changes: 22 additions & 0 deletions tests/test_integration/test_backend_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,25 @@ def test_create_postgraas_bad_username(self):
db_entry.container_id = self.this_app.postgraas_backend.create(db_entry, db_credentials)
self.this_app.postgraas_backend.delete(db_entry)
assert 'syntax error at or near "-"' in excinfo.value.message

def test_delete_nonexisting_db(self):
db_credentials = {
"db_name": 'tests_postgraas_instance_name',
"db_username": 'tests_postgraas_db-bad username',
"db_pwd": 'test_db_pwd',
"host": pid.get_hostname(),
"port": pid.get_open_port()
}
db_entry = DBInstance(
postgraas_instance_name=db_credentials['db_name'],
db_name=db_credentials['db_name'],
username=db_credentials['db_username'],
password="",
hostname=db_credentials['host'],
port=db_credentials['port'],
container_id="4n8nz48az49prdmdmprmr4doesnotexit"

)
with pytest.raises(PostgraasApiException) as excinfo:
db_entry.container_id = self.this_app.postgraas_backend.delete(db_entry)
assert 'does not exist' in excinfo.value.message

0 comments on commit b6d9fba

Please sign in to comment.