Skip to content

Add Cockroach DB Container Support #281

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- arangodb
- azurite
- clickhouse
- cockroachdb
- compose
- core
- elasticsearch
Expand Down
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ testcontainers-python facilitates the use of Docker containers for functional an
arangodb/README
azurite/README
clickhouse/README
cockroachdb/README
compose/README
elasticsearch/README
google/README
Expand Down
1 change: 1 addition & 0 deletions cockroachdb/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.. autoclass:: testcontainers.cockroachdb.CockroachDbContainer
20 changes: 20 additions & 0 deletions cockroachdb/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from setuptools import setup, find_namespace_packages

description = "CockroachDB component of testcontainers-python."

setup(
name="testcontainers-cockroachdb",
version="0.0.1rc1",
packages=find_namespace_packages(),
description=description,
long_description=description,
long_description_content_type="text/x-rst",
url="https://github.com/testcontainers/testcontainers-python",
install_requires=[
"testcontainers-core",
"sqlalchemy",
"sqlalchemy-cockroachdb",
"psycopg2-binary",
],
python_requires=">=3.7",
)
85 changes: 85 additions & 0 deletions cockroachdb/testcontainers/cockroachdb/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
import os

from testcontainers.core.generic import DbContainer


class CockroachDbContainer(DbContainer):
"""
Cockroach database container.

Example
-------
The example spins up a Cockroach database and connects to it using the :code:`psycopg` driver.
.. doctest::

>>> from testcontainers.cockroachdb import CockroachDBContainer
>>> import sqlalchemy

>>> crdb_container = CockroachDBContainer("cockroachdb/cockroach:latest")
>>> with crdb_container as crdb:
... e = sqlalchemy.create_engine(crdb.get_connection_url())
... result = e.execute("select version()")
... version, = result.fetchone()
>>> version
'CockroachDB CCL v22.2.0...'
"""

COCKROACH_USER = os.environ.get("COCKROACH_USER", "cockroach")
COCKROACH_PASSWORD = os.environ.get("COCKROACH_PASSWORD", "arthropod")
COCKROACH_DATABASE = os.environ.get("COCKROACH_DATABASE", "roach")
REST_API_PORT = 8080
DB_PORT = 26257

def __init__(
self,
image="cockroachdb/cockroach:latest",
user=None,
password=None,
dbname=None,
driver="cockroachdb+psycopg2",
**kwargs
): # pylint: disable=too-many-arguments
super().__init__(image=image, **kwargs)
self.COCKROACH_USER = user or self.COCKROACH_USER
self.COCKROACH_PASSWORD = password or self.COCKROACH_PASSWORD
self.COCKROACH_DATABASE = dbname or self.COCKROACH_DATABASE
self.port_to_expose = self.DB_PORT
self.driver = driver

cmd = "start-single-node"
if not self.COCKROACH_PASSWORD:
cmd += " --insecure"

self.with_command(cmd)
self.with_exposed_ports(self.REST_API_PORT, self.DB_PORT)

def _configure(self):
self.with_env("COCKROACH_USER", self.COCKROACH_USER)
if self.COCKROACH_PASSWORD:
self.with_env("COCKROACH_PASSWORD", self.COCKROACH_PASSWORD)
self.with_env("COCKROACH_DATABASE", self.COCKROACH_DATABASE)

def get_connection_url(self):
conn_str = super()._create_connection_url(
dialect=self.driver,
username=self.COCKROACH_USER,
password=self.COCKROACH_PASSWORD,
db_name=self.COCKROACH_DATABASE,
port=self.DB_PORT,
)

if self.COCKROACH_PASSWORD:
conn_str += "?sslmode=require"

return conn_str
18 changes: 18 additions & 0 deletions cockroachdb/tests/test_cockroachdb.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from sqlalchemy import create_engine, text
from testcontainers.cockroachdb import CockroachDbContainer


