Skip to content

#391 build test fixes #392

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

3 changes: 2 additions & 1 deletion compose/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
url="https://github.com/testcontainers/testcontainers-python",
install_requires=[
"testcontainers-core",
"docker-compose",
# docker compose support has been removed to fix dependency issues in the build
# "docker-compose",
],
python_requires=">=3.7",
)
12 changes: 12 additions & 0 deletions compose/tests/test_docker_compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
ROOT = os.path.dirname(__file__)


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_spawn_service_via_compose():
with DockerCompose(ROOT) as compose:
host = compose.get_service_host("hub", 4444)
Expand All @@ -19,6 +20,7 @@ def test_can_spawn_service_via_compose():
assert port == "4444"


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_pull_images_before_spawning_service_via_compose():
with DockerCompose(ROOT, pull=True) as compose:
host = compose.get_service_host("hub", 4444)
Expand All @@ -27,6 +29,7 @@ def test_can_pull_images_before_spawning_service_via_compose():
assert port == "4444"


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_build_images_before_spawning_service_via_compose():
with patch.object(DockerCompose, "_call_command") as call_mock:
with DockerCompose(ROOT, build=True) as compose:
Expand All @@ -39,6 +42,7 @@ def test_can_build_images_before_spawning_service_via_compose():
assert "--build" in docker_compose_cmd


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_specify_services():
with patch.object(DockerCompose, "_call_command") as call_mock:
with DockerCompose(ROOT, services=["hub", "firefox"]) as compose:
Expand All @@ -52,6 +56,7 @@ def test_can_specify_services():
assert "chrome" not in docker_compose_cmd


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
@pytest.mark.parametrize("should_run_hub", [
[True],
[False],
Expand All @@ -71,23 +76,27 @@ def test_can_run_specific_services(should_run_hub: bool):
assert compose.get_service_host("hub", 4444)


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_throw_exception_if_no_port_exposed():
with DockerCompose(ROOT) as compose:
with pytest.raises(NoSuchPortExposed):
compose.get_service_host("hub", 5555)


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_compose_wait_for_container_ready():
with DockerCompose(ROOT) as compose:
docker = DockerClient()
compose.wait_for("http://%s:4444/wd/hub" % docker.host())


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
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!")


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_parse_multiple_compose_files():
with DockerCompose(filepath=ROOT,
compose_file_name=["docker-compose.yml", "docker-compose-2.yml"]) as compose:
Expand All @@ -102,6 +111,7 @@ def test_can_parse_multiple_compose_files():
assert port == "4444"


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_get_logs():
with DockerCompose(ROOT) as compose:
docker = DockerClient()
Expand All @@ -110,13 +120,15 @@ def test_can_get_logs():
assert stdout, 'There should be something on stdout'


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_pass_env_params_by_env_file():
with DockerCompose(ROOT, compose_file_name='docker-compose-3.yml',
env_file='.env.test') as compose:
stdout, *_ = compose.exec_in_container("alpine", ["printenv"])
assert stdout.splitlines()[0], 'test_has_passed'


@pytest.mark.skip(reason="compose support has been removed to fix dependency issues in the build")
def test_can_exec_commands():
with DockerCompose(ROOT) as compose:
result = compose.exec_in_container('hub', ['echo', 'my_test'])
Expand Down
4 changes: 2 additions & 2 deletions elasticsearch/testcontainers/elasticsearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# under the License.
import logging
import re
import urllib
import urllib.request
from typing import Dict
from urllib.error import URLError

Expand Down Expand Up @@ -88,7 +88,7 @@ def __init__(self, image: str = "elasticsearch", port: int = 9200, **kwargs) ->

@wait_container_is_ready(URLError)
def _connect(self) -> None:
res = urllib.request.urlopen(self.get_url())
res = urllib.request.urlopen(self.get_url(), timeout=1)
if res.status != 200:
raise Exception()

Expand Down
6 changes: 4 additions & 2 deletions elasticsearch/tests/test_elasticsearch.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
import urllib
import urllib.request
import pytest

from testcontainers.elasticsearch import ElasticSearchContainer
Expand All @@ -8,6 +8,8 @@
# The versions below were the current supported versions at time of writing (2022-08-11)
@pytest.mark.parametrize('version', ['6.8.23', '7.17.5', '8.3.3'])
def test_docker_run_elasticsearch(version):
with ElasticSearchContainer(f'elasticsearch:{version}') as es:
# adding the mem_limit as per: https://stackoverflow.com/a/75701019
# could also add (but not necessary): .with_env('discovery.type', 'single-node')
with ElasticSearchContainer(f'elasticsearch:{version}', mem_limit='3G') as es:
resp = urllib.request.urlopen(es.get_url())
assert json.loads(resp.read().decode())['version']['number'] == version
19 changes: 19 additions & 0 deletions nginx/testcontainers/nginx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import urllib.error
import urllib.parse
import urllib.request

from testcontainers.core.container import DockerContainer
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready


class NginxContainer(DockerContainer):
Expand All @@ -20,3 +25,17 @@ def __init__(self, image: str = "nginx:latest", port: int = 80, **kwargs) -> Non
super(NginxContainer, self).__init__(image, **kwargs)
self.port = port
self.with_exposed_ports(self.port)

def start(self) -> 'NginxContainer':
super().start()

host = self.get_container_host_ip()
port = str(self.get_exposed_port(self.port))
self._connect(host, port)

return self

@wait_container_is_ready(urllib.error.URLError)
def _connect(self, host: str, port: str) -> None:
url = urllib.parse.urlunsplit(('http', f'{host}:{port}', '', '', ''))
urllib.request.urlopen(url, timeout=1)
4 changes: 0 additions & 4 deletions requirements/macos-latest-3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these files should be manually edited, since they contain the following comment:

# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
#    pip-compile --output-file=requirements.txt --resolver=backtracking

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is true but I cannot auto generate this with the required os/env.

# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -317,8 +315,6 @@ pytz==2023.3
# via
# clickhouse-driver
# neo4j
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
4 changes: 0 additions & 4 deletions requirements/ubuntu-latest-3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -322,8 +320,6 @@ pytz==2023.3
# via
# clickhouse-driver
# neo4j
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
4 changes: 0 additions & 4 deletions requirements/ubuntu-latest-3.11.txt
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -317,8 +315,6 @@ pytz==2023.3
# via
# clickhouse-driver
# neo4j
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
4 changes: 0 additions & 4 deletions requirements/ubuntu-latest-3.7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -337,8 +335,6 @@ pytz==2023.3
# babel
# clickhouse-driver
# neo4j
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
4 changes: 0 additions & 4 deletions requirements/ubuntu-latest-3.8.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -328,8 +326,6 @@ pytz==2023.3
# babel
# clickhouse-driver
# neo4j
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
4 changes: 0 additions & 4 deletions requirements/ubuntu-latest-3.9.txt
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -323,8 +321,6 @@ pytz==2023.3
# via
# clickhouse-driver
# neo4j
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
4 changes: 0 additions & 4 deletions requirements/windows-latest-3.10.txt
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,6 @@ docker[ssh]==6.1.3
# via
# docker-compose
# testcontainers-core
docker-compose==1.29.2
# via testcontainers-compose
dockerpty==0.4.1
# via docker-compose
docopt==0.6.2
Expand Down Expand Up @@ -327,8 +325,6 @@ pywin32==306
# via docker
pywin32-ctypes==0.2.0
# via keyring
pyyaml==5.4.1
# via docker-compose
readme-renderer==37.3
# via twine
redis==4.5.5
Expand Down
17 changes: 11 additions & 6 deletions selenium/testcontainers/selenium/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@
# under the License.

from selenium import webdriver
from selenium.webdriver.common.options import ArgOptions
from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_container_is_ready
from typing import Optional
import urllib3
from typing import Any, Dict, Optional
import urllib3.exceptions


IMAGES = {
Expand All @@ -24,7 +25,7 @@
}


def get_image_name(capabilities: str) -> str:
def get_image_name(capabilities: Dict[str, Any]) -> str:
return IMAGES[capabilities['browserName']]


Expand All @@ -45,9 +46,9 @@ class BrowserWebDriverContainer(DockerContainer):
You can easily change browser by passing :code:`DesiredCapabilities.FIREFOX` instead.
"""

def __init__(self, capabilities: str, image: Optional[str] = None, port: int = 4444,
def __init__(self, capabilities: Dict[str, Any], image: Optional[str] = None, port: int = 4444,
vnc_port: int = 5900, **kwargs) -> None:
self.capabilities = capabilities
self.capabilities = capabilities or webdriver.DesiredCapabilities.CHROME
self.image = image or get_image_name(capabilities)
self.port = port
self.vnc_port = vnc_port
Expand All @@ -60,9 +61,13 @@ def _configure(self) -> None:

@wait_container_is_ready(urllib3.exceptions.HTTPError)
def _connect(self) -> webdriver.Remote:
options = ArgOptions()
caps = options.capabilities
for k, v in self.capabilities.items(): caps[k] = v # noqa

return webdriver.Remote(
command_executor=(self.get_connection_url()),
desired_capabilities=self.capabilities)
options=options)

def get_driver(self) -> webdriver.Remote:
return self._connect()
Expand Down
4 changes: 3 additions & 1 deletion selenium/tests/test_selenium.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
from testcontainers.core.utils import is_arm


@pytest.mark.parametrize("caps", [DesiredCapabilities.CHROME, DesiredCapabilities.FIREFOX])
@pytest.mark.parametrize("caps",
[DesiredCapabilities.CHROME, DesiredCapabilities.FIREFOX],
ids=['chrome', 'ff'])
def test_webdriver_container_container(caps):
if is_arm():
pytest.skip('https://github.com/SeleniumHQ/docker-selenium/issues/1076')
Expand Down