test_openstack.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: `Tyler Johnson <tjohnson@saltstack.com>`
  4. tests.unit.cloud.clouds.openstack_test
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. '''
  7. # Import Python libs
  8. from __future__ import absolute_import, print_function, unicode_literals
  9. # Import Salt Libs
  10. from salt.cloud.clouds import openstack
  11. # Import Salt Testing Libs
  12. from tests.support.mixins import LoaderModuleMockMixin
  13. from tests.support.unit import TestCase, skipIf
  14. from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch
  15. class MockImage(object):
  16. name = 'image name'
  17. id = 'image id'
  18. class MockNode(object):
  19. name = 'node name'
  20. id = 'node id'
  21. flavor = MockImage()
  22. status = 'node status'
  23. def __init__(self, image):
  24. self.image = image
  25. def __iter__(self):
  26. return iter(())
  27. class MockConn(object):
  28. def __init__(self, image):
  29. self.node = MockNode(image)
  30. def get_image(self, *args, **kwargs):
  31. return self.node.image
  32. def get_flavor(self, *args, **kwargs):
  33. return self.node.flavor
  34. def get_server(self, *args, **kwargs):
  35. return self.node
  36. def list_servers(self, *args, **kwargs):
  37. return [self.node]
  38. @skipIf(NO_MOCK, NO_MOCK_REASON)
  39. class OpenstackTestCase(TestCase, LoaderModuleMockMixin):
  40. '''
  41. Unit TestCase for salt.cloud.clouds.openstack module.
  42. '''
  43. def setup_loader_modules(self):
  44. return {
  45. openstack: {
  46. '__active_provider_name__': '',
  47. '__opts__': {
  48. 'providers': {
  49. 'my-openstack-cloud': {
  50. 'openstack': {
  51. 'auth': 'daenerys',
  52. 'region_name': 'westeros',
  53. 'cloud': 'openstack',
  54. }
  55. }
  56. }
  57. }
  58. }
  59. }
  60. def test_get_configured_provider_bad(self):
  61. with patch.dict(openstack.__opts__, {'providers': {}}):
  62. result = openstack.get_configured_provider()
  63. self.assertEqual(result, False)
  64. def test_get_configured_provider_auth(self):
  65. config = {
  66. 'region_name': 'westeros',
  67. 'auth': 'daenerys',
  68. }
  69. with patch.dict(openstack.__opts__, {'providers': {'my-openstack-cloud': {'openstack': config}}}):
  70. result = openstack.get_configured_provider()
  71. self.assertEqual(config, result)
  72. def test_get_configured_provider_cloud(self):
  73. config = {
  74. 'region_name': 'westeros',
  75. 'cloud': 'foo',
  76. }
  77. with patch.dict(openstack.__opts__, {'providers': {'my-openstack-cloud': {'openstack': config}}}):
  78. result = openstack.get_configured_provider()
  79. self.assertEqual(config, result)
  80. def test_get_dependencies(self):
  81. HAS_SHADE = (True, 'Please install newer version of shade: >= 1.19.0')
  82. with patch('salt.cloud.clouds.openstack.HAS_SHADE', HAS_SHADE):
  83. result = openstack.get_dependencies()
  84. self.assertEqual(result, True)
  85. def test_get_dependencies_no_shade(self):
  86. HAS_SHADE = (False, 'Install pypi module shade >= 1.19.0')
  87. with patch('salt.cloud.clouds.openstack.HAS_SHADE', HAS_SHADE):
  88. result = openstack.get_dependencies()
  89. self.assertEqual(result, False)
  90. def test_list_nodes_full_image_str(self):
  91. node_image = 'node image'
  92. conn = MockConn(node_image)
  93. with patch('salt.cloud.clouds.openstack._get_ips', return_value=[]):
  94. ret = openstack.list_nodes_full(conn=conn)
  95. self.assertEqual(ret[conn.node.name]['image'], node_image)
  96. def test_list_nodes_full_image_obj(self):
  97. conn = MockConn(MockImage())
  98. with patch('salt.cloud.clouds.openstack._get_ips', return_value=[]):
  99. ret = openstack.list_nodes_full(conn=conn)
  100. self.assertEqual(ret[conn.node.name]['image'], MockImage.name)
  101. def test_show_instance(self):
  102. conn = MockConn(MockImage())
  103. with patch('salt.cloud.clouds.openstack._get_ips', return_value=[]):
  104. ret = openstack.show_instance(conn.node.name, conn=conn, call='action')
  105. self.assertEqual(ret['image'], MockImage.name)