test_vmware.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # -*- coding: utf-8 -*-
  2. '''
  3. :codeauthor: Megan Wilhite <mwilhite@saltstack.com>
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. # Import Salt Libs
  9. from salt.config import cloud_providers_config, cloud_config
  10. from salt.ext import six
  11. # Import Salt Testing LIbs
  12. from tests.support.case import ShellCase
  13. from tests.support.paths import FILES
  14. from tests.support.helpers import expensiveTest, generate_random_name
  15. # Create the cloud instance name to be used throughout the tests
  16. INSTANCE_NAME = generate_random_name('CLOUD-TEST-')
  17. PROVIDER_NAME = 'vmware'
  18. TIMEOUT = 500
  19. class VMWareTest(ShellCase):
  20. '''
  21. Integration tests for the vmware cloud provider in Salt-Cloud
  22. '''
  23. @expensiveTest
  24. def setUp(self):
  25. '''
  26. Sets up the test requirements
  27. '''
  28. # check if appropriate cloud provider and profile files are present
  29. profile_str = 'vmware-config'
  30. providers = self.run_cloud('--list-providers')
  31. if profile_str + ':' not in providers:
  32. self.skipTest(
  33. 'Configuration file for {0} was not found. Check {0}.conf files '
  34. 'in tests/integration/files/conf/cloud.*.d/ to run these tests.'
  35. .format(PROVIDER_NAME)
  36. )
  37. # check if user, password, url and provider are present
  38. config = cloud_providers_config(
  39. os.path.join(
  40. FILES,
  41. 'conf',
  42. 'cloud.providers.d',
  43. PROVIDER_NAME + '.conf'
  44. )
  45. )
  46. user = config[profile_str][PROVIDER_NAME]['user']
  47. password = config[profile_str][PROVIDER_NAME]['password']
  48. url = config[profile_str][PROVIDER_NAME]['url']
  49. conf_items = [user, password, url]
  50. missing_conf_item = []
  51. for item in conf_items:
  52. if item == '':
  53. missing_conf_item.append(item)
  54. if missing_conf_item:
  55. self.skipTest(
  56. 'A user, password, and url must be provided to run these tests.'
  57. 'One or more of these elements is missing. Check'
  58. 'tests/integration/files/conf/cloud.providers.d/{0}.conf'
  59. .format(PROVIDER_NAME)
  60. )
  61. def test_instance(self):
  62. '''
  63. Tests creating and deleting an instance on vmware and installing salt
  64. '''
  65. # create the instance
  66. profile = os.path.join(
  67. FILES,
  68. 'conf',
  69. 'cloud.profiles.d',
  70. PROVIDER_NAME + '.conf'
  71. )
  72. profile_config = cloud_config(profile)
  73. disk_datastore = profile_config['vmware-test']['devices']['disk']['Hard disk 2']['datastore']
  74. instance = self.run_cloud('-p vmware-test {0}'.format(INSTANCE_NAME), timeout=TIMEOUT)
  75. ret_str = '{0}:'.format(INSTANCE_NAME)
  76. disk_datastore_str = ' [{0}] {1}/Hard disk 2-flat.vmdk'.format(disk_datastore, INSTANCE_NAME)
  77. # check if instance returned with salt installed
  78. try:
  79. self.assertIn(ret_str, instance)
  80. self.assertIn(disk_datastore_str, instance,
  81. msg='Hard Disk 2 did not use the Datastore {0} '.format(disk_datastore))
  82. except AssertionError:
  83. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  84. raise
  85. # delete the instance
  86. delete = self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  87. ret_str = '{0}:\', \' True'.format(INSTANCE_NAME)
  88. # check if deletion was performed appropriately
  89. self.assertIn(ret_str, six.text_type(delete))
  90. def test_snapshot(self):
  91. '''
  92. Tests creating snapshot and creating vm with --no-deploy
  93. '''
  94. # create the instance
  95. instance = self.run_cloud('-p vmware-test {0} --no-deploy'.format(INSTANCE_NAME),
  96. timeout=TIMEOUT)
  97. ret_str = '{0}:'.format(INSTANCE_NAME)
  98. # check if instance returned with salt installed
  99. try:
  100. self.assertIn(ret_str, instance)
  101. except AssertionError:
  102. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  103. raise
  104. create_snapshot = self.run_cloud('-a create_snapshot {0} \
  105. snapshot_name=\'Test Cloud\' \
  106. memdump=True -y'.format(INSTANCE_NAME),
  107. timeout=TIMEOUT)
  108. s_ret_str = 'Snapshot created successfully'
  109. self.assertIn(s_ret_str, six.text_type(create_snapshot))
  110. # delete the instance
  111. delete = self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)
  112. ret_str = '{0}:\', \' True'.format(INSTANCE_NAME)
  113. self.assertIn(ret_str, six.text_type(delete))
  114. def tearDown(self):
  115. '''
  116. Clean up after tests
  117. '''
  118. query = self.run_cloud('--query')
  119. ret_str = ' {0}:'.format(INSTANCE_NAME)
  120. # if test instance is still present, delete it
  121. if ret_str in query:
  122. self.run_cloud('-d {0} --assume-yes'.format(INSTANCE_NAME), timeout=TIMEOUT)