Skip to content

Commit

Permalink
all tests pass with py3
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianneubauer committed Dec 1, 2017
1 parent 749effa commit 4ca65f1
Show file tree
Hide file tree
Showing 14 changed files with 76 additions and 87 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- '2.7'
- '3.6'
services:
- docker
- postgresql
Expand Down
2 changes: 1 addition & 1 deletion postgraas_server/backends/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ConfigParser
from configparser import ConfigParser

from .docker import DockerBackend
from .postgres_cluster import PGClusterBackend
Expand Down
4 changes: 2 additions & 2 deletions postgraas_server/backends/docker/create_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def create_db_container():
"port": config.get('metadb', 'port')
}
if pg.check_container_exists('postgraas_master_db'):
print "warning container already exists"
print("warning container already exists")
postgraas_db = pg.get_container_by_name('postgraas_master_db')
db_credentials['container_id'] = postgraas_db.id
else:
Expand All @@ -24,7 +24,7 @@ def create_db_container():


def main():
print "creating container for the management db"
print("creating container for the management db")
db_credentials = create_db_container()
wait_for_postgres(
db_credentials['db_name'], db_credentials['db_username'], db_credentials['db_pwd'],
Expand Down
2 changes: 1 addition & 1 deletion postgraas_server/backends/postgres_cluster/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def delete(self, entity):
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:
if 'does not exist' in e.args[0]:
raise PostgraasApiException("Could not delete, does not exist {}".format(entity.db_name))
else:
raise PostgraasApiException(str(e))
Expand Down
20 changes: 11 additions & 9 deletions postgraas_server/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import json
import errno
import logging
import ConfigParser
from configparser import ConfigParser

__all__ = ['get_config', 'get_default_config_filename', 'get_application_config', 'expand_env_vars']

Expand All @@ -17,8 +17,9 @@ def get_default_config_filename():

def _load_secrets(filename='/secrets'):
try:
with open(filename, 'rb') as secrets_file:
with open(filename, 'r') as secrets_file:
secrets = json.loads(secrets_file.read())
print(secrets)

This comment has been minimized.

Copy link
@StephanErb

StephanErb Dec 3, 2017

Member

You might want to remove that :)

except IOError as e:
if e.errno in (errno.ENOENT, errno.EISDIR):
return {}
Expand All @@ -28,15 +29,16 @@ def _load_secrets(filename='/secrets'):


def get_config(config_filename=get_default_config_filename(), secrets_file='/secrets'):
config = ConfigParser.RawConfigParser()
config = ConfigParser()
logger.debug('config filename: {}'.format(config_filename))
secrets = _load_secrets(filename=secrets_file)
if secrets:
from cryptography.fernet import Fernet
f = Fernet(secrets['encryption_key'].encode())
with open(config_filename, 'rb') as cfg:
cfg_content = f.decrypt(cfg.read())
config.readfp(io.BytesIO(cfg_content))
print(cfg_content)
config.read_string(cfg_content.decode("utf-8") )
else:
config.read(config_filename)
expand_env_vars(config)
Expand All @@ -51,8 +53,8 @@ def expand_env_vars(config):

def get_application_config(config):
try:
return config.items('application')
except ConfigParser.NoSectionError:
return config['application']
except KeyError:
return []


Expand All @@ -70,9 +72,9 @@ def get_meta_db_config_path(config):


def get_user(config):
try:
server = config.get('metadb', 'server')
server = config.get('metadb', 'server', fallback=None)
if server:
username = '@'.join([config.get('metadb', 'db_username'), server])
except ConfigParser.NoOptionError:
else:
username = config.get('metadb', 'db_username')
return username
10 changes: 3 additions & 7 deletions postgraas_server/init_db.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import ConfigParser

from postgraas_server.configuration import get_config
from postgraas_server.management_database import init_db
from postgraas_server.utils import wait_for_postgres
Expand All @@ -16,17 +14,15 @@ def main():
"port": config.get('metadb', 'port')
}

try:
server = config.get('metadb', 'server')
server = config.get('metadb', 'server', fallback=None)
if server:
db_credentials['db_username'] = '@'.join([db_credentials['db_username'], server])
except ConfigParser.NoOptionError:
pass

wait_for_postgres(
db_credentials['db_name'], db_credentials['db_username'], db_credentials['db_pwd'],
db_credentials['host'], db_credentials['port']
)
print "initializing db"
print("initializing db")
init_db(db_credentials, postgraas_api.app)


Expand Down
2 changes: 1 addition & 1 deletion postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def delete(self, id):
dbname=entity.db_name
)
conn.close()
except StandardError as ex:
except Exception as ex:
connection_error = str(ex)

if connection_error is not None:
Expand Down
2 changes: 1 addition & 1 deletion postgraas_server/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ def wait_for_postgres(dbname, user, password, host, port):
psycopg2.connect(dbname=dbname, user=user, password=password, host=host, port=port)
return
except psycopg2.OperationalError:
print i, " ..waiting for db"
print(i, " ..waiting for db")
time.sleep(1)
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import os
import uuid
import json
import ConfigParser
import StringIO
import io
import pytest
from configparser import ConfigParser

import postgraas_server.configuration as configuration
from postgraas_server.create_app import create_app
Expand Down Expand Up @@ -104,9 +104,9 @@ def delete_instance_by_name(self, db_credentials, client):
class TestPostgraasApi(PostgraasApiTestBase):

def test_delete_db_and_user(self):
config = ConfigParser.ConfigParser()
config = ConfigParser()

config.readfp(StringIO.StringIO(CONFIGS[self.backend]))
config.read_string(CONFIGS[self.backend])
backend_config = dict(config.items('backend'))
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
Expand All @@ -125,22 +125,22 @@ def test_delete_db_and_user(self):
response = self.app_client.post('/api/v2/postgraas_instances',
data=json.dumps(db_credentials),
headers={'Content-Type': 'application/json'})
print response.data
assert ("database or user already exists" in json.loads(response.data)['description']) is True
print(response.get_data(as_text=True))
assert ("database or user already exists" in json.loads(response.get_data(as_text=True))['description']) is True
delete_test_database_and_user(db_credentials['db_name'], db_credentials['db_username'], backend_config)
response = self.app_client.post('/api/v2/postgraas_instances',
data=json.dumps(db_credentials),
headers={'Content-Type': 'application/json'})
print response.data
assert ("test_create_postgres_instance_exists" in json.loads(response.data)['db_name']) is True
print(response.get_data(as_text=True))
assert ("test_create_postgres_instance_exists" in json.loads(response.get_data(as_text=True))['db_name']) is True


def test_create_postgres_instance_exists(self):
config = ConfigParser.ConfigParser()
config = ConfigParser()

config.readfp(StringIO.StringIO(CONFIGS[self.backend]))
config.read_string(CONFIGS[self.backend])
backend_config = dict(config.items('backend'))
print config
print(config)
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": 'test_create_postgres_instance_exists',
Expand All @@ -152,22 +152,22 @@ def test_create_postgres_instance_exists(self):
delete_test_database_and_user(db_credentials['db_name'], db_credentials['db_username'], backend_config)
exists = pgcd.check_db_or_user_exists(db_credentials['db_name'], db_credentials['db_username'], backend_config)
assert exists is False
print db_credentials
print(db_credentials)
pgcd.create_postgres_db(db_credentials, backend_config)
exists = pgcd.check_db_or_user_exists(db_credentials['db_name'], db_credentials['db_username'], backend_config)
assert exists is True
response = self.app_client.post('/api/v2/postgraas_instances',
data=json.dumps(db_credentials),
headers={'Content-Type': 'application/json'})
delete_test_database_and_user(db_credentials['db_name'], db_credentials['db_username'], backend_config)
assert ("database or user already exists" in json.loads(response.data)['description']) is True
assert ("database or user already exists" in json.loads(response.get_data(as_text=True))['description']) is True

def test_create_postgres_instance_username_exists(self):
config = ConfigParser.ConfigParser()
config = ConfigParser()

config.readfp(StringIO.StringIO(CONFIGS[self.backend]))
config.read_string(CONFIGS[self.backend])
backend_config = dict(config.items('backend'))
print config
print(config)
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": 'test_create_postgres_instance_exists',
Expand All @@ -187,7 +187,7 @@ def test_create_postgres_instance_username_exists(self):
delete_test_database_and_user(db_credentials['db_name'], db_credentials['db_username'], backend_config)
exists = pgcd.check_db_or_user_exists(db_credentials['db_name'], db_credentials['db_username'], backend_config)
assert exists is False
print db_credentials
print(db_credentials)
pgcd.create_postgres_db(db_credentials, backend_config)
exists = pgcd.check_db_or_user_exists(db_credentials['db_name'], db_credentials['db_username'], backend_config)
assert exists is True
Expand All @@ -199,14 +199,14 @@ def test_create_postgres_instance_username_exists(self):
headers={'Content-Type': 'application/json'})
delete_test_database_and_user(db_credentials['db_name'], db_credentials['db_username'], backend_config)
delete_test_database_and_user(db_credentials_same_user['db_name'], db_credentials_same_user['db_username'], backend_config)
assert ("database or user already exists" in json.loads(response.data)['description']) is True
assert ("database or user already exists" in json.loads(response.get_data(as_text=True))['description']) is True

def test_create_postgres_instance_bad_username(self):
config = ConfigParser.ConfigParser()
config = ConfigParser()

config.readfp(StringIO.StringIO(CONFIGS[self.backend]))
config.read_string(CONFIGS[self.backend])
backend_config = dict(config.items('backend'))
print config
print(config)
db_credentials = {
"postgraas_instance_name": "tests_postgraas_test_create_postgres_instance_api",
"db_name": 'test_create_postgres_instance_exists',
Expand All @@ -218,6 +218,6 @@ def test_create_postgres_instance_bad_username(self):
response = self.app_client.post('/api/v2/postgraas_instances',
data=json.dumps(db_credentials),
headers={'Content-Type': 'application/json'})
print response.data
print(response.get_data(as_text=True))
delete_test_database_and_user(db_credentials['db_name'], db_credentials['db_username'], backend_config)
assert ('syntax error at or near "-"' in json.loads(response.data)['msg']) is True
assert ('syntax error at or near "-"' in json.loads(response.get_data(as_text=True))['msg']) is True
5 changes: 0 additions & 5 deletions tests/test_integration/test_backend_behaviour.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,23 +69,18 @@ def delete_all_test_postgraas_container():
def delete_all_test_database_and_user(config):
con = pgcd._create_pg_connection(config)
cur = con.cursor()
print "hier"
cur.execute(
'''SELECT d.datname, u.usename
FROM pg_database d
JOIN pg_user u ON (d.datdba = u.usesysid);''')
for db in cur:
print db[0]
if db[0].startswith("tests_postgraas_"):
print db
delete_test_database_and_user(db[0], db[1], config)
cur.execute(
'''SELECT u.usename
FROM pg_user u;''')
for db in cur:
print db[0]
if db[0].startswith("tests_postgraas_"):
print db
pgcd.delete_user(db[0], config)


Expand Down
Loading

0 comments on commit 4ca65f1

Please sign in to comment.