1
0

test_digitalocean.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. # Import Salt Testing Libs
  10. from tests.integration.cloud.helpers.cloud_test_base import CloudTest, TIMEOUT
  11. # Import Salt Libs
  12. from salt.ext.six.moves import range
  13. import salt.crypt
  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. if salt.crypt.HAS_M2:
  55. rsa_key = salt.crypt.RSA.gen_key(4096, 65537, lambda: None)
  56. pub = b'ssh-rsa %s' % base64.b64encode(
  57. b'\x00\x00\x00\x07ssh-rsa%s%s' % rsa_key.pub())
  58. else:
  59. ssh_key = salt.crypt.RSA.generate(4096)
  60. pub = ssh_key.publickey().exportKey("OpenSSH")
  61. pub = salt.utils.stringutils.to_str(pub)
  62. key_hex = hashlib.md5(base64.b64decode(pub.strip().split()[1].encode())).hexdigest()
  63. finger_print = ':'.join([key_hex[x:x+2] for x in range(0, len(key_hex), 2)])
  64. try:
  65. _key = self.run_cloud('-f create_key {0} name="{1}" public_key="{2}"'.format(self.PROVIDER,
  66. do_key_name, pub))
  67. # Upload public key
  68. self.assertIn(
  69. finger_print,
  70. [i.strip() for i in _key]
  71. )
  72. # List all keys
  73. list_keypairs = self.run_cloud('-f list_keypairs {0}'.format(self.PROVIDER))
  74. self.assertIn(
  75. finger_print,
  76. [i.strip() for i in list_keypairs]
  77. )
  78. # List key
  79. show_keypair = self.run_cloud('-f show_keypair {0} keyname={1}'.format(self.PROVIDER, do_key_name))
  80. self.assertIn(
  81. finger_print,
  82. [i.strip() for i in show_keypair]
  83. )
  84. except AssertionError:
  85. # Delete the public key if the above assertions fail
  86. self.run_cloud('-f remove_key {0} id={1}'.format(self.PROVIDER, finger_print))
  87. raise
  88. finally:
  89. # Delete public key
  90. self.assertTrue(self.run_cloud('-f remove_key {0} id={1}'.format(self.PROVIDER, finger_print)))
  91. def test_instance(self):
  92. '''
  93. Test creating an instance on DigitalOcean
  94. '''
  95. # check if instance with salt installed returned
  96. ret_str = self.run_cloud('-p digitalocean-test {0}'.format(self.instance_name), timeout=TIMEOUT)
  97. self.assertInstanceExists(ret_str)
  98. self.assertDestroyInstance()