test_digitalocean.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # -*- coding: utf-8 -*-
  2. '''
  3. Integration tests for DigitalOcean APIv2
  4. '''
  5. # Import Python Libs
  6. from __future__ import absolute_import, print_function, unicode_literals
  7. import base64
  8. import hashlib
  9. from Crypto.PublicKey import RSA
  10. # Import Salt Testing Libs
  11. from tests.integration.cloud.helpers.cloud_test_base import CloudTest, TIMEOUT
  12. # Import Salt Libs
  13. from salt.ext.six.moves import range
  14. import salt.utils.stringutils
  15. class DigitalOceanTest(CloudTest):
  16. '''
  17. Integration tests for the DigitalOcean cloud provider in Salt-Cloud
  18. '''
  19. PROVIDER = 'digitalocean'
  20. REQUIRED_PROVIDER_CONFIG_ITEMS = ('personal_access_token', 'ssh_key_file', 'ssh_key_name')
  21. def test_list_images(self):
  22. '''
  23. Tests the return of running the --list-images command for digitalocean
  24. '''
  25. image_list = self.run_cloud('--list-images {0}'.format(self.PROVIDER))
  26. self.assertIn(
  27. '14.04.5 x64',
  28. [i.strip() for i in image_list]
  29. )
  30. def test_list_locations(self):
  31. '''
  32. Tests the return of running the --list-locations command for digitalocean
  33. '''
  34. _list_locations = self.run_cloud('--list-locations {0}'.format(self.PROVIDER))
  35. self.assertIn(
  36. 'San Francisco 2',
  37. [i.strip() for i in _list_locations]
  38. )
  39. def test_list_sizes(self):
  40. '''
  41. Tests the return of running the --list-sizes command for digitalocean
  42. '''
  43. _list_sizes = self.run_cloud('--list-sizes {0}'.format(self.PROVIDER))
  44. self.assertIn(
  45. '16gb',
  46. [i.strip() for i in _list_sizes]
  47. )
  48. def test_key_management(self):
  49. '''
  50. Test key management
  51. '''
  52. do_key_name = self.instance_name + '-key'
  53. # generate key and fingerprint
  54. ssh_key = RSA.generate(4096)
  55. pub = salt.utils.stringutils.to_str(ssh_key.publickey().exportKey("OpenSSH"))
  56. key_hex = hashlib.md5(base64.b64decode(pub.strip().split()[1].encode())).hexdigest()
  57. finger_print = ':'.join([key_hex[x:x+2] for x in range(0, len(key_hex), 2)])
  58. try:
  59. _key = self.run_cloud('-f create_key {0} name="{1}" public_key="{2}"'.format(self.PROVIDER,
  60. do_key_name, pub))
  61. # Upload public key
  62. self.assertIn(
  63. finger_print,
  64. [i.strip() for i in _key]
  65. )
  66. # List all keys
  67. list_keypairs = self.run_cloud('-f list_keypairs {0}'.format(self.PROVIDER))
  68. self.assertIn(
  69. finger_print,
  70. [i.strip() for i in list_keypairs]
  71. )
  72. # List key
  73. show_keypair = self.run_cloud('-f show_keypair {0} keyname={1}'.format(self.PROVIDER, do_key_name))
  74. self.assertIn(
  75. finger_print,
  76. [i.strip() for i in show_keypair]
  77. )
  78. except AssertionError:
  79. # Delete the public key if the above assertions fail
  80. self.run_cloud('-f remove_key {0} id={1}'.format(self.PROVIDER, finger_print))
  81. raise
  82. finally:
  83. # Delete public key
  84. self.assertTrue(self.run_cloud('-f remove_key {0} id={1}'.format(self.PROVIDER, finger_print)))
  85. def test_instance(self):
  86. '''
  87. Test creating an instance on DigitalOcean
  88. '''
  89. # check if instance with salt installed returned
  90. ret_str = self.run_cloud('-p digitalocean-test {0}'.format(self.instance_name), timeout=TIMEOUT)
  91. self.assertInstanceExists(ret_str)
  92. self.assertDestroyInstance()