test_openstack.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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 os
  9. # Import Salt Testing libs
  10. from tests.support.case import ModuleCase, ShellCase
  11. from tests.support.paths import FILES
  12. from tests.support.unit import skipIf
  13. from tests.support.helpers import destructiveTest, expensiveTest, generate_random_name
  14. from tests.support.mixins import SaltReturnAssertsMixin
  15. # Import Salt Libs
  16. from salt.config import cloud_providers_config
  17. log = logging.getLogger(__name__)
  18. try:
  19. import keystoneclient # pylint: disable=import-error,unused-import
  20. from libcloud.common.openstack_identity import OpenStackIdentity_3_0_Connection
  21. from libcloud.common.openstack_identity import OpenStackIdentityTokenScope
  22. HAS_KEYSTONE = True
  23. except ImportError:
  24. HAS_KEYSTONE = False
  25. # Import Third-Party Libs
  26. try:
  27. import shade # pylint: disable=unused-import
  28. HAS_SHADE = True
  29. except ImportError:
  30. HAS_SHADE = False
  31. # Create the cloud instance name to be used throughout the tests
  32. INSTANCE_NAME = generate_random_name('CLOUD-TEST-')
  33. PROVIDER_NAME = 'openstack'
  34. DRIVER_NAME = 'openstack'
  35. @skipIf(
  36. not HAS_KEYSTONE,
  37. 'Please install keystoneclient and a keystone server before running'
  38. 'openstack integration tests.'
  39. )
  40. class OpenstackTest(ModuleCase, SaltReturnAssertsMixin):
  41. '''
  42. Validate the keystone state
  43. '''
  44. endpoint = 'http://localhost:35357/v2.0'
  45. token = 'administrator'
  46. @destructiveTest
  47. def test_aaa_setup_keystone_endpoint(self):
  48. ret = self.run_state('keystone.service_present',
  49. name='keystone',
  50. description='OpenStack Identity',
  51. service_type='identity',
  52. connection_endpoint=self.endpoint,
  53. connection_token=self.token)
  54. self.assertTrue(ret['keystone_|-keystone_|-keystone_|-service_present']['result'])
  55. ret = self.run_state('keystone.endpoint_present',
  56. name='keystone',
  57. region='RegionOne',
  58. publicurl='http://localhost:5000/v2.0',
  59. internalurl='http://localhost:5000/v2.0',
  60. adminurl='http://localhost:35357/v2.0',
  61. connection_endpoint=self.endpoint,
  62. connection_token=self.token)
  63. self.assertTrue(ret['keystone_|-keystone_|-keystone_|-endpoint_present']['result'])
  64. ret = self.run_state('keystone.tenant_present',
  65. name='admin',
  66. description='Admin Project',
  67. connection_endpoint=self.endpoint,
  68. connection_token=self.token)
  69. self.assertTrue(ret['keystone_|-admin_|-admin_|-tenant_present']['result'])
  70. ret = self.run_state('keystone.tenant_present',
  71. name='demo',
  72. description='Demo Project',
  73. connection_endpoint=self.endpoint,
  74. connection_token=self.token)
  75. self.assertTrue(ret['keystone_|-demo_|-demo_|-tenant_present']['result'])
  76. ret = self.run_state('keystone.role_present',
  77. name='admin',
  78. connection_endpoint=self.endpoint,
  79. connection_token=self.token)
  80. self.assertTrue(ret['keystone_|-admin_|-admin_|-role_present']['result'])
  81. ret = self.run_state('keystone.role_present',
  82. name='user',
  83. connection_endpoint=self.endpoint,
  84. connection_token=self.token)
  85. self.assertTrue(ret['keystone_|-user_|-user_|-role_present']['result'])
  86. ret = self.run_state('keystone.user_present',
  87. name='admin',
  88. email='admin@example.com',
  89. password='adminpass',
  90. tenant='admin',
  91. roles={'admin': ['admin']},
  92. connection_endpoint=self.endpoint,
  93. connection_token=self.token)
  94. self.assertTrue(ret['keystone_|-admin_|-admin_|-user_present']['result'])
  95. ret = self.run_state('keystone.user_present',
  96. name='demo',
  97. email='demo@example.com',
  98. password='demopass',
  99. tenant='demo',
  100. roles={'demo': ['user']},
  101. connection_endpoint=self.endpoint,
  102. connection_token=self.token)
  103. self.assertTrue(ret['keystone_|-demo_|-demo_|-user_present']['result'])
  104. @destructiveTest
  105. def test_zzz_teardown_keystone_endpoint(self):
  106. ret = self.run_state('keystone.user_absent',
  107. name='admin',
  108. connection_endpoint=self.endpoint,
  109. connection_token=self.token)
  110. self.assertTrue(ret['keystone_|-admin_|-admin_|-user_absent']['result'])
  111. ret = self.run_state('keystone.user_absent',
  112. name='demo',
  113. connection_endpoint=self.endpoint,
  114. connection_token=self.token)
  115. self.assertTrue(ret['keystone_|-demo_|-demo_|-user_absent']['result'])
  116. ret = self.run_state('keystone.role_absent',
  117. name='admin',
  118. connection_endpoint=self.endpoint,
  119. connection_token=self.token)
  120. self.assertTrue(ret['keystone_|-admin_|-admin_|-role_absent']['result'])
  121. ret = self.run_state('keystone.role_absent',
  122. name='user',
  123. connection_endpoint=self.endpoint,
  124. connection_token=self.token)
  125. self.assertTrue(ret['keystone_|-user_|-user_|-role_absent']['result'])
  126. ret = self.run_state('keystone.tenant_absent',
  127. name='admin',
  128. connection_endpoint=self.endpoint,
  129. connection_token=self.token)
  130. self.assertTrue(ret['keystone_|-admin_|-admin_|-tenant_absent']['result'])
  131. ret = self.run_state('keystone.tenant_absent',
  132. name='demo',
  133. connection_endpoint=self.endpoint,
  134. connection_token=self.token)
  135. self.assertTrue(ret['keystone_|-demo_|-demo_|-tenant_absent']['result'])
  136. ret = self.run_state('keystone.service_absent',
  137. name='keystone',
  138. connection_endpoint=self.endpoint,
  139. connection_token=self.token)
  140. self.assertTrue(ret['keystone_|-keystone_|-keystone_|-service_absent']['result'])
  141. @destructiveTest
  142. def test_libcloud_auth_v3(self):
  143. driver = OpenStackIdentity_3_0_Connection(auth_url='http://localhost:5000',
  144. user_id='admin',
  145. key='adminpass',
  146. token_scope=OpenStackIdentityTokenScope.PROJECT,
  147. domain_name='Default',
  148. tenant_name='admin')
  149. driver.authenticate()
  150. self.assertTrue(driver.auth_token)
  151. @skipIf(not HAS_SHADE, 'openstack driver requires `shade`')
  152. class RackspaceTest(ShellCase):
  153. '''
  154. Integration tests for the Rackspace cloud provider using the Openstack driver
  155. '''
  156. @expensiveTest
  157. def setUp(self):
  158. '''
  159. Sets up the test requirements
  160. '''
  161. super(RackspaceTest, self).setUp()
  162. # check if appropriate cloud provider and profile files are present
  163. profile_str = 'openstack-config'
  164. providers = self.run_cloud('--list-providers')
  165. if profile_str + ':' not in providers:
  166. self.skipTest(
  167. 'Configuration file for {0} was not found. Check {0}.conf files '
  168. 'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
  169. .format(PROVIDER_NAME)
  170. )
  171. # check if personal access token, ssh_key_file, and ssh_key_names are present
  172. config = cloud_providers_config(
  173. os.path.join(
  174. FILES,
  175. 'conf',
  176. 'cloud.providers.d',
  177. PROVIDER_NAME + '.conf'
  178. )
  179. )
  180. region_name = config[profile_str][DRIVER_NAME].get('region_name')
  181. auth = config[profile_str][DRIVER_NAME].get('auth')
  182. cloud = config[profile_str][DRIVER_NAME].get('cloud')
  183. if not region_name or not (auth or cloud):
  184. self.skipTest(
  185. 'A region_name and (auth or cloud) must be provided to run these '
  186. 'tests. Check tests/integration/files/conf/cloud.providers.d/{0}.conf'
  187. .format(PROVIDER_NAME)
  188. )
  189. def test_instance(self):
  190. '''
  191. Test creating an instance on rackspace with the openstack driver
  192. '''
  193. # check if instance with salt installed returned
  194. try:
  195. self.assertIn(
  196. INSTANCE_NAME,
  197. [i.strip() for i in self.run_cloud('-p rackspace-test {0}'.format(INSTANCE_NAME), timeout=500)]
  198. )
  199. except AssertionError:
  200. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)
  201. raise
  202. # delete the instance
  203. try:
  204. self.assertIn(
  205. INSTANCE_NAME + ':',
  206. [i.strip() for i in self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)]
  207. )
  208. except AssertionError:
  209. raise
  210. def tearDown(self):
  211. '''
  212. Clean up after tests
  213. '''
  214. query = self.run_cloud('--query')
  215. ret = ' {0}:'.format(INSTANCE_NAME)
  216. # if test instance is still present, delete it
  217. if ret in query:
  218. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=500)