1
0

test_openstack.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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
  14. from tests.support.mock import 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. class OpenstackTestCase(TestCase, LoaderModuleMockMixin):
  39. '''
  40. Unit TestCase for salt.cloud.clouds.openstack module.
  41. '''
  42. def setup_loader_modules(self):
  43. return {
  44. openstack: {
  45. '__active_provider_name__': '',
  46. '__opts__': {
  47. 'providers': {
  48. 'my-openstack-cloud': {
  49. 'openstack': {
  50. 'auth': 'daenerys',
  51. 'region_name': 'westeros',
  52. 'cloud': 'openstack',
  53. }
  54. }
  55. }
  56. }
  57. }
  58. }
  59. def test_get_configured_provider_bad(self):
  60. with patch.dict(openstack.__opts__, {'providers': {}}):
  61. result = openstack.get_configured_provider()
  62. self.assertEqual(result, False)
  63. def test_get_configured_provider_auth(self):
  64. config = {
  65. 'region_name': 'westeros',
  66. 'auth': 'daenerys',
  67. }
  68. with patch.dict(openstack.__opts__, {'providers': {'my-openstack-cloud': {'openstack': config}}}):
  69. result = openstack.get_configured_provider()
  70. self.assertEqual(config, result)
  71. def test_get_configured_provider_cloud(self):
  72. config = {
  73. 'region_name': 'westeros',
  74. 'cloud': 'foo',
  75. }
  76. with patch.dict(openstack.__opts__, {'providers': {'my-openstack-cloud': {'openstack': config}}}):
  77. result = openstack.get_configured_provider()
  78. self.assertEqual(config, result)
  79. def test_get_dependencies(self):
  80. HAS_SHADE = (True, 'Please install newer version of shade: >= 1.19.0')
  81. with patch('salt.cloud.clouds.openstack.HAS_SHADE', HAS_SHADE):
  82. result = openstack.get_dependencies()
  83. self.assertEqual(result, True)
  84. def test_get_dependencies_no_shade(self):
  85. HAS_SHADE = (False, 'Install pypi module shade >= 1.19.0')
  86. with patch('salt.cloud.clouds.openstack.HAS_SHADE', HAS_SHADE):
  87. result = openstack.get_dependencies()
  88. self.assertEqual(result, False)
  89. def test_list_nodes_full_image_str(self):
  90. node_image = 'node image'
  91. conn = MockConn(node_image)
  92. with patch('salt.cloud.clouds.openstack._get_ips', return_value=[]):
  93. ret = openstack.list_nodes_full(conn=conn)
  94. self.assertEqual(ret[conn.node.name]['image'], node_image)
  95. def test_list_nodes_full_image_obj(self):
  96. conn = MockConn(MockImage())
  97. with patch('salt.cloud.clouds.openstack._get_ips', return_value=[]):
  98. ret = openstack.list_nodes_full(conn=conn)
  99. self.assertEqual(ret[conn.node.name]['image'], MockImage.name)
  100. def test_show_instance(self):
  101. conn = MockConn(MockImage())
  102. with patch('salt.cloud.clouds.openstack._get_ips', return_value=[]):
  103. ret = openstack.show_instance(conn.node.name, conn=conn, call='action')
  104. self.assertEqual(ret['image'], MockImage.name)