test_openstack.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 Testing libs
  9. from tests.support.case import ModuleCase
  10. from tests.support.unit import skipIf
  11. from tests.support.helpers import destructiveTest
  12. from tests.support.mixins import SaltReturnAssertsMixin
  13. # Import Salt Libs
  14. from tests.integration.cloud.helpers.cloud_test_base import TIMEOUT, CloudTest
  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('keystone.service_present',
  43. name='keystone',
  44. description='OpenStack Identity',
  45. service_type='identity',
  46. connection_endpoint=self.endpoint,
  47. connection_token=self.token)
  48. self.assertTrue(ret['keystone_|-keystone_|-keystone_|-service_present']['result'])
  49. ret = self.run_state('keystone.endpoint_present',
  50. name='keystone',
  51. region='RegionOne',
  52. publicurl='http://localhost:5000/v2.0',
  53. internalurl='http://localhost:5000/v2.0',
  54. adminurl='http://localhost:35357/v2.0',
  55. connection_endpoint=self.endpoint,
  56. connection_token=self.token)
  57. self.assertTrue(ret['keystone_|-keystone_|-keystone_|-endpoint_present']['result'])
  58. ret = self.run_state('keystone.tenant_present',
  59. name='admin',
  60. description='Admin Project',
  61. connection_endpoint=self.endpoint,
  62. connection_token=self.token)
  63. self.assertTrue(ret['keystone_|-admin_|-admin_|-tenant_present']['result'])
  64. ret = self.run_state('keystone.tenant_present',
  65. name='demo',
  66. description='Demo Project',
  67. connection_endpoint=self.endpoint,
  68. connection_token=self.token)
  69. self.assertTrue(ret['keystone_|-demo_|-demo_|-tenant_present']['result'])
  70. ret = self.run_state('keystone.role_present',
  71. name='admin',
  72. connection_endpoint=self.endpoint,
  73. connection_token=self.token)
  74. self.assertTrue(ret['keystone_|-admin_|-admin_|-role_present']['result'])
  75. ret = self.run_state('keystone.role_present',
  76. name='user',
  77. connection_endpoint=self.endpoint,
  78. connection_token=self.token)
  79. self.assertTrue(ret['keystone_|-user_|-user_|-role_present']['result'])
  80. ret = self.run_state('keystone.user_present',
  81. name='admin',
  82. email='admin@example.com',
  83. password='adminpass',
  84. tenant='admin',
  85. roles={'admin': ['admin']},
  86. connection_endpoint=self.endpoint,
  87. connection_token=self.token)
  88. self.assertTrue(ret['keystone_|-admin_|-admin_|-user_present']['result'])
  89. ret = self.run_state('keystone.user_present',
  90. name='demo',
  91. email='demo@example.com',
  92. password='demopass',
  93. tenant='demo',
  94. roles={'demo': ['user']},
  95. connection_endpoint=self.endpoint,
  96. connection_token=self.token)
  97. self.assertTrue(ret['keystone_|-demo_|-demo_|-user_present']['result'])
  98. @destructiveTest
  99. def test_zzz_teardown_keystone_endpoint(self):
  100. ret = self.run_state('keystone.user_absent',
  101. name='admin',
  102. connection_endpoint=self.endpoint,
  103. connection_token=self.token)
  104. self.assertTrue(ret['keystone_|-admin_|-admin_|-user_absent']['result'])
  105. ret = self.run_state('keystone.user_absent',
  106. name='demo',
  107. connection_endpoint=self.endpoint,
  108. connection_token=self.token)
  109. self.assertTrue(ret['keystone_|-demo_|-demo_|-user_absent']['result'])
  110. ret = self.run_state('keystone.role_absent',
  111. name='admin',
  112. connection_endpoint=self.endpoint,
  113. connection_token=self.token)
  114. self.assertTrue(ret['keystone_|-admin_|-admin_|-role_absent']['result'])
  115. ret = self.run_state('keystone.role_absent',
  116. name='user',
  117. connection_endpoint=self.endpoint,
  118. connection_token=self.token)
  119. self.assertTrue(ret['keystone_|-user_|-user_|-role_absent']['result'])
  120. ret = self.run_state('keystone.tenant_absent',
  121. name='admin',
  122. connection_endpoint=self.endpoint,
  123. connection_token=self.token)
  124. self.assertTrue(ret['keystone_|-admin_|-admin_|-tenant_absent']['result'])
  125. ret = self.run_state('keystone.tenant_absent',
  126. name='demo',
  127. connection_endpoint=self.endpoint,
  128. connection_token=self.token)
  129. self.assertTrue(ret['keystone_|-demo_|-demo_|-tenant_absent']['result'])
  130. ret = self.run_state('keystone.service_absent',
  131. name='keystone',
  132. connection_endpoint=self.endpoint,
  133. connection_token=self.token)
  134. self.assertTrue(ret['keystone_|-keystone_|-keystone_|-service_absent']['result'])
  135. @destructiveTest
  136. def test_libcloud_auth_v3(self):
  137. driver = OpenStackIdentity_3_0_Connection(auth_url='http://localhost:5000',
  138. user_id='admin',
  139. key='adminpass',
  140. token_scope=OpenStackIdentityTokenScope.PROJECT,
  141. domain_name='Default',
  142. tenant_name='admin')
  143. driver.authenticate()
  144. self.assertTrue(driver.auth_token)
  145. @skipIf(not HAS_SHADE, 'openstack driver requires `shade`')
  146. class RackspaceTest(CloudTest):
  147. '''
  148. Integration tests for the Rackspace cloud provider using the Openstack driver
  149. '''
  150. PROVIDER = 'openstack'
  151. REQUIRED_PROVIDER_CONFIG_ITEMS = ('auth', 'cloud', 'region_name')
  152. def test_instance(self):
  153. '''
  154. Test creating an instance on rackspace with the openstack driver
  155. '''
  156. # check if instance with salt installed returned
  157. ret_val = self.run_cloud('-p rackspace-test {0}'.format(self.instance_name), timeout=TIMEOUT)
  158. self.assertInstanceExists(ret_val)
  159. self.assertDestroyInstance()