Added container-runners.py to test against different environments
This commit is contained in:
18
test/README.md
Normal file
18
test/README.md
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
# Tests for wait-for-it
|
||||||
|
|
||||||
|
* wait-for-it.py - pytests for wait-for-it.sh
|
||||||
|
* container-runners.py - Runs wait-for-it.py tests in multiple containers
|
||||||
|
* requirements.txt - pip requirements for container-runners.py
|
||||||
|
|
||||||
|
To run the basic tests:
|
||||||
|
|
||||||
|
```
|
||||||
|
python wait-for-it.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Many of the issues encountered have been related to differences between operating system versions. The container-runners.py script provides an easy way to run the python wait-for-it.py tests against multiple system configurations:
|
||||||
|
|
||||||
|
```
|
||||||
|
pip install -r requirements.txt
|
||||||
|
python container-runners.py
|
||||||
|
```
|
36
test/container-runners.py
Executable file
36
test/container-runners.py
Executable file
@ -0,0 +1,36 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
|
# Unit tests to run wait-for-it.py unit tests in several different docker images
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
from ddt import ddt, data
|
||||||
|
import os
|
||||||
|
import docker
|
||||||
|
|
||||||
|
client = docker.from_env()
|
||||||
|
app_path = os.path.abspath(os.path.join(os.path.dirname( __file__ ), '..'))
|
||||||
|
volumes = {app_path: {'bind': '/app', 'mode': 'ro'}}
|
||||||
|
|
||||||
|
@ddt
|
||||||
|
class TestContainers(unittest.TestCase):
|
||||||
|
"""
|
||||||
|
Test multiple container types with the test cases in wait-for-it.py
|
||||||
|
"""
|
||||||
|
|
||||||
|
@data(
|
||||||
|
"python:3.5-buster",
|
||||||
|
"python:3.5-stretch",
|
||||||
|
"dougg/alpine-busybox:alpine-3.11.3_busybox-1.30.1",
|
||||||
|
"dougg/alpine-busybox:alpine-3.11.3_busybox-1.31.1",
|
||||||
|
)
|
||||||
|
def test_image(self, image):
|
||||||
|
print(image)
|
||||||
|
command="/app/test/wait-for-it.py"
|
||||||
|
container = client.containers.run(image, command=command, volumes=volumes, detach=True)
|
||||||
|
result = container.wait()
|
||||||
|
logs = container.logs()
|
||||||
|
container.remove()
|
||||||
|
self.assertEqual(result, 0, logs)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
unittest.main()
|
2
test/requirements.txt
Normal file
2
test/requirements.txt
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
docker>=2.0.0
|
||||||
|
dtt>=1.2.0
|
@ -34,9 +34,9 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
s.listen(timeout)
|
s.listen(timeout)
|
||||||
return s, s.getsockname()[1]
|
return s, s.getsockname()[1]
|
||||||
|
|
||||||
def check_args(self, args, stdout_regex, stderr_regex, exitcode):
|
def check_args(self, args, stdout_regex, stderr_regex, should_succeed):
|
||||||
command = self.wait_script + " " + args
|
command = self.wait_script + " " + args
|
||||||
actual_exitcode, out, err = self.execute(command)
|
exitcode, out, err = self.execute(command)
|
||||||
|
|
||||||
# Check stderr
|
# Check stderr
|
||||||
msg = ("Failed check that STDERR:\n" +
|
msg = ("Failed check that STDERR:\n" +
|
||||||
@ -53,7 +53,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
self.assertIsNotNone(re.match(stdout_regex, out, re.DOTALL), msg)
|
self.assertIsNotNone(re.match(stdout_regex, out, re.DOTALL), msg)
|
||||||
|
|
||||||
# Check exit code
|
# Check exit code
|
||||||
self.assertEqual(actual_exitcode, exitcode)
|
self.assertEqual(should_succeed, exitcode == 0)
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
script_path = os.path.dirname(sys.argv[0])
|
script_path = os.path.dirname(sys.argv[0])
|
||||||
@ -69,7 +69,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"",
|
"",
|
||||||
"^$",
|
"^$",
|
||||||
MISSING_ARGS_TEXT,
|
MISSING_ARGS_TEXT,
|
||||||
1
|
False
|
||||||
)
|
)
|
||||||
# Return code should be 1 when called with no args
|
# Return code should be 1 when called with no args
|
||||||
exitcode, out, err = self.execute(self.wait_script)
|
exitcode, out, err = self.execute(self.wait_script)
|
||||||
@ -81,7 +81,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"--help",
|
"--help",
|
||||||
"",
|
"",
|
||||||
HELP_TEXT,
|
HELP_TEXT,
|
||||||
1
|
False
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_no_port(self):
|
def test_no_port(self):
|
||||||
@ -90,7 +90,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"--host=localhost",
|
"--host=localhost",
|
||||||
"",
|
"",
|
||||||
MISSING_ARGS_TEXT,
|
MISSING_ARGS_TEXT,
|
||||||
1
|
False
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_no_host(self):
|
def test_no_host(self):
|
||||||
@ -99,7 +99,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"--port=80",
|
"--port=80",
|
||||||
"",
|
"",
|
||||||
MISSING_ARGS_TEXT,
|
MISSING_ARGS_TEXT,
|
||||||
1
|
False
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_host_port(self):
|
def test_host_port(self):
|
||||||
@ -109,7 +109,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"--host=localhost --port={0} --timeout=1".format(port),
|
"--host=localhost --port={0} --timeout=1".format(port),
|
||||||
"",
|
"",
|
||||||
"wait-for-it.sh: waiting 1 seconds for localhost:{0}".format(port),
|
"wait-for-it.sh: waiting 1 seconds for localhost:{0}".format(port),
|
||||||
0
|
True
|
||||||
)
|
)
|
||||||
soc.close()
|
soc.close()
|
||||||
|
|
||||||
@ -123,7 +123,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"localhost:{0} --timeout=1".format(port),
|
"localhost:{0} --timeout=1".format(port),
|
||||||
"",
|
"",
|
||||||
"wait-for-it.sh: waiting 1 seconds for localhost:{0}".format(port),
|
"wait-for-it.sh: waiting 1 seconds for localhost:{0}".format(port),
|
||||||
0
|
True
|
||||||
)
|
)
|
||||||
soc.close()
|
soc.close()
|
||||||
|
|
||||||
@ -136,7 +136,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"localhost:8929 --timeout=1",
|
"localhost:8929 --timeout=1",
|
||||||
"",
|
"",
|
||||||
".*timeout occurred after waiting 1 seconds for localhost:8929",
|
".*timeout occurred after waiting 1 seconds for localhost:8929",
|
||||||
124
|
False
|
||||||
)
|
)
|
||||||
|
|
||||||
def test_command_execution(self):
|
def test_command_execution(self):
|
||||||
@ -148,7 +148,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"localhost:{0} -- echo \"CMD OUTPUT\"".format(port),
|
"localhost:{0} -- echo \"CMD OUTPUT\"".format(port),
|
||||||
"CMD OUTPUT",
|
"CMD OUTPUT",
|
||||||
".*wait-for-it.sh: localhost:{0} is available after 0 seconds".format(port),
|
".*wait-for-it.sh: localhost:{0} is available after 0 seconds".format(port),
|
||||||
0
|
True
|
||||||
)
|
)
|
||||||
soc.close()
|
soc.close()
|
||||||
|
|
||||||
@ -162,7 +162,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"localhost:{0} -- ls not_real_file".format(port),
|
"localhost:{0} -- ls not_real_file".format(port),
|
||||||
"",
|
"",
|
||||||
".*No such file or directory\n",
|
".*No such file or directory\n",
|
||||||
2
|
False
|
||||||
)
|
)
|
||||||
soc.close()
|
soc.close()
|
||||||
|
|
||||||
@ -175,7 +175,7 @@ class TestWaitForIt(unittest.TestCase):
|
|||||||
"localhost:8929 --timeout=1 -- echo \"CMD OUTPUT\"",
|
"localhost:8929 --timeout=1 -- echo \"CMD OUTPUT\"",
|
||||||
"CMD OUTPUT",
|
"CMD OUTPUT",
|
||||||
".*timeout occurred after waiting 1 seconds for localhost:8929",
|
".*timeout occurred after waiting 1 seconds for localhost:8929",
|
||||||
0
|
True
|
||||||
)
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
Reference in New Issue
Block a user