1
0

test_openstack.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. # -*- coding: utf-8 -*-
  2. """
  3. Tests for the Openstack Cloud Provider
  4. """
  5. # Import python libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import logging
  8. # Import Salt Libs
  9. from tests.integration.cloud.helpers.cloud_test_base import TIMEOUT, CloudTest
  10. # Import Salt Testing libs
  11. from tests.support.case import ModuleCase
  12. from tests.support.helpers import destructiveTest
  13. from tests.support.mixins import SaltReturnAssertsMixin
  14. from tests.support.unit import skipIf
  15. log = logging.getLogger(__name__)
  16. try:
  17. import keystoneclient # pylint: disable=import-error,unused-import
  18. from libcloud.common.openstack_identity import OpenStackIdentity_3_0_Connection
  19. from libcloud.common.openstack_identity import OpenStackIdentityTokenScope
  20. HAS_KEYSTONE = True
  21. except ImportError:
  22. HAS_KEYSTONE = False
  23. # Import Third-Party Libs
  24. try:
  25. import shade # pylint: disable=unused-import
  26. HAS_SHADE = True
  27. except ImportError:
  28. HAS_SHADE = False
  29. @skipIf(
  30. not HAS_KEYSTONE,
  31. "Please install keystoneclient and a keystone server before running"
  32. "openstack integration tests.",
  33. )
  34. class OpenstackTest(ModuleCase, SaltReturnAssertsMixin):
  35. """
  36. Validate the keystone state
  37. """
  38. endpoint = "http://localhost:35357/v2.0"
  39. token = "administrator"
  40. @destructiveTest
  41. def test_aaa_setup_keystone_endpoint(self):
  42. ret = self.run_state(
  43. "keystone.service_present",
  44. name="keystone",
  45. description="OpenStack Identity",
  46. service_type="identity",
  47. connection_endpoint=self.endpoint,
  48. connection_token=self.token,
  49. )
  50. self.assertTrue(
  51. ret["keystone_|-keystone_|-keystone_|-service_present"]["result"]
  52. )
  53. ret = self.run_state(
  54. "keystone.endpoint_present",
  55. name="keystone",
  56. region="RegionOne",
  57. publicurl="http://localhost:5000/v2.0",
  58. internalurl="http://localhost:5000/v2.0",
  59. adminurl="http://localhost:35357/v2.0",
  60. connection_endpoint=self.endpoint,
  61. connection_token=self.token,
  62. )
  63. self.assertTrue(
  64. ret["keystone_|-keystone_|-keystone_|-endpoint_present"]["result"]
  65. )
  66. ret = self.run_state(
  67. "keystone.tenant_present",
  68. name="admin",
  69. description="Admin Project",
  70. connection_endpoint=self.endpoint,
  71. connection_token=self.token,
  72. )
  73. self.assertTrue(ret["keystone_|-admin_|-admin_|-tenant_present"]["result"])
  74. ret = self.run_state(
  75. "keystone.tenant_present",
  76. name="demo",
  77. description="Demo Project",
  78. connection_endpoint=self.endpoint,
  79. connection_token=self.token,
  80. )
  81. self.assertTrue(ret["keystone_|-demo_|-demo_|-tenant_present"]["result"])
  82. ret = self.run_state(
  83. "keystone.role_present",
  84. name="admin",
  85. connection_endpoint=self.endpoint,
  86. connection_token=self.token,
  87. )
  88. self.assertTrue(ret["keystone_|-admin_|-admin_|-role_present"]["result"])
  89. ret = self.run_state(
  90. "keystone.role_present",
  91. name="user",
  92. connection_endpoint=self.endpoint,
  93. connection_token=self.token,
  94. )
  95. self.assertTrue(ret["keystone_|-user_|-user_|-role_present"]["result"])
  96. ret = self.run_state(
  97. "keystone.user_present",
  98. name="admin",
  99. email="admin@example.com",
  100. password="adminpass",
  101. tenant="admin",
  102. roles={"admin": ["admin"]},
  103. connection_endpoint=self.endpoint,
  104. connection_token=self.token,
  105. )
  106. self.assertTrue(ret["keystone_|-admin_|-admin_|-user_present"]["result"])
  107. ret = self.run_state(
  108. "keystone.user_present",
  109. name="demo",
  110. email="demo@example.com",
  111. password="demopass",
  112. tenant="demo",
  113. roles={"demo": ["user"]},
  114. connection_endpoint=self.endpoint,
  115. connection_token=self.token,
  116. )
  117. self.assertTrue(ret["keystone_|-demo_|-demo_|-user_present"]["result"])
  118. @destructiveTest
  119. def test_zzz_teardown_keystone_endpoint(self):
  120. ret = self.run_state(
  121. "keystone.user_absent",
  122. name="admin",
  123. connection_endpoint=self.endpoint,
  124. connection_token=self.token,
  125. )
  126. self.assertTrue(ret["keystone_|-admin_|-admin_|-user_absent"]["result"])
  127. ret = self.run_state(
  128. "keystone.user_absent",
  129. name="demo",
  130. connection_endpoint=self.endpoint,
  131. connection_token=self.token,
  132. )
  133. self.assertTrue(ret["keystone_|-demo_|-demo_|-user_absent"]["result"])
  134. ret = self.run_state(
  135. "keystone.role_absent",
  136. name="admin",
  137. connection_endpoint=self.endpoint,
  138. connection_token=self.token,
  139. )
  140. self.assertTrue(ret["keystone_|-admin_|-admin_|-role_absent"]["result"])
  141. ret = self.run_state(
  142. "keystone.role_absent",
  143. name="user",
  144. connection_endpoint=self.endpoint,
  145. connection_token=self.token,
  146. )
  147. self.assertTrue(ret["keystone_|-user_|-user_|-role_absent"]["result"])
  148. ret = self.run_state(
  149. "keystone.tenant_absent",
  150. name="admin",
  151. connection_endpoint=self.endpoint,
  152. connection_token=self.token,
  153. )
  154. self.assertTrue(ret["keystone_|-admin_|-admin_|-tenant_absent"]["result"])
  155. ret = self.run_state(
  156. "keystone.tenant_absent",
  157. name="demo",
  158. connection_endpoint=self.endpoint,
  159. connection_token=self.token,
  160. )
  161. self.assertTrue(ret["keystone_|-demo_|-demo_|-tenant_absent"]["result"])
  162. ret = self.run_state(
  163. "keystone.service_absent",
  164. name="keystone",
  165. connection_endpoint=self.endpoint,
  166. connection_token=self.token,
  167. )
  168. self.assertTrue(
  169. ret["keystone_|-keystone_|-keystone_|-service_absent"]["result"]
  170. )
  171. @destructiveTest
  172. def test_libcloud_auth_v3(self):
  173. driver = OpenStackIdentity_3_0_Connection(
  174. auth_url="http://localhost:5000",
  175. user_id="admin",
  176. key="adminpass",
  177. token_scope=OpenStackIdentityTokenScope.PROJECT,
  178. domain_name="Default",
  179. tenant_name="admin",
  180. )
  181. driver.authenticate()
  182. self.assertTrue(driver.auth_token)
  183. @skipIf(not HAS_SHADE, "openstack driver requires `shade`")
  184. class RackspaceTest(CloudTest):
  185. """
  186. Integration tests for the Rackspace cloud provider using the Openstack driver
  187. """
  188. PROVIDER = "openstack"
  189. REQUIRED_PROVIDER_CONFIG_ITEMS = ("auth", "cloud", "region_name")
  190. def test_instance(self):
  191. """
  192. Test creating an instance on rackspace with the openstack driver
  193. """
  194. # check if instance with salt installed returned
  195. ret_val = self.run_cloud(
  196. "-p rackspace-test {0}".format(self.instance_name), timeout=TIMEOUT
  197. )
  198. self.assertInstanceExists(ret_val)
  199. self.assertDestroyInstance()