1
0

test_openstack.py 7.1 KB

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