test_libcloud_storage.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: :email:`Anthony Shaw <anthonyshaw@apache.org>`
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, unicode_literals, print_function
  7. # Import Salt Testing Libs
  8. from tests.support.mixins import LoaderModuleMockMixin
  9. from tests.support.unit import TestCase, skipIf
  10. from tests.support.mock import (
  11. patch,
  12. MagicMock,
  13. )
  14. import salt.modules.libcloud_storage as libcloud_storage
  15. try:
  16. from libcloud.storage.base import Container, BaseDriver, Object
  17. HAS_LIBCLOUD = True
  18. except ImportError:
  19. HAS_LIBCLOUD = False
  20. if HAS_LIBCLOUD:
  21. class MockStorageDriver(BaseDriver):
  22. def __init__(self): # pylint: disable=W0231
  23. self._TEST_CONTAINER = Container(name='test_container', extra={}, driver=self)
  24. self._TEST_OBJECT = Object(name='test_obj',
  25. size=1234,
  26. hash='123sdfsdf',
  27. extra={},
  28. meta_data={'key': 'value'},
  29. container=self._TEST_CONTAINER,
  30. driver=self)
  31. def list_containers(self):
  32. return [self._TEST_CONTAINER]
  33. def get_container(self, container_name):
  34. assert container_name == 'test_container'
  35. return self._TEST_CONTAINER
  36. def list_container_objects(self, container):
  37. assert container.name == 'test_container'
  38. return [self._TEST_OBJECT]
  39. def create_container(self, container_name):
  40. assert container_name == 'new_test_container'
  41. return self._TEST_CONTAINER
  42. def get_container_object(self, container_name, object_name):
  43. assert container_name == 'test_container'
  44. assert object_name == 'test_obj'
  45. return self._TEST_OBJECT
  46. else:
  47. MockStorageDriver = object
  48. def get_mock_driver():
  49. return MockStorageDriver()
  50. @skipIf(not HAS_LIBCLOUD, 'No libcloud installed')
  51. @patch('salt.modules.libcloud_storage._get_driver',
  52. MagicMock(return_value=MockStorageDriver()))
  53. class LibcloudStorageModuleTestCase(TestCase, LoaderModuleMockMixin):
  54. def setup_loader_modules(self):
  55. module_globals = {
  56. '__salt__': {
  57. 'config.option': MagicMock(return_value={
  58. 'test': {
  59. 'driver': 'test',
  60. 'key': '2orgk34kgk34g'
  61. }
  62. })
  63. }
  64. }
  65. if libcloud_storage.HAS_LIBCLOUD is False:
  66. module_globals['sys.modules'] = {'libcloud': MagicMock()}
  67. return {libcloud_storage: module_globals}
  68. def test_module_creation(self):
  69. client = libcloud_storage._get_driver('test')
  70. self.assertFalse(client is None)
  71. def test_init(self):
  72. with patch('salt.utils.compat.pack_dunder', return_value=False) as dunder:
  73. libcloud_storage.__init__(None)
  74. dunder.assert_called_with('salt.modules.libcloud_storage')
  75. def test_list_containers(self):
  76. containers = libcloud_storage.list_containers('test')
  77. self.assertEqual(len(containers), 1)
  78. self.assertEqual(containers[0]['name'], 'test_container')
  79. def test_list_container_objects(self):
  80. objects = libcloud_storage.list_container_objects('test_container', 'test')
  81. self.assertEqual(len(objects), 1)
  82. self.assertEqual(objects[0]['name'], 'test_obj')
  83. self.assertEqual(objects[0]['size'], 1234)
  84. def test_create_container(self):
  85. container = libcloud_storage.create_container('new_test_container', 'test')
  86. self.assertEqual(container['name'], 'test_container')
  87. def test_get_container(self):
  88. container = libcloud_storage.get_container('test_container', 'test')
  89. self.assertEqual(container['name'], 'test_container')
  90. def test_get_container_object(self):
  91. obj = libcloud_storage.get_container_object('test_container', 'test_obj', 'test')
  92. self.assertEqual(obj['name'], 'test_obj')
  93. self.assertEqual(obj['size'], 1234)