From 5707379ed59ec78f75f4e748c9c0482cba51f459 Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 11:57:38 -0700 Subject: [PATCH 01/10] Test the http sample --- tests/test_samples.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 tests/test_samples.py diff --git a/tests/test_samples.py b/tests/test_samples.py new file mode 100644 index 00000000..c5df6f81 --- /dev/null +++ b/tests/test_samples.py @@ -0,0 +1,31 @@ +import pytest +import os +import sh +import time + + +@pytest.mark.slow_integration_test +def test_cloud_run_http(): + + cwd = os.getcwd() + os.chdir(cwd + "/../examples/cloud_run_http") + TAG = "cloud_run_http" + sh.docker(["build", "--no-cache", "--tag=%s" % TAG, "."]) + cmd = sh.docker(["run", "-p:8080:8080", "-d", TAG]) + id = cmd.stdout.decode("utf-8") + + container_up = False + timeout = 10 + while not container_up: + output = sh.docker(["ps", "-f id=%s" % id, "-f status=running", "-q"]).stdout.decode("utf-8") + container_up = output != "" + time.sleep(1) + timeout -= 1 + if timeout == 0: + assert 0 + + sample_output = sh.curl("http://localhost:8080") + assert sample_output == "Hello world!" + + sh.docker(["stop", id]) + From a0dfc8450c44d8a603b1f345639893e8fb87a95a Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 12:57:26 -0700 Subject: [PATCH 02/10] Test the http sample --- tests/test_samples.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_samples.py b/tests/test_samples.py index c5df6f81..5e2e39d5 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -2,10 +2,12 @@ import os import sh import time +import docker @pytest.mark.slow_integration_test def test_cloud_run_http(): + client = docker.from_env() cwd = os.getcwd() os.chdir(cwd + "/../examples/cloud_run_http") From b2c2182cea44880c1e6d3f96d524abb7362b2a54 Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 14:06:08 -0700 Subject: [PATCH 03/10] Convert shell to docker py api --- tests/test_samples.py | 39 ++++++++++++++++++++------------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/tests/test_samples.py b/tests/test_samples.py index 5e2e39d5..1dbce869 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -1,33 +1,34 @@ -import pytest -import os -import sh +import pathlib import time + import docker +import pytest +import requests + +SAMPLES_DIR = pathlib.Path(__file__).resolve().parent.parent / "examples" @pytest.mark.slow_integration_test def test_cloud_run_http(): client = docker.from_env() - - cwd = os.getcwd() - os.chdir(cwd + "/../examples/cloud_run_http") TAG = "cloud_run_http" - sh.docker(["build", "--no-cache", "--tag=%s" % TAG, "."]) - cmd = sh.docker(["run", "-p:8080:8080", "-d", TAG]) - id = cmd.stdout.decode("utf-8") - container_up = False + client.images.build(path=str(SAMPLES_DIR / "cloud_run_http"), tag={TAG}) + # TODO(joelgerard): Check port and deflake, etc. + container = client.containers.run(image=TAG, detach=True, ports={8080: 8080}) timeout = 10 - while not container_up: - output = sh.docker(["ps", "-f id=%s" % id, "-f status=running", "-q"]).stdout.decode("utf-8") - container_up = output != "" + success = False + while success == False and timeout > 0: + try: + response = requests.get("http://localhost:8080") + if response.text == "Hello world!": + success = True + except: + pass + time.sleep(1) timeout -= 1 - if timeout == 0: - assert 0 - - sample_output = sh.curl("http://localhost:8080") - assert sample_output == "Hello world!" - sh.docker(["stop", id]) + container.stop() + assert success From 90daa579cfaada74a855690bc23d9d71a6caacf7 Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 14:14:27 -0700 Subject: [PATCH 04/10] Include docker-py dep --- setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index 2a16fe7f..cb31f2c2 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,7 @@ "watchdog>=0.10.0", "gunicorn>=19.2.0,<21.0; platform_system!='Windows'", ], - extras_require={"test": ["pytest", "tox"]}, + extras_require={"test": ["pytest", "tox","docker-py"]}, entry_points={ "console_scripts": [ "functions-framework=functions_framework._cli:_cli", From 4af5f04a2367d0fa2f034a38615ec9a4e605e10e Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 14:25:46 -0700 Subject: [PATCH 05/10] Extra deps are in TOX --- setup.py | 1 - tox.ini | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/setup.py b/setup.py index cb31f2c2..0d8841be 100644 --- a/setup.py +++ b/setup.py @@ -53,7 +53,6 @@ "watchdog>=0.10.0", "gunicorn>=19.2.0,<21.0; platform_system!='Windows'", ], - extras_require={"test": ["pytest", "tox","docker-py"]}, entry_points={ "console_scripts": [ "functions-framework=functions_framework._cli:_cli", diff --git a/tox.ini b/tox.ini index f5b65064..052e68dd 100644 --- a/tox.ini +++ b/tox.ini @@ -4,6 +4,7 @@ envlist = py{35,36,37,38}-{ubuntu-latest,macos-latest,windows-latest},lint [testenv] usedevelop = true deps = + docker-py pytest-cov pretend setenv = From e27e151100ed967572c17477ed3f979d32a137c7 Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 14:28:21 -0700 Subject: [PATCH 06/10] Docker-py from pip is weird. --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index 052e68dd..36b51844 100644 --- a/tox.ini +++ b/tox.ini @@ -4,7 +4,7 @@ envlist = py{35,36,37,38}-{ubuntu-latest,macos-latest,windows-latest},lint [testenv] usedevelop = true deps = - docker-py + https://github.com/docker/docker-py/archive/4.2.1.zip pytest-cov pretend setenv = From 228f0dab65455e32628a8b85098a6f5eac4abae2 Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 15:16:12 -0700 Subject: [PATCH 07/10] Only run on linux. --- tests/test_samples.py | 44 +++++++++++++++++++++++-------------------- tox.ini | 3 ++- 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/tests/test_samples.py b/tests/test_samples.py index 1dbce869..e90beebf 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -1,4 +1,5 @@ import pathlib +import sys import time import docker @@ -8,27 +9,30 @@ SAMPLES_DIR = pathlib.Path(__file__).resolve().parent.parent / "examples" -@pytest.mark.slow_integration_test -def test_cloud_run_http(): - client = docker.from_env() - TAG = "cloud_run_http" +@pytest.mark.skipif(sys.platform != "linux", reason="only works on linux") +class TestSamples: - client.images.build(path=str(SAMPLES_DIR / "cloud_run_http"), tag={TAG}) - # TODO(joelgerard): Check port and deflake, etc. - container = client.containers.run(image=TAG, detach=True, ports={8080: 8080}) - timeout = 10 - success = False - while success == False and timeout > 0: - try: - response = requests.get("http://localhost:8080") - if response.text == "Hello world!": - success = True - except: - pass + @pytest.mark.slow_integration_test + def test_cloud_run_http(self): + client = docker.from_env() + TAG = "cloud_run_http" - time.sleep(1) - timeout -= 1 + client.images.build(path=str(SAMPLES_DIR / "cloud_run_http"), tag={TAG}) + # TODO(joelgerard): Check port and deflake, etc. + container = client.containers.run(image=TAG, detach=True, ports={8080: 8080}) + timeout = 10 + success = False + while success == False and timeout > 0: + try: + response = requests.get("http://localhost:8080") + if response.text == "Hello world!": + success = True + except: + pass - container.stop() + time.sleep(1) + timeout -= 1 - assert success + container.stop() + + assert success diff --git a/tox.ini b/tox.ini index 36b51844..f2cd7fe8 100644 --- a/tox.ini +++ b/tox.ini @@ -4,8 +4,9 @@ envlist = py{35,36,37,38}-{ubuntu-latest,macos-latest,windows-latest},lint [testenv] usedevelop = true deps = - https://github.com/docker/docker-py/archive/4.2.1.zip + docker pytest-cov + pytest-integration pretend setenv = PYTESTARGS = --cov=functions_framework --cov-branch --cov-report term-missing --cov-fail-under=100 From f46cbd9743bd568735e36d5750f06cc8ef204e84 Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 15:26:22 -0700 Subject: [PATCH 08/10] Stop running containers if they are running. --- tests/test_samples.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/test_samples.py b/tests/test_samples.py index e90beebf..6d48260c 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -6,19 +6,23 @@ import pytest import requests -SAMPLES_DIR = pathlib.Path(__file__).resolve().parent.parent / "examples" +EXAMPLES_DIR = pathlib.Path(__file__).resolve().parent.parent / "examples" @pytest.mark.skipif(sys.platform != "linux", reason="only works on linux") class TestSamples: + def stop_all_containers(self, docker_client): + containers = docker_client.containers.list() + for container in containers: + container.stop() @pytest.mark.slow_integration_test def test_cloud_run_http(self): client = docker.from_env() - TAG = "cloud_run_http" + self.stop_all_containers(client) - client.images.build(path=str(SAMPLES_DIR / "cloud_run_http"), tag={TAG}) - # TODO(joelgerard): Check port and deflake, etc. + TAG = "cloud_run_http" + client.images.build(path=str(EXAMPLES_DIR / "cloud_run_http"), tag={TAG}) container = client.containers.run(image=TAG, detach=True, ports={8080: 8080}) timeout = 10 success = False From b613864387acb5c3006016dc0fc45474d878426e Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 15:26:52 -0700 Subject: [PATCH 09/10] Improve skip message. --- tests/test_samples.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_samples.py b/tests/test_samples.py index 6d48260c..8d9bb6c8 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -9,7 +9,7 @@ EXAMPLES_DIR = pathlib.Path(__file__).resolve().parent.parent / "examples" -@pytest.mark.skipif(sys.platform != "linux", reason="only works on linux") +@pytest.mark.skipif(sys.platform != "linux", reason="docker only works on linux in GH actions") class TestSamples: def stop_all_containers(self, docker_client): containers = docker_client.containers.list() From 6d13b63b9e1d3fe8632ed1166350adcc121a946c Mon Sep 17 00:00:00 2001 From: joelgerard Date: Thu, 18 Jun 2020 15:29:02 -0700 Subject: [PATCH 10/10] lint error fixes --- tests/test_samples.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_samples.py b/tests/test_samples.py index 8d9bb6c8..65cee7d0 100644 --- a/tests/test_samples.py +++ b/tests/test_samples.py @@ -9,7 +9,9 @@ EXAMPLES_DIR = pathlib.Path(__file__).resolve().parent.parent / "examples" -@pytest.mark.skipif(sys.platform != "linux", reason="docker only works on linux in GH actions") +@pytest.mark.skipif( + sys.platform != "linux", reason="docker only works on linux in GH actions" +) class TestSamples: def stop_all_containers(self, docker_client): containers = docker_client.containers.list()