Utility to re-run a docker container with slightly different arguments
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 3.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/python3
  2. import subprocess
  3. from nose.tools import with_setup
  4. def _run(cmd):
  5. subprocess.call(cmd,
  6. stdout=subprocess.DEVNULL,
  7. stderr=subprocess.DEVNULL)
  8. def setup():
  9. _run(['docker', 'run', '--name=testA', '-d', 'hello-world'])
  10. _run(['docker', 'run', '--name=testB', '-d', 'hello-world'])
  11. def teardown():
  12. _run(['docker', 'rm', '-f', 'testA'])
  13. _run(['docker', 'rm', '-f', 'testB'])
  14. def setup_each():
  15. _run(['docker', 'rm', '-f', 'test123'])
  16. def teardown_each():
  17. _run(['docker', 'rm', '-f', 'test123'])
  18. @with_setup(setup, teardown)
  19. def test_command_matches():
  20. yield check, ['docker', 'run', '--name=test123', '-d', 'hello-world']
  21. yield check, ['docker', 'run', '--name=test123', '-d', 'hello-world:latest']
  22. yield check, ['docker', 'run', '--name=test123', '-d', 'hello-world', '/hello', 'world...']
  23. yield check, ['docker', 'run', '--env=PATH=/root', '--env=Z=X', '--name=test123', '-d', 'hello-world']
  24. yield check, ['docker', 'run', '--env=FOO=bar baz', '--name=test123', '-d', 'hello-world']
  25. yield check, ['docker', 'run', '--name=test123', '--restart=always', '-d', 'hello-world']
  26. yield check, ['docker', 'run', '--name=test123', '--restart=on-failure:10', '-d', 'hello-world']
  27. yield check, ['docker', 'run', '--name=test123', '--net=host', '-d', 'hello-world']
  28. yield check, ['docker', 'run', '--name=test123', '-d', '-p=127.0.0.1:443:443', '-p=127.0.0.1::1336/udp', 'hello-world']
  29. yield check, ['docker', 'run', '--name=test123', '-d', '-p=443', 'hello-world']
  30. yield check, ['docker', 'run', '--name=test123', '--user=root', '-d', 'hello-world', '/hello', 'foobar']
  31. yield check, ['docker', 'run', '--name=test123', '--net=host', '--user=root:root', '-d', 'hello-world']
  32. yield check, ['docker', 'run', '--name=test123', '--volume=/dev/null:/null', '--volume=/dev/urandom:/mnt/random', '-d', 'hello-world']
  33. yield check, ['docker', 'run', '--label=com.example=123 456', '--name=test123', '-d', 'hello-world']
  34. yield check, ['docker', 'run', '--label=com.example.1', '--label=com.example.2=345', '--name=test123', '-d', 'hello-world']
  35. yield check, ['docker', 'run', '--link=testA:bar', '--link=testB', '--name=test123', '-d', 'hello-world']
  36. @with_setup(setup, teardown)
  37. def test_modifiers():
  38. yield (check,
  39. ['docker', 'run', '--name=test123', '-d', 'hello-world'],
  40. ['--image', 'tutum/hello-world'],
  41. ['docker', 'run', '--name=test123', '-d', 'tutum/hello-world'])
  42. yield (check,
  43. ['docker', 'run', '--name=test123', '-d', 'hello-world'],
  44. ['--image', 'hello-world:latest'],
  45. ['docker', 'run', '--name=test123', '-d', 'hello-world:latest'])
  46. @with_setup(setup_each, teardown_each)
  47. def check(command, args=[], expected=None):
  48. _run(command)
  49. output = subprocess.check_output(['./docker-rerun', '--dry-run', 'test123'] + args)
  50. output = output.decode('utf-8').strip().splitlines()
  51. expected = ' '.join(expected or command)
  52. assert output[3] == expected, 'Expected "%s" but got "%s"' % (expected, output[3])