test_dockermod.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. """
  3. Integration tests for the docker_container states
  4. """
  5. from __future__ import absolute_import, print_function, unicode_literals
  6. import random
  7. import string
  8. import sys
  9. import pytest
  10. import salt.utils.path
  11. from salt.ext.six.moves import range
  12. from tests.support.case import ModuleCase
  13. from tests.support.mixins import SaltReturnAssertsMixin
  14. from tests.support.unit import skipIf
  15. def _random_name(prefix=""):
  16. ret = prefix
  17. for _ in range(8):
  18. ret += random.choice(string.ascii_lowercase)
  19. return ret
  20. @pytest.mark.destructive_test
  21. @skipIf(not salt.utils.path.which("dockerd"), "Docker not installed")
  22. class DockerCallTestCase(ModuleCase, SaltReturnAssertsMixin):
  23. """
  24. Test docker_container states
  25. """
  26. def setUp(self):
  27. """
  28. setup docker.call tests
  29. """
  30. # Create temp dir
  31. self.random_name = _random_name(prefix="salt_test_")
  32. self.image_tag = sys.version_info[0]
  33. self.run_state("docker_image.present", tag=self.image_tag, name="python")
  34. self.run_state(
  35. "docker_container.running",
  36. name=self.random_name,
  37. image="python:{0}".format(self.image_tag),
  38. entrypoint="tail -f /dev/null",
  39. )
  40. def tearDown(self):
  41. """
  42. teardown docker.call tests
  43. """
  44. self.run_state("docker_container.absent", name=self.random_name, force=True)
  45. self.run_state(
  46. "docker_image.absent",
  47. images=["python:{0}".format(self.image_tag)],
  48. force=True,
  49. )
  50. delattr(self, "random_name")
  51. delattr(self, "image_tag")
  52. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  53. def test_docker_call(self):
  54. """
  55. check that docker.call works, and works with a container not running as root
  56. """
  57. ret = self.run_function("docker.call", [self.random_name, "test.ping"])
  58. assert ret is True
  59. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  60. def test_docker_sls(self):
  61. """
  62. check that docker.sls works, and works with a container not running as root
  63. """
  64. ret = self.run_function("docker.apply", [self.random_name, "core"])
  65. self.assertSaltTrueReturn(ret)
  66. @pytest.mark.slow_test(seconds=60) # Test takes >30 and <=60 seconds
  67. def test_docker_highstate(self):
  68. """
  69. check that docker.highstate works, and works with a container not running as root
  70. """
  71. ret = self.run_function("docker.apply", [self.random_name])
  72. self.assertSaltTrueReturn(ret)