1
0

test_openstack.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 pytest
  10. # Import Salt Libs
  11. from salt.cloud.clouds import openstack
  12. # Import Salt Testing Libs
  13. from tests.support.mixins import LoaderModuleMockMixin
  14. from tests.support.mock import patch
  15. from tests.support.unit import TestCase
  16. class MockImage(object):
  17. name = "image name"
  18. id = "image id"
  19. class MockNode(object):
  20. name = "node name"
  21. id = "node id"
  22. flavor = MockImage()
  23. status = "node status"
  24. def __init__(self, image):
  25. self.image = image
  26. def __iter__(self):
  27. return iter(())
  28. class MockConn(object):
  29. def __init__(self, image):
  30. self.node = MockNode(image)
  31. def get_image(self, *args, **kwargs):
  32. return self.node.image
  33. def get_flavor(self, *args, **kwargs):
  34. return self.node.flavor
  35. def get_server(self, *args, **kwargs):
  36. return self.node
  37. def list_servers(self, *args, **kwargs):
  38. return [self.node]
  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(
  70. openstack.__opts__,
  71. {"providers": {"my-openstack-cloud": {"openstack": config}}},
  72. ):
  73. result = openstack.get_configured_provider()
  74. self.assertEqual(config, result)
  75. def test_get_configured_provider_cloud(self):
  76. config = {
  77. "region_name": "westeros",
  78. "cloud": "foo",
  79. }
  80. with patch.dict(
  81. openstack.__opts__,
  82. {"providers": {"my-openstack-cloud": {"openstack": config}}},
  83. ):
  84. result = openstack.get_configured_provider()
  85. self.assertEqual(config, result)
  86. def test_get_dependencies(self):
  87. HAS_SHADE = (True, "Please install newer version of shade: >= 1.19.0")
  88. with patch("salt.cloud.clouds.openstack.HAS_SHADE", HAS_SHADE):
  89. result = openstack.get_dependencies()
  90. self.assertEqual(result, True)
  91. def test_get_dependencies_no_shade(self):
  92. HAS_SHADE = (False, "Install pypi module shade >= 1.19.0")
  93. with patch("salt.cloud.clouds.openstack.HAS_SHADE", HAS_SHADE):
  94. result = openstack.get_dependencies()
  95. self.assertEqual(result, False)
  96. def test_list_nodes_full_image_str(self):
  97. node_image = "node image"
  98. conn = MockConn(node_image)
  99. with patch("salt.cloud.clouds.openstack._get_ips", return_value=[]):
  100. ret = openstack.list_nodes_full(conn=conn)
  101. self.assertEqual(ret[conn.node.name]["image"], node_image)
  102. @pytest.mark.slow_test(seconds=1) # Test takes >0.1 and <=1 seconds
  103. def test_list_nodes_full_image_obj(self):
  104. conn = MockConn(MockImage())
  105. with patch("salt.cloud.clouds.openstack._get_ips", return_value=[]):
  106. ret = openstack.list_nodes_full(conn=conn)
  107. self.assertEqual(ret[conn.node.name]["image"], MockImage.name)
  108. def test_show_instance(self):
  109. conn = MockConn(MockImage())
  110. with patch("salt.cloud.clouds.openstack._get_ips", return_value=[]):
  111. ret = openstack.show_instance(conn.node.name, conn=conn, call="action")
  112. self.assertEqual(ret["image"], MockImage.name)