-
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.
- Loading branch information
Showing
11 changed files
with
282 additions
and
6 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# | ||
# 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 | ||
# under the License. | ||
from influxdb_client import InfluxDBClient | ||
from testcontainers.core.config import MAX_TRIES | ||
from testcontainers.core.generic import DbContainer | ||
from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs | ||
|
||
from cratedb_toolkit.testing.testcontainers.util import KeepaliveContainer | ||
|
||
|
||
class InfluxDB2Container(KeepaliveContainer, DbContainer): | ||
""" | ||
InfluxDB database container. | ||
- https://en.wikipedia.org/wiki/Influxdb | ||
Example: | ||
The example spins up an InfluxDB2 database instance. | ||
""" | ||
|
||
ORGANIZATION = "example" | ||
TOKEN = "token" # noqa: S105 | ||
|
||
# TODO: Dual-port use with 8083+8086. | ||
def __init__( | ||
self, | ||
# TODO: Use `influxdb:latest` by default? | ||
image: str = "influxdb:2.7", | ||
port: int = 8086, | ||
dialect: str = "influxdb2", | ||
**kwargs, | ||
) -> None: | ||
super().__init__(image=image, **kwargs) | ||
|
||
self._name = "testcontainers-influxdb" # -{os.getpid()} | ||
|
||
self.port_to_expose = port | ||
self.dialect = dialect | ||
|
||
self.with_exposed_ports(self.port_to_expose, 8083) | ||
|
||
self.debug = False | ||
|
||
def _configure(self) -> None: | ||
self.with_env("DOCKER_INFLUXDB_INIT_MODE", "setup") | ||
self.with_env("DOCKER_INFLUXDB_INIT_USERNAME", "admin") | ||
self.with_env("DOCKER_INFLUXDB_INIT_PASSWORD", "secret1234") | ||
self.with_env("DOCKER_INFLUXDB_INIT_ORG", self.ORGANIZATION) | ||
self.with_env("DOCKER_INFLUXDB_INIT_BUCKET", "default") | ||
self.with_env("DOCKER_INFLUXDB_INIT_ADMIN_TOKEN", self.TOKEN) | ||
|
||
def get_connection_url(self, host=None) -> str: | ||
return super()._create_connection_url( | ||
dialect="http", | ||
username=self.ORGANIZATION, | ||
password=self.TOKEN, | ||
host=host, | ||
port=self.port_to_expose, | ||
) | ||
|
||
@wait_container_is_ready() | ||
def _connect(self) -> InfluxDBClient: | ||
# TODO: Better use a network connectivity health check? | ||
# In `testcontainers-java`, there is the `HttpWaitStrategy`. | ||
wait_for_logs(self, predicate="Listening.*tcp-listener.*8086", timeout=MAX_TRIES) | ||
return InfluxDBClient(url=self.get_connection_url(), org=self.ORGANIZATION, token=self.TOKEN, debug=self.debug) | ||
|
||
def get_connection_client(self) -> InfluxDBClient: | ||
return self._connect() |
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
Empty file.
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,66 @@ | ||
import logging | ||
|
||
import pytest | ||
from influxdb_client import InfluxDBClient | ||
|
||
from cratedb_toolkit.testing.testcontainers.influxdb2 import InfluxDB2Container | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
# Define buckets to be deleted before running each test case. | ||
RESET_BUCKETS = [ | ||
"testdrive", | ||
] | ||
|
||
|
||
class InfluxDB2Fixture: | ||
""" | ||
A little helper wrapping Testcontainer's `InfluxDB2Container`. | ||
""" | ||
|
||
def __init__(self): | ||
self.container = None | ||
self.client: InfluxDBClient = None | ||
self.setup() | ||
|
||
def setup(self): | ||
# TODO: Make image name configurable. | ||
self.container = InfluxDB2Container() | ||
self.container.start() | ||
self.client = self.container.get_connection_client() | ||
|
||
def finalize(self): | ||
self.container.stop() | ||
|
||
def reset(self): | ||
""" | ||
Delete all buckets used for testing. | ||
""" | ||
for bucket_name in RESET_BUCKETS: | ||
bucket = self.client.buckets_api().find_bucket_by_name(bucket_name) | ||
if bucket is not None: | ||
self.client.buckets_api().delete_bucket(bucket) | ||
|
||
def get_connection_url(self, *args, **kwargs): | ||
return self.container.get_connection_url(*args, **kwargs) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def influxdb_service(): | ||
""" | ||
Provide an InfluxDB service instance to the test suite. | ||
""" | ||
db = InfluxDB2Fixture() | ||
db.reset() | ||
yield db | ||
db.finalize() | ||
|
||
|
||
@pytest.fixture(scope="function") | ||
def influxdb(influxdb_service): | ||
""" | ||
Provide a fresh canvas to each test case invocation, by resetting database content. | ||
""" | ||
influxdb_service.reset() | ||
yield influxdb_service |
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,35 @@ | ||
from click.testing import CliRunner | ||
from influxio.model import InfluxDbAdapter | ||
from influxio.testdata import DataFrameFactory | ||
|
||
from cratedb_toolkit.cli import cli | ||
|
||
|
||
def test_influxdb2_load_table(caplog, cratedb, influxdb): | ||
""" | ||
CLI test: Invoke `ctk load table`. | ||
""" | ||
cratedb_url = f"{cratedb.get_connection_url()}/testdrive/demo" | ||
influxdb_url = f"{influxdb.get_connection_url()}/testdrive/demo" | ||
|
||
# Populate source database with a few records worth of data. | ||
adapter = InfluxDbAdapter.from_url(influxdb_url) | ||
adapter.ensure_bucket() | ||
dff = DataFrameFactory(rows=42) | ||
df = dff.make("dateindex") | ||
adapter.write_df(df) | ||
|
||
# Run transfer command. | ||
runner = CliRunner(env={"CRATEDB_SQLALCHEMY_URL": cratedb_url}) | ||
influxdb_url = influxdb_url.replace("http://", "influxdb2://") | ||
result = runner.invoke( | ||
cli, | ||
args=f"load table {influxdb_url}", | ||
catch_exceptions=False, | ||
) | ||
assert result.exit_code == 0 | ||
|
||
# Verify data in target database. | ||
assert cratedb.database.table_exists("testdrive.demo") is True | ||
assert cratedb.database.refresh_table("testdrive.demo") is True | ||
assert cratedb.database.count_records("testdrive.demo") == 42 |