def test_docker_generic_db(): mongo_container = GenericContainer("mongo:latest") mongo_container.with_bind_ports(27017, 27017) with mongo_container: def connect(): return MongoClient("mongodb://{}:{}".format( mongo_container.get_container_host_ip(), mongo_container.get_exposed_port(27017))) db = wait_for(connect).primer result = db.restaurants.insert_one({ "address": { "street": "2 Avenue", "zipcode": "10075", "building": "1480", "coord": [-73.9557413, 40.7720266] }, "borough": "Manhattan", "cuisine": "Italian", "name": "Vella", "restaurant_id": "41704620" }) print(result.inserted_id) cursor = db.restaurants.find({"borough": "Manhattan"}) for document in cursor: print(document)
def test_docker_kargs(): code_dir = Path(__file__).parent container_first = GenericContainer("nginx:latest") container_first.with_volume_mapping(code_dir, '/code') container_second = GenericContainer("nginx:latest") with container_first: container_second.with_kargs(volumes_from=[container_first._container.short_id]) with container_second: files_first = container_first.exec('ls /code').output.decode('utf-8').strip() files_second = container_second.exec('ls /code').output.decode('utf-8').strip() assert files_first == files_second
def test_docker_custom_image(): container = GenericContainer("mysql:5.7.17") container.with_exposed_ports(3306) container.with_env("MYSQL_ROOT_PASSWORD", "root") with container: port = container.get_exposed_port(3306) assert int(port) > 0
from testcontainers.core.generic import GenericContainer from pht.station import StationClient import time import tempfile import os # Environment variables that need to be set for the test container # "PHT_DATA_STORAGE_FILENAME" # "PHT_DATA_STORAGE_NAME" PORT = 5000 STATION_NAME = "station" station_container_name = "personalhealthtrain/test-data-storage-service:latest" station_container = GenericContainer(station_container_name)\ .with_exposed_ports(PORT)\ .with_env('PHT_DATA_STORAGE_FILENAME', 'processed.cleveland.data')\ .with_env("PHT_DATA_STORAGE_NAME", STATION_NAME) def station_client_of(station): time.sleep(2) host = station.get_container_host_ip() port = station.get_exposed_port(PORT) return StationClient(host + ':' + port) def count_lines_in_file(filename): with open(filename, 'r') as f: return len(list(f)) def test_station_client_1():
def test_docker_parameters_using_kwargs(): code_dir = Path(__file__).parent container_first = GenericContainer("nginx:latest") container_first.with_volume_mapping(code_dir, '/code') container_second = GenericContainer("nginx:latest", working_dir='/code') print(container_second._kwargs) with container_first: container_second.with_kwargs( volumes_from=[container_first._container.short_id]) with container_second: files_first = container_first.exec('ls /code').output.decode( 'utf-8').strip() files_second = container_second.exec('ls /code').output.decode( 'utf-8').strip() assert files_first == files_second pwd_first = container_first.exec('pwd').output.decode( 'utf-8').strip() pwd_second = container_second.exec('pwd').output.decode( 'utf-8').strip() assert pwd_first != pwd_second assert pwd_second == '/code'