test_docker_image.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Unit tests for the docker state
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase
  10. from tests.support.mock import (
  11. MagicMock,
  12. patch
  13. )
  14. # Import Salt Libs
  15. import salt.modules.dockermod as docker_mod
  16. import salt.states.docker_image as docker_state
  17. class DockerImageTestCase(TestCase, LoaderModuleMockMixin):
  18. '''
  19. Test docker_image states
  20. '''
  21. def setup_loader_modules(self):
  22. return {
  23. docker_mod: {
  24. '__context__': {'docker.docker_version': ''}
  25. },
  26. docker_state: {
  27. '__opts__': {'test': False}
  28. }
  29. }
  30. def test_present_already_local(self):
  31. '''
  32. According following sls,
  33. .. code-block:: yaml
  34. image:latest:
  35. docker_image.present:
  36. - force: true
  37. if ``image:latest`` is already downloaded locally the state
  38. should not report changes.
  39. '''
  40. docker_inspect_image = MagicMock(return_value={'Id': 'abcdefghijkl'})
  41. docker_pull = MagicMock(
  42. return_value={'Layers':
  43. {'Already_Pulled': ['abcdefghijkl'],
  44. 'Pulled': []},
  45. 'Status': 'Image is up to date for image:latest',
  46. 'Time_Elapsed': 1.1})
  47. docker_list_tags = MagicMock(return_value=['image:latest'])
  48. docker_resolve_tag = MagicMock(return_value='image:latest')
  49. __salt__ = {'docker.list_tags': docker_list_tags,
  50. 'docker.pull': docker_pull,
  51. 'docker.inspect_image': docker_inspect_image,
  52. 'docker.resolve_tag': docker_resolve_tag}
  53. with patch.dict(docker_state.__dict__,
  54. {'__salt__': __salt__}):
  55. ret = docker_state.present('image:latest', force=True)
  56. self.assertEqual(ret,
  57. {'changes': {},
  58. 'result': True,
  59. 'comment': "Image 'image:latest' was pulled, "
  60. "but there were no changes",
  61. 'name': 'image:latest',
  62. })
  63. def test_present_and_force(self):
  64. '''
  65. According following sls,
  66. .. code-block:: yaml
  67. image:latest:
  68. docker_image.present:
  69. - force: true
  70. if ``image:latest`` is not downloaded and force is true
  71. should pull a new image successfully.
  72. '''
  73. docker_inspect_image = MagicMock(return_value={'Id': '1234567890ab'})
  74. docker_pull = MagicMock(
  75. return_value={'Layers':
  76. {'Pulled': ['abcdefghijkl']},
  77. 'Status': "Image 'image:latest' was pulled",
  78. 'Time_Elapsed': 1.1})
  79. docker_list_tags = MagicMock(side_effect=[[], ['image:latest']])
  80. docker_resolve_tag = MagicMock(return_value='image:latest')
  81. __salt__ = {'docker.list_tags': docker_list_tags,
  82. 'docker.pull': docker_pull,
  83. 'docker.inspect_image': docker_inspect_image,
  84. 'docker.resolve_tag': docker_resolve_tag}
  85. with patch.dict(docker_state.__dict__,
  86. {'__salt__': __salt__}):
  87. ret = docker_state.present('image:latest', force=True)
  88. self.assertEqual(ret,
  89. {'changes': {
  90. 'Layers': {'Pulled': ['abcdefghijkl']},
  91. 'Status': "Image 'image:latest' was pulled",
  92. 'Time_Elapsed': 1.1},
  93. 'result': True,
  94. 'comment': "Image 'image:latest' was pulled",
  95. 'name': 'image:latest',
  96. })