|
| 1 | +from os import environ |
| 2 | +from tempfile import NamedTemporaryFile |
| 3 | +from testcontainers.core.generic import DbContainer |
| 4 | + |
| 5 | +SINGLESTORE_INIT_SCRIPT_FILE = '/init.sql' |
| 6 | + |
| 7 | + |
| 8 | +class SingleStoreContainer(DbContainer): |
| 9 | + """ |
| 10 | + SingleStore database container. |
| 11 | +
|
| 12 | + Example: |
| 13 | +
|
| 14 | + .. doctest:: |
| 15 | +
|
| 16 | + >>> import sqlalchemy |
| 17 | + >>> from singlestore.testcontainers.singlestore import SingleStoreContainer |
| 18 | + >>> with SingleStoreContainer() as singlestore: |
| 19 | + ... engine = sqlalchemy.create_engine(singlestore.get_connection_url()) |
| 20 | + ... with engine.begin() as connection: |
| 21 | + ... result = connection.execute(sqlalchemy.text(f'use {singlestore.get_database_name()}; select 1;')) |
| 22 | + >>> result |
| 23 | + """ |
| 24 | + |
| 25 | + def __init__( |
| 26 | + self, |
| 27 | + image: str = "ghcr.io/singlestore-labs/singlestoredb-dev:latest", |
| 28 | + port_to_expose: int = 3306, |
| 29 | + password: str = 'password', |
| 30 | + license_key: str = None, |
| 31 | + dbname: str = 'test_db', |
| 32 | + singlestore_version: str = None, |
| 33 | + dialect: str = 'mysql+pymysql', |
| 34 | + **kwargs |
| 35 | + ): |
| 36 | + super(SingleStoreContainer, self).__init__(image, **kwargs) |
| 37 | + |
| 38 | + self.port_to_expose = port_to_expose |
| 39 | + self.with_exposed_ports(self.port_to_expose) |
| 40 | + |
| 41 | + self.SINGLESTORE_USER = 'root' |
| 42 | + self.SINGLESTORE_ROOT_PASSWORD = password or environ.get( |
| 43 | + 'SINGLESTORE_ROOT_PASSWORD', |
| 44 | + 'password' |
| 45 | + ) |
| 46 | + self.SINGLESTORE_LICENSE = license_key or environ.get('SINGLESTORE_LICENSE') |
| 47 | + self.SINGLESTORE_DATABASE = dbname or environ.get('SINGLESTORE_DATABASE', 'test_db') |
| 48 | + self.SINGLESTORE_VERSION = singlestore_version or environ.get('SINGLESTORE_VERSION') |
| 49 | + |
| 50 | + self.dialect = dialect |
| 51 | + |
| 52 | + self.init_script = NamedTemporaryFile(prefix='singlestore-init-script') |
| 53 | + |
| 54 | + def _configure(self): |
| 55 | + self.with_env("ROOT_PASSWORD", self.SINGLESTORE_ROOT_PASSWORD) |
| 56 | + self.with_env("SINGLESTORE_LICENSE", self.SINGLESTORE_LICENSE) |
| 57 | + self.with_env("LICENSE_KEY", self.SINGLESTORE_LICENSE) |
| 58 | + self.with_env("START_AFTER_INIT", 'Y') |
| 59 | + |
| 60 | + if self.SINGLESTORE_VERSION: |
| 61 | + self.with_env("SINGLESTORE_VERSION", self.SINGLESTORE_VERSION) |
| 62 | + |
| 63 | + self.maybe_emulate_amd64() |
| 64 | + |
| 65 | + self.map_init_script() |
| 66 | + |
| 67 | + def get_connection_url(self) -> str: |
| 68 | + return super()._create_connection_url( |
| 69 | + dialect=self.dialect, |
| 70 | + username=self.SQLSERVER_USER, |
| 71 | + password=self.SQLSERVER_PASSWORD, |
| 72 | + db_name=self.SQLSERVER_DBNAME, |
| 73 | + port=self.port_to_expose |
| 74 | + ) |
| 75 | + |
| 76 | + def map_init_script(self): |
| 77 | + self.init_script.write('CREATE DATABASE IF NOT EXISTS {};'.format(self.SINGLESTORE_DATABASE).encode('utf-8')) |
| 78 | + self.init_script.flush() |
| 79 | + self.with_volume_mapping(self.init_script.name, SINGLESTORE_INIT_SCRIPT_FILE) |
| 80 | + |
| 81 | + def stop(self, force=True, delete_volume=True): |
| 82 | + self.init_script.close() |
| 83 | + super().stop(force, delete_volume) |
| 84 | + |
| 85 | + def get_user(self): |
| 86 | + return self.SINGLESTORE_USER |
| 87 | + |
| 88 | + def get_password(self): |
| 89 | + return self.SINGLESTORE_ROOT_PASSWORD |
| 90 | + |
| 91 | + def get_database_name(self): |
| 92 | + return self.SINGLESTORE_DATABASE |
| 93 | + |
| 94 | + def get_port(self): |
| 95 | + return self.port_to_expose |
0 commit comments