def test_multiple_containers_with_multiple_domains_per_container(self): """ There are two containers. Matching is by environment variable INFR_VIRTUAL_HOST={{ domain }} One with single domain: google.com Second one: duckduck.com and bing.com Checks: - There is a correct connection to docker, and proper parsing of the data from docker - "tls" check is correctly called """ first = DockerContainer(image='nginx:1.19').with_env( 'INFR_VIRTUAL_HOST', 'duckduckgo.com,bing.com').start() second = DockerContainer(image='nginx:1.19').with_env( 'INFR_VIRTUAL_HOST', 'google.com').start() try: wait_for_logs(first, 'ready for start up') wait_for_logs(second, 'ready for start up') os.environ['PATH'] = path + ':' + os.environ['PATH'] check = TlsDockerNetworkCheck(param_type='environment', param_name='INFR_VIRTUAL_HOST', alert_days_before=1)\ .main() finally: first.stop() second.stop() self.assertIn('Domain google.com is OK', check[0]) self.assertIn('Domain bing.com is OK', check[0]) self.assertIn('Domain duckduckgo.com is OK', check[0]) self.assertTrue(check[1])
def test_containers_matched_by_label(self): """ There are two docker containers. Matching is by label org.riotkit.domain: {{ domain }} Checks: - Parsing of the labels syntax """ first = DockerContainer(image='nginx:1.19').with_kwargs( labels={ 'org.riotkit.domain': 'duckduckgo.com' }).start() second = DockerContainer(image='nginx:1.19')\ .with_kwargs(labels={'org.riotkit.domain': 'riseup.net,bing.com'}).start() try: wait_for_logs(first, 'ready for start up') wait_for_logs(second, 'ready for start up') os.environ['PATH'] = path + ':' + os.environ['PATH'] check = TlsDockerNetworkCheck(param_type='label', param_name='org.riotkit.domain', alert_days_before=1) \ .main() finally: first.stop() second.stop() self.assertIn('Domain duckduckgo.com is OK', check[0]) self.assertIn('Domain bing.com is OK', check[0]) self.assertIn('Domain riseup.net is OK', check[0]) self.assertTrue(check[1])
def __init__(self, project_name: str, **kwargs): super().__init__(project_name) self.tables_created: List[str] = [] if "offline_container" not in kwargs or not kwargs.get( "offline_container", None): # If we don't get an offline container provided, we try to create it on the fly. # the problem here is that each test creates its own conatiner, which basically # browns out developer laptops. current_file = pathlib.Path(__file__).parent.resolve() catalog_dir = current_file.parent.joinpath("catalog") self.container = ( DockerContainer("trinodb/trino:376").with_volume_mapping( catalog_dir, "/etc/catalog/").with_exposed_ports("8080")) self.container.start() self.provided_container = False log_string_to_wait_for = "SERVER STARTED" wait_for_logs(container=self.container, predicate=log_string_to_wait_for, timeout=30) else: self.provided_container = True self.container = kwargs["offline_container"] self.exposed_port = self.container.get_exposed_port("8080") self.client = Trino( user="******", catalog="memory", host="localhost", port=self.exposed_port, )
def create_online_store(self) -> Dict[str, str]: self.container.start() log_string_to_wait_for = "Ready to accept connections" wait_for_logs( container=self.container, predicate=log_string_to_wait_for, timeout=5 ) exposed_port = self.container.get_exposed_port("6379") return {"type": "redis", "connection_string": f"localhost:{exposed_port},db=0"}
def create_online_store(self) -> Dict[str, str]: self.container.start() log_string_to_wait_for = r"\[datastore\] Dev App Server is now running" wait_for_logs( container=self.container, predicate=log_string_to_wait_for, timeout=5 ) exposed_port = self.container.get_exposed_port("8081") os.environ[datastore.client.DATASTORE_EMULATOR_HOST] = f"0.0.0.0:{exposed_port}" return {"type": "datastore", "project_id": "test-project"}
def _setup_minio(self): self.minio = DockerContainer(self.minio_image) self.minio.with_exposed_ports(9000).with_exposed_ports(9001).with_env( "MINIO_ROOT_USER", self.access_key ).with_env("MINIO_ROOT_PASSWORD", self.secret).with_command( 'server /data --console-address ":9001"' ) self.minio.start() log_string_to_wait_for = ( "API" # The minio container will print "API: ..." when ready. ) wait_for_logs(container=self.minio, predicate=log_string_to_wait_for, timeout=5)
def create_online_store(self) -> Dict[str, str]: self.container.start() log_string_to_wait_for = ( "Initializing DynamoDB Local with the following configuration:") wait_for_logs(container=self.container, predicate=log_string_to_wait_for, timeout=5) exposed_port = self.container.get_exposed_port("8000") return { "type": "dynamodb", "endpoint_url": f"http://localhost:{exposed_port}", "region": "us-west-2", }
def _connect(self): # First we wait for Neo4j to say it's listening wait_for_logs( self, "Remote interface available at", Neo4jContainer.NEO4J_STARTUP_TIMEOUT_SECONDS, ) # Then we actually check that the container really is listening with self.get_driver() as driver: # Drivers may or may not be lazy # force them to do a round trip to confirm neo4j is working with driver.session() as session: session.run("RETURN 1").single()
def get_singleton(cls): if not cls.is_running: cls.container = ( DockerContainer("trinodb/trino:376").with_volume_mapping( cls.catalog_dir, "/etc/catalog/").with_exposed_ports("8080")) cls.container.start() log_string_to_wait_for = "SERVER STARTED" wait_for_logs(container=cls.container, predicate=log_string_to_wait_for, timeout=30) cls.is_running = True return cls.container
def _connect(self): deadline = time.time() + Neo4jContainer.NEO4J_STARTUP_TIMEOUT_SECONDS regex = re.compile("Remote interface available at", re.MULTILINE).search # First we wait for Neo4j to say it's listening wait_for_logs(self, regex, Neo4jContainer.NEO4J_STARTUP_TIMEOUT_SECONDS) # Then we actually check that the container really is listening while time.time() < deadline: with self.get_driver() as driver: # Drivers may or may not be lazy # force them to do a round trip to confirm neo4j is working with driver.session() as session: session.run("RETURN 1").single() return raise TimeoutException("Neo4j did not start within %.3f seconds" % Neo4jContainer.NEO4J_STARTUP_TIMEOUT_SECONDS)
def before_all(context): # Adding label to identify Docker container as testcontainer container (testcontainers-java does this # by default). # # Much of the testcontainers-python library is simply a wrapper around Docker SDK for Python. This is # a good example of how to make use of the parameters that the Docker SDK makes available, even if # testcontainers doesn't do so explicitly. When using testcontainers-python, it is therefore advisable # to be familiar with the Docker SDK (https://docker-py.readthedocs.io/en/stable/containers.html) # and the testcontainers-python source code (https://github.com/testcontainers/testcontainers-python). # context.rabbit = DockerContainer('rabbitmq:3.8.3-management', labels = {"org.testcontainers":"true"}) \ .with_exposed_ports(RABBIT_PORT) context.rabbit.start() wait_for_logs(context.rabbit, r"Server startup complete; \d+ plugins started", timeout=120) context.server = SimpleHTTPServer( HOST, 0, context.rabbit.get_exposed_port(RABBIT_PORT)) context.server.serve()
def test_pubsub_container(): with PubSubContainer() as pubsub: wait_for_logs(pubsub, r"Server started, listening on \d+", timeout=10) # Create a new topic publisher = pubsub.get_publisher_client() topic_path = publisher.topic_path(pubsub.project, "my-topic") publisher.create_topic(topic_path) # Create a subscription subscriber = pubsub.get_subscriber_client() subscription_path = subscriber.subscription_path( pubsub.project, "my-subscription") subscriber.create_subscription(subscription_path, topic_path) # Publish a message publisher.publish(topic_path, b"Hello world!") # Receive the message queue = Queue() subscriber.subscribe(subscription_path, queue.put) message = queue.get(timeout=1) assert message.data == b"Hello world!" message.ack()
def _start_container(self, container: DockerContainer): warnings.simplefilter("ignore") self.container = container self.container.start() wait_for_logs(self.container, 'daemon started')
def test_raise_timeout(): with pytest.raises(TimeoutError): with DockerContainer("alpine").with_command("sleep 2") as container: wait_for_logs(container, "Hello from Docker!", timeout=1e-3)
def test_can_get_logs(): with DockerContainer("hello-world") as container: wait_for_logs(container, "Hello from Docker!") stdout, stderr = container.get_logs() assert stdout, 'There should be something on stdout'
def test_wait_for_hello(): with DockerContainer("hello-world") as container: wait_for_logs(container, "Hello from Docker!")
def start(self, timeout=60): super().start() wait_for_logs(self, r'Ready\.\n', timeout=timeout) return self
def test_compose_can_wait_for_logs(): with DockerCompose(filepath=ROOT, compose_file_name="docker-compose-4.yml") as compose: wait_for_logs(compose, "Hello from Docker!")