test_vultrpy.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Integration tests for Vultr
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import time
  9. # Import Salt Testing Libs
  10. from tests.support.case import ShellCase
  11. from tests.support.paths import FILES
  12. from tests.support.helpers import expensiveTest, generate_random_name
  13. # Import Salt Libs
  14. from salt.config import cloud_providers_config
  15. from salt.ext import six
  16. # Create the cloud instance name to be used throughout the tests
  17. INSTANCE_NAME = generate_random_name('CLOUD-TEST-')
  18. PROVIDER_NAME = 'vultr'
  19. TIMEOUT = 500
  20. class VultrTest(ShellCase):
  21. '''
  22. Integration tests for the Vultr cloud provider in Salt-Cloud
  23. '''
  24. @expensiveTest
  25. def setUp(self):
  26. '''
  27. Sets up the test requirements
  28. '''
  29. super(VultrTest, self).setUp()
  30. # check if appropriate cloud provider and profile files are present
  31. profile_str = 'vultr-config'
  32. providers = self.run_cloud('--list-providers')
  33. if profile_str + ':' not in providers:
  34. self.skipTest(
  35. 'Configuration file for {0} was not found. Check {0}.conf files '
  36. 'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
  37. .format(PROVIDER_NAME)
  38. )
  39. # check if api_key, ssh_key_file, and ssh_key_names are present
  40. config = cloud_providers_config(
  41. os.path.join(
  42. FILES,
  43. 'conf',
  44. 'cloud.providers.d',
  45. PROVIDER_NAME + '.conf'
  46. )
  47. )
  48. api_key = config[profile_str][PROVIDER_NAME]['api_key']
  49. ssh_file = config[profile_str][PROVIDER_NAME]['ssh_key_file']
  50. ssh_name = config[profile_str][PROVIDER_NAME]['ssh_key_name']
  51. if api_key == '' or ssh_file == '' or ssh_name == '':
  52. self.skipTest(
  53. 'An API key, an ssh key file, and an ssh key name '
  54. 'must be provided to run these tests. Check '
  55. 'tests/integration/files/conf/cloud.providers.d/{0}.conf'
  56. .format(PROVIDER_NAME)
  57. )
  58. def test_list_images(self):
  59. '''
  60. Tests the return of running the --list-images command for Vultr
  61. '''
  62. image_list = self.run_cloud('--list-images {0}'.format(PROVIDER_NAME))
  63. self.assertIn(
  64. 'Debian 8 x64 (jessie)',
  65. [i.strip() for i in image_list]
  66. )
  67. def test_list_locations(self):
  68. '''
  69. Tests the return of running the --list-locations command for Vultr
  70. '''
  71. location_list = self.run_cloud('--list-locations {0}'.format(PROVIDER_NAME))
  72. self.assertIn(
  73. 'New Jersey',
  74. [i.strip() for i in location_list]
  75. )
  76. def test_list_sizes(self):
  77. '''
  78. Tests the return of running the --list-sizes command for Vultr
  79. '''
  80. size_list = self.run_cloud('--list-sizes {0}'.format(PROVIDER_NAME))
  81. self.assertIn(
  82. '32768 MB RAM,110 GB SSD,40.00 TB BW',
  83. [i.strip() for i in size_list]
  84. )
  85. # Commented for now, Vultr driver does not yet support key management
  86. # def test_key_management(self):
  87. # '''
  88. # Test key management
  89. # '''
  90. # pub = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAAAQQDDHr/jh2Jy4yALcK4JyWbVkPRaWmhck3IgCoeOO3z1e2dBowLh64QAM+Qb72pxekALga2oi4GvT+TlWNhzPH4V example'
  91. # finger_print = '3b:16:bf:e4:8b:00:8b:b8:59:8c:a9:d3:f0:19:45:fa'
  92. #
  93. # _key = self.run_cloud('-f create_key {0} name="MyPubKey" public_key="{1}"'.format(PROVIDER_NAME, pub))
  94. #
  95. # # Upload public key
  96. # self.assertIn(
  97. # finger_print,
  98. # [i.strip() for i in _key]
  99. # )
  100. #
  101. # try:
  102. # # List all keys
  103. # list_keypairs = self.run_cloud('-f list_keypairs {0}'.format(PROVIDER_NAME))
  104. #
  105. # self.assertIn(
  106. # finger_print,
  107. # [i.strip() for i in list_keypairs]
  108. # )
  109. #
  110. # # List key
  111. # show_keypair = self.run_cloud('-f show_keypair {0} keyname={1}'.format(PROVIDER_NAME, 'MyPubKey'))
  112. #
  113. # self.assertIn(
  114. # finger_print,
  115. # [i.strip() for i in show_keypair]
  116. # )
  117. # except AssertionError:
  118. # # Delete the public key if the above assertions fail
  119. # self.run_cloud('-f remove_key {0} id={1}'.format(PROVIDER_NAME, finger_print))
  120. # raise
  121. #
  122. # # Delete public key
  123. # self.assertTrue(self.run_cloud('-f remove_key {0} id={1}'.format(PROVIDER_NAME, finger_print)))
  124. def test_instance(self):
  125. '''
  126. Test creating an instance on Vultr
  127. '''
  128. # check if instance with salt installed returned
  129. try:
  130. create_vm = self.run_cloud('-p vultr-test {0}'.format(INSTANCE_NAME), timeout=800)
  131. self.assertIn(
  132. INSTANCE_NAME,
  133. [i.strip() for i in create_vm]
  134. )
  135. self.assertNotIn('Failed to start', six.text_type(create_vm))
  136. except AssertionError:
  137. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  138. raise
  139. # Vultr won't let us delete an instance less than 5 minutes old.
  140. time.sleep(420)
  141. # delete the instance
  142. results = self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  143. try:
  144. self.assertIn(
  145. 'True',
  146. [i.strip() for i in results]
  147. )
  148. except AssertionError:
  149. raise
  150. # Final clean-up of created instance, in case something went wrong.
  151. # This was originally in a tearDown function, but that didn't make sense
  152. # To run this for each test when not all tests create instances.
  153. # Also, Vultr won't let instances be deleted unless they have been alive for 5 minutes.
  154. # If we exceed 6 minutes and the instance is still there, quit
  155. ct = 0
  156. while ct < 12 and INSTANCE_NAME in [i.strip() for i in self.run_cloud('--query')]:
  157. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  158. time.sleep(30)
  159. ct = ct + 1