Skip to content

Commit f3bb6d3

Browse files
committed
Start using pytest
1 parent 7861df0 commit f3bb6d3

File tree

6 files changed

+48
-32
lines changed

6 files changed

+48
-32
lines changed

conftest.py

Whitespace-only changes.

orderly_web/docker_helpers.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def container_wait_running(container, poll=0.1, timeout=1):
8383
if container.status != "running":
8484
raise Exception("container '{}' ({}) is not running ({})".format(
8585
container.name, container.id[:8], container.status))
86-
time.sleep(poll)
86+
time.sleep(timeout)
8787
container.reload()
8888
if container.status != "running":
8989
raise Exception("container '{}' ({}) is no longer running ({})".format(

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
docker
22
docopt
3+
pytest
34
pyyaml

setup.cfg

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[aliases]
2+
test=pytest

setup.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,20 @@
1212
"License :: OSI Approved :: MIT License",
1313
"Operating System :: OS Independent",
1414
],
15-
# keywords
1615
url="https://github.com/vimc/orderly-web-deploy",
1716
author="Rich FitzJohn",
1817
author_email="[email protected]",
1918
license='MIT',
2019
packages=find_packages(),
21-
# install_requires
22-
test_suite="nose.collector",
23-
tests_require=["nose"],
2420
entry_points={
2521
'console_scripts': ['orderly-web=orderly_web.cli:main'],
2622
},
2723
include_package_data=True,
2824
zip_safe=False,
2925
# Extra:
3026
long_description_content_type="text/markdown",
27+
setup_requires=["pytest-runner"],
28+
tests_require=["pytest"],
3129
requires=[
3230
"docker",
3331
"docopt",

test/test_docker_helpers.py

+42-27
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,49 @@
1-
from unittest import TestCase
1+
import pytest
22

33
from orderly_web.docker_helpers import *
44

55

6-
class TestDockerHelpers(TestCase):
6+
def test_exec_safely_throws_on_failure():
7+
with docker_client() as cl:
8+
container = cl.containers.run("alpine", ["sleep", "10"],
9+
detach=True, auto_remove=True)
10+
with pytest.raises(Exception):
11+
exec_safely(container, "missing_command")
12+
container.kill()
713

8-
def test_exec_safely_throws_on_failure(self):
9-
with docker_client() as cl:
10-
container = cl.containers.run("alpine", ["sleep", "10"],
11-
detach=True, auto_remove=True)
12-
with self.assertRaises(Exception):
13-
exec_safely(container, "missing_command")
14-
container.kill()
14+
def test_stop_and_remove_container_works():
15+
with docker_client() as cl:
16+
container = cl.containers.run("alpine", ["sleep", "10"],
17+
detach=True)
18+
name = container.name
19+
assert container_exists(cl, name)
20+
stop_and_remove_container(cl, name, False, 1)
21+
assert not container_exists(cl, name)
1522

16-
def test_stop_and_remove_container_works(self):
17-
with docker_client() as cl:
18-
container = cl.containers.run("alpine", ["sleep", "10"],
23+
def test_string_into_container():
24+
with docker_client() as cl:
25+
container = cl.containers.run("alpine", ["sleep", "20"],
26+
detach=True, auto_remove=True)
27+
text = "a\nb\nc\n"
28+
string_into_container(container, text, "/test")
29+
out = container.exec_run(["cat", "/test"])
30+
assert out[0] == 0
31+
assert out[1].decode("UTF-8") == text
32+
container.kill()
33+
34+
35+
def test_container_wait_running_detects_start_failure():
36+
with docker_client() as cl:
37+
container = cl.containers.create("alpine")
38+
with pytest.raises(Exception) as e:
39+
container_wait_running(container, 0.1, 0.1)
40+
assert "is not running (created)" in str(e)
41+
42+
43+
def test_container_wait_running_detects_slow_failure():
44+
with docker_client() as cl:
45+
with pytest.raises(Exception) as e:
46+
container = cl.containers.run("alpine", ["sleep", "1"],
1947
detach=True)
20-
name = container.name
21-
self.assertTrue(container_exists(cl, name))
22-
stop_and_remove_container(cl, name, False, 1)
23-
self.assertFalse(container_exists(cl, name))
24-
25-
def test_string_into_container(self):
26-
with docker_client() as cl:
27-
container = cl.containers.run("alpine", ["sleep", "20"],
28-
detach=True, auto_remove=True)
29-
text = "a\nb\nc\n"
30-
string_into_container(container, text, "/test")
31-
out = container.exec_run(["cat", "/test"])
32-
self.assertEqual(out[0], 0)
33-
self.assertEqual(out[1].decode("UTF-8"), text)
34-
container.kill()
48+
container_wait_running(container, 0.1, 1.2)
49+
assert "is no longer running (exited)" in str(e)

0 commit comments

Comments
 (0)