-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add seaworthy tests to ensure docker container works before publishing
- Loading branch information
1 parent
6750a65
commit d972d71
Showing
5 changed files
with
96 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import os | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption( | ||
"--hub-image", action="store", default=os.environ.get( | ||
"HUB_IMAGE", "praekeltfoundation/ndoh-hub:develop"), | ||
help="NDOH Hub image to test" | ||
) | ||
|
||
|
||
def pytest_report_header(config): | ||
return "NDOH Hub Docker image: {}".format(config.getoption("--hub-image")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
from seaworthy.definitions import ContainerDefinition | ||
from seaworthy.containers.postgresql import PostgreSQLContainer | ||
|
||
HUB_IMAGE = pytest.config.getoption("--hub-image") | ||
|
||
|
||
class HubContainer(ContainerDefinition): | ||
WAIT_PATTERNS = (r"Listening at: unix:/var/run/gunicorn/gunicorn.sock",) | ||
|
||
def __init__(self, name, db_url, image=HUB_IMAGE): | ||
super().__init__(name, image, self.WAIT_PATTERNS) | ||
self.db_url = db_url | ||
|
||
def base_kwargs(self): | ||
return { | ||
"ports": { | ||
"8000/tcp": None, | ||
}, | ||
"environment": { | ||
"HUB_DATABASE": self.db_url, | ||
}, | ||
} | ||
|
||
|
||
postgresql_container = PostgreSQLContainer("postgresql") | ||
postgresql_fixture, clean_postgresql_fixture = ( | ||
postgresql_container.pytest_clean_fixtures("postgresql_container") | ||
) | ||
|
||
hub_container = HubContainer("ndoh-hub", postgresql_container.database_url()) | ||
hub_fixture = hub_container.pytest_fixture( | ||
"hub_container", dependencies=["postgresql_container"]) | ||
|
||
__all__ = [ | ||
"clean_postgresql_fixture", | ||
"hub_fixture", | ||
"postgresql_fixture", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
seaworthy[pytest] >= 0.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import cgi | ||
|
||
from fixtures import * # noqa: F401,F403 | ||
|
||
|
||
def mime_type(content_type): | ||
return cgi.parse_header(content_type)[0] | ||
|
||
|
||
class TestHubContainer: | ||
def test_db_tables_created(self, hub_container, postgresql_container): | ||
""" | ||
When the Django container starts, it should run its migrations | ||
""" | ||
django_logs = hub_container.get_logs().decode("utf-8") | ||
assert "Running migrations" in django_logs | ||
|
||
psql_output = postgresql_container.exec_psql( | ||
("SELECT COUNT(*) FROM information_schema.tables WHERE " | ||
"table_schema='public';") | ||
) | ||
count = int(psql_output.output.strip()) | ||
assert count > 0 | ||
|
||
def test_admin_page(self, hub_container, postgresql_container): | ||
""" | ||
When we try to access the django admin page, it should be returned | ||
""" | ||
client = hub_container.http_client() | ||
response = client.get("/admin") | ||
|
||
assert response.status_code == 200 | ||
assert mime_type(response.headers["content-type"]) == "text/html" | ||
assert "<title>Log in | Django site admin</title>" in response.text |