-
Notifications
You must be signed in to change notification settings - Fork 296
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8c28a86
commit ded27fc
Showing
8 changed files
with
169 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.. autoclass:: testcontainers.db2.Db2Container | ||
.. title:: testcontainers.db2.Db2Container |
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,61 @@ | ||
from os import environ | ||
from typing import Optional | ||
|
||
from testcontainers.core.generic import DbContainer | ||
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs | ||
|
||
|
||
class Db2Container(DbContainer): | ||
""" | ||
IBM Db2 database container. | ||
Example: | ||
.. doctest:: | ||
>>> import sqlalchemy | ||
>>> from testcontainers.db2 import Db2Container | ||
>>> with Db2Container("icr.io/db2_community/db2:latest") as db2: | ||
... engine = sqlalchemy.create_engine(db2.get_connection_url()) | ||
... with engine.begin() as connection: | ||
... result = connection.execute(sqlalchemy.text("select service_level from sysibmadm.env_inst_info")) | ||
""" | ||
|
||
def __init__( | ||
self, | ||
image: str = "icr.io/db2_community/db2:latest", | ||
username: str = "db2inst1", | ||
password: Optional[str] = None, | ||
port: int = 50000, | ||
dbname: str = "testdb", | ||
dialect: str = "db2+ibm_db", | ||
**kwargs, | ||
) -> None: | ||
super().__init__(image, **kwargs) | ||
|
||
self.port = port | ||
self.with_exposed_ports(self.port) | ||
|
||
self.password = password or environ.get("DB2_PASSWORD", "password") | ||
self.username = username | ||
self.dbname = dbname | ||
self.dialect = dialect | ||
|
||
def _configure(self) -> None: | ||
self.with_env("LICENSE", "accept") | ||
self.with_env("DB2INSTANCE", self.username) | ||
self.with_env("DB2INST1_PASSWORD", self.password) | ||
self.with_env("DBNAME", self.dbname) | ||
self.with_env("ARCHIVE_LOGS", "false") | ||
self.with_env("AUTOCONFIG", "false") | ||
self.with_kwargs(privileged=True) | ||
|
||
@wait_container_is_ready() | ||
def _connect(self) -> None: | ||
wait_for_logs(self, predicate="Setup has completed") | ||
|
||
def get_connection_url(self) -> str: | ||
return super()._create_connection_url( | ||
dialect=self.dialect, username=self.username, password=self.password, dbname=self.dbname, port=self.port | ||
) |
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,43 @@ | ||
from unittest import mock | ||
|
||
import pytest | ||
import sqlalchemy | ||
|
||
from testcontainers.core.utils import is_arm | ||
from testcontainers.db2 import Db2Container | ||
|
||
|
||
@pytest.mark.skipif(is_arm(), reason="db2 container not available for ARM") | ||
@pytest.mark.parametrize("version", ["11.5.9.0", "11.5.8.0"]) | ||
def test_docker_run_db2(version: str): | ||
with Db2Container(f"icr.io/db2_community/db2:{version}", password="password") as db2: | ||
engine = sqlalchemy.create_engine(db2.get_connection_url()) | ||
with engine.begin() as connection: | ||
result = connection.execute(sqlalchemy.text("select service_level from sysibmadm.env_inst_info")) | ||
for row in result: | ||
assert row[0] == f"DB2 v{version}" | ||
|
||
|
||
# This is a feature in the generic DbContainer class | ||
# but it can't be tested on its own | ||
# so is tested in various database modules: | ||
# - mysql / mariadb | ||
# - postgresql | ||
# - sqlserver | ||
# - mongodb | ||
# - db2 | ||
def test_quoted_password(): | ||
user = "db2inst1" | ||
dbname = "testdb" | ||
password = "p@$%25+0&%rd :/!=?" | ||
quoted_password = "p%40%24%2525+0%26%25rd %3A%2F%21%3D%3F" | ||
kwargs = { | ||
"username": user, | ||
"password": password, | ||
"dbname": dbname, | ||
} | ||
with Db2Container("icr.io/db2_community/db2:11.5.9.0", **kwargs) as container: | ||
port = container.get_exposed_port(50000) | ||
host = container.get_container_host_ip() | ||
expected_url = f"db2+ibm_db://{user}:{quoted_password}@{host}:{port}/{dbname}" | ||
assert expected_url == container.get_connection_url() |
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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