Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spin up separate database for pytest execution #235

Merged
merged 13 commits into from
Nov 24, 2021
Merged
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ pylint: compose-build

pytest: compose-build
@docker-compose up -d $(IMAGE_NAME)
@$(RUN) pytest
@$(RUN) pytest -x

xenon: compose-build
@$(RUN_NO_DEPS) xenon src \
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
dockerfile: Dockerfile
command: uvicorn --host 0.0.0.0 --port 8080 --reload fidesapi.main:app
healthcheck:
test: ["CMD", "curl", "-f", "http://0.0.0.0:8080/health"]
test: [ "CMD", "curl", "-f", "http://0.0.0.0:8080/health" ]
interval: 5s
timeout: 5s
retries: 5
Expand All @@ -27,7 +27,7 @@ services:
fidesctl-db:
image: postgres:12
healthcheck:
test: ["CMD-SHELL", "pg_isready -U postgres"]
test: [ "CMD-SHELL", "pg_isready -U postgres" ]
interval: 5s
timeout: 5s
retries: 5
Expand All @@ -52,4 +52,4 @@ services:
- "8000:8000"

volumes:
postgres:
postgres: null
1 change: 1 addition & 0 deletions docs/fides/docs/installation/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ server_url = "http://localhost:8080"

[api]
database_url = "postgresql://postgres:postgres@localhost:5432/fidesctl"
test_database_url = "postgresql://postgres:postgres@localhost:5432/fidesctl_test"
```

By default fidesctl will look for a configuration file in the following three places:
Expand Down
2 changes: 2 additions & 0 deletions docs/fides/docs/tutorial/add.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ server_url = "http://localhost:8080"

[api]
database_url = "postgresql://postgres:postgres@localhost:5432/fidesctl"
test_database_url = "postgresql://postgres:postgres@localhost:5432/fidesctl_test"
```

- The `[cli]`-scoped `server_url` option specifies the address that the fidesctl CLI will use when connecting to the fidesctl server.
- The `[api]`-scoped `database_url` option specifies the connection string that the fidesctl API will use to connect to the PostgreSQL database created in the previous step.
- The `[api]`-scoped `test_database_url` option that will override `database_url` when the fidesctl server is started with the environment variable `FIDESCTL_TEST_MODE` set to `True`

## Run Fidesctl via Docker

Expand Down
1 change: 1 addition & 0 deletions env_files/fidesctl.env
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
FIDESCTL_CONFIG_PATH="/fides/fidesctl/fidesctl.toml"
FIDESCTL_TEST_MODE="True"
earmenda marked this conversation as resolved.
Show resolved Hide resolved
1 change: 1 addition & 0 deletions fidesctl/fidesctl.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ server_url = "http://fidesctl:8080"

[api]
database_url = "postgresql+psycopg2://postgres:fidesctl@fidesctl-db:5432/fidesctl"
test_database_url = "postgresql+psycopg2://postgres:fidesctl@fidesctl-db:5432/fidesctl_test"
1 change: 1 addition & 0 deletions fidesctl/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@ pydantic==1.8.2
pyyaml==5.4.1
requests==2.25.1
sqlalchemy==1.4.14
SQLAlchemy-Utils==0.37.8
toml==0.10.2
versioneer==0.19
9 changes: 9 additions & 0 deletions fidesctl/src/fidesapi/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""
import os

from sqlalchemy_utils.functions import create_database, database_exists
from alembic import command
from alembic.config import Config
from alembic.migration import MigrationContext
Expand Down Expand Up @@ -41,6 +42,14 @@ def init_db(database_url: str) -> None:
load_default_taxonomy()


def create_db_if_not_exists(database_url: str) -> None:
"""
Creates a database which does not exist already.
"""
if not database_exists(database_url):
create_database(database_url)


def load_default_taxonomy() -> None:
"Upserts the default taxonomy into the database."
print("UPSERTING the default fideslang taxonomy")
Expand Down
6 changes: 4 additions & 2 deletions fidesctl/src/fidesapi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ def configure_routes() -> None:

def configure_db() -> None:
"Set up the db to be used by the app."
db_session.global_init(config.api.database_url)
database.init_db(config.api.database_url)
databse_url = config.api.database_url
database.create_db_if_not_exists(databse_url)
db_session.global_init(databse_url)
database.init_db(databse_url)


@app.get("/health", tags=["Health"])
Expand Down
18 changes: 17 additions & 1 deletion fidesctl/src/fidesctl/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,23 @@ class Config:
class APISettings(FidesSettings):
"""Class used to store values from the 'cli' section of the config."""

database_url: str = "postgresql+psycopg2://fidesdb:fidesdb@localhost:5432/fidesdb"
# This has to be defined before database_url for validation
test_database_url: str = (
"postgresql+psycopg2://postgres:fidesctl@fidesctl-db:5432/fidesctl_test"
)
database_url: str = (
"postgresql+psycopg2://postgres:fidesctl@fidesctl-db:5432/fidesctl"
)

@validator("database_url", pre=True, always=True)
def get_database_url(cls: FidesSettings, value: str, values: Dict) -> str:
"Set the database_url to the test_database_url if in test mode."
url = (
values["test_database_url"]
if os.getenv("FIDESCTL_TEST_MODE") == "True"
else value
)
return url

class Config:
env_prefix = "FIDESCTL__API__"
Expand Down
20 changes: 19 additions & 1 deletion fidesctl/tests/core/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest

from fidesctl.core.config import get_config, FidesctlConfig
from fidesctl.core.config import get_config, FidesctlConfig, APISettings


# Unit
Expand Down Expand Up @@ -43,3 +43,21 @@ def test_config_from_env_vars():
# assert config.user.user_id == "2"
# assert config.user.api_key == "test_api_key"
# assert config.cli.server_url == "test"

@pytest.mark.unit
def test_database_url_test_mode_disabled():
os.environ["FIDESCTL_TEST_MODE"] = "False"
api_settings = APISettings(
test_database_url="test_database_url",
database_url="database_url"
)
assert api_settings.database_url == "database_url"

@pytest.mark.unit
def test_database_url_test_mode_enabled():
os.environ["FIDESCTL_TEST_MODE"] = "True"
api_settings = APISettings(
test_database_url="test_database_url",
database_url="database_url"
)
assert api_settings.database_url == "test_database_url"
2 changes: 1 addition & 1 deletion fidesctl/tests/test_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
server_url = "http://fidesctl:8080"

[api]
database_url = "postgresql+psycopg2://postgres:fidesctl@fidesctl-db:5432/fidesctl"
test_database_url = "postgresql+psycopg2://postgres:fidesctl@fidesctl-db:5432/fidesctl_test"