test_cloud.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Integration tests for functions located in the salt.cloud.__init__.py file.
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import os
  8. import random
  9. import string
  10. # Import Salt Testing libs
  11. from tests.support.case import ShellCase
  12. from tests.support.helpers import expensiveTest
  13. from tests.support.runtests import RUNTIME_VARS
  14. # Import Salt libs
  15. import salt.cloud
  16. from salt.ext.six.moves import range
  17. def __random_name(size=6):
  18. '''
  19. Generates a random cloud instance name
  20. '''
  21. return 'CLOUD-TEST-' + ''.join(
  22. random.choice(string.ascii_uppercase + string.digits)
  23. for x in range(size)
  24. )
  25. # Create the cloud instance name to be used throughout the tests
  26. INSTANCE_NAME = __random_name()
  27. class CloudClientTestCase(ShellCase):
  28. '''
  29. Integration tests for the CloudClient class. Uses DigitalOcean as a salt-cloud provider.
  30. '''
  31. @expensiveTest
  32. def setUp(self):
  33. self.config_file = os.path.join(RUNTIME_VARS.TMP_CONF_CLOUD_PROVIDER_INCLUDES,
  34. 'digitalocean.conf')
  35. self.provider_name = 'digitalocean-config'
  36. self.image_name = '14.04.5 x64'
  37. # Use a --list-images salt-cloud call to see if the DigitalOcean provider is
  38. # configured correctly before running any tests.
  39. images = self.run_cloud('--list-images {0}'.format(self.provider_name))
  40. if self.image_name not in [i.strip() for i in images]:
  41. self.skipTest(
  42. 'Image \'{0}\' was not found in image search. Is the {1} provider '
  43. 'configured correctly for this test?'.format(
  44. self.provider_name,
  45. self.image_name
  46. )
  47. )
  48. def test_cloud_client_create_and_delete(self):
  49. '''
  50. Tests that a VM is created successfully when calling salt.cloud.CloudClient.create(),
  51. which does not require a profile configuration.
  52. Also checks that salt.cloud.CloudClient.destroy() works correctly since this test needs
  53. to remove the VM after creating it.
  54. This test was created as a regression check against Issue #41971.
  55. '''
  56. cloud_client = salt.cloud.CloudClient(self.config_file)
  57. # Create the VM using salt.cloud.CloudClient.create() instead of calling salt-cloud
  58. created = cloud_client.create(
  59. provider=self.provider_name,
  60. names=[INSTANCE_NAME],
  61. image=self.image_name,
  62. location='sfo1',
  63. size='512mb',
  64. vm_size='512mb'
  65. )
  66. # Check that the VM was created correctly
  67. self.assertIn(INSTANCE_NAME, created)
  68. # Clean up after ourselves and delete the VM
  69. deleted = cloud_client.destroy(names=[INSTANCE_NAME])
  70. # Check that the VM was deleted correctly
  71. self.assertIn(INSTANCE_NAME, deleted)