def test_docker_run_cockroachdb():
with CockroachDbContainer("cockroachdb/cockroach:latest") as crdb:
with create_engine(crdb.get_connection_url()).connect() as conn:
result = conn.execute(text("select version();"))
assert "CockroachDB" in result.first().version


def test_docker_run_cockroachdb_with_user_and_pass():
with CockroachDbContainer(
"cockroachdb/cockroach:latest", user="geralt", password="unicorn"
) as crdb:
with create_engine(crdb.get_connection_url()).connect() as conn:
result = conn.execute(text("select version();"))
assert "CockroachDB" in result.first().version
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-e file:arangodb
-e file:azurite
-e file:clickhouse
-e file:cockroachdb
-e file:core
-e file:compose
-e file:elasticsearch
Expand Down
19 changes: 14 additions & 5 deletions requirements/3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# via -r requirements.in
-e file:clickhouse
# via -r requirements.in
-e file:cockroachdb
# via -r requirements.in
-e file:compose
# via -r requirements.in
-e file:core
Expand All @@ -21,6 +23,7 @@
# testcontainers-arangodb
# testcontainers-azurite
# testcontainers-clickhouse
# testcontainers-cockroachdb
# testcontainers-compose
# testcontainers-elasticsearch
# testcontainers-gcp
Expand Down Expand Up @@ -197,7 +200,7 @@ importlib-metadata==6.0.0
# via
# keyring
# twine
iniconfig==1.1.1
iniconfig==2.0.0
# via pytest
isodate==0.6.1
# via msrest
Expand All @@ -219,7 +222,7 @@ markupsafe==2.1.1
# via jinja2
mccabe==0.6.1
# via flake8
minio==7.1.12
minio==7.1.13
# via testcontainers-minio
more-itertools==9.0.0
# via jaraco-classes
Expand All @@ -244,7 +247,7 @@ pg8000==1.29.4
# via -r requirements.in
pika==1.3.1
# via testcontainers-rabbitmq
pkginfo==1.9.4
pkginfo==1.9.5
# via twine
pluggy==1.0.0
# via pytest
Expand All @@ -255,7 +258,9 @@ protobuf==3.20.3
# googleapis-common-protos
# grpcio-status
psycopg2-binary==2.9.5
# via testcontainers-postgres
# via
# testcontainers-cockroachdb
# testcontainers-postgres
pyasn1==0.4.8
# via
# pyasn1-modules
Expand Down Expand Up @@ -371,7 +376,7 @@ snowballstemmer==2.2.0
# via sphinx
sortedcontainers==2.4.0
# via trio
sphinx==6.1.1
sphinx==6.1.2
# via -r requirements.in
sphinxcontrib-applehelp==1.0.2
# via sphinx
Expand All @@ -387,10 +392,14 @@ sphinxcontrib-serializinghtml==1.1.5
# via sphinx
sqlalchemy==1.4.46
# via
# sqlalchemy-cockroachdb
# testcontainers-cockroachdb
# testcontainers-mssql
# testcontainers-mysql
# testcontainers-oracle
# testcontainers-postgres
sqlalchemy-cockroachdb==1.4.4
# via testcontainers-cockroachdb
texttable==1.6.7
# via docker-compose
tomli==2.0.1
Expand Down
17 changes: 13 additions & 4 deletions requirements/3.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# via -r requirements.in
-e file:clickhouse
# via -r requirements.in
-e file:cockroachdb
# via -r requirements.in
-e file:compose
# via -r requirements.in
-e file:core
Expand All @@ -21,6 +23,7 @@
# testcontainers-arangodb
# testcontainers-azurite
# testcontainers-clickhouse
# testcontainers-cockroachdb
# testcontainers-compose
# testcontainers-elasticsearch
# testcontainers-gcp
Expand Down Expand Up @@ -213,7 +216,7 @@ importlib-metadata==6.0.0
# twine
importlib-resources==5.10.2
# via keyring
iniconfig==1.1.1
iniconfig==2.0.0
# via pytest
isodate==0.6.1
# via msrest
Expand All @@ -235,7 +238,7 @@ markupsafe==2.1.1
# via jinja2
mccabe==0.6.1
# via flake8
minio==7.1.12
minio==7.1.13
# via testcontainers-minio
more-itertools==9.0.0
# via jaraco-classes
Expand All @@ -260,7 +263,7 @@ pg8000==1.29.4
# via -r requirements.in
pika==1.3.1
# via testcontainers-rabbitmq
pkginfo==1.9.4
pkginfo==1.9.5
# via twine
pluggy==1.0.0
# via pytest
Expand All @@ -271,7 +274,9 @@ protobuf==3.20.3
# googleapis-common-protos
# grpcio-status
psycopg2-binary==2.9.5
# via testcontainers-postgres
# via
# testcontainers-cockroachdb
# testcontainers-postgres
pyasn1==0.4.8
# via
# pyasn1-modules
Expand Down Expand Up @@ -403,10 +408,14 @@ sphinxcontrib-serializinghtml==1.1.5
# via sphinx
sqlalchemy==1.4.46
# via
# sqlalchemy-cockroachdb
# testcontainers-cockroachdb
# testcontainers-mssql
# testcontainers-mysql
# testcontainers-oracle
# testcontainers-postgres
sqlalchemy-cockroachdb==1.4.4
# via testcontainers-cockroachdb
texttable==1.6.7
# via docker-compose
tomli==2.0.1
Expand Down
19 changes: 14 additions & 5 deletions requirements/3.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
# via -r requirements.in
-e file:clickhouse
# via -r requirements.in
-e file:cockroachdb
# via -r requirements.in
-e file:compose
# via -r requirements.in
-e file:core
Expand All @@ -21,6 +23,7 @@
# testcontainers-arangodb
# testcontainers-azurite
# testcontainers-clickhouse
# testcontainers-cockroachdb
# testcontainers-compose
# testcontainers-elasticsearch
# testcontainers-gcp
Expand Down Expand Up @@ -204,7 +207,7 @@ importlib-metadata==6.0.0
# twine
importlib-resources==5.10.2
# via keyring
iniconfig==1.1.1
iniconfig==2.0.0
# via pytest
isodate==0.6.1
# via msrest
Expand All @@ -226,7 +229,7 @@ markupsafe==2.1.1
# via jinja2
mccabe==0.6.1
# via flake8
minio==7.1.12
minio==7.1.13
# via testcontainers-minio
more-itertools==9.0.0
# via jaraco-classes
Expand All @@ -251,7 +254,7 @@ pg8000==1.29.4
# via -r requirements.in
pika==1.3.1
# via testcontainers-rabbitmq
pkginfo==1.9.4
pkginfo==1.9.5
# via twine
pluggy==1.0.0
# via pytest
Expand All @@ -262,7 +265,9 @@ protobuf==3.20.3
# googleapis-common-protos
# grpcio-status
psycopg2-binary==2.9.5
# via testcontainers-postgres
# via
# testcontainers-cockroachdb
# testcontainers-postgres
pyasn1==0.4.8
# via
# pyasn1-modules
Expand Down Expand Up @@ -378,7 +383,7 @@ snowballstemmer==2.2.0
# via sphinx
sortedcontainers==2.4.0
# via trio
sphinx==6.1.1
sphinx==6.1.2
# via -r requirements.in
sphinxcontrib-applehelp==1.0.2
# via sphinx
Expand All @@ -394,10 +399,14 @@ sphinxcontrib-serializinghtml==1.1.5
# via sphinx
sqlalchemy==1.4.46
# via
# sqlalchemy-cockroachdb
# testcontainers-cockroachdb
# testcontainers-mssql
# testcontainers-mysql
# testcontainers-oracle
# testcontainers-postgres
sqlalchemy-cockroachdb==1.4.4
# via testcontainers-cockroachdb
texttable==1.6.7
# via docker-compose
tomli==2.0.1
Expand Down
Loading