test_dockermod.py 2.6 KB

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