From 3a37f05c5e36724c93da53b405300801549980c4 Mon Sep 17 00:00:00 2001 From: jessicasinger Date: Wed, 23 Jun 2021 10:16:17 -0300 Subject: [PATCH 1/7] asd --- .DS_Store | Bin 0 -> 8196 bytes Makefile | 2 +- requirements/3.9.txt | 218 +++++++++++++++++++++++++++ testcontainers/core/container.py | 6 + testcontainers/core/waiting_utils.py | 4 +- tests/test_db_containers.py | 2 +- tests/test_docker_compose.py | 6 + 7 files changed, 235 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 requirements/3.9.txt diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..177cd9c6ce7781750146508e30c2550d9865da37 GIT binary patch literal 8196 zcmeHMNo*WN6sVjM5y5HfM(gz<)PoZyHVuW>x?nQrVB?e58R z&x{?%$RL3LArOlcB;XQpGmujx$b}1=1Y)(=1PF;6C&UdQmfux9mgD8X1yIr@RlTbE z^{eXtdefy}LkNNHtlCaUH6cX6>>}NT#TLnxyLnRzcs<34fchkvx6?_(N@Dx{O{4t| z-vhn}d=K~@@IByr;Q!zOytCaTY;*5(sbBgY@I7!-dcgfYgfP1p^>CRZ`*dKzLI6ZJ z1Yim4)U6D+fU6>MRq#R#;Of{P^2S9yT;|Bt33$N=cq@Y!p#WPQ=MS;r z1fm@M()WPxfz2Lpe?G+|M>JxR-1_)EnKpFW35UOeKv8kYj-7%a2~w$aE;eiBV~*lv z?QSJ=)@_|poY|yTk6V_pR#%cEx|UDSeLa@xD7vX-n6j>FIAn5EH&rX&ZDmc>X7j|j zKoCShOwh{3#c*@zXn3hPwAdbw9BB?kIwDI;qEOY)etckhZsEe>#ifTXaf`tFDv+`b z7QVRNJj2fQ@aKuZrGj~17t9l-UGl)mK`BA^=5W_TX-BtAdqm6FhzVL|qoo5W%RH0O zT={6qGO}}~ouK6@#Yklh#nHM9Bc@-_>@i21cih^9ZDpJuHgHTwUAHemY1Y>6*HA&v zX}zdsQxI2nR|M;BKN!C2*xkLWrF+U~`CdtyK!=!){(!D!hV#1W%#JH|N;6g6oEgpO zww^S!xT(x(^nS>0Kn^@70G*k3D4lPd1uw57VMsPw9U z^Wf_BwX%HHmGopZELBvXd&)|sM!89(iV(Na5Z==wwTaXg;}7C#vL5-6bXcIOIBY0M z&EQ=TsY9TH;%JV0lXrGXCxmin7ROT7+#K3zvz4P=vOMLkd`hvMp|ob=u0Fu!t1GKY zZ6VPyqO#7qha0>@h+($t$1hMOD2)oX|h0`B2SZN$V=o^a)n$a zZ{z#aN8}UoDfx_iOMWCjlV8d22& zA&A2{n1X3gUjdNq^_mUA0|FA72H0G8 zGzBGEgxnQbv$ahM(h{Vpz?yd+mLyt?Jm%7)9Vp#_jO9~FC#0z8{>Ug{D@hGOQ560; z`!16ea*e!4K19}iNq!(dkzdGf$hZZ4iY0Z~z*i2@XOF9D#_(y*}uN0XT{L zi^B+v!8lAn3N&QjEa;Fy_T?ZC^YAb{!dUn?JPXeu4_|;+;4-XiVBJ80Zye#Z^{mUq zGNxgfGuT&P>Gi|?){WwUB5p{v8~^uh|NeiYnCsi&d%*X=79N1&-dJxp3by;;yRmi% z^E75Rx#N~OG8HUXIF87|am2Mh46z= Container: def get_docker_client(self) -> DockerClient: return self._docker + def get_logs(self): + return self._container.logs(stderr=False), self._container.logs(stdout=False) + def exec(self, command): if not self._container: raise ContainerStartException("Container should be started before") return self.get_wrapped_container().exec_run(command) + diff --git a/testcontainers/core/waiting_utils.py b/testcontainers/core/waiting_utils.py index 9b224d3b4..a711ace1d 100644 --- a/testcontainers/core/waiting_utils.py +++ b/testcontainers/core/waiting_utils.py @@ -84,7 +84,9 @@ def wait_for_logs(container, predicate, timeout=None, interval=1): start = time.time() while True: duration = time.time() - start - if predicate(container._container.logs().decode()): + stdout = container.get_logs()[0].decode() + stderr = container.get_logs()[1].decode() + if predicate(stdout) or predicate(stderr): return duration if timeout and duration > timeout: raise TimeoutError("container did not emit logs satisfying predicate in %.3f seconds" diff --git a/tests/test_db_containers.py b/tests/test_db_containers.py index 2fdc706e8..2fda23037 100644 --- a/tests/test_db_containers.py +++ b/tests/test_db_containers.py @@ -22,7 +22,7 @@ def test_docker_run_mysql(): assert row[0] == '5.7.17' -def test_docker_run_postgress(): +def test_docker_run_postgres(): postgres_container = PostgresContainer("postgres:9.5") with postgres_container as postgres: e = sqlalchemy.create_engine(postgres.get_connection_url()) diff --git a/tests/test_docker_compose.py b/tests/test_docker_compose.py index 747be5e16..9cb9f0307 100644 --- a/tests/test_docker_compose.py +++ b/tests/test_docker_compose.py @@ -4,6 +4,7 @@ from testcontainers.compose import DockerCompose from testcontainers.core.docker_client import DockerClient from testcontainers.core.exceptions import NoSuchPortExposed +from testcontainers.core.waiting_utils import wait_for_logs def test_can_spawn_service_via_compose(): @@ -34,6 +35,11 @@ def test_compose_wait_for_container_ready(): compose.wait_for("http://%s:4444/wd/hub" % docker.host()) +def test_compose_wait_for_logs(): + with DockerCompose("tests") as compose: + wait_for_logs(compose, "firefox") + + def test_can_parse_multiple_compose_files(): with DockerCompose(filepath="tests", compose_file_name=["docker-compose.yml", "docker-compose-2.yml"]) as compose: From 0c3c899207ec3f25e833ea187aaab85f9c81df0f Mon Sep 17 00:00:00 2001 From: jessicasinger Date: Wed, 23 Jun 2021 15:50:53 -0300 Subject: [PATCH 2/7] updated docker compose for tests --- requirements/3.9.txt | 218 ----------------------------------- tests/docker-compose-4.yml | 2 + tests/test_docker_compose.py | 6 +- 3 files changed, 5 insertions(+), 221 deletions(-) delete mode 100644 requirements/3.9.txt create mode 100644 tests/docker-compose-4.yml diff --git a/requirements/3.9.txt b/requirements/3.9.txt deleted file mode 100644 index ca9fda6f5..000000000 --- a/requirements/3.9.txt +++ /dev/null @@ -1,218 +0,0 @@ -# -# This file is autogenerated by pip-compile -# To update, run: -# -# pip-compile --output-file=requirements/3.9.txt requirements.in -# --e file:. - # via -r requirements.in -alabaster==0.7.12 - # via sphinx -attrs==19.3.0 - # via - # jsonschema - # pytest -babel==2.8.0 - # via sphinx -bcrypt==3.2.0 - # via paramiko -cached-property==1.5.1 - # via docker-compose -cachetools==4.1.1 - # via google-auth -certifi==2020.6.20 - # via requests -cffi==1.14.2 - # via - # bcrypt - # cryptography - # pynacl -chardet==3.0.4 - # via requests -codecov==2.1.8 - # via -r requirements.in -coverage==5.2.1 - # via - # codecov - # pytest-cov -cryptography==3.0 - # via paramiko -cx-oracle==8.0.0 - # via testcontainers -deprecation==2.1.0 - # via testcontainers -distro==1.5.0 - # via docker-compose -docker-compose==1.26.2 - # via testcontainers -docker[ssh]==4.2.2 - # via - # -r requirements.in - # docker-compose - # testcontainers -dockerpty==0.4.1 - # via docker-compose -docopt==0.6.2 - # via docker-compose -docutils==0.16 - # via sphinx -flake8==3.8.3 - # via -r requirements.in -google-api-core[grpc]==1.22.1 - # via google-cloud-pubsub -google-auth==1.20.1 - # via google-api-core -google-cloud-pubsub==1.7.0 - # via testcontainers -googleapis-common-protos[grpc]==1.52.0 - # via - # google-api-core - # grpc-google-iam-v1 -grpc-google-iam-v1==0.12.3 - # via google-cloud-pubsub -grpcio==1.31.0 - # via - # google-api-core - # googleapis-common-protos - # grpc-google-iam-v1 -idna==2.10 - # via requests -imagesize==1.2.0 - # via sphinx -iniconfig==1.0.1 - # via pytest -jinja2==2.11.2 - # via sphinx -jsonschema==3.2.0 - # via docker-compose -kafka-python==2.0.2 # via testcontainers -markupsafe==1.1.1 - # via jinja2 -mccabe==0.6.1 - # via flake8 -more-itertools==8.4.0 - # via pytest -neo4j==4.1.0 - # via testcontainers -packaging==20.4 - # via - # deprecation - # pytest - # sphinx -paramiko==2.7.1 - # via docker -pluggy==0.13.1 - # via pytest -protobuf==3.13.0 - # via - # google-api-core - # googleapis-common-protos -psycopg2-binary==2.8.5 - # via testcontainers -py==1.9.0 - # via pytest -pyasn1-modules==0.2.8 - # via google-auth -pyasn1==0.4.8 - # via - # pyasn1-modules - # rsa -pycodestyle==2.6.0 - # via flake8 -pycparser==2.20 - # via cffi -pyflakes==2.2.0 - # via flake8 -pygments==2.6.1 - # via sphinx -pymongo==3.11.0 - # via testcontainers -pymysql==0.10.0 - # via testcontainers -pynacl==1.4.0 - # via paramiko -pyodbc==4.0.30 - # via testcontainers -pyparsing==2.4.7 - # via packaging -pyrsistent==0.16.0 - # via jsonschema -pytest-cov==2.10.1 - # via -r requirements.in -pytest==6.0.1 - # via - # -r requirements.in - # pytest-cov -python-dotenv==0.14.0 - # via docker-compose -pytz==2020.1 - # via - # babel - # google-api-core - # neo4j -pyyaml==5.3.1 - # via docker-compose -redis==3.5.3 - # via testcontainers -requests==2.24.0 - # via - # codecov - # docker - # docker-compose - # google-api-core - # sphinx -rsa==4.6 - # via google-auth -selenium==3.141.0 - # via testcontainers -six==1.15.0 - # via - # bcrypt - # cryptography - # docker - # docker-compose - # dockerpty - # google-api-core - # google-auth - # grpcio - # jsonschema - # packaging - # protobuf - # pynacl - # pyrsistent - # websocket-client -snowballstemmer==2.0.0 - # via sphinx -sphinx==3.2.1 - # via -r requirements.in -sphinxcontrib-applehelp==1.0.2 - # via sphinx -sphinxcontrib-devhelp==1.0.2 - # via sphinx -sphinxcontrib-htmlhelp==1.0.3 - # via sphinx -sphinxcontrib-jsmath==1.0.1 - # via sphinx -sphinxcontrib-qthelp==1.0.3 - # via sphinx -sphinxcontrib-serializinghtml==1.1.4 - # via sphinx -sqlalchemy==1.3.18 - # via testcontainers -texttable==1.6.2 - # via docker-compose -toml==0.10.1 - # via pytest -urllib3==1.25.10 - # via - # requests - # selenium -websocket-client==0.57.0 - # via - # docker - # docker-compose -wrapt==1.12.1 - # via testcontainers - -# The following packages are considered to be unsafe in a requirements file: -# setuptools diff --git a/tests/docker-compose-4.yml b/tests/docker-compose-4.yml new file mode 100644 index 000000000..9966c6549 --- /dev/null +++ b/tests/docker-compose-4.yml @@ -0,0 +1,2 @@ +hello-world: + image: "hello-world" diff --git a/tests/test_docker_compose.py b/tests/test_docker_compose.py index 9cb9f0307..28650d58d 100644 --- a/tests/test_docker_compose.py +++ b/tests/test_docker_compose.py @@ -35,9 +35,9 @@ def test_compose_wait_for_container_ready(): compose.wait_for("http://%s:4444/wd/hub" % docker.host()) -def test_compose_wait_for_logs(): - with DockerCompose("tests") as compose: - wait_for_logs(compose, "firefox") +def test_compose_can_wait_for_logs(): + with DockerCompose(filepath="tests", compose_file_name="docker-compose-4.yml") as compose: + wait_for_logs(compose, "Hello from Docker!") def test_can_parse_multiple_compose_files(): From 8451bf238deb4604ed9ef8630722220baab88a3e Mon Sep 17 00:00:00 2001 From: jessicasinger Date: Wed, 23 Jun 2021 15:53:42 -0300 Subject: [PATCH 3/7] python versions makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index b7ccc98ad..93ad8bedd 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -PYTHON_VERSIONS = 3.6 3.7 3.8 3.9 +PYTHON_VERSIONS = 3.6 3.7 3.8 REQUIREMENTS = $(addprefix requirements/,${PYTHON_VERSIONS:=.txt}) TESTS = $(addprefix tests/,${PYTHON_VERSIONS}) IMAGES = $(addprefix image/,${PYTHON_VERSIONS}) From 9e135b353bc8099d08f5c841c4372868dc462c69 Mon Sep 17 00:00:00 2001 From: jessicasinger Date: Wed, 23 Jun 2021 15:54:30 -0300 Subject: [PATCH 4/7] removed unused import --- testcontainers/core/container.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/testcontainers/core/container.py b/testcontainers/core/container.py index d025979bd..b8810994d 100644 --- a/testcontainers/core/container.py +++ b/testcontainers/core/container.py @@ -1,5 +1,3 @@ -import subprocess - from deprecation import deprecated from docker.models.containers import Container From 073a42806cdaa3fe6d089be2d810a39bc660d3d7 Mon Sep 17 00:00:00 2001 From: jessicasinger Date: Wed, 23 Jun 2021 19:08:15 -0300 Subject: [PATCH 5/7] remove DS_Store --- .DS_Store | Bin 8196 -> 0 bytes 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 177cd9c6ce7781750146508e30c2550d9865da37..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 8196 zcmeHMNo*WN6sVjM5y5HfM(gz<)PoZyHVuW>x?nQrVB?e58R z&x{?%$RL3LArOlcB;XQpGmujx$b}1=1Y)(=1PF;6C&UdQmfux9mgD8X1yIr@RlTbE z^{eXtdefy}LkNNHtlCaUH6cX6>>}NT#TLnxyLnRzcs<34fchkvx6?_(N@Dx{O{4t| z-vhn}d=K~@@IByr;Q!zOytCaTY;*5(sbBgY@I7!-dcgfYgfP1p^>CRZ`*dKzLI6ZJ z1Yim4)U6D+fU6>MRq#R#;Of{P^2S9yT;|Bt33$N=cq@Y!p#WPQ=MS;r z1fm@M()WPxfz2Lpe?G+|M>JxR-1_)EnKpFW35UOeKv8kYj-7%a2~w$aE;eiBV~*lv z?QSJ=)@_|poY|yTk6V_pR#%cEx|UDSeLa@xD7vX-n6j>FIAn5EH&rX&ZDmc>X7j|j zKoCShOwh{3#c*@zXn3hPwAdbw9BB?kIwDI;qEOY)etckhZsEe>#ifTXaf`tFDv+`b z7QVRNJj2fQ@aKuZrGj~17t9l-UGl)mK`BA^=5W_TX-BtAdqm6FhzVL|qoo5W%RH0O zT={6qGO}}~ouK6@#Yklh#nHM9Bc@-_>@i21cih^9ZDpJuHgHTwUAHemY1Y>6*HA&v zX}zdsQxI2nR|M;BKN!C2*xkLWrF+U~`CdtyK!=!){(!D!hV#1W%#JH|N;6g6oEgpO zww^S!xT(x(^nS>0Kn^@70G*k3D4lPd1uw57VMsPw9U z^Wf_BwX%HHmGopZELBvXd&)|sM!89(iV(Na5Z==wwTaXg;}7C#vL5-6bXcIOIBY0M z&EQ=TsY9TH;%JV0lXrGXCxmin7ROT7+#K3zvz4P=vOMLkd`hvMp|ob=u0Fu!t1GKY zZ6VPyqO#7qha0>@h+($t$1hMOD2)oX|h0`B2SZN$V=o^a)n$a zZ{z#aN8}UoDfx_iOMWCjlV8d22& zA&A2{n1X3gUjdNq^_mUA0|FA72H0G8 zGzBGEgxnQbv$ahM(h{Vpz?yd+mLyt?Jm%7)9Vp#_jO9~FC#0z8{>Ug{D@hGOQ560; z`!16ea*e!4K19}iNq!(dkzdGf$hZZ4iY0Z~z*i2@XOF9D#_(y*}uN0XT{L zi^B+v!8lAn3N&QjEa;Fy_T?ZC^YAb{!dUn?JPXeu4_|;+;4-XiVBJ80Zye#Z^{mUq zGNxgfGuT&P>Gi|?){WwUB5p{v8~^uh|NeiYnCsi&d%*X=79N1&-dJxp3by;;yRmi% z^E75Rx#N~OG8HUXIF87|am2Mh46z= Date: Wed, 23 Jun 2021 19:12:23 -0300 Subject: [PATCH 6/7] nice catch fellow ivan --- testcontainers/core/container.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/testcontainers/core/container.py b/testcontainers/core/container.py index b8810994d..109319886 100644 --- a/testcontainers/core/container.py +++ b/testcontainers/core/container.py @@ -128,6 +128,8 @@ def get_docker_client(self) -> DockerClient: return self._docker def get_logs(self): + if not self._container: + raise ContainerStartException("Container should be started before") return self._container.logs(stderr=False), self._container.logs(stdout=False) def exec(self, command): From 987ce2fd885c4fa90e2d0b1bfce26d4553613312 Mon Sep 17 00:00:00 2001 From: jessicasinger Date: Wed, 23 Jun 2021 19:16:45 -0300 Subject: [PATCH 7/7] added test for container logs --- tests/test_core.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tests/test_core.py b/tests/test_core.py index 0b5aced4b..c4ac6f05a 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -13,3 +13,10 @@ def test_raise_timeout(): def test_wait_for_hello(): with DockerContainer("hello-world") as container: wait_for_logs(container, "Hello from Docker!") + + +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' \ No newline at end of file