Skip to content

Commit

Permalink
Apply PEP8 formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Frank Fischer authored and sebastianneubauer committed Jan 5, 2018
1 parent 4c84815 commit 81970d3
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 53 deletions.
3 changes: 1 addition & 2 deletions postgraas_server/backends/docker/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#from . import postgres_instance_driver as pg
# from . import postgres_instance_driver as pg
from ..exceptions import PostgraasApiException


Expand Down Expand Up @@ -26,7 +26,6 @@ def delete(self, entity):
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
2 changes: 1 addition & 1 deletion postgraas_server/backends/postgres_cluster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
from . import postgres_cluster_driver as pgcd
from ..exceptions import PostgraasApiException

class PGClusterBackend(object):

class PGClusterBackend(object):
def __init__(self, config):
self.config = config

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,11 @@ def delete_database(db_name, config):
raise ValueError(e.args[0])



def delete_user(username, config):
con = _create_pg_connection(config)
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cur = con.cursor()
try:
cur.execute('''DROP USER "{}";'''.format(get_normalized_username(username)))
except psycopg2.ProgrammingError as e:
raise ValueError(e.args[0])
raise ValueError(e.args[0])
8 changes: 4 additions & 4 deletions postgraas_server/configuration.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import io
import os
import json
import errno
import json
import logging
from configparser import ConfigParser

import os

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

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -38,7 +38,7 @@ def get_config(config_filename=get_default_config_filename(), secrets_file='/sec
with open(config_filename, 'rb') as cfg:
cfg_content = f.decrypt(cfg.read())
print(cfg_content)
config.read_string(cfg_content.decode("utf-8") )
config.read_string(cfg_content.decode("utf-8"))
else:
config.read(config_filename)
expand_env_vars(config)
Expand Down
2 changes: 1 addition & 1 deletion postgraas_server/create_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
from raven.contrib.flask import Sentry

import postgraas_server
from postgraas_server.management_resources import DBInstanceResource, DBInstanceCollectionResource, db
from postgraas_server.backends import get_backend
from postgraas_server.configuration import get_application_config, get_meta_db_config_path
from postgraas_server.management_resources import DBInstanceResource, DBInstanceCollectionResource, db

INT_OPTIONS = [
'SQLALCHEMY_POOL_RECYCLE', 'SQLALCHEMY_POOL_SIZE', 'SQLALCHEMY_POOL_TIMEOUT',
Expand Down
1 change: 1 addition & 0 deletions postgraas_server/management_database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging

from sqlalchemy import inspect
from sqlalchemy.ext.declarative.clsregistry import _ModuleMarker
from sqlalchemy.orm import RelationshipProperty
Expand Down
24 changes: 12 additions & 12 deletions postgraas_server/management_resources.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import datetime
import logging
import psycopg2

import psycopg2
from flask import current_app
from flask_restful import fields, Resource, marshal_with, reqparse, abort
from flask_sqlalchemy import SQLAlchemy
Expand All @@ -25,14 +25,14 @@ class DBInstance(db.Model):
container_id = db.Column(db.String(100))

def __init__(
self,
postgraas_instance_name,
db_name,
username,
password,
hostname,
port,
container_id=None
self,
postgraas_instance_name,
db_name,
username,
password,
hostname,
port,
container_id=None
):
self.postgraas_instance_name = postgraas_instance_name
self.creation_timestamp = datetime.datetime.now()
Expand Down Expand Up @@ -140,7 +140,7 @@ def post(self):
).first():
return {
'msg':
"postgraas_instance_name already exists {}".format(args['postgraas_instance_name'])
"postgraas_instance_name already exists {}".format(args['postgraas_instance_name'])
}

db_credentials = {
Expand All @@ -161,8 +161,8 @@ def post(self):
)
if current_app.postgraas_backend.exists(db_entry):
abort(409,
description="database or user already exists {}, {}".format(args['db_name'], args['db_username'])
)
description="database or user already exists {}, {}".format(args['db_name'], args['db_username'])
)

try:
db_entry.container_id = current_app.postgraas_backend.create(db_entry, db_credentials)
Expand Down
8 changes: 4 additions & 4 deletions postgraas_server/prometheus_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import logging
from contextlib import contextmanager
import configuration as cfg
import datetime
import psycopg2

import click
import psycopg2
from flask import Blueprint, Flask, Response, abort
from prometheus_client import CONTENT_TYPE_LATEST, generate_latest
from prometheus_client.core import REGISTRY, GaugeMetricFamily
Expand Down Expand Up @@ -87,7 +87,7 @@ def metrics():
@click.option("--port", default=8000, help="Set the port for running the web server that is scraped by prometheus")
def run_server(test, port):
"""
This script provides monitoring information concerning the export performed by the aedir-exporter CLI.
This script provides monitoring information about the postgraas server.
"""
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(name)-12s %(levelname)-8s %(message)s")

Expand All @@ -108,4 +108,4 @@ def run_server(test, port):


if __name__ == '__main__':
run_server()
run_server()
3 changes: 2 additions & 1 deletion setup_integration_test_docker.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#!/usr/bin/env bash
## I hate setup bash scripts...but this one sets up a local postgres cluster for the postgres cluster backend...quite handy...
export PGDATABASE=postgres
export PGUSER=postgres
export PGPASSWORD=mysecret
export PGPORT=54320
export PGHOST=localhost

docker run --name some-postgres -e POSTGRES_PASSWORD=$PGPASSWORD -e POSTGRES_USER=$PGUSER -e POSTGRES_DB=$PGDATABASE -p $PGPORT:5432 -d postgres
docker run --name some-postgres -e POSTGRES_PASSWORD=$PGPASSWORD -e POSTGRES_USER=$PGUSER -e POSTGRES_DB=$PGDATABASE -p $PGPORT:5432 -d postgres
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import os
import uuid
import json
import io
import pytest
import uuid
from configparser import ConfigParser

import os
import pytest

import postgraas_server.backends.postgres_cluster.postgres_cluster_driver as pgcd
import postgraas_server.configuration as configuration
from postgraas_server.create_app import create_app
import postgraas_server.backends.postgres_cluster.postgres_cluster_driver as pgcd
from postgraas_server.backends.postgres_cluster import PGClusterBackend

CLUSTER_CONFIG = u"""[metadb]
db_name = postgraas
Expand Down Expand Up @@ -80,6 +79,7 @@ def parametrized_setup(request, tmpdir):
db.drop_all()
ctx.pop()


class PostgraasApiTestBase:
def get_postgraas_by_name(self, name, client):
headers = {'Content-Type': 'application/json'}
Expand All @@ -100,9 +100,9 @@ def delete_instance_by_name(self, db_credentials, client):
headers=headers
)


@pytest.mark.usefixtures('parametrized_setup')
class TestPostgraasApi(PostgraasApiTestBase):

def test_delete_db_and_user(self):
config = ConfigParser()

Expand Down Expand Up @@ -132,8 +132,8 @@ def test_delete_db_and_user(self):
data=json.dumps(db_credentials),
headers={'Content-Type': 'application/json'})
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

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()
Expand Down Expand Up @@ -198,7 +198,8 @@ def test_create_postgres_instance_username_exists(self):
data=json.dumps(db_credentials_same_user),
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)
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.get_data(as_text=True))['description']) is True

def test_create_postgres_instance_bad_username(self):
Expand Down
19 changes: 7 additions & 12 deletions tests/test_integration/test_backend_behaviour.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import os
import json
import uuid

import docker
import os
import pytest
from mock import patch, MagicMock, Mock

import postgraas_server.backends.docker.postgres_instance_driver as pid
import postgraas_server.backends.postgres_cluster.postgres_cluster_driver as pgcd
import postgraas_server.configuration as configuration
from postgraas_server.backends.exceptions import PostgraasApiException
from postgraas_server.create_app import create_app
from postgraas_server.management_resources import DBInstance
from .utils import wait_for_postgres_listening

DOCKER_CONFIG = """
[metadb]
Expand Down Expand Up @@ -70,15 +66,15 @@ def delete_all_test_database_and_user(config):
con = pgcd._create_pg_connection(config)
cur = con.cursor()
cur.execute(
'''SELECT d.datname, u.usename
FROM pg_database d
JOIN pg_user u ON (d.datdba = u.usesysid);''')
'''SELECT d.datname, u.usename
FROM pg_database d
JOIN pg_user u ON (d.datdba = u.usesysid);''')
for db in cur:
if db[0].startswith("tests_postgraas_"):
delete_test_database_and_user(db[0], db[1], config)
cur.execute(
'''SELECT u.usename
FROM pg_user u;''')
'''SELECT u.usename
FROM pg_user u;''')
for db in cur:
if db[0].startswith("tests_postgraas_"):
pgcd.delete_user(db[0], config)
Expand Down Expand Up @@ -122,7 +118,6 @@ def parametrized_setup(request, tmpdir):

@pytest.mark.usefixtures('parametrized_setup')
class TestPostgraasApi():

def test_create_and_delete_postgres_instance(self):
db_credentials = {
"db_name": 'tests_postgraas_instance_name',
Expand Down Expand Up @@ -211,4 +206,4 @@ def test_delete_nonexisting_db(self):
)
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
assert 'does not exist' in excinfo.value.message
3 changes: 2 additions & 1 deletion tests/test_integration/test_create_db.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from subprocess import check_call

import os
import pytest
from subprocess import check_call

import postgraas_server.backends.docker.postgres_instance_driver as pid
from postgraas_server import init_db
Expand Down
3 changes: 2 additions & 1 deletion tests/test_integration/test_management_database.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import unittest

import os

import postgraas_server.configuration as configuration
from postgraas_server.create_app import create_app
from postgraas_server.management_database import is_sane_database
Expand Down
2 changes: 1 addition & 1 deletion tests/test_integration/test_postgras_api.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import os
import json
import uuid

import docker
import os
import pytest
from mock import patch, MagicMock, Mock

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import postgraas_server.backends.postgres_cluster.postgres_cluster_driver as pgcd


def test_get_normalized_username():
username_with_host = 'testuser1234@testcluster1'
stripped_username = pgcd.get_normalized_username(username_with_host)
assert stripped_username == "testuser1234"
username_without_host = 'testuser1234'
stripped_username = pgcd.get_normalized_username(username_with_host)
assert stripped_username == "testuser1234"
assert stripped_username == "testuser1234"

0 comments on commit 81970d3

Please sign in to comment